Hello. I want to handle a button and blinking LED in main function with software timer in the interrupt. It working at one side: or blinking or button and doesn't work properly together.
Could someone explain where I have a brake. I'm looking at code and...
I have looked at "state machine" thread, but should I apply this principle here in simple case?
https://www.avrfreaks.net/forum/t...
Thanks.
#define F_CPU 1200000UL #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> volatile uint16_t timer_counter=0; #define BLINK_LED 500 ISR (TIM0_COMPA_vect) { timer_counter++; if (timer_counter >= BLINK_LED) { timer_counter=0; } } void setup () { // LEDs on PB2, PB3. DDRB = 0b00011101; PORTB = 0b00100010; // Analog comparator OFF. ACSR |= (1 << ACD); // Start timer T0 with prescaler 8. TCCR0B |= (1<<CS01); // Enable time T0 overflow interrupt. TIMSK0 |= (1<<OCIE0A); // Enable CTC Mode. TCCR0A |= (1<<WGM01); // T0 will overflow each 1 ms. OCR0A = 150; // Reset timer T0 flags. TIFR0 = 0; } int main(void) { setup (); sei(); while(1) { if (timer_counter == 0) { PORTB ^= (1<<PB3); } if (!(PINB & (1<<PB1))) { PORTB |= (1<<PB2); } else { PORTB &=~(1<<PB2); } } }