day06/ex00/include/timer1.h

160 lines
2.7 KiB
C
Raw Normal View History

2026-04-21 16:40:57 +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);
TCCR1B = BV(WGM12) | 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_init_ctc_4(e_timer_prescaler prescaler) {
// CTC mode 4
TCCR1A = 0;
TCCR1B = BV(WGM12);
// 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_counter(uint16_t val) {
TCNT1 = val;
}
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);
}
}
static inline void t1_set_ocr(enum e_timer_output output, uint16_t value) {
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 */