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