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 support@ccsinfo.com

Control baud rate using a software uart

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



Joined: 21 Feb 2008
Posts: 24

View user's profile Send private message

Control baud rate using a software uart
PostPosted: Thu Sep 11, 2008 11:13 am     Reply with quote

How do you control the baud rate using a software uart?

When using a hardware uart you use the SYNC bit, BRG16 bit and BRGH bit to set up the baud rate generator.

How does that work for a software uart?

I'm using the following statement to set up my software uart...
Code:
#use rs232(baud=2400,parity=N,xmit=DEBUG_TX,rcv=DEBUG_RX,bits=8,restart_wdt,STREAM=DEBUG232)

But it seems to only work reliably at a baud rate of 2400. At 9600, hyperterminal doesn't always display the data correctly.
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: Control baud rate using a software uart
PostPosted: Thu Sep 11, 2008 11:41 am     Reply with quote

The software UART is just a CCS library function that uses instruction timing to generate the serial bit stream. It does this based on what you said the processor clock rate was (with the #use delay (clock=...) directive). The CCS library just trusts you on that info. It has no way to verify that the clock is what you say it is. So if you are using a clock that is not coming from a crystal or a resonator, or if the crystal is not what you said it was, it is possible for the CCS-generated baudrate to be in error. Another thing to consider is interrupts. I don't know if the CCS software UART code disables interrupts while it is using instruction times, but if it doesn't, and if you have some interrupt that occurs during the generation of the output bit stream, then the time spent in the interrupt will add to the bit times generated by the UART code. This could also affect the correctness of the serial data as received by hyperterminal.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting


Last edited by RLScott on Thu Sep 11, 2008 11:46 am; edited 1 time in total
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Sep 11, 2008 11:42 am     Reply with quote

That is how you do it. The only reason it wouldn't work at higher speed would be clock speed or hardware problems. What is your PIC, clock speed and compiler version?
_________________
The search for better is endless. Instead simply find very good and get the job done.
ntgcp_08



Joined: 21 Feb 2008
Posts: 24

View user's profile Send private message

PostPosted: Thu Sep 11, 2008 1:29 pm     Reply with quote

The PIC I'm using is 18LF6722
The clock speed is 4MHz
The compiler is PCH C Compiler, Version 4.066

It has worked in the past at a baud rate of 9600, but I commented out some code (nothing to do with interrupts or my Uarts) and now it only prints to the debug port correctly at 2400 baud.

I will try disabling some of my interrupts and see if that fixes the issue.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 11, 2008 1:32 pm     Reply with quote

CCS has a feature to do this. Add this parameter to your #use rs232()
statement for the software UART. From the manual:
Quote:
DISABLE_INTS --
Will cause interrupts to be disabled when the
routines get or put a character. This prevents
character distortion for software implemented I/O
and prevents interaction between I/O in interrupt
handlers and the main program when using the UART.
ntgcp_08



Joined: 21 Feb 2008
Posts: 24

View user's profile Send private message

PostPosted: Thu Sep 11, 2008 2:12 pm     Reply with quote

The DISABLE_INTS fixed it. Thanks! Now I need to see disabling the interrupts while printing to the debug port messes anything else up.
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