Go Down

Topic: Changing clock speed makes Arduino unprogrammable [SOLVED] (Read 90 times) previous topic - next topic

jbeeston

I thought I'd post this as I almost broke one of my ATMega328 chips last night while learning how to use the various sleep modes and low power options.

I changed the clock ratio down to 1/128 th of the normal clock speed by setting CLKPR values in my sketch. But it seems that setting these in the sketch fixes the chip into that speed even through a power cycle. Presumably the same issue would occur for 1/256th of the clock speed and maybe some lesser dividers.

The code I used to get in this little predicament was:

Code: [Select]
void setup() {
  CLKPR=0x80;
  CLKPR=0x7;
}


Once that was uploaded it did do as intended - slow the cpu down to 1/128th of it's normal clock speed. But when I next went to upload the sketch again to remove the slowdown it failed. I should point out that through all of this I was programming an Arduino on a breadboard using a USBasp module. When attempting to upload it would say target isn't answering.

After reading through some old threads on here I heard that the passing some extra options to avrdude might slow down it's programming enough to work. This thread At first I couldn't figure out how to run avrdude manually from the command line as it complained about cygwin1.dll being missing. Turns out you just need to be in the root Arduino directory where arduino.exe and cygwin1.dll exist (obvious now). What I did was turn on the verbose output during upload option in the Arduino IDE and then cut and paste the call to avrdude. I tried adding the -i 40 option as suggested in the other thread but it still didn't work, so I tried other values rather randomly and eventually when I tried -i 10 it successfully programmed the chip again where I was able to get rid of the clock speed slowdown. You might need slightly different values for other slow clock ratios.

Command line used was as follows:
Code: [Select]
C:\arduino-1.5.8>c:\arduino-1.5.8/hardware/tools/avr/bin/avrdude -CC:\arduino-1.5.8/hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega328p -i 10 -cusbasp -Pusb -Uflash:w:C:\Users\jbeeston\AppData\Local\Temp\build3386211458186181035.tmp/B link_Test_Atmega328.cpp.hex:i

By cutting and pasting the same call to avrdude as used by the IDE it references the same freshly compiled hex image which contains the fixed clock prescaler and once it succeeds the chip is then fixed.

Hope this might help someone else out if they get into the same situation I did.

Riva

If you have a spare pin then it might be worth putting a 'get out of jail free' condition around the clock code. Something like below will bypass the clock code if you ground pin 9
Code: [Select]
void setup() {
  pinMode(9,INPUT_PULLUP);
  if (digitalRead(9) == HIGH){
    CLKPR=0x80;
    CLKPR=0x7;
  }
}

KenF

If you have a spare pin then it might be worth putting a 'get out of jail free' condition around the clock code. Something like below will bypass the clock code if you ground pin 9
Code: [Select]
void setup() {
  pinMode(9,INPUT_PULLUP);
  if (digitalRead(9) == HIGH){
    CLKPR=0x80;
    CLKPR=0x7;
  }
}

Shouln't that be == LOW ?

Riva

Shouln't that be == LOW ?
Quote
Something like below will bypass the clock code if you ground pin 9
Clock change code will normally run with nothing on the pin but if you ground pin 9 before applying power it will not.

jremington

Quote
But it seems that setting these in the sketch fixes the chip into that speed even through a power cycle
Not according to the ATmega328 data sheet. According to the data sheet, upon power up, the CLKPS bits of CLKPR register are set to one of two default values for the clock division bits, as follows:

Quote
The CKDIV8 Fuse determines the initial value of the CLKPS bits. If CKDIV8 is unprogrammed,
the CLKPS bits will be reset to "0000". If CKDIV8 is programmed, CLKPS bits are reset to
"0011", giving a division factor of 8 at start up.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy