day03/ex03/include/timer0.h

113 lines
2 KiB
C
Raw Normal View History

2026-04-16 17:29:49 +02:00
#ifndef TIMER0_H
# define TIMER0_H
# include <avr/io.h>
# include "mystd.h"
# include "timer_global.h"
static inline void t0_init_fpwm_3(e_timer_prescaler prescaler) {
// Fast PWM (8-bit): WGM22:0 = 0b011
TCCR0A = _BV(WGM00) | _BV(WGM01);
// reset to zero -> timer off
TCCR0B &= ~(_BV(CS02) | _BV(CS01) | _BV(CS00));
// set the correct prescaler
switch (prescaler) {
case (PRESCALER_1): {
TCCR0B |= (_BV(CS00));
break;
}
case (PRESCALER_8): {
TCCR0B |= (_BV(CS01));
break;
}
case (PRESCALER_64): {
TCCR0B |= (_BV(CS01) | _BV(CS00));
break;
}
case (PRESCALER_256): {
TCCR0B |= (_BV(CS02));
break;
}
case (PRESCALER_1024): {
TCCR0B |= (_BV(CS02) | _BV(CS00));
break;
}
case (PRESCALER_OFF): {
break;
}
}
}
static inline void t0_set_ocr(enum e_timer_output output, uint8_t value) {
if (output & TO_A)
OCR0A = value;
if (output & TO_B)
OCR0B = value;
}
static inline void t0_set_out_mode(enum e_timer_output output, enum e_timer_output_mode mode) {
if (output & TO_A) {
TCCR0A &= ~(_BV(COM0A1) | _BV(COM0A0));
switch (mode) {
case (TOM_00): {
break;
}
case (TOM_10): {
TCCR0A |= (_BV(COM0A1));
break;
}
case (TOM_01): {
TCCR0A |= (_BV(COM0A0));
break;
}
case (TOM_11): {
TCCR0A |= (_BV(COM0A1) | _BV(COM0A0));
break;
}
}
}
if (output & TO_B) {
TCCR0A &= ~(_BV(COM0B1) | _BV(COM0B0));
switch (mode) {
case (TOM_00): {
break;
}
case (TOM_10): {
TCCR0A |= (_BV(COM0B1));
break;
}
case (TOM_01): {
TCCR0A |= (_BV(COM0B0));
break;
}
case (TOM_11): {
TCCR0A |= (_BV(COM0B1) | _BV(COM0B0));
break;
}
}
}
}
// OC2B => RED => PD3
// OC0B => GREEN => PD5
// OC0A => BLUE => PD6
#endif /* TIMER0_H */
/*
// OC2B = PD3 → output
DDRD |= _BV(DDD3) | _BV(DDD5) | _BV(DDD6);
// Fast PWM (8-bit): WGM22:0 = 0b011
TCCR0A = _BV(WGM00) | _BV(WGM01);
TCCR0A |= _BV(COM0B1);
// 50% duty cycle
OCR0B = 128;
// Start timer, prescaler = 64
TCCR0B = _BV(CS02);
*/