/* IMPORTANT: I don't know if it's necessary, but I put 10k resistors on * the transmit and receive pins of the pic, just to be safe, and it works fine. */ /* rs232 interface (serial port communications) without a level converter. * Therefore microcontroller pins are connected DIRECTLY to the serial cable. * Therefore inverted (from standard) logic is used. * * PIC PIN01 PORTA2 - TRANSMIT (receive at PC end of serial cable) white * PIC PIN02 PORTA3 - RECEIVE (transmit at PC end of serial cable) orange */ /* looking head on at the female connector of a serial mouse coard: * i.e. the end that plugs into your pc * * 5 4 3 2 1 = blue null orange white null * 9 8 7 6 = null null yellow null * * the following are loose wires on the other end of the coard. * i.e. where it would have been soldered to the mouse circuit board. * * 5 = blue = gnd = GND * 3 = orange = PC_td = PIC PIN02 PORTA3 * 2 = white = PC_rd = PIC PIN01 PORTA2 * 7 = yellow = rts = unused */ /* If you're changing the pinout of the code, these vars are the only changes you have to make. * Plus, you'll have to change the TRIS in your main program. */ static volatile bit RS232_TD @ (unsigned)&PORTA*8+2; static volatile bit RS232_RD @ (unsigned)&PORTA*8+3; /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// /********************************************************************************************** * Initialize the transmit pin, * by ensuring it is low for an entire message cycle. */ void rs232_initialize() { RS232_TD = 0; delay_ms(1); } /********************************************************************************************** * Send a single char out the transmit pin. */ void rs232_send_char(unsigned char c) { c = ~c; // start bit RS232_TD = 1; delay_us(100); // 8 data bits RS232_TD = (c & 1); c = c >> 1; delay_us(98); RS232_TD = (c & 1); c = c >> 1; delay_us(98); RS232_TD = (c & 1); c = c >> 1; delay_us(98); RS232_TD = (c & 1); c = c >> 1; delay_us(98); RS232_TD = (c & 1); c = c >> 1; delay_us(98); RS232_TD = (c & 1); c = c >> 1; delay_us(98); RS232_TD = (c & 1); c = c >> 1; delay_us(98); RS232_TD = (c & 1); c = c >> 1; delay_us(98); // stop bit RS232_TD = 0; delay_us(100); } /********************************************************************************************** * Send a string of chars out the transmit pin. */ void rs232_send_string(const char * s) { while(*s) { rs232_send_char(*s++); } } /********************************************************************************************** * Get a single char from the receive pin. * Then send that char over the transmit pin. */ unsigned char rs232_echo_char() { unsigned char c = 0; // start bit while (RS232_RD == 0) { continue; } delay_us(125); // 8 data bits if (RS232_RD == 0) { c = c | 0b00000001; } delay_us(97); if (RS232_RD == 0) { c = c | 0b00000010; } delay_us(97); if (RS232_RD == 0) { c = c | 0b00000100; } delay_us(97); if (RS232_RD == 0) { c = c | 0b00001000; } delay_us(97); if (RS232_RD == 0) { c = c | 0b00010000; } delay_us(97); if (RS232_RD == 0) { c = c | 0b00100000; } delay_us(97); if (RS232_RD == 0) { c = c | 0b01000000; } delay_us(97); if (RS232_RD == 0) { c = c | 0b10000000; } delay_us(97); // stop bit delay_us(100); rs232_send_char(c); return c; }