CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to

interrupt driven RS-232 for PIC16 w/o EUSART hardware

Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message

Joined: 20 Nov 2007
Posts: 2120
Location: albany ny

View user's profile Send private message AIM Address

interrupt driven RS-232 for PIC16 w/o EUSART hardware
PostPosted: Wed Mar 04, 2015 3:00 pm     Reply with quote

//     this is an interrupt driven routine that enhances the soft
//     RS-232 EUSART function of the CCS compiler. In this example
//     using 4.9152mhz external clock for zero error  baud division
//     NB: max safe baud rate 9600   using  9.83 mhz clock up to 19200
//     SO USING BUFFER OF 16 in this example
//     timer0 used to insure incoming packets are complete
//     before attempting a reply - to avoid char distortion and loss
//     due to interruption caused by the receive service,
//     while transmitting. do not transmit unless
//     you are clear of incoming characters
//     NB:  transmitting is NEVER safe in this mode
//     if ANY ints are enabled. suggest disabling int_ext before
//     a transmit attempt

#include <16f818.h>
#include <stdlib.h>
#use delay( clock=4915200)
#use rs232(baud=9600 , xmit=PIN_B1, rcv=PIN_B0, sample_early)
#use fast_io(A)
#use fast_io(B)
#bit  T0IF    = 0x0B.2  // flag in INTCON reg

#define BUFFER_SIZE 16
BYTE next_in = 0,next_out = 0;
#define bkbhit (next_in!=next_out)

byte bgetc(void) {
   byte q; q=buffer[next_out++];    // NOTE : using GLObal return char variable
   if (BUFFER_SIZE==next_out) next_out=0;
   void serial_isr() {
   static int t;
   // NOTE: CCS getc() in this mode reads portB and clears the port INT
   buffer[next_in]=getc();  t=next_in++;
   if (BUFFER_SIZE==next_in) next_in=0;
   if(next_in==next_out)  next_in=t;           // Buffer full !!
void main()
    char v;  char t,z;  unsigned int8 a,b;
    setup_oscillator ( 4915200 ); //
    output_a (0); output_b (0);  set_tris_a (0xFF);  //  - all INPUT
    setup_timer_0( RTCC_DIV_64);  // set for rollovers ~12 ms holdoff later
    set_tris_b (0b00000001);  output_b (0);
    clear_interrupt( int_ext );
    printf ("*Hello*\r");  // just check output
    delay_ms(2000);  // delay to let me send a packet in
    while (1) {           // now how to getc AND send at same time..
      a=next_in;          // get mark of RX buffer 'in' position
      T0IF = 0; b=4;      // clear t0if and do 4x12 MS  ~ 50 msecs +/-
      while(b){           // TIMER0 just rolls over endlessly
             if (  T0IF  )  {   // not using INTS - just watching timer
                --b;  T0IF=0;
                if (!b && a==next_in ){ // unload IF no NU chars R caught
                   //  insert  your (safer) xmit routine here
                   while (bkbhit){ z=bgetc();}  //simple test
                }  // Implied ELSE - keep waiting
             } // END : IF t0if
       } //       END : WHILE B
    }  //         END : while  1
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group