SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By havard
#135435
So I was playing around with interrupts. Uploading was working without problem, and everything was fine. Until I uploaded the sketch below. It also worked, blinked the LED at pin 13 twice a second. I thought I'd change that, and tried to upload something new, but then the programmer could no longer upload. It fails with the dreaded error "avrdude: ser_recv(): programmer is not responding, avrdude: stk500_recv(): programmer is not responding".

I've tried resetting, tried unplugging and re-plugging, holding the reset-button till right before the programmer kicks in. I've tried different baud rates. I've tried removing the avrdude -D switch (Disable auto erase for flash). No luck. I can no longer program the chip, while the light is blinking away happily. Oh, and the usual tx/rx blinks when programming never occurs.

It seems the code below is overloading the chip? Any other ideas before I have to start looking into programming it off a bread board?

Here's the sketch of death. (Underscores deliberately added, so nobody will run this without effort).

*** PLEASE DO NOT RUN THIS LIKE I DID! ***
Code: Select all
static volatile uint8_t state = 0;

void __setup__() {
  pinMode(13, OUTPUT);
  
  uint16_t speed = 2;
  uint16_t ocr1a = (F_CPU / 8UL) / speed;
   
  TCCR1A = 0;
  TCCR1B = _BV(WGM12) | _BV(CS10);
  
  OCR1A = ocr1a;
  TIMSK1 |= _BV(OCIE1A);
}

ISR__(TIMER1_COMPA_vect) {
  state = ~state;
}

void loop__() {
  digitalWrite(13, state);
}
By stevech
#135440
not bricked. Maybe stuck in an interrupt loop and hard but not impossible to get it into the still-there bootloader.
The ISR fails to reset the cause of the interrupt. All ISRs must do so. In this case, it's the timer.
By havard
#135478
But why is my timer code such a disaster? Was the value to OCR1A out of bounds? There was a comment where I found this (in the Virtual Wire library), that there was something special with 16 bit values. The default value for "speed" in that use case was 2000, which would have made "ocr1a" in the code above 1000. So still more than 8 bits. In my case, I used speed=2, which made ocr1a 1.000.000. Major overflow of the 16 bits value, but should result in 16960, if I understand this correctly.

But anyway, what would have been the better way to set these interrupt options?