/* Code to interface to teh PIC UART 2004 justin_sane huntjas2@msu.edu */ #define l_bank 0 // ID for the left servo bank #define r_bank 1 // Same for right side /* for some reason, the on board uart fails when the servos move */ /* Set the serial port to 9600 8N1 */ /*void setup_serial(void) { RCSTA = 0x80; // serial enabled TXSTA = 0x24; // transmitt enable, high speed SPBRG = 25; // divisor for 9600 baud }*/ /* end of setup serial */ /* sends a byte to the serial port */ /*void send_serial(unsigned char byte) { while ((TXSTA & 0x02) == 0); // wait until buffer is empty TXREG = byte; // set the transmit byte }*/ /* end of send byte */ /* delay for 9600 baud */ void serial_delay(void) { unsigned r = 17; while(--r); } /* end of serial dealy */ /* sends a byte to the serial port */ // data is 9600 8N1 void send_serial(unsigned char byte) { char count = 9; // loop will actually cycle eght times // send start bit serial_out = 0; serial_delay(); while (--count) { serial_out = (byte & 0x01) == 1; // grab next bit serial_delay(); byte >>= 1; // shit next bit over } // send stop bit serial_out = 1; // send two stop bits serial_delay(); serial_delay(); } /* end of send byte */ unsigned get_dig(unsigned *num) { unsigned tmp = *num % 10; *num /= 10; return(tmp); } /* send the numerical value of a byte, in decimal */ void send_num(unsigned d) { unsigned dig1 = get_dig(&d); unsigned dig2 = get_dig(&d); unsigned dig3 = get_dig(&d); // send 100's place if (dig3 > 0) send_serial(dig3 + '0'); else send_serial(' '); // send tens place if (dig2 > 0 || dig3 > 0) send_serial(dig2 + '0'); else send_serial(' '); // end ones place send_serial(dig1 + '0'); } /* end of send_num */ /* sends a byte as hex */ void send_hex(unsigned char d) { // send highst nibble unsigned char r = d >> 4; if (r > 9) send_serial(r - 10 + 'A'); else send_serial(r + '0'); // send lowest nibble r = d & 0x0f; if (r > 9) send_serial(r - 10 + 'A'); else send_serial(r + '0'); } /* end of send_hex */ /* sends a string */ void send_string(const char *s) { unsigned index = 0; while (s[index]) { send_serial(s[index]); index++; } } /* end of send_string */ /* sends the new line chars */ void new_line( void ) { send_serial(0x0d); send_serial(0x0a); } /* end of new line */ /* sends the space char */ void space(void ) { send_serial(' '); } /* end of space */ /* sends a byte of data to the servo bank */ void send_byte(int bank, unsigned char byte) { char count = 9; // loop will actually cycle eght times // for (count = 0; count < 8; count++) while (--count) { data_out = (byte & 0x01) == 1; // grab next bit if (bank == l_bank) { l_clock = 1; while(l_ready); // wait for ready low l_clock = 0; while(!l_ready); // wait for ready high }else { r_clock = 1; while(r_ready); // wait for ready low r_clock = 0; while(!r_ready); // wait for ready high } byte >>= 1; // shit next bit over } } /* end of send byte */