rs232.c

Example code for 16F819(8xx), which measures temperature via SMT160-30, shows it on LCD and communicates with RS232 (sends values of temperature to terminal) (by Petr Mervart).




//  RS 232 declaration file
// -------------------------
// 
//   !! All constants for 4Mhz oscilator and 9600-8-N-1 mode !!


#define RxD  0        // PORTB pin for Receive DATA from PC (RB0 - is suitable,uses INTF interrupt)
#define TxD  1     // PORTB pin for Transmit DATA to PC
#define Bd9600 1Fh   // const: 1fh for 9600 b/s, (013h pro 14400 b/s - very sensitive!)
#define pocetb 8     // how many data bits are sended ?

char POCET,TMP,CAS;

void Send_Str( const char *string );      // sends string : Send_Str("message");
void Send_Byte(char n);                // sends 1 char 
char Get_Byte();                   // recieve 1 char (Warning: waiting for start bit !)            
void Send_Int2(char num);                 // sends 2 digits number as 2 bytes 25=>'2''5'

char Get_Byte()
{
  asm
  {
   ;******************************
   ; Receive routine RS232
   ;******************************
   scirxd   movlw pocetb
         movwf _POCET
         btfsc PORTB,RxD       ;!!! WAITING FOR START BIT - if no START bit is sended from PC,
         goto  scirxd           ;!!!  here is infinite loop !!!!
         movlw (Bd9600/5);+3    ; 1bit time/2 waiting
         movwf _CAS
   scirx1   btfsc PORTB,RxD
         goto  scirxd
         decfsz   _CAS,f
         goto  scirx1
   ;******************************
   scirx2   movlw Bd9600         ;1bit time waiting
         call  cekej2
         nop                  ;adjust 1bit time
      ;    nop                ;adjust 1bit time
      ;    nop                ;adjust 1bit time
      ;  nop                  ;adjust 1bit time
         rrf   PORTB,w           ;bit dat do TMP
         rrf   _TMP,f
         decfsz   _POCET,f
         goto  scirx2
         movfw _TMP        ;recieved byte to Wreg
         return
   ;*******************************************
   ;******WAITING ROUTINE**********************
   cekej2   movwf _CAS
    cek2 decfsz   _CAS,f
         goto  cek2
         return
  }
  return (TMP);
}

void Send_Byte(char n)     //SENDs: START_BIT_D0_D1...D7_STOP_BIT , 9bits together 
{
  asm
  {
    ;******************************
   ; Transmit routine RS232
   ;******************************
    scitxd  MOVWF _TMP
         MOVLW pocetb+1
         MOVWF _POCET
         BSF      STATUS,C       ;STOP bit to C
         BCF      PORTB,TxD         ;send START bit
    scitx1  MOVLW   Bd9600
         CALL  cekej
         ;nop                  ;adjust 1bit time
         ;nop                  ;adjust 1bit time
         RRF      _TMP,f            ;send data bit (rotate)
         BTFSC   STATUS,C        ;sending log 1 ?
         BSF      PORTB,TxD
         BTFSS    STATUS,C        ;sending log 0 ?
         BCF      PORTB,TxD
         DECFSZ   _POCET,f
         GOTO  scitx1              ;..until all 9bits are sended
         MOVLW Bd9600            ;already yes -> wait STOP bit
    ;*******************************************
    ;******WAITING ROUTINE**********************
    cekej   MOVWF _CAS
    cek1 DECFSZ   _CAS,f
         GOTO  cek1
         RETURN
   }
}
void Send_Int2(char num)
{
   Send_Byte( '0' + (num / 10));
   Send_Byte( '0' + num % 10 );
}

void Send_Str( const char *string )    // [ Send_string("string string"); ]
{
   char n;
   n = 0;

   while( string[n] != 0 )              // Check for end of string
        Send_Byte(string[n++]);            // Send 1 char to Txd 
}



http://www.sourceboost.com/home.html

Copyright © 2002-2006 SourceBoost Technologies