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

Porting Single HW UART Code to Dual HW & SW UART

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



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

Porting Single HW UART Code to Dual HW & SW UART
PostPosted: Tue Sep 05, 2006 8:25 pm     Reply with quote

Hello All,

Working on some code for an 18F2525 that previously only used the HW UART. Now I need to have a software UART also. I've been reading up on the posts concerning this and had some specific questions.

I'm assuming I need a second ring buffer for the incoming data on the new SW UART? With that, I'll need a unique set of variables, functions, etc., to deal with the second UART? (bkbhit, etc.)

To convert the hardware ISR/ring buffer to work with the new stream setup, all I'll I need to do is:

Code:
   int t;

   buffer[next_in]=fgetc(STREAM);    <------- Changed
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
   next_in=t;           // Buffer full !!


Any other tips or tricks?

Thanks,

John
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Sep 06, 2006 1:10 am     Reply with quote

Short answer: Conceptually you are right however you do not normally need a receive ring buffer for the CCS software UART.

Long answer: With the hardware UART the ring buffer is typically used by the receive ISR used to hold received characters until your main thread can process then. The mainthread would typically periodically extract characters from the buffer and, depending on the application, build a command or data sequence into a linear buffer until a terminating character has been found at which point the main thread will pass control to a handler to parse the sequence.

If you are using the CCS software UART code then this code typically does not use interrupts (although it is possible to use an interrupt to detect the start bit condition assuming the receive pin is one of the PICs interrupt input). The software UART then has 100% control of the CPU unit the character has been serialized into the PIC. While serializing in the bits the PIC can do nothing else (including process the HW UARTs interrupts) without affecting the bit timing of the software UART. This is a long winded way of saying you typically do not need a ring buffer with the CCS software UART as you would bypass this phase and go straight to the linear buffer.

Alternatively you could implement your own software UART that functions more along the lines of the hardware UART. This generally requires two timer interrupts per software UART (tx bit and rx bit) although there are techniques at lower bt rates to use a single timer. With this scenario you would expect to see a ring buffer implemented for the receive data.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
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