#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#define PIN_RELAY PINB0 //Пин реле
#define PIN_BUTTON PINB1 //Пин кнопки
#define DELAY_RELAY_ON 5000 //Задержка после нажатия кнопки
uint16_t micro_timer = 0;
uint16_t sec_timer = 0;
void _loop() {
if(sec_timer > DELAY_RELAY_ON) {
sec_timer = 0;
PORTB &= ~(1<< PIN_RELAY);
}
else {
if(!(PINB & (1<<PIN_BUTTON)) && !sec_timer) {
sec_timer = 1;
PORTB |= (1 << PIN_RELAY); // вклчаем
}
}
}
ISR(TIMER0_COMPA_vect) {
micro_timer++;
if(micro_timer > 100) {
micro_timer = 0;
if(sec_timer) sec_timer++;
}
_loop();
}
void timer_init() {
TCCR0A = (1<<WGM01);
TCCR0B = (1<<CS01);
OCR0A = 1;
TIMSK = (1<<OCIE0A);
sei();
}
int main(void)
{
DDRB |= (1 << PIN_RELAY); // устанавлеваем как выход
PORTB &= ~(1<< PIN_RELAY); //выключаем рел
DDRB &= ~(1 << PIN_BUTTON); // устанавливаем как вход;
PORTB |= (1 << PIN_BUTTON); //подтягиваем
timer_init();//настраиваем таймерж
}