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 CCS Technical Support

How to set and use SYNCHRONOUS USART using C-PIC compiler?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Judy_Perri
Guest







How to set and use SYNCHRONOUS USART using C-PIC compiler?
PostPosted: Thu Mar 18, 2004 11:53 am     Reply with quote

I am using the PCW C compiler to compile my C code for PIC628A on which there is an USART. I would like to use it in SYNCHRONOUS mode. How should I set this up and use the interupt to receive data? Can I use the builtin function for RS232? If not how should I do it? Thanks in advance.
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Thu Mar 18, 2004 6:17 pm     Reply with quote

Here's some bits and pieces to get you started:

Code:

// Interrupt flags
#bit TXIF = 0x0C.4
#bit RCIF = 0x0C.5

#bit RX9D = 0x18.0
#bit OERR = 0x18.1
#bit FERR = 0x18.2
#bit ADEN = 0x18.3
#bit CREN = 0x18.4
#bit SREN = 0x18.5
#bit RX9  = 0x18.6
#bit SPEN = 0x18.7

#bit TX9D = 0x98.0
#bit TRMT = 0x98.1
#bit BRGH = 0x98.2
// bit 0x98.3 is undefined in 16F627
#bit SYNC = 0x98.4
#bit TXEN = 0x98.5
#bit TX9  = 0x98.6
#bit CSRC = 0x98.7

// Interrupt enables
#bit TXIE = 0x8C.4
#bit RCIE = 0x8C.5

#byte TXREG = 0x19
#byte RCREG = 0x1A
#byte SPBRG = 0x99

void put_serial(int tx_data)
{
   while(!TXIF)
   {
      restart_wdt();
   }
   TXREG = tx_data;
   return;
}

int get_serial()
{
   while(!RCIF)
   {
      restart_wdt();
   }
   return RCREG;
}

void TransmitData(void)
{
   int j = 0;
   SYNC = TRUE;
   CREN = FALSE;
   CSRC = TRUE;
   do
   {
      put_serial(TransmitBuffer.Data[j++]);  // Send the data then increment j
   } while ((TransmitBuffer.Data[j-1] != 0x0D) && (j < BUFFERLENGTH));
}

main() // extracted from main
{
      // set comm to sync master transmit
      SPBRG = 0x19;
      SYNC = TRUE;
      CREN = FALSE;
      SPEN = TRUE;
      CSRC = TRUE;
      TXEN = TRUE;
      // Construct a message and send
   
   your code goes here

     TransmitData();
      // wait for transmit to complete
      // then loop for multiple commands
      // first wait for the transmit interrupt flag (TXREG is empty)
      while (1)
      {
         while (!TXIF)
         {
            restart_wdt();
         }
         // then wait for the TSR register to empty
         while (!TRMT)
         {
            restart_wdt();
         }
         // set comm to sync slave receive
         SYNC = TRUE;
         CSRC = FALSE;
         CREN = TRUE;
         ReceiveBuffer.Index = 0;
         do
         {
            // Wait for data to be received
            while (!RCIF)
            {
               restart_wdt();
            }
            receiveddata = get_serial();
            _other code_
         } while ((receiveddata != 0x0D) &&
               (Index < BufferLength));
}


The above is NOT fully functional, it is just the main stuff you would need to get started with sync serial setup. The original program ping-ponged messages back and forth, switching between master and slave.

You could set this up with true interrupts, but for my application, it was just waiting for a message, then did something based on the message received.

Hope this helps.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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