/* rs232 interface (serial port communications) using MAX232 level converter. * * PIC PIN18 PORTA1 - MAX232 PIN11 T1IN * PIC PIN06 PORTB0 - MAX232 PIN12 R1OUT */ #include #include "delay.c" /* Initialize the transmit pin, * by ensuring it is high for an entire message cycle. */ void rs232_initialize() { RA1 = 1; delay_ms(1); } /* Send a single char out the transmit pin. */ void rs232_send_char(unsigned char c) { // start bit RA1 = 0; 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 = 1; 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 == 1) { continue; } delay_us(125); // 8 data bits if (RB0 == 1) { c = c | 0b00000001; } delay_us(97); if (RB0 == 1) { c = c | 0b00000010; } delay_us(97); if (RB0 == 1) { c = c | 0b00000100; } delay_us(97); if (RB0 == 1) { c = c | 0b00001000; } delay_us(97); if (RB0 == 1) { c = c | 0b00010000; } delay_us(97); if (RB0 == 1) { c = c | 0b00100000; } delay_us(97); if (RB0 == 1) { c = c | 0b01000000; } delay_us(97); if (RB0 == 1) { c = c | 0b10000000; } delay_us(97); // stop bit delay_us(100); rs232_send_char(c); }