ATMega321P using internal clock with divide by 8 bit set - how to fix??

Hello All

I have managed to program my stand alone 328p but when running a 'hello world' program - blink in this case, it seems the chip has been set to use the internal 8MHz oscillator and has the divide by 8 bit set.

When I compile with a board set to 8Mhz the blink takes 8 seconds, compile for 16MHz board blink takes 16 seconds and similarly 20MHz blink takes 20 seconds. This is regardless of whether place a 16MHz crystal on my breadboard or not.

Can anyone tell me how I would go about clearing the divide by 8 bit and also to use a crystal if provided?

I have the Pololu USB AVR Programmer.

I assume I will be using avrdude but I am not sure which fuses etc.

Thanks

It sounds like you bought a ATMega328P from a vendor and didn't change the fuse bits (they normally come set to the 8MHz internal RC oscillator and with the DIV8 fuse bit selected).

Solution: Put a 16Mhz crystal with associated capacitors onto your board (XTAL1 and XTAL2). Then, burn the Optiboot bootloader to the IC (the one you get by selecting Arduino Uno in the boards menu) via your Pololu programmer. This should set the fuses correctly for your desired operation. Thereafter, you can reflash the IC with whichever sketch you were trying to do - the Arduino IDE only changes the fuse bits when attempting to flash the bootloader.

Magic

Thanks dude

Hi Aeturnalus (or anyone else that may be able to assist)

The fix recommended by Aeturnalus, ie. to load the Uno bootloader and then reload my sketch seems to have worked (after I finally figured my crystals are 6MHz and not 16MHz as ordered. We will see if Tecnobots are prepared to sort that out with no cost to me as the order is more than 6 months ago - will give feedback on that)

Anyway, it now seems that if I remove the crystal the board does not use the internal crystal and hence does not run.

I am guessing the AVR has been instructed to use the external oscillator?

How would one tell it to use the internal oscillator or alternatively to use the internal unless an external one is supplied in which case it should use the external one?

Thanks people

You're correct - the AVR has been set to expect an external 16MHz oscillator. The easiest way to set it to use the internal oscillator with the DIV8 bit off is to set the 'board' to the Lilypad and then burn the bootloader, since that's how the Lilypad Arduino does its timing. As far as I know, there isn't a way to swap between oscillator settings without reburning the fuses - someone with more AVR experience would be helpful here.

Since crystals are cheap, it might be beneficial for you to just order them from a electronics parts supplier instead of trying to work through a returns process - the highest cost would be shipping, although a local brick'n'mortar store would get rid of that problem too.

Thanks Aeturnalus

Our local components store sells them at almost 5 times the price of getting them from technobots. Problem is technobots charges £2.99 for postage :o

Given I have spent £100 at that store I do hope they sort me out - it is the principle that matters now.

Thanks again for your help :slight_smile:

Just an update... Technobots (UK) said they will send out two 16MHz components to replace the 6MHz ones the mistakenly sent me and this is at no additional cost to me, so I will continue using that store :slight_smile:

Have you tried to set the fuses back ?

Hey fuh

I am afraid I have not done anything with fuses yet, so I don't know which or how...

Did you bought the m328p blank and burned after the bootloader on it ?

Hey fuh

That is correct. A blank ATMega328p which seemed to be set to use the internal clock with the divide by 8 bit set.

But were you able to bootstrap it or you directly burning the program without any bootloader.

I initially programmed it directly without loading a bootloader (since I have a Pololu USB AVR Programmer).

I then found the problems regarding the timing as described in my first post in this thread when Aeturnalus suggested I burn the bootloader which would have the side effect of setting the fuses? to use the external oscillator. I then loaded my blink sketch over the bootloader...

While I have the AVR programmer I will not use bootloaders.

The bootloader do not have nothing to do with the clock; you just have to tell avr-gcc to compile to a specific speed, that's one of the reasons you have to select the board from the menu.
So the fuses are the ones that will the the chip what's the clock we will be using, the way to go is to change the fuses on the m328p to be set 0xff 0xde 0x05 (low high extended).

Please pay attention that you can "break" the chip by setting a strange clock..