terminal/timer.c

26 lines
761 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <avr/interrupt.h>
#include "timer.h"
/*
* timer.c
*
* Created: 13.04.2023 14:13:50
* Author: mrnek
*/
static unsigned long millis = 0;
void setup_timer() {
TCCR1B |= (1 << WGM12); // настраиваем режим CTC (Clear Timer on Compare Match)
OCR1A = 250; // задаем значение, при котором будет срабатывать прерывание (в данном случае 15999, так как Atmega328p работает на частоте 16 МГц и нужно получить прерывание раз в 1 мс)
TIMSK1 |= (1 << OCIE1A); // включаем прерывание по совпадению со значением регистра OCR1A
sei(); // разрешаем глобальные прерывания
TCCR1B |= (1 << CS11) | (1 << CS10); // запускаем таймер с предделителем 64 (16000000/64=250000)
}
unsigned long get_millis() {
return millis;
}
ISR(TIMER1_COMPA_vect) {
++millis;
}