Programming Atmega328

Dear readers,

I am aware this topic has been covered and I have been looking all around for an answer to my question. It seems I do not understand this. Could you please point me in the right direction and shed some light on my problem as follows:

I am using Arduino Uno (and Nano) with Atmega328 to communicate with a sensor through I2C communication using i2cmaster library (http://homepage.hispeed.ch/peterfleury/avr-software.html). This is the only library I am using.

As far as I understand, the Atmega328 clock frequency is 20MHz, but Arduino uses only 16MHz oscillator, mainly because of the libraries (timing). I am trying to build a controller using an Atmega328-TQFP only for I2C communication only.

Can I program the Atmega328-TQPF with Arduino Uno and using 20MHz oscillator form my project? I saw a tutorial for bootloading the Atmega328 chip (the DIP package) which is simple to connect using a breadboard. The tutorial also uses a16MHz oscillator. Why not 20MHz? Because of the Arduino libraries? What would be the best way to program the Atmega328-TQPF? I found one that already has Arduino bootloader (http://www.jayconsystems.com/atmega328p-with-arduino-uno-bootloader.html). I would like to burn the bootloader myself through Arduino. Would I need to build a board and burn the bootloader directly?

Thank you for any help an suggestion.

Best regards,
K

Chimera:
As far as I understand, the Atmega328 clock frequency is 20MHz, but Arduino uses only 16MHz oscillator, mainly because of the libraries (timing).

The clock can be whatever you want it to be, up to a maximum of 20MHz. Why did Arduino choose 16MHz? I don't know.

Chimera:
Can I program the Atmega328-TQPF with Arduino Uno and using 20MHz oscillator form my project?

You can do it without any crystal or oscillator whatsoever, it has internal clock sources. A crystal will be more accurate than the internal clock for critical timing, but I2C doesn't need that.

I really appreciate your prompt answer.

So basically I just need to set up my Atmega like shown here: http://arduino.cc/en/Tutorial/ArduinoISP? And burn the bootloader?

How can I determine the clock frequency? If I use internal clock with the Arduino bootloader, the clock frequency would surely be set to 16MHz? What if I use an external oscillator at 20MHz? It seems I would need to rebuild the bootloader?

Thank you and best regards,
K

Chimera:
I really appreciate your prompt answer.

So basically I just need to set up my Atmega like shown here: http://arduino.cc/en/Tutorial/ArduinoISP? And burn the bootloader?

Yep.

Chimera:
How can I determine the clock frequency? If I use internal clock with the Arduino bootloader, the clock frequency would surely be set to 16MHz? What if I use an external oscillator at 20MHz? It seems I would need to rebuild the bootloader?

By default it's going to set the chip to expect an external clock at 16MHz, yes.

It's all controlled by a file called "boards.txt" which is in your Arduino installation somewhere. If you want 20MHz you'll have to copy/paste the Arduino Uno entry and edit it a bit. It's quite obvious how to do it if you look at the file.

The entries in that file are what appear in the "Tools->Board" menu in the IDE.

This is the boards.txt file for Arduino Uno with Atmega328.

#uno.name=Arduino Uno
uno.upload.protocol=arduino
uno.upload.maximum_size=32256
uno.upload.speed=115200
uno.bootloader.low_fuses=0xff
uno.bootloader.high_fuses=0xde
uno.bootloader.extended_fuses=0x05
uno.bootloader.path=optiboot
uno.bootloader.file=optiboot_atmega328.hex
uno.bootloader.unlock_bits=0x3F
uno.bootloader.lock_bits=0x0F
uno.build.mcu=atmega328p
uno.build.f_cpu=16000000L
uno.build.core=arduino
uno.build.variant=standard

Here I can change the f_cpu=16000000L from 16 to 20MHz. What about the "bootloader.file=optiboot_atmega328.hex" dependency? I still think I need to rebuild the hex file... Or is the change in boards.txt enough?

Best regards,
K

Chimera:
This is the boards.txt file for Arduino Uno with Atmega328.

#uno.name=Arduino Uno

uno.upload.protocol=arduino
uno.upload.maximum_size=32256
uno.upload.speed=115200
uno.bootloader.low_fuses=0xff
uno.bootloader.high_fuses=0xde
uno.bootloader.extended_fuses=0x05
uno.bootloader.path=optiboot
uno.bootloader.file=optiboot_atmega328.hex
uno.bootloader.unlock_bits=0x3F
uno.bootloader.lock_bits=0x0F
uno.build.mcu=atmega328p
uno.build.f_cpu=16000000L
uno.build.core=arduino
uno.build.variant=standard




Here I can change the f_cpu=16000000L from 16 to 20MHz. What about the "bootloader.file=optiboot_atmega328.hex" dependency? I still think I need to rebuild the hex file... Or is the change in boards.txt enough?

Best regards,
K

The bootloader would have to be recompiled to run at the specified baud rate at the new clock rate.

Lefty

retrolefty:
The bootloader would have to be recompiled to run at the specified baud rate at the new clock rate.

Oh, I forgot the bootloader.

Really though, bootloaders are more trouble than they're worth when you're making standalone devices. Once you start using ISP programmers you don't need them.

If I use internal clock with the Arduino bootloader, the clock frequency would surely be set to 16MHz?

The internal clock, if configured in the fuses, goes up to 8 MHz nominally.

If you run at odd frequencies, the bootloader needs to be recompiled to allow for the change (various pre-compiled ones are around). This is because it gets its data using serial comms.

As fungus said, you can manage without any bootloader. The bootloader makes uploading new sketches a bit easier, but if you develop on a development board and get everything right, then just use ICSP to get the finished product on your standalone board. If you choose to run a 20 MHz crystal with the existing libraries, be aware that timing will be out (everything will be a bit faster). You can change F_CPU to compensate, although I had some odd problems doing that in the past.

I'm still not clear on why you're planning to use 20MHz. Nothing you've said seems to need it, so if it's going to cause problems then why do it?

fungus:

retrolefty:
The bootloader would have to be recompiled to run at the specified baud rate at the new clock rate.

Oh, I forgot the bootloader.

Really though, bootloaders are more trouble than they're worth when you're making standalone devices. Once you start using ISP programmers you don't need them.

Now that the IDE has the 'upload using programmer' option, bootloaders really are optional configuration for standalone avr projects. However if you have the hardware and are using serial commands to the PC anyway then a bootloader is still pretty convenient to have.

Hi
thanks for that topic,

I just changed the bootloader and a compiler massage came up: error: #error "Clock must be 16, 8, 4, 2 or 1 MHz"

anyway was still fun trying it out.

thx
coolf

Thank you for your answers! I really appreciate the responsiveness and tolerance. :slight_smile:

fungus:
I’m still not clear on why you’re planning to use 20MHz. Nothing you’ve said seems to need it, so if it’s going to cause problems then why do it?

I am using an I2C IR array temperature sensor and I am trying to achieve as highest framerate as possible with Atmega328 chip. I believe I cannot optimize my code any further (also using lookup tables). I already achieve < 10 ms refresh rate, but I am trying to go even lower. In my opinion this is one option. The other (not to my liking, but would resolve my problems for sure) is using 32-bit controller. I have no idea how to program 32-bit controllers and the libraries I am using only work with 8-bit AVR chips.

So you are saying that there will be problems if trying to achieve 20MHz? What about alternatives? Atmel Studio for example? Could you please suggest something, since you have a great deal more experience than me…

According to the datasheet, Atmega328 can achieve 20MHz. So it is only possible to get 16, 8, 4, 2 or 1 MHz clock with Arduino bootloader?

Sincerely,
K

You should be able to run at 20 MHz without changing anything (except the crystal, obviously).

You upload using ICSP so you don't care about the bootloader. Internally you adjust any results from millis and micros as they will be 20/16 faster (1.25). Similarly if you use delay (presumably you don't if you are worried about speed) you delay for slightly less time.

coolf:
Hi
thanks for that topic,

I just changed the bootloader and a compiler massage came up: error: #error "Clock must be 16, 8, 4, 2 or 1 MHz"

anyway was still fun trying it out.

thx
coolf

This link details both the boards.txt changes and boot loader rebuild:

Ray