; CC5X Version 3.0E, Copyright (c) B. Knudsen Data ; C compiler for the PICmicro family ; ************ 19. Dec 1999 19:57 ************* processor 16F84 radix DEC OPTION_REG EQU 0x81 PORTA EQU 0x05 TRISA EQU 0x85 PORTB EQU 0x06 TRISB EQU 0x86 PCLATH EQU 0x0A INTCON EQU 0x0B Carry EQU 0 Zero_ EQU 2 RP0 EQU 5 _TxOUT EQU 0 RxD_pin EQU 0 rxBuf EQU 0x0E timeout EQU 0x0D idx EQU 0x0C idx_2 EQU 0x7F GOTO main ; FILE D:\WINDOWS\DESKTOP\IRV00\SERIAL9.C ;/* ; * 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_uSeg MOVWF timeout ;/*--------------------------------------------------------------------------*/ ; ; // delay = (3*timeout + 7) * 0.4uS (including call and return) ; ; while (1) { m001 DECFSZ timeout,1 ; timeout--; GOTO m001 ; if (timeout==0) { NOP ; nop(); NOP ; nop(); RETURN ; return; ; } ; } ; ;}// Delay_uSeg ; ;/*--------------------------------------------------------------------------*/ ;void TxSerial (void) { TxSerial ;/*--------------------------------------------------------------------------*/ ; ;/* ; ;---------------------------------------------------------------------------* ; ; 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; ; BSF rxBuf,1 ; rxBuf[0] = rxBuf[0] | 0x02; // stop bit added BCF 0x03,Carry ; Carry = 0; // start bit in carry ; MOVLW .11 ; for (idx=11; idx; idx--) { // 3*0.4us=1.2us MOVWF idx m002 MOVF idx,1 BTFSC 0x03,Zero_ GOTO m003 BTFSS 0x03,Carry ; _TxOUT = Carry; // 4*0.4us=1.6us BCF 0x05,_TxOUT BTFSC 0x03,Carry BSF 0x05,_TxOUT MOVLW .80 ; Delay_uSeg(80); // (80*3+7)*0.4us = 98.8 us CALL Delay_uSeg RRF rxBuf,1 ; rxBuf[0] = rr(rxBuf[0]); // 1*0.4us=0.4us RRF rxBuf+1,1 ; rxBuf[1] = rr(rxBuf[1]); // 1*0.4us=0.4us DECF idx,1 ; } // 3*0.4us=1.2us GOTO m002 m003 RETURN ;} ; ;/*--------------------------------------------------------------------------*/ ;void RxSerial (void) { RxSerial ;/*--------------------------------------------------------------------------*/ ; ;/* ; ;---------------------------------------------------------------------------* ; ; 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; ; MOVLW .4 ; rxBuf[0] = 4; // 5.135 ms timeout MOVWF rxBuf CLRF idx_2 ; idx = 0; ; ; while (1) ; { m004 BTFSS 0x06,RxD_pin ; while (RxD_pin) // input "high" GOTO m005 ; { DECFSZ idx_2,1 ; if ((-- idx)==0) GOTO m004 ; { DECFSZ rxBuf,1 ; rxBuf[0]--; GOTO m004 ; if (rxBuf[0]==0) ; { BCF 0x03,Carry ; Carry = 0; RETURN ; return; ; } ; } ; } ; m005 MOVLW .40 ; Delay_uSeg(40); // 1/2 bit delay (40*3+7)*0.4us=50.8us CALL Delay_uSeg BTFSC 0x06,RxD_pin ; if (RxD_pin) GOTO m004 ; continue; // false start bit detection ; MOVLW .1 ; rxBuf[0] = 0x01; // 9 bits counter and reception buffer MOVWF rxBuf CLRF rxBuf+1 ; rxBuf[1] = 0x00; ; ; do ; { m006 MOVLW .81 ; Delay_uSeg(81); // (81*3+7)*0.4us=100us CALL Delay_uSeg NOP ; nop(); // 0.4us NOP ; nop(); // 0.4us BCF 0x03,Carry ; Carry = RxD_pin; // 1.2us bit read BTFSC 0x06,RxD_pin BSF 0x03,Carry RRF rxBuf,1 ; rxBuf[0] = rr(rxBuf[0]); // 0.4us store and count RRF rxBuf+1,1 ; rxBuf[1] = rr(rxBuf[1]); // 0.4us ; } // 1.2us BTFSS 0x03,Carry ; while (Carry==0); GOTO m006 MOVLW .81 ; Delay_uSeg(81); // (81*3+7)*0.4us=100us CALL Delay_uSeg NOP ; nop(); // 0.4us BCF 0x03,Carry ; Carry = RxD_pin; // stop bit read BTFSC 0x06,RxD_pin BSF 0x03,Carry RETURN ; return; // 100 us availiable ; } ;} ; ; ;/*--------------------------------------------------------------------------*/ ;void main (void) ;/*--------------------------------------------------------------------------*/ ;{ main ; CLRF INTCON ; INTCON = 0; // no interrupts CLRF PCLATH ; PCLATH = 0; // bank 0 MOVLW .1 ; PORTA = 1; MOVWF PORTA CLRF PORTB ; PORTB = 0; BSF 0x03,RP0 ; RP0 = 1; // RAM bank 1 CLRF TRISA ; TRISA = DEF_TRISA; MOVWF TRISB ; TRISB = DEF_TRISB; MOVLW .195 ; OPTION = 0b11000011; // Prescaler Timer0 1:16 MOVWF OPTION_REG BCF 0x03,RP0 ; RP0 = 0; // RAM bank 0 ; ; while (1) { ;// RxSerial(); m007 MOVLW .1 ; rxBuf[0]=0x01; // 9th bit MOVWF rxBuf MOVLW .170 ; rxBuf[1]=0xAA; // test byte MOVWF rxBuf+1 CALL TxSerial ; TxSerial(); GOTO m007 ; } ; END