PICcoder.co.uk Forum  


::post new topic::
<< Start < Prev 1 2 3 Next > End >>
problem setting the EUART speed
Date: 2006/07/29 11:22 By: majestrix Status: User  
Karma: 0  
Posts: 29
graphgraph
Hey guys,
I have been developing a USB application with a pic18f2550 and now i need to incoporate it with UART to communicate with an RF module. Here follows the piece of code i'm using to open my uart

OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 129);

i'm using 129 as said in the page 243 of datasheet, since my oscillator is 20MHz, for a 9600 baud.
I'm just trying to send a string to the UART with putrsUSART("string");
the problem is I get some garbage characters on the display whn i test with hypertrminal.
my configuration settings as as follows.

oscillator: HSPLL - HS osc. PLL ena.
USB clk selection: USB clk. from 96 MHz PLL /2
sys clock postscaler: No postscaler
PLL Prescaler: Divided by 5 (20 MHz osc in.)

with these sattings my USB works. but UART is messed up as i said before..
so i changed my oscillator to an 8MHz crystal without changing anything else, just for a hunch.. well what do u know it works, but as expected USB dosent work.
So can someone please help me with this problem.
All i wanna do is use UART at a baud rate of 9600 along with USB.
thanks a lot.
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/29 11:49 By: dariog Status: User  
Karma: 6  
Posts: 107
graphgraph
Hi,
when working with USB, PIC18F2550 runs at 48MHz, so calculations for USART must be done using this number.

I'll take the time for calculation later (and/or copy from my source since I've been doing the same - are you using LPRS RF radios ??),
but if you're ok at 8MHz when it should work at 20, try multiplying the USART BAUD generator value by 2.25 ...
(you'll probably have to switch to BRGH_LOW)
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/29 13:03 By: majestrix Status: User  
Karma: 0  
Posts: 29
graphgraph
Hi dariog,
thanks for the reply.
So with those configurations settings (as i have said above) my PIC actaully works at 48MHz is it..?? well, i didnt know that..
my RF module is EasyRadio's ER400TRS. i'm trying to set it to 9600 baud, yeah its sorta programable.
so what i shuold be doing is 129 x 2.25 is it..?? (well i tried with 290 and 291.. but I still see weird characters)
can u pls tell me the method u are using to calculate this value. when i used the method in the datasheet i get values for the first table when BRGH = 0. but i was using when BRGH = 1 as its being said that way its more error free (although i dunno what it really does).
also what do u mean that i probably have to switch to BRGH_LOW..? sorry if these are too dumb qns to ask.. but i couldnt get my head around this.
thanks a lot

Post edited by: majestrix, at: 2006/07/29 14:39
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/29 13:41 By: dariog Status: User  
Karma: 6  
Posts: 107
graphgraph
majestrix wrote:

So with those configurations settings (as i have said above) my PIC actaully works at 48MHz is it..?? well, i didnt know that..


yeah, that's really fast
it's a pity we can't overclock a litle bit... I'm used to that, when there's no USB costraint )


my RF module is EasyRadio's ER400TRS. i'm trying to set it to 9600 baud, yeah its sorta programable.


funny, I'm using ER900TRS... but @19200 BAUD


so what i shuold be doing is 129 x 2.25 is it..??


I mean around 290

This is my code,

OpenUSART(USART_RX_INT_OFF & USART_TX_INT_OFF & USART_ASYNCH_MODE &
USART_NINE_BIT & USART_CONT_RX & USART_BRGH_HIGH,
104);
baudUSART(BAUD_16_BIT_RATE & BAUD_WAKEUP_OFF & BAUD_AUTO_OFF);
SPBRGH=0; // I found a bug in MCC header, in mag 2005

This is for 115200.
To run at 9600, this must be multiplied by 12, so...

(NEED TO SWITCH THE OTHER PC ON BUT NOW I CANT... WAIT PLEASE!)



can u pls tell me the method u are using to calculate this value. when i used the method in the datasheet i get values for the first table when BRGH = 0. but i was using when BRGH = 1 as its being said that way its more error free


I'm using the datasheet too.
For sure, BRGH has to be used carefully, depending on the baud rate you're going to obtain: it's a divider (sort of prescaler) so it will be better left HIGH for some baud-rates, and low for some others. If a given baud-rate can be obtaind with both BRGH=1 and 0, the less error-prone (i.e. more precise) is the one which gives a Higher value for the Baud Generator.


also what do u mean that i probably have to switch to BRGH_LOW..? sorry if


I meant BRGH=0, in "C" fashion
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/29 14:10 By: majestrix Status: User  
Karma: 0  
Posts: 29
graphgraph
Hi,
thanks for the promt reply..
well I tried with 104 as in ur code and connected with 115.2k, it works perfectly. then i replaced 104 with 1248 (104 x 12), it starts to give usless characters again..
well, running it at 19.2k baud, is not at all a prob for me either.. and what a coincidence that we are using almost the same trs.
well i guess this is one of those days that i cant get anything right.. tsk tsk
oh yeah, i'll wait till u switch on ur other PC.
and one more thing.. how did u get 104..??
and hey, thanks a lot again for helping out.

Post edited by: majestrix, at: 2006/07/29 15:14
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/29 14:26 By: dariog Status: User  
Karma: 6  
Posts: 107
graphgraph
majestrix wrote:

well I tried with 104 as in ur code and connected with 115.2k, it works perfectly. then i replaced 104 with 1248 (104 x 12), it starts to give usless characters again..


you're doing everything correctly, but the parameter can't go past 255 (i.e. 1 byte) so you can't use 1248... that's why you have to use BRGH.
Anyway I'm happy that something is working, at least.


well, running it at 19.2k baud, is not at all a prob for me either.. and what a coincidence that we are using almost the same trs.


yeah... they're not bad, not Zigbee but I'm not minding at the moment. rather easy and robust.


oh yeah, i'll wait till u switch on ur other PC.


busy on formatting/reinstalling a customer's one...
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/29 14:31 By: majestrix Status: User  
Karma: 0  
Posts: 29
graphgraph
hehe.. ouch.. i didnt know that it cant go above a byte..
well i tried som numbers again.. but dosent look like this is gonna work..

Post edited by: majestrix, at: 2006/07/29 15:40
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/29 23:01 By: dariog Status: User  
Karma: 6  
Posts: 107
graphgraph
Here I am... meanwhile, I had dinner at a chinese restaurant with friends just to inform you all about this saturday night of mine !

Back to serious, I suppose that

OpenUSART(USART_RX_INT_OFF & USART_TX_INT_OFF & USART_ASYNCH_MODE &
USART_NINE_BIT & USART_CONT_RX & USART_BRGH_LOW,
77);


oughtta be ok.

(the other statement,
baudUSART(BAUD_16_BIT_RATE & BAUD_WAKEUP_OFF & BAUD_AUTO_OFF);

could be changed to

baudUSART(BAUD_8_BIT_RATE & BAUD_WAKEUP_OFF & BAUD_AUTO_OFF);

but as BRG16 flag should be off by default, it should not matter)


If you look at the table on page 239 of PIC18F4550 datasheet, related to BAUD RATE CALCULATION, you see that
baud_rate=FOSC/(64*(n+1)) that is baud_rate=48000000/(64*/(77+1))

which is about 9600. We have to clear both BRGH and BRG16.
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/30 09:33 By: majestrix Status: User  
Karma: 0  
Posts: 29
graphgraph
hi dariog,
Thanks so much man.. well it was what i thought i was doing.. oh my.. it seems like i need to brush up my calculus. hehe..
well thanks so much.. and yeah now it works well and i can connect with 19.2k as well. all thanks to u.

cheers,
Hasan
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/30 15:49 By: dariog Status: User  
Karma: 6  
Posts: 107
graphgraph
Very well!

BTW, can you tell me how far will you go, between a couple of LPRS radios?
Just to compare with mine...

Indoor and/or outdoor?

Thanks
reply | quote

Re:problem setting the EUART speed
Date: 2006/07/30 17:39 By: majestrix Status: User  
Karma: 0  
Posts: 29
graphgraph
hey,
yeah I will be communicating between multi RF modules (but for prototyping i will be using three modules) one of them being the central which is connected to a computer via USB. its an indoor project. more like a paging system.
i'm using an adressing system to communicate between these modules. -it uses a format like <command> <module number>
inorder to avoid collision i'm planing to adopt a timing method like in wireless lan, but thats an improvement. for now i'm just trying to get the main backbone running. oh yeah, its my thesis project.. i'm doing BEng (in electrical and computer systems engineering).
how abut urs..?

cheers,
Hasan
reply | quote

<< Start < Prev 1 2 3 Next > End >>
::post new topic::