|
|
View previous topic :: View next topic |
Author |
Message |
ChawCS Guest
|
UART baudrate using internal Osc. |
Posted: Tue Oct 10, 2006 11:09 pm |
|
|
Hi there,
I need some help on setting the UART baudrate of PIC using CCS C-Compiler.
I am using the internal osc at 8MHz. When I set it to 38400 bps, I get about 41666 bps, using the #use RS232.
I think it been set at low speed (BRGH = 0).
Is there anyway, the BRGH = 1, from calculation that will be about 38461 bps near to 38400 bps that i need.
Thanks
CS |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 10, 2006 11:34 pm |
|
|
1. Post that PIC that you're using.
2. Post your compiler version.
3. Post the #use rs232() statement. |
|
|
ChawCS Guest
|
|
Posted: Wed Oct 11, 2006 6:55 pm |
|
|
1) PIC16F914/7
2) CCS C-compiler PCWH 3.249
3) #use rs232(baud = 38400, xmit = PIN_C6, rcv = PIN_C7, parity = N, bits = 8, DISABLE_INTS) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 11, 2006 8:14 pm |
|
|
Quote: |
I need some help on setting the UART baudrate of PIC using CCS
C-Compiler.
I am using the internal osc at 8MHz. When I set it to 38400 bps, I get
about 41666 bps, using the #use RS232.
|
The value that the compiler puts into the baud rate register is correct.
Quote: | 0004: CLRF 04
0005: MOVLW 1F
0006: ANDWF 03,F
0007: MOVLW 0C
0008: BSF 03.5 // Bank 1
0009: MOVWF 19 // SPBRG register
|
If the BRGH bit was wrong, the baud rate would be off by a factor of 4,
not be slightly off as it is in your case. According to your post, the
baud rate is about 8.5% too high. That's a lot. How are you measuring
this ?
According to the 16F914 data sheet, the internal oscillator is calibrated
to be within 1% accuracy at room temperature, at 5v Vdd. If you get
away from that temperature and voltage, it can be up to 5% off frequency.
You can adjust the internal oscillator by writing to the OSCTUNE register.
Are you accidently doing this already ? Could that be the reason for
the frequency being off ?
Another quick way to compensate for an incorrect internal frequency
would be to adjust your #use delay() statement to reflect the actual
frequency. If the oscillator is running 8.5% too high, you could multiply
8 MHz x .915, which gives 7.32 MHz. Then plug that value into your
#use delay() statement, as shown in bold below. Of course, you don't
want to do this for production. It's just a temporary quick fix.
Quote: |
#include <16F914.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT
#use delay(clock=7320000)
#use rs232(baud=38400, xmit=PIN_C6, rcv=PIN_C7, ERRORS) |
Ideally, you would find out why this problem is happening. Maybe you
got a bad PIC ? |
|
|
ChawCS Guest
|
|
Posted: Wed Oct 11, 2006 9:38 pm |
|
|
hi PCM programmer,
My calculation are from the PIC datasheet.
Baud Rate Formula
(Asynchronous)
BRGH = 0
Baud Rate = Fosc/(64(x+1))
BRGH = 1
Baud Rate = Fosc/(16(x+1))
1) Now interface with another uP with internal osc at 8MHz also but the bps need to set to about 41666 to get less error.
2) I may use the PIC to inteface with the PC. Unless I can adjust the PC bps to 41666 bps then no problem, but I understand most of the PC software bps are fix at certain standard like 38400 bps.
Do I make a mistake some where?
CS |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 11, 2006 10:37 pm |
|
|
I think I know what you're doing.
1. You're not using the CCS #use rs232() statement.
2. You are setting up the UART registers with your own code.
3. When you solved the baud rate equation for the value of
SPBRG, you did it wrong. You forgot to subtract 1 from
the result. So you used a value of 13 instead of 12.
4. When you put in the wrong value for SPBRG, it made the
baud rate be off by about 8.33% (1/12), because of this
error in the equation.
Solution:
Don't do it yourself. Use the CCS library code. Do it with a
#use rs232() statement. |
|
|
ChawCS Guest
|
|
Posted: Thu Oct 12, 2006 7:24 am |
|
|
Hi PCM Programmer,
I am using the CCS #use RS232, just that i cannot get the right baud rate to interface with the other uP then only i do the calculation to know that the result is about 41666 bps not 38400 bps as i needed. If i am using the asm code it will much easy for me. I dont want to mix my code some using C and some asm. |
|
|
Ttelmah Guest
|
|
Posted: Thu Oct 12, 2006 9:20 am |
|
|
The compiler will actually complain, if the predicted baud rate, is not within about 3.5% of the required value. So it thinks that the value is right, and it has to be using the BRGH bit, or it'd be complaining. Are you sure that the oscillator is running on frequency (the behaviour you are getting, is exactly what is seen if the chip has been programmed with the osccal value set to something like 0).
A quick look at the code generated for the BRG initialisation, using your compiler version, shows the registers are being correctly set, with the divider at 12 (/13), and BRGH=1, to give 38461Hz.
Your problem is probably that your oscillator is not actually running at 8MHz.
Best Wishes |
|
|
|
|
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
|