1

I want to run an atmega328 on a breadboard at 2MHz and upload to it via the serial pins (pins 2 and 3). So I am setting the clock divide by 8 fuse bit to enable this, meaning the atmega runs at 2MHz eith the 16MHz crystal. But I can't upload, probably because the bootloader is not expecting the clock to be running at 2MHz. The baud rates are probably mismatched by 8 times, the IDE is trying to upload at 57600 but since the bootloader code is not aware its being clocked at 2MHz, its probably running 8 times too slow. I've messed around a bit with the arduino conf files changing baud rates but had no joy. I was wondering if anyone knows how to do this off the top of their head.

Thanks, Pete

  • Edit the programmers.txt file somewhere inside the arduino folder. Duplicate the avrisp section. Replace avrisp. with avrispslow. or something, and change the .name=AVR ISP with .name=AVR ISP Slow. Add avrispslow.speed=7200 and avrispslow.program.speed=7200. Restart the IDE. You should now have a new item in the Boards=>Programmers menu. – Gerben Sep 9 '15 at 9:08
  • 1
    Thanks, thats basically what I was looking for but in the end it didn't work. I believe the reason is that my PC do that baud rate without certain extra measures which are not practical (ie using new style termios2 struct for serial access). So I ended up using Ignacios method. – Pete Sep 10 '15 at 12:58
2

Unprogram the CKDIV8 fuse and add the following to the sketch instead, near the beginning:

#include <avr/power.h>

void setprescaler(void) __attribute__ ((naked)) \
  __attribute__ ((section(".init1")));

void setprescaler(void)
{
  clock_prescale_set(clock_div_8);
}

This will do essentially the same thing as the fuse, but won't interfere with the bootloader.

  • Is there not a risk to running the atmega out of its SOA even if only for a few cycles? I guess I could use that technique while working on the project and change to fuse divider for 'production' but I was wondering if theres either a setting in the IDE to modify the serial speed during upload, or a easy way to set the baud in the bootloader, even if it means recompiling it. – Pete Sep 8 '15 at 20:31
  • What voltage are you running it at, 1.8V? – Ignacio Vazquez-Abrams Sep 8 '15 at 20:33
  • If you are running at 1.8V, I don't see how you can be using a serial upload, because the voltages will be too high. If you are running at 5V you can do what Ignacio Vazquez-Abrams suggested. – Nick Gammon Sep 8 '15 at 22:01
  • No I'm initially trying at 5v just to get it working before dropping to 3.6, the project has a SD card (currently unplugged) which determines the voltage. So I also have to work on it at 3.6 to avoid damage to the SD. I use the usbasp and today, I bricked 2 atmega328s. I only have one left until my next order turns up. What's happening is if I forget to switch the usbasp back to 5v before uploading, the fuses get set to 0, setting the reset pin to disable, bricking the chip. PITA. – Pete Sep 8 '15 at 22:11
  • Don't know why this happens but it does (I get an error at the end of upload about fuse settings not matching, despite code upload apparently succeeding). Since I need 3.3v for SD I have to clock lower than 12MHz but only have 16MHz crystals hence divider bit. Plan is to use serial instead of USBASP to avoid fuse bits being touched during upload. All works fine if I disable clock divider, so problem is related to speed. – Pete Sep 8 '15 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.