/* 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 PIN18 PORTA1 - TRANSMIT (receive at PC end of serial cable) white * PIC PIN06 PORTB0 - 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 = td = PIC PIN06 PORTB0 * 2 = white = rd = PIC PIN18 PORTA1 * 7 = yellow = rts = unused */ #include #include "delay.c" /* Initialize the transmit pin, * by ensuring it is low for an entire message cycle. */ void rs232_initialize() { RA1 = 0; delay_ms(1); } /* Send a single char out the transmit pin. */ void rs232_send_char(unsigned char c) { c = ~c; // start bit RA1 = 1; delay_us(100); // 8 data bits RA1 = (c & 1); c = c >> 1; delay_us(98); RA1 = (c & 1); c = c >> 1; delay_us(98); RA1 = (c & 1); c = c >> 1; delay_us(98); RA1 = (c & 1); c = c >> 1; delay_us(98); RA1 = (c & 1); c = c >> 1; delay_us(98); RA1 = (c & 1); c = c >> 1; delay_us(98); RA1 = (c & 1); c = c >> 1; delay_us(98); RA1 = (c & 1); c = c >> 1; delay_us(98); // stop bit RA1 = 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. */ void rs232_echo_char() { unsigned char c = 0; // start bit while (RB0 == 0) { continue; } delay_us(125); // 8 data bits if (RB0 == 0) { c = c | 0b00000001; } delay_us(97); if (RB0 == 0) { c = c | 0b00000010; } delay_us(97); if (RB0 == 0) { c = c | 0b00000100; } delay_us(97); if (RB0 == 0) { c = c | 0b00001000; } delay_us(97); if (RB0 == 0) { c = c | 0b00010000; } delay_us(97); if (RB0 == 0) { c = c | 0b00100000; } delay_us(97); if (RB0 == 0) { c = c | 0b01000000; } delay_us(97); if (RB0 == 0) { c = c | 0b10000000; } delay_us(97); // stop bit delay_us(100); rs232_send_char(c); }