|
|
View previous topic :: View next topic |
Author |
Message |
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Mon Apr 16, 2012 7:06 am |
|
|
I fixed the problem by changing oscillator to 7.3728Mhz and baudrate 9600. Can someone please explain why I can't use oscillator with other frequency like 4 or 8Mhz ? If I use those types, the value received on the computer is all wrong.
Thanks for everyone's help |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Mon Apr 16, 2012 7:28 am |
|
|
Well, the oscillator you selected in your fuses, the FRC, is 7.37 MHz, so using 8 MHz would give incorrect math. You have to specify the clock you are using in the #use delay() statement correctly, or the math to generate things such as baud rate, would be wrong.
If you connect a 8MHz or 4MHz crystal to the primary oscillator input and set your FUSES appropriately, you can use 8 or 4 MHz. It just has to match whatever you are using. |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Mon Apr 16, 2012 7:32 am |
|
|
Oh, when I said "changing", I mean I changed both the hardware and software. Previously, I use 4 or 8 Mhz (also in the code) but things are not working as expected. But when I change to 7.3728Mhz, I got the result right away. My question is : Is there any relation between oscillator frequency and baudrate setting? Is there any differences if I use 4Mhz or 8Mhz with the same baudrate at 9600 ?
Thanks |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Mon Apr 16, 2012 8:29 am |
|
|
Well, if you were using an 8MHz previously, then you had your FUSES set for 7.37MHz, which is why you had problems. Fuses need to match your actual hardware setup.
The baud rate is generated using the oscillator (via a set of counters/dividers and other logic). The amount of error in your 9600 baud rate is directly affected by which oscillator frequency you pick. For example, if you run at 22.1184 MHz, then 22118400/9600 = 2304, which is an integer value, so the error will be 0%. Using 4MHz, then 4000000/9600 = 416.666666667, which isn't an integer value, so your baud rate will not be exactly 9600 and will generate some error. The baud rate generated for 4MHz input is typically 9615.3846, which is a 0.16% error rate
In your previous code when you had:
Code: |
#FUSES FRC,NOWDT
#use delay(clock=8000000)
|
You were using a 7.3728 MHz frequency (FRC fuse) but telling the compiler you were using 8 MHz instead (#use delay(clock=8000000)). So instead of generating 9600 baud like it should have, it generated 8862.9 baud rate
When you switched to 4MHz in your code:
Code: |
#FUSES FRC,NOWDT
#use delay(clock=4000000)
|
You were using a 7.3728 MHz frequency (FRC fuse) but telling the compiler you were using 4 MHz instead (#use delay(clock=4000000)). So instead of generating 9600 baud like it should have, it generated 17723.1 baud rate
What you use and what you tell the compiler have to match for it to work correctly. |
|
|
|
|
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
|