Did this interrupt code brick my Arduino?

For the discussion of Arduino related topics.

Moderator: phalanx

Did this interrupt code brick my Arduino?

Postby havard » Sun Nov 06, 2011 10:47 am

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).


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);
Posts: 5
Joined: Tue Oct 19, 2010 8:59 pm

Re: Did this interrupt code brick my Arduino?

Postby stevech » Sun Nov 06, 2011 6:51 pm

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.
Support Volunteer
Posts: 2762
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Re: Did this interrupt code brick my Arduino?

Postby havard » Mon Nov 07, 2011 1:36 pm

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?
Posts: 5
Joined: Tue Oct 19, 2010 8:59 pm

Return to Arduino

Who is online

Users browsing this forum: No registered users and 7 guests