View previous topic :: View next topic |
Author |
Message |
ntgcp_08
Joined: 21 Feb 2008 Posts: 24
|
Control baud rate using a software uart |
Posted: Thu Sep 11, 2008 11:13 am |
|
|
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
|
Re: Control baud rate using a software uart |
Posted: Thu Sep 11, 2008 11:41 am |
|
|
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
|
|
Posted: Thu Sep 11, 2008 11:42 am |
|
|
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
|
|
Posted: Thu Sep 11, 2008 1:29 pm |
|
|
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
|
|
Posted: Thu Sep 11, 2008 1:32 pm |
|
|
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
|
|
Posted: Thu Sep 11, 2008 2:12 pm |
|
|
The DISABLE_INTS fixed it. Thanks! Now I need to see disabling the interrupts while printing to the debug port messes anything else up. |
|
|
|