Create library for software timers and enable it

This commit is contained in:
2020-11-08 13:02:08 +01:00
parent 7a586e8643
commit 0378d53456
3 changed files with 35 additions and 1 deletions

3
main.c
View File

@@ -1,6 +1,6 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "ptimer.h"
#include "i2c.h"
#include "rtc.h"
#include "led.h"
@@ -11,6 +11,7 @@
int main()
{
ptimer_init();
i2c_init(I2C_BITRATE);
rtc_int0_init();
led_init();

23
ptimer.c Normal file
View File

@@ -0,0 +1,23 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include "ptimer.h"
volatile uint16_t timers[TIMERS_COUNT];
void ptimer_init(void)
{
for(uint8_t i=0; i<TIMERS_COUNT; ++i) timers[i] = 0;
TCCR2 |= (1<<WGM21); // CTC Mode
TCCR2 |= (1<<CS22) | (1<<CS21) | (1<<CS20); // Prescaler 1024
OCR2 = 78; // ~100Hz
TIMSK |= (1<<OCIE2); // Enable CTC interruptions
}
ISR(TIMER2_COMP_vect)
{
for(uint8_t i=0; i<TIMERS_COUNT; ++i)
{
if(timers[i]) --timers[i];
}
}

10
ptimer.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef __PTIMER_H__
#define __PTIMER_H__
#define TIMERS_COUNT 0
extern volatile uint16_t timers[TIMERS_COUNT];
void ptimer_init(void);
#endif