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

Another question regarding rs232

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







Another question regarding rs232
PostPosted: Thu Nov 23, 2006 5:11 am     Reply with quote

Good day,
I need to use an rs232 at baud rates higher than 115200. As far as i see, the compiler doesn't allow me to exceed 115200, while, for what i know, writing in assembler, could have made it possible. Does anyone know a way to overcome this problem still using the ccs compiler?
Thank you in advance,
Alan
asmallri



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

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

PostPosted: Thu Nov 23, 2006 5:19 am     Reply with quote

If the restriction is real then it is still easy to do.

Code:

#define XTAL_FREQ 40000000
#define BaudRate 115200
#define BRG_divisor XTAL_FREQ/(16*BaudRate)

// define register location for this PIC
#define SPBRG 0x0FAF

...

// cheat and use #use rs232 to set up everything for you at 115200
#use rs232 ......

// now set the divisor you really need
SPBRG = BRG_divisor;


_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Ttelmah
Guest







PostPosted: Thu Nov 23, 2006 6:17 am     Reply with quote

It depend on your master crystal rate.
The following:
Code:

#use delay(clock=40000000)
#use rs232(baud=250000, xmit=PIN_C6, rcv=PIN_C7)

Happily compiles (since 250K, is nice and easy to do from a 40Hz crystal). However:
Code:

#use delay(clock=10000000)
#use rs232(baud=460800, xmit=PIN_C6, rcv=PIN_C7)

Will error on the baud rate, since the possible nearest dividers (/5, and /6), will give about 8% error in the rate (the compiler will not allow errors about about 3%). However changing to a slightly slower clock, that is a nicer multiple:
Code:

#use delay(clock=9216000)
#use rs232(baud=460800, xmit=PIN_C6, rcv=PIN_C7)

Will happily work again.
There is no limit at 115200, in the compiler. The limit is based on whether the rate you are selecting will work with the crystal you are using. I have used 921600bps, with a 9.216MHz crystal, and the *4 PLL (36.864MHz).
There is separately, a limit in the code, when using 'software' RS232, since this requires several instructions per loop, and again the compiler will error on the baud rate, if there is not enough time for the code needed.

Best Wishes
Alan
Guest







rs232
PostPosted: Thu Nov 23, 2006 6:41 am     Reply with quote

Thank you both very much. I'll try and let you know.
Alan
Sigma



Joined: 03 May 2004
Posts: 48
Location: Sg

View user's profile Send private message

PostPosted: Wed Mar 21, 2007 7:29 pm     Reply with quote

Hi, Ttelmah,

If I am using a 4Mhz crystal and 16F76 device, the following statement is allowed by the compiler.

#use delay(clock=3790000)
#use rs232(baud=115200, parity=N, BITS=8, xmit=TXD, rcv=RXD) //Hardware UART

What will be the consequences as a result of inconsistency of frequency?

regards
Sigma
Ttelmah
Guest







PostPosted: Thu Mar 22, 2007 4:11 am     Reply with quote

It won't work.
The compiler will solve the divider as:

(3790000/115200)/4 = 8.22

Hence it'll select /8 as the divider (7 into the setup register).

With your real crystal, this will give:

4000000/(4*8) = 125000bps.

Error = 125000/115200 = 1.0857*.
8.57%

Now, the error that can exist without problems, depends on the sampling algorithm of the receive UART, the word length etc. etc., but in general, 3 to 4% is a 'good guide', with 5.5% being the absolute upper limit for most systems. This works out, because the simpler chips generally sample in the middle of the bit time. For a 10 bit transmission (1 start bit, 8 bit data, and one stop bit), with sampling only occurring on the data bits, if the system starts counting from the leading edge of the start bit, it'll go outside the data bits, if the total timing error exceeds this.
The CCS default, is not silly. It tells you if your hardware _can_ handle the speeds required, and though in a few small circumstances, 'tricking' it, may be worthwhile, in general, if the compiler says the rate won't work, it won't...

Best Wishes
Sigma



Joined: 03 May 2004
Posts: 48
Location: Sg

View user's profile Send private message

PostPosted: Thu Mar 22, 2007 7:47 pm     Reply with quote

Thanks. Ttelmah.
That means if the baud rate is fixed, lets say, 115200bps. We had to choose the correct crystal?

regards
Sigma
Ttelmah
Guest







PostPosted: Fri Mar 23, 2007 3:36 am     Reply with quote

Sigma wrote:
Thanks. Ttelmah.
That means if the baud rate is fixed, lets say, 115200bps. We had to choose the correct crystal?

regards
Sigma


Exactly.
You want a crystal, that is an integer multiple of the required rate*4.
So for 115200bps, and a crystal 'near' 4MHz, you want:
115200*4 = 460800

integer multiples are:
*8 = 3686400Hz
*9 = 4147200Hz

Either of these crystals wll work fine. The former is a standard crystal, for exactly this reason (it also works nicely for all the other standard baud rates).

Best Wishes
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