This code may be compiled to 16F84 changing the 12c508.h file Sorry for the tabs, cut&paste missed it. BR's J Souto /*--------------------------------------------------------------------------+ | Asynchronous Serial communication routines for 12c508 | | Author: Jose Luiz Pinto Souto | | souto@cryogen.com | | 9600 BPS, 1 start bit, 8 data bits, 1 stop bit | | | | Compiled with CC5xFree from www.bknd.com | +--------------------------------------------------------------------------*/ /* ** 4 MHz Internal Oscillator */ #include <12c508.h> char rxBuf; #pragma bit RxD_pin @ GP3 #pragma bit _TxOUT @ GP2 /* ;----------------------------------------------------------------------------* ; GPIO.0 : output - unused ; GPIO.1 : output - unused ; GPIO.2 : output - TxD pin ; GPIO.3 : input - RxD pin (obs.) ; GPIO.4 : output - unused ; GPIO.5 : output - unused ;----------------------------------------------------------------------------* obs.: requires external pull-up or internal week pull-up enabled */ /*--------------------------------------------------------------------------*/ void Delay_uSeg (char timeout) /*--------------------------------------------------------------------------*/ { // delay = 3*timeout + 7uS (including call and return) while (1) { timeout--; if (timeout==0) { nop(); nop(); return; } } } /*--------------------------------------------------------------------------*/ void TxSerial (char txBuf) /*--------------------------------------------------------------------------*/ /* ;---------------------------------------------------------------------------* ; Transmit 1 start bit Lo, 8 data bits and 1 stop bit Hi at 9600 bps ; No Parity ; Byte time = 1.040 mS ; Bit time = 104 uS (0.16% erro w/4.00 Mhz Internal RC) ; Input : W = byte to be transmitted ; Output: byte transmitted by serial pin ;---------------------------------------------------------------------------* */ { char idx; while (1) { Carry = 0; // start bit for (idx=10; idx; idx--) { // 3us _TxOUT = Carry; // 4us Delay_uSeg(28); // 91us (28*3+7) Carry = 1; // 1us txBuf = rr(txBuf); // 1us nop(); // 1us } // 3us return; } } /*--------------------------------------------------------------------------*/ void RxSerial (void) /*--------------------------------------------------------------------------*/ /* ;---------------------------------------------------------------------------* ; Receives 1 start bit Lo, 8 data bits and 1 stop bit Hi at 9600 bps ; No Parity ; Byte time = 1.040 mS ; Bit time = 104 uS (0.16% erro w/4.00 Mhz Internal RC) ; ; False start bit check ; ; Start bit hunting timeout = 4*1.283ms ; ; Input : none ; Output : Carry = 1 => success ; rxBuf = input byte ; Carry = 0 => error (timeout or stop bit=0) ;---------------------------------------------------------------------------* */ { char idx; rxBuf = 4; // 5.135 ms timeout idx = 0; while (1) { while (RxD_pin) { // input "high" if ((-- idx)==0) { if ((-- rxBuf)==0) { Carry = 0; return; } } } Delay_uSeg(14); // 1/2 bit delay (14*3+7) if (RxD_pin) continue; // false start bit detection rxBuf = 0x80; // 8 bits counter and reception buffer nop(); nop(); // timming adjustment do { Delay_uSeg(30); // (30*3+7)us Carry = RxD_pin; // bit read rxBuf = rr(rxBuf); // store and count } while (Carry==0); Delay_uSeg(30); // 1 bit delay nop(); // timming adjustment Carry = RxD_pin; // stop bit read return; // 100 us availiable } } /*--------------------------------------------------------------------------*/ void main( void) /*--------------------------------------------------------------------------*/ { OSCCAL = W; // Oscilator Calibration Value GPIO = 0b00011000; TRIS = 0b00001000; OPTION = 0b10000000; // intenal pull-up enabled // Serial routines usage example char a; a = 0xAA; TxSerial(a); TxSerial(0x55); do { RxSerial(); } while (Carry==0); a = rxBuf; RxSerial(); if (Carry==1) { a = rxBuf; } }