/* * 9 bit BitBang Serial routines * 19/Dec/1999 JLS * (c) 1999 Jose Luiz Pinto Souto * souto@cryogen.com * CC5x Compiler (www.bknd.com) * 10 MHz operation */ #include <16f84.h> #pragma update_RP 0 /* OFF */ #define DEF_TRISA 0x00 #define DEF_TRISB 0x01 bit _TxOUT @ PORTA.0; bit RxD_pin @ PORTB.0; unsigned char rxBuf[2]; void Delay_uSeg (unsigned char timeout); void TxSerial (void); void RxSerial (void); /*--------------------------------------------------------------------------*/ void Delay_uSeg (unsigned char timeout) { /*--------------------------------------------------------------------------*/ // delay = (3*timeout + 7) * 0.4uS (including call and return) while (1) { timeout--; if (timeout==0) { nop(); nop(); return; } } }// Delay_uSeg /*--------------------------------------------------------------------------*/ void TxSerial (void) { /*--------------------------------------------------------------------------*/ /* ;---------------------------------------------------------------------------* ; Transmit 1 start bit Lo, 9 data bits and 1 stop bit Hi at 9600 bps ; Byte time = 1.144 mS ; Bit time = 104 uS (0.16% error @ 10 MHz) ; Input : rxBuf[0] = 9th bit (bit 0) ; rxBuf[1] = byte to be transmitted ; Output: byte transmitted by serial pin ;---------------------------------------------------------------------------* */ char idx; rxBuf[0] = rxBuf[0] | 0x02; // stop bit added Carry = 0; // start bit in carry for (idx=11; idx; idx--) { // 3*0.4us=1.2us _TxOUT = Carry; // 4*0.4us=1.6us Delay_uSeg(80); // (80*3+7)*0.4us = 98.8 us rxBuf[0] = rr(rxBuf[0]); // 1*0.4us=0.4us rxBuf[1] = rr(rxBuf[1]); // 1*0.4us=0.4us } // 3*0.4us=1.2us } /*--------------------------------------------------------------------------*/ void RxSerial (void) { /*--------------------------------------------------------------------------*/ /* ;---------------------------------------------------------------------------* ; Receives 1 start bit Lo, 9 data bits and 1 stop bit Hi at 9600 bps ; Byte time = 1.144 mS ; Bit time = 104 uS (0.16% erro w/10 Mhz) ; ; False start bit check ; ; Start bit hunting timeout = 4*1.283ms ; ; Input : none ; Output : Carry = 1 => success ; rxBuf[0,1] = input byte +9th bit ; Carry = 0 => error (timeout or stop bit=0) ;---------------------------------------------------------------------------* */ char idx; rxBuf[0] = 4; // 5.135 ms timeout idx = 0; while (1) { while (RxD_pin) // input "high" { if ((-- idx)==0) { rxBuf[0]--; if (rxBuf[0]==0) { Carry = 0; return; } } } Delay_uSeg(40); // 1/2 bit delay (40*3+7)*0.4us=50.8us if (RxD_pin) continue; // false start bit detection rxBuf[0] = 0x01; // 9 bits counter and reception buffer rxBuf[1] = 0x00; do { Delay_uSeg(81); // (81*3+7)*0.4us=100us nop(); // 0.4us nop(); // 0.4us Carry = RxD_pin; // 1.2us bit read rxBuf[0] = rr(rxBuf[0]); // 0.4us store and count rxBuf[1] = rr(rxBuf[1]); // 0.4us } // 1.2us while (Carry==0); Delay_uSeg(81); // (81*3+7)*0.4us=100us nop(); // 0.4us Carry = RxD_pin; // stop bit read return; // 100 us availiable } } /*--------------------------------------------------------------------------*/ void main (void) /*--------------------------------------------------------------------------*/ { INTCON = 0; // no interrupts PCLATH = 0; // bank 0 PORTA = 1; PORTB = 0; RP0 = 1; // RAM bank 1 TRISA = DEF_TRISA; TRISB = DEF_TRISB; OPTION = 0b11000011; // Prescaler Timer0 1:16 RP0 = 0; // RAM bank 0 while (1) { // RxSerial(); rxBuf[0]=0x01; // 9th bit rxBuf[1]=0xAA; // test byte TxSerial(); } }