day04/ex01/include/timer1.h

123 lines
2.1 KiB
C
Raw Permalink Normal View History

2026-04-17 18:20:13 +02:00
#ifndef TIMER1_H
#define TIMER1_H
#include <avr/io.h>
#include "mystd.h"
#include "timer_global.h"
static inline void t1_init_fpwm_14(e_timer_prescaler prescaler) {
// Fast PWM (8-bit): WGM22:0 = 0b011
TCCR1A = _BV(WGM11) | _BV(WGM12);
TCCR1B = _BV(WGM13);
// set the correct prescaler
switch (prescaler) {
case (PRESCALER_1): {
TCCR1B |= (_BV(CS10));
break;
}
case (PRESCALER_8): {
TCCR1B |= (_BV(CS11));
break;
}
case (PRESCALER_64): {
TCCR1B |= (_BV(CS11) | _BV(CS10));
break;
}
case (PRESCALER_256): {
TCCR1B |= (_BV(CS12));
break;
}
case (PRESCALER_1024): {
TCCR1B |= (_BV(CS12) | _BV(CS10));
break;
}
case (PRESCALER_OFF): {
break;
}
}
}
static inline void t1_set_icr1(uint16_t value) {
ICR1 = value;
}
static inline void t1_overflow_interrupt(bool enable) {
if (enable)
TIMSK1 |= _BV(TOIE1);
else
TIMSK1 &= ~_BV(TOIE1);
}
static inline void t1_interrupt(enum e_timer_output output, bool enable) {
if (output & TO_A) {
if (enable)
TIMSK1 |= _BV(OCIE1A);
else
TIMSK1 &= ~_BV(OCIE1A);
}
if (output & TO_B) {
if (enable)
TIMSK1 |= _BV(OCIE1B);
else
TIMSK1 &= ~_BV(OCIE1B);
}
}
2026-04-17 23:01:35 +02:00
static inline void t1_set_ocr(enum e_timer_output output, uint16_t value) {
2026-04-17 18:20:13 +02:00
if (output & TO_A)
OCR1A = value;
if (output & TO_B)
OCR1B = value;
}
static inline void t1_set_out_mode(enum e_timer_output output, enum e_timer_output_mode mode) {
if (output & TO_A) {
TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
switch (mode) {
case (TOM_00): {
break;
}
case (TOM_10): {
TCCR1A |= (_BV(COM1A1));
break;
}
case (TOM_01): {
TCCR1A |= (_BV(COM1A0));
break;
}
case (TOM_11): {
TCCR1A |= (_BV(COM1A1) | _BV(COM1A0));
break;
}
}
}
if (output & TO_B) {
TCCR1A &= ~(_BV(COM1B1) | _BV(COM1B0));
switch (mode) {
case (TOM_00): {
break;
}
case (TOM_10): {
TCCR1A |= (_BV(COM1B1));
break;
}
case (TOM_01): {
TCCR1A |= (_BV(COM1B0));
break;
}
case (TOM_11): {
TCCR1A |= (_BV(COM1B1) | _BV(COM1B0));
break;
}
}
}
}
// OC2B => RED => PD3
// OC0B => GREEN => PD5
// OC0A => BLUE => PD6
#endif /* TIMER1_H */