How do I make this LED blink using in interrupt?

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define SET_BIT(reg, pin)		    (reg) |= (1 << (pin))
#define CLEAR_BIT(reg, pin)		  (reg) &= ~(1 << (pin))

//Functions declaration
void setup(void);
void process(void);

void setup(void) {

	// Timer 1 in normal mode, with pre-scaler 8 ==> ~60Hz overflow.
	// Timer overflow on.
	TCCR1A = 0;
  CLEAR_BIT(TCCR1B,WGM12);
  CLEAR_BIT(TCCR1B,WGM13);
  		SET_BIT(TCCR1B,CS12);
		CLEAR_BIT(TCCR1B,CS11);
		SET_BIT(TCCR1B,CS10);
  SET_BIT(TIMSK1, TOIE1);

    // Enable B5 as output, led on B5
	SET_BIT(DDRB, 5);

	// Enable timer overflow, and turn on interrupts.
	sei();
}

volatile int overflow_counter = 0;

ISR(TIMER0_OVF_vect) {
	overflow_counter ++;
}

void process(void) {

  SET_BIT(PORTB, 5);
}

int main(void) {
	setup();
	for ( ;; ) {
		process();
      _delay_ms(1000);
	}
}

Im using timer1 to make this blink led faster when it enters the loop. I dont want it on all the time, only when it enters the process loop in main(). Been looking at this for a while but not sure what i have set incorrectly.
image

  • where is overflow_counter used outside of the interrupt
  • why not have the interrupt toggle the LED pin?
  • why is there a delay?
  • should process be monitoring the value of overflow_counter to detect when it changes (by comparing it to a companion variable)?
  • does overlfow_counter need to be anything more than a byte?

Uh... are you allowed to do I/O stuff within an interrupt routine?

nothing prevents anything from being done in an interrupt except performance.

It would be easier to code and have it run like you want if you used Arduino code.

I thought it was only used for the ISR only.
How can I do that?
I was suggested I should try delay to fix it but that person is also a rookie so i will remove.
Im not sure what you meant by the last 2.

The docs still recommend this: "Generally, an ISR should be as short and fast as possible." - attachInterrupt() - Arduino Reference

Therefor, using a flag and then doing your actual logic outside the ISR is superior to doing a digitalWrite() directly in the ISR.

The problem with OP's code is clearly that process() always sets the LED to on, and it is never set to off. So, as you have pointed out, the flag set by the ISR is never checked, or used to control anything.

as Paul suggests, there are more conventional ways of doing this, but presumably you want to do this to learn something

where is the code to toggle the LED? put those lines in the ISR

@cogoro is not using attachInterrupt() or digitalWrite(). This is not Arduino code. It might run on some Arduino, but not all.

i thought sei() had something to do with ISR. Im in a class which is teaching us microcontroller the hard way.

I'm not sure @cogoro is trying to learn anything more than flashing a led. So far, it's "the blind leading the blind".

"where is the code to toggle the LED? put those lines in the ISR"
which lines did you meant?

Why post your question on the Arduino forum? Arduino is not about using microcontrollers the hard way.

Programming microcontrollers can be hard. Why make it harder? Arduino makes it a little easier and the same Arduino code will run on many different microcontrollers, not just one particular microcontroller.

cogoro has already been clearly told what is wrong with their code (the LED is always set to "on" in processing, and the flag from the ISR is never read). So discussing if this is the right or wrong place to ask this question is kind of a waste of time.

(But I'm getting the impression that trying to help OP is also a waste of time so I'm out of here.)

If i had a choice i wouldnt have but we are given an arduino so i didnt know where to go

As is discussing what is superior, or not, to use in interrupts!

So write Arduino code!

Explain, in English, without using technical terms, what you want to achieve.

i want to able to have a blinking led. so i can add a button to this later on which lets me blink the led for couple seconds when its pressed 1 time.

is this a homework assignment?

if so, what guidance/instruction were you given?

1 Like

the outcome was to do something with all 3 timers. i have timer 0 and 2 working. but since timer1 is 16 bit, im not sure what i did incorrectly.