ATmega88 @ 8MHz Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* timer_01.c ATmega88 @ 8MHz */ | |
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#define statusled (1<<PB1) // LED an PB1 | |
uint16_t timer_overflow_counter = 0; // Fehler im Buch: uint8_t kann nur Werte bis 255 speichern | |
void init_timer_0 (void) | |
{ | |
TCCR0A = 0x00; | |
TCCR0B = (1<<CS01) + (1<<CS00); // Prescaler = 64 -> 8Mhz/64 = 125000 | |
TIMSK0 = (1<<TOIE0); // Timer0 overflow interrupt enable | |
TCNT0 = 131; // Timer mit 131 vorladen | |
} | |
int main(void) | |
{ | |
DDRB = 0xFF; // PORTB Richtungsregister = Ausgang | |
init_timer_0(); // Timer 0 initialisieren | |
sei(); // Interrupts einschalten | |
while(1) | |
{ | |
asm ("NOP"); // Nichts tun | |
} | |
} | |
ISR(TIMER0_OVF_vect) // Timer Interrupt wird alle 1ms ausgeführt | |
{ | |
TCNT0 = 131; // Timer0 erneut mit 131 vorladen | |
if (timer_overflow_counter <= 999) // Zählvariable 999 mal hochzählen | |
timer_overflow_counter ++; | |
else // Den else-Zweig beim 1000. Durchlauf (1 Sekunde) einmal ausführen | |
{ | |
PORTB ^= statusled; // Status LED toggeln | |
timer_overflow_counter = 0; // Zähler zurücksetzen | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment