Atmega328 - programming problem at 10 Mhz


I have made an simple arduino compatible breadboard using an Atmega328 microcontroller. I followed the instructions from this:

I have used another ftdi usb-ttl converter but with similar functionality. Also the entire circuit is powered from the ftdi +Vcc board which permits 5V or 3.3V.

I programmed for testing the simple "blink" example. The circuit is working fine at 5v and 3.3V.

For some reason I would like to run this setup at 10 Mhz. So I changed the 16Mhz crystal with a 10Mhz one. The program already loaded into microcontroller works fine at 10Mhz, only the timing is different as would expected.

But I can't program the microcontroller at this frequency whatever type of board I choose. The error message is: "avrdude: stk500_recv(): programmer is not responding".

Hi, welcome to the forum.

When you say : "For some reason...", we all like to know the reason. There might be other ways to solve it.

The bootloader is written for a certain clock speed. If you change the crystal, the baudrate of the bootloader will be wrong.

That page has an error. Please do not connect 5V to AREF. Leave AREF open.

Could you use 16MHz or 8MHz ? With those frequencies you can find an Arduino compatible board.

Thank you for a fast response.

I need to drive a stepper motor at a very constant and specific frequency (about 201,73....Hz). In order to keep the resolution/precision as higher as possible I would not use prescalers. I can't use the 16MHz quartz as for these conditions the TOP value for timer1 is too big. And another reason is that I already have a TCXO at 10MHz and I would use it.

I changed the circuit and now AREF is open.

I can't use a 8MHz crystal because I haven't one. Of course I could buy one, but the resolution would be a little bit lower. And in my final application as I said I can't use a 16MHz crystal as the TOP value for 200Hz is 80000 (prescaler=1) and the counter register is 16 bit.

You need to make a step or micro-step 201.73 times a second ?

16MHz, prescaler 2, 16-bit timer with 39657. Result : 201.729833321 Hz 10MHz, prescaler 1, 16-bit timer with 49571. Result : 201.730850699 Hz

I say the 16MHz is closer to 201.73 Hz

Do you have a super accurate temperature compensated 10MHz ? If you don't, there will be a little long term drift, and you might just as well use the 16MHz.

Try to find the file "boards.txt" arduino \ hardware \ arduino \ avr It shows the baudrate of the Arduino boards. If you have a Arduino Uno compatible setup, the baudrate is 115200 for uploading a sketch. With 10MHz, the baudrate is 10/16 of that is 72000. That means that if you keep the bootloader, you need to create your own custom board in "boards.txt" with 72000 baud and the microcontroller running at 10MHz. You could also make your own bootloader with Optiboot for 10MHz. But you still would have to add your own custom board to "boards.txt".

Sorry. As my English is not so good I didn't explained very well what I need in my application. I don't know yet the exact value for frequency. It will depend on some mechanical features which are not ready yet. But the frequency should be somewhere between 201.70 - 201.76, so I should be able to tune the frequency with a precision at least = 0.01 Hz.

The first calculation (for 16Mhz) is for CTC mode? After seeing the prescaler=2 and your calculations, I realised that I ignored that mode and I have made all my estimations in fast pwm mode.

Sorry again.

But indeed I already have an accurate TCXO at 10MHz and I would try to use it as my application could be exposed to some temperature variations.

I need to make a microstep in that period.

I added a custom board in boards.txt. I am working on a mac so the file was in a different location. But it still don't work. Now I have more error messages.

avrdude: ser_setspeed(): tcsetattr() failed avrdude: ser_open(): can't set attributes for device "/dev/tty.usbserial-A98VRT15": Invalid argument ioctl("TIOCMGET"): Bad file descriptor avrdude: ser_close(): can't reset attributes for device: Bad file descriptor

I suppose that the usb-ttl adapter don't understand that baud = 72000 I copied the records from arduino uno and I changed the name, the baud and the clock. These are the lines added: 10Mhz test.upload.protocol=arduino test.upload.maximum_size=32256 test.upload.speed=72000 test.bootloader.low_fuses=0xff test.bootloader.high_fuses=0xde test.bootloader.extended_fuses=0x05 test.bootloader.path=optiboot test.bootloader.file=optiboot_atmega328.hex test.bootloader.unlock_bits=0x3F test.bootloader.lock_bits=0x0F

I didn’t think about avrdude.
Perhaps you have to make your own bootloader.
That is not easy, because Optiboot 5.0a uses older Arduino paths and tools to make a bootloader.

TIMER1 has a timer input, the input is without the prescaler.
Perhaps it is possible to run the ATmega at 16MHz and TIMER1 at 10MHz.
If you need absolute accuracy, creating a step in an software interrupt might not be very consistant. You need perhaps the hardware output of the timer.
It is possible to have an accurate software interrupt when there are no other interrupts. That means no use of the Serial library, and stop TIMER0 (used by Arduino for timing).

You could use a programmer and forget the bootloader.
The Arduino IDE has an option in the menu to upload a sketch with a programmer (without the bootloader).
You still have to use the custom board and avrdude is still used, but without the bootloader.

While I was writing this, I think you need a dedicated ATmega chip. Perhaps without Arduino libraries. I suggest to develop it using the Arduino and try to make it work. You can later always strip parts of the software or switch to plain ATmega code without Arduino.

For non-Arduino code, see the site
The Projects section is great for finding code.

I don’t have my own custom board settings here, I can check it later.

I suppose that the usb-ttl adapter don’t understand that baud = 72000

Either that, or the unix system calls used to change the speed in avrdude. The “standard” tcsetattr system call only accepts certain standard speeds. There is some relevant and possibly helpful discussion here:

Thank you all for your responses

Peter: I haven't a programmer for AVR's. I have a pickit2 for microchip but i suppose it is useless here. I remember I read somewhere, something about programming an AVR with an Arduino board as a programmer. If it is possible I would try. For the moment I just change the crystal on the breadboard.

It is interesting this idea and I suppose I could obtain few bytes of extra memory without the bootloader. And I think I have an Atmega chip without bootloader.

Regarding the timer1, in my application I don't need other timers or delay, millis etc. But I need a serial or I2C communication. I thought that any serial interrupt will not affect a pwm output, because the timer, compare registers etc works independently. I activated a timer interrupt for counting the steps, but in the interrupt routine I have only few lines of code and also I thought that, if this process will be perturbed by other interrupts, this will not be critical as it would not affect the pwm itself, but just the counting.

You need a programmer for the avr chips that works with avrdude. I use USBasp version 2, others use an Arduino Uno as ISP. Pickit2 is from another planet, and Picket3... well... you know... let's not go there.

Arduino as programmer : and this : and maybe this :

I2C and Serial are using interrupts. That means other interrupts could be delayed a little. Everything works independently, that is correct. You can use TIMER1 with hardware output (and maybe hardware input), and it will not be affected by interrupts or delay or whatever. An timer interrupt only for counting is okay of course.

Thank you very much.