Files
embedded-programming/Timer-Interrupt/main.c
2026-04-06 21:25:29 +10:00

39 lines
618 B
C

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
volatile uint32_t ms = 0;
void timer1_init(void) {
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TCCR1B |= (1 << WGM12);
OCR1A = 249;
TIMSK1 |= (1 << OCIE1A);
TCCR1B |= (1 << CS11) | (1 << CS10);
sei();
}
ISR(TIMER1_COMPA_vect) {
ms++;
}
void delay_ms(uint32_t delay){
uint32_t start = ms;
while ((ms - start) < delay) {
}
}
int main(void) {
DDRC |= (1<<PC0);
timer1_init();
while(1) {
PORTC |= (1<<PC0);
delay_ms(500);
PORTC &= ~(1<<PC0);
delay_ms(500);
}
}