Since both the 168 and the 328 are rated at 20Mhz, why do they run at 16 Mhz in an Arduino (or any clones) ? When using a 20 Mhz crystal/resonator, should you change the bootloader? If yes: why, how? :-/
why do they run at 16 Mhz in an Arduino
I suspect that it is because the timing routines end up being much simpler. Of course, you're free to modify all of the timing related code to make it work at 20MHz. (I've done it for the base code - it's not that difficult.)
It's historical, the ATmega8 used in the original Arduino had a maximum clock speed of 16MHz.
So I did some searching (which I should have done before asking my questions, but this was at 2:45 AM) and it seems that all I need to do to get the bootloader working is change the AVR_FREQ in the makefile. Is this right? It's just such a waste to leave 25% of processing power/time unused. Especially for those guys who are working on large led matrixes and just need that bit more speed for their multiplexing or scanning or whatever... The baudrate of the UART doesnt seem to be a problem. It's actually a bit more accurate at 20 Mhz than at 16 Mhz. (according to the datasheet, page 203) As far as the timing goes: since the applications I'm working on aren't really time critical I probably won't bother with this but for those who will: What exactly should be modified? Just the delay.h or is there much more to it?
If this is easy to do then why is it not included in the Bootloader?
The bootloader is one big reason the standard arduino hasn't shifted to 20MHz. The fact that the rest of the arduino core probably hasn't been thoroughly tested at 20MHz is another.
You can easily change the clock rate in boards.txt (add a new entry) if you want to program with a hardware programmer (no bootloader). Not sure what it would take to modify the bootloader for 20MHz.
The original ATmega8 Arduino ran at 16MHz, which was the top rated clock speed for the ATmega8 cpu used. When "upgraded" to ATmega168 (with a 20MHz top cpu speed), the clock was left at 16MHz (probably) because the designers thought that more people/code would have backward compatibility issues with a new clock rate than would benefit from the extra 25% cpu performance. I certainly think they were right...
Sheesh - at 16MHz, the Arduino is 16 time faster than the average 4 MHz PIC and HUNDREDS of times faster than a basic stamp or similar "interpreter." A 16Mhz AVR can generate video signals directly in software; there are very few cases where the extra 4MHz would be important, and many more cases where having different Arduinos running at different speeds would be more of an issue. (Hmm. That WAS before Arduino Pro and Lilypad showed up at 8Mhz. I haven't seen a lot of complaints WRT those on either absolute speed or "different speed" issues...)
I think it probably wouldn't be too hard to port the bootloader and core libraries to work at 20 MHz, but so far it hasn't seem important enough. If we start seeing lots of cases that would benefit from the extra speed, it would be worth considering.
I think it probably wouldn’t be too hard to port the bootloader and core libraries to work at 20 MHz
It’s not, I’ve done it. The main issue that you run into is that the code for the millisecond timing is more complicated because none of the available prescaler choices results in a convenient interrupt rate. Beyond that, it’s pretty straightforward.
In which case, now we have upgraded to 328’s why isn’t the bootloader updated to run at 20MHz? Surely ther can be several types of Bootloader depending on what board you choose in the IDE?
As he just said, the problem is the libraries.
If a library expects a certain number of operations per second, everything will speed up. Sort of like playing an old DOS game on a new Athlon - it runs insanely fast.
Same problem here.
You could look through the libraries and identify what requires an update - may take you a while
What needs to be changed in the bootloader code to run the 328 at 20mhz? Not an coding expert, I've looked at the code in avr studio but nothing stands out.
What needs to be changed in the bootloader code to run the 328 at 20mhz
For the bootloader, the main thing you’d need to change would be the constants used to set the UART bit rate.
(actually, it looks like this may happen automatically just by changing AVR_FREQ in the Makefile…)
too bad there isn't a compiled version. I have some 20mhz resonator I just want to use, don't care if delays and what not are sped up as I don't need accuracy.
Would love to revisit this. Some video/OSD applications need just that extra little boost of speed to keep up with a realtime signal.
A 16Mhz AVR can generate video signals directly in software; there are very few cases where the extra 4MHz would be important
like driving video signals, where the software signal takes up almost all the cpu time, IF you can fudge it in there with direct port manipulation
anyway, we have different speed arduinos out there and doing well, and the extra little bit of speed would be very handy for those of us who like to do silly "high speed" things
maybe we need to form a little group and nail this down to the floor!
Could anyone please compile (build) a 20 Mhz 328 bootloader? I've tried several different things but cannot get it to compile succesfully :( Maybe in another topic, or on the playground, someone can explain this dumbass what I'm missing? (besides skills, knowledge and brains :) )
I've got some working 20MHz bootloaders at home, I'll upload them later today.
It's not really a big deal, you take the existing bootloader source and recompile it with the new frequency. Most of my problems were from setting up the environment and memory sections in AVR Studio because I wanted to use that instead of the normal makefile.
Okay here's a zip with bootloaders for the ATmega168 and the ATmega328p running at 20MHz. Baud rate for the 168 is 19200 and baud rate for the 328p is 57600. I also included a boards.txt file you can use to replace your existing one, to provide separate boards for the 20MHz versions. I haven't tested these on a 328p so I'd appreciate feedback.
Could anyone please compile (build) a 20 Mhz 328 bootloader?
You should be able to get this working even without flashing a new bootoader. If you replace the oscillator/crystal - upload baudrate will change accordingly. E.g. an ATmega 328 bootloader compiled for 57000 baud at 16MHz, would equal 72000 baud at 20MHz (57600*20/16).
To use the 20MHz board you create a new entry in boards.txt as follows:
atmega328_20.name=ATmega328 20 Mhz
atmega328_20.upload.protocol=stk500 atmega328_20.upload.maximum_size=30720 atmega328_20.upload.speed=72000
atmega328_20.bootloader.low_fuses=0xFF atmega328_20.bootloader.high_fuses=0xDA atmega328_20.bootloader.extended_fuses=0x05 atmega328_20.bootloader.path=atmega atmega328_20.bootloader.file=ATmegaBOOT_168_atmega328.hex atmega328_20.bootloader.unlock_bits=0x3F atmega328_20.bootloader.lock_bits=0x0F
atmega328_20.build.mcu=atmega328p atmega328_20.build.f_cpu=20000000L atmega328_20.build.core=arduino