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 change parity for RS232 hardware UART

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



Joined: 15 Sep 2003
Posts: 226

View user's profile Send private message

How to change parity for RS232 hardware UART
PostPosted: Thu Mar 30, 2006 1:32 pm     Reply with quote

SETUP_UART( ) allows Baud rate changes but how to change the parity.
As my code is using interrupts for RS232 reception the normal #use statement and ISR can't be duplicated.

I suspect the answer is to do it in assembly !
If anyone know a a CCS function to change Parity on the fly please let me know !

Thanks
hansw

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS)

#int_RDA
RDA_isr()
{
stuff received here will use parity=None
}

#use rs232(baud=9600,parity=O,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS)

#int_RDA
RDA_isr()
{
stuff received here will use parity=Odd
}
dmendesf



Joined: 31 Dec 2005
Posts: 32

View user's profile Send private message

PostPosted: Thu Mar 30, 2006 1:47 pm     Reply with quote

I�ve tried this in the past, but it didn�t worked (or at least no with all compiler versions). Maybe you can try it again:
Code:


signed int set_mod_uart(unsigned long int baud, par_type parity, int stopbits)
{

   disable_modbus();
   g_parity = parity;
   g_baud = baud;

   switch(parity)
   {
      case O:
         if(stopbits == 1)
         {
            #use rs232(baud=9600,parity=O,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS,stream=MODBUS)
         }
         else return -1;
         break;
      case E:
         if(stopbits == 1)
         {
            #use rs232(baud=9600,parity=E,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS,stream=MODBUS)
         }
         else return -1;
         break;
      case N:
         if(stopbits == 1)
         {
            #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS,stream=MODBUS)
         }
         else if(stopbits == 2)
         {
            #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS,stream=MODBUS)
            TXSTA |= 0x41;
         }
         else return -1;
         break;
   }

   switch(baud)
   {
#if CCLOCK < 9497000
      case 1200:
         set_uart_speed(1200);
         break;
#endif
      case 2400:
         set_uart_speed(2400);
         break;
      case 4800:
         set_uart_speed(4800);
          break;
      case 9600:
         set_uart_speed(9600);
         break;
      case 19200:
         set_uart_speed(19200);
         break;
      case 38400:
         set_uart_speed(38400);
         break;
      case 57600:
         set_uart_speed(57600);
         break;
      case 115200:
         set_uart_speed(115200);
         break;
      default:
         return -1;
   }
   return 0;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 30, 2006 1:49 pm     Reply with quote

I haven't tested this in hardware, but it looks like it should work.
You would set the "odd_parity_flag" at runtime, and then inside
the isr, the appropriate stream's code would be selected to receive
the character.
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS, stream=No_Parity)
#use rs232(baud=9600,parity=O,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS, stream=Odd_Parity)


int8 odd_parity_flag = FALSE;


#int_RDA
RDA_isr()
{
char c;
 
if(odd_parity_flag == TRUE)
   c = fgetc(Odd_Parity);
else
   c = fgetc(No_Parity);
}


//============================
void main()
{



while(1);
}
Hans Wedemeyer



Joined: 15 Sep 2003
Posts: 226

View user's profile Send private message

possible solution
PostPosted: Thu Mar 30, 2006 1:51 pm     Reply with quote

After reading the data sheet, Hardware uarts do not support parity. It has to be done in software.
I called CCS support and the suggestion is:

Use a second #use RS232 statement associated with a stream for the ODD parity, and then use fgetc() and fputc() for that stream. The compiler will figure out how to do the odd parity.

The ISR for the original #use RS232 will still get interrupted for each character received, however it's up to me to decide where in the code odd parity is needed.


If someone has a better suggestion please let me know...

Arrrrrrgh ! why does Trimble default to Odd parity !
Hans Wedemeyer



Joined: 15 Sep 2003
Posts: 226

View user's profile Send private message

Thanks
PostPosted: Thu Mar 30, 2006 1:54 pm     Reply with quote

PCM programmer:
Thanks it's similar to the CCS support suggestion, I'll give it a try...

hansw
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 30, 2006 1:54 pm     Reply with quote

Well I answered that before CCS support did and I gave you sample code.
Hans Wedemeyer



Joined: 15 Sep 2003
Posts: 226

View user's profile Send private message

Not sure about this:
PostPosted: Thu Mar 30, 2006 1:56 pm     Reply with quote

dmendesf wrote:
I�ve tried this in the past, but it didn�t worked (or at least no with all compiler versions). Maybe you can try it again:
Code:


signed int set_mod_uart(unsigned long int baud, par_type parity, int stopbits)
{

   disable_modbus();
   g_parity = parity;
   g_baud = baud;

   switch(parity)
   {
      case O:
         if(stopbits == 1)
         {
            #use rs232(baud=9600,parity=O,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS,stream=MODBUS)
         }
         else return -1;
         break;
      case E:
         if(stopbits == 1)
         {
            #use rs232(baud=9600,parity=E,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS,stream=MODBUS)
         }
         else return -1;
         break;
      case N:
         if(stopbits == 1)
         {
            #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS,stream=MODBUS)
         }
         else if(stopbits == 2)
         {
            #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS,stream=MODBUS)
            TXSTA |= 0x41;
         }
         else return -1;
         break;
   }

   switch(baud)
   {
#if CCLOCK < 9497000
      case 1200:
         set_uart_speed(1200);
         break;
#endif
      case 2400:
         set_uart_speed(2400);
         break;
      case 4800:
         set_uart_speed(4800);
          break;
      case 9600:
         set_uart_speed(9600);
         break;
      case 19200:
         set_uart_speed(19200);
         break;
      case 38400:
         set_uart_speed(38400);
         break;
      case 57600:
         set_uart_speed(57600);
         break;
      case 115200:
         set_uart_speed(115200);
         break;
      default:
         return -1;
   }
   return 0;
}


I think the #use RS232 statemnent is a compile time statement and can't be used at run time !

Thanks for the input.
hansw
dmendesf



Joined: 31 Dec 2005
Posts: 32

View user's profile Send private message

PostPosted: Thu Mar 30, 2006 2:30 pm     Reply with quote

Well, i know that #defines are compile time statements but i�ve been told that in CCS the last #use defines what will your serial port do, so i�ve wrote this (stupid) code. I have a very good Modbus library (master and slave) but it can�t change stop bits / parity in real time beacuse of such oddities. Baudrate can be changed, no problems. I�ll try to recode this function using this idea of multiple streams... I�m just afraid of having to do this:
Code:


// Let�s transmit x to the serial port

switch(parity)
{
    case N:
       putc(x,NOPAR);
       break;
    case O:
       putc(x,ODDPAR);
       break;
    case E:
       putc(x,EVENPAR);
       break;
}


This would be silly.
Hans Wedemeyer



Joined: 15 Sep 2003
Posts: 226

View user's profile Send private message

Yes you did !
PostPosted: Thu Mar 30, 2006 2:35 pm     Reply with quote

PCM programmer wrote:
Well I answered that before CCS support did and I gave you sample code.


Yes you did, and I appreciate the response, I'm sorry if you interpreted any other way.

Your replies are much respected, and valued my myself, and many other people.

I was just pointing out that is almost what CCS suggested !
Your reply came in while I was talking to CCS.
Hans Wedemeyer



Joined: 15 Sep 2003
Posts: 226

View user's profile Send private message

PostPosted: Thu Mar 30, 2006 2:38 pm     Reply with quote

dmendesf wrote:
Well, i know that #defines are compile time statements but i�ve been told that in CCS the last #use defines what will your serial port do, so i�ve wrote this (stupid) code. I have a very good Modbus library (master and slave) but it can�t change stop bits / parity in real time beacuse of such oddities. Baudrate can be changed, no problems. I�ll try to recode this function using this idea of multiple streams... I�m just afraid of having to do this:
Code:


// Let�s transmit x to the serial port

switch(parity)
{
    case N:
       putc(x,NOPAR);
       break;
    case O:
       putc(x,ODDPAR);
       break;
    case E:
       putc(x,EVENPAR);
       break;
}


This would be silly.


Thanks for the suggestion.
The issue is as you say it is.
The compiler will use the last #use statement which is a compile time statement.

Thanks.
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