Hi all,
I am building project where I need to have 1MHz clock speed of ATMEGA328P. Then I am using hardware serial pins (rx, tx) for serial communication with other device. The baud rate is set to 9600 and can not be changed to different value on the device. It has to stay as it is. But looks there is problem in the hardware serial communication. For example the device was working correctly for a day but then it start to has difficulties. Sometimes it has problems with the communication, sometimes it is ok. Because of this I think it is caused by the 1MHz speed of the ATMEGA328P. I tryied to run the same code on th 16MHz arduino uno and looks there is no such problem. So I think it has to be caused by the 1MHz AVR speed.
I would like to ask you if there is some solution or what do you think about it. If I am correct with saying that.
With a 1 MHz CPU clock the closest to 9600 baud you can get is 9615 baud (using 104 as the clock divider) and add to that the errors associated with the internal RC oscillator means it is easy to be too far away from 9600 for the receiver to be able to live with. The RC oscillator will drift with both temperature and time make the problem worse.
Ok, I understand the problem. Just to describe a bit more my aim. I want a device which will operate with only 2AA NiMH bateries. It means at the end of battery capacity it will have 1.8V (0.9V each). I want to have the device without any voltage regulator, directly connected to the batteries. All the devices connected to it are ok with 1.8V. I can not use 3AA batteries as it would be already too much when batteries fully charged.
What about to switch to 4MHz external oscilator. Then the AVR can still operate with 1.8V. Would be that sufficient for the 9600 baud rate?
stowite:
With a 1 MHz CPU clock the closest to 9600 baud you can get is 9615 baud (using 104 as the clock divider) and add to that the errors associated with the internal RC oscillator means it is easy to be too far away from 9600 for the receiver to be able to live with. The RC oscillator will drift with both temperature and time make the problem worse.
I really doubt that a 1.5% error could cause much problem in most UARTs. In the worst case where synchronization begins with the start bit, the deviation is then 15% by the 10th (last) bit. That is adequate framing.
It's more likely that the oscillator is going wacky.
The internal RC oscillator is not factory calibrated accurately enough to guarantee that serial will work. The frequency of the internal oscillator also has much stronger temperature and voltage dependence. I suspect that you're right on the edge, so as batteries discharge or charge, and as the temperature around the device changes, it goes in and out of the range of frequencies that work.
An external crystal (1mhz or 4mhz) should sort it out (assuming this is the issue, not something else)
OK. I will try to get the 1MHz and 4MHz external oscilator to test it. Those external oscilators are more accurate even with the temperature changes, right? The device is going to be used outside, lets say whole year. If I understand correctly then I have to also change the fuses of AVR to tell it that the external cristal will be used. Currently I am using APM Optiboot internal 1MHz noBOD 9600 baud bootloader. Do you know what fuses should be exactely changed to use the external one? And what should be also changed for 4MHz external clock? Or better ready to use source for bootloader in such configuration?
Another question would be regarding the link which stowite provided - here. I can see that in the table for 1MHz speed there is "U2Xn = 1" and for value 9600 I can see error 0.2% which should be ok. But I have no idea what that means and how to set it.
UX2 is always used on Arduino. Problem is that the 1MHz isn't actually 1MHz (or that's what I think the problem is - you should verify by having it blink an LED or something to make sure it's not dying entirely or something)
You would change the low fuse. I think you want 0xFB for the low fuse at 1MHz, 0xFD for 4MHz. If you can't program the chip after setting this, the crystal is not connected correctly.
Do you have a bootloader built for 4MHz? If not, you can use the one you have at 4MHz - you just need to use it at 38400 baud instead of 9600.
I do not have the bootloader for 4MHz. I will try to search on internet if there is same ready to use with 4MHz already. And you ment 1MHz in your last sentence, right?
"If not, you can use the one you have at 4MHz" should be 1MHz I guess.