ATtiny13 timer + interrupt
Pre overenie funkcie časovača a prerušenia je zapojenie obvodu jednoduché:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#define LED0 0
#define LED4 4
#define SWITCH3 3
volatile int hodnota=0;
int main (void)
{
DDRB |= (1 << LED0); // definujem vystupny port pre LED
DDRB |= (1 << LED4); // definujem vystupny port pre LED
DDRB &= ~(1 << SWITCH3); // nastavujem vstup pre tlacitko
PORTB |= (1 << SWITCH3); //aktivujem pull-up
GIMSK |= _BV (PCIE); // Povolenie externych preruseni
PCMSK |= (1<<PCINT3);
TCCR0A |= (1<<WGM01); // Casovac do CTC modu
TIMSK0 |= (1<<OCIE0A); // Povolenie CTC prerusenia
TCCR0B |= ((1 << CS02) | (1 << CS00)); // spomalenie CLOCK/1024
OCR0A = 0xFD; //hodnota do casovaca
sei();
while (1) {
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_mode();
}
}
ISR(TIM0_COMPA_vect)
{
//predlzenie casoveho intervalu 10x
if (hodnota++ > 10) {
PORTB ^= (1 << LED0); // LED on/off
hodnota=0;
// ak svieti LED aktivovana tlacitkom, zhasnem ju
PORTB &= ~(1 << LED4);
}
}
//stlacene tlacitko, rozsvietim LED
ISR(PCINT0_vect)
{
PORTB |= (1 << LED4);
}

- Pridať nový komentár
- prečítané 989x
