chore(macros/ex02): used homebrew BV macro and fixed ex02 to use timer interupt

This commit is contained in:
Maix0 2026-04-19 15:38:04 +02:00
parent 508ba6e857
commit 29b0d72ff9
17 changed files with 191 additions and 200 deletions

View file

@ -8,30 +8,30 @@
static inline void t2_init_fpwm_3(e_timer_prescaler prescaler) {
// Fast PWM (8-bit): WGM22:0 = 0b011
TCCR2A = _BV(WGM20) | _BV(WGM21);
TCCR2A = BV(WGM20) | BV(WGM21);
// reset to zero -> timer off
TCCR2B &= ~(_BV(CS22) | _BV(CS21) | _BV(CS20));
TCCR2B &= ~(BV(CS22) | BV(CS21) | BV(CS20));
// set the correct prescaler
switch (prescaler) {
case (PRESCALER_1): {
TCCR2B |= (_BV(CS20));
TCCR2B |= (BV(CS20));
break;
}
case (PRESCALER_8): {
TCCR2B |= (_BV(CS21));
TCCR2B |= (BV(CS21));
break;
}
case (PRESCALER_64): {
TCCR2B |= (_BV(CS21) | _BV(CS20));
TCCR2B |= (BV(CS21) | BV(CS20));
break;
}
case (PRESCALER_256): {
TCCR2B |= (_BV(CS22));
TCCR2B |= (BV(CS22));
break;
}
case (PRESCALER_1024): {
TCCR2B |= (_BV(CS22) | _BV(CS20));
TCCR2B |= (BV(CS22) | BV(CS20));
break;
}
case (PRESCALER_OFF): {
@ -42,23 +42,23 @@ static inline void t2_init_fpwm_3(e_timer_prescaler prescaler) {
static inline void t2_overflow_interrupt(bool enable) {
if (enable)
TIMSK2 |= _BV(TOIE2);
TIMSK2 |= BV(TOIE2);
else
TIMSK2 &= ~_BV(TOIE2);
TIMSK2 &= ~BV(TOIE2);
}
static inline void t2_interrupt(enum e_timer_output output, bool enable) {
if (output & TO_A) {
if (enable)
TIMSK2 |= _BV(OCIE2A);
TIMSK2 |= BV(OCIE2A);
else
TIMSK2 &= ~_BV(OCIE2A);
TIMSK2 &= ~BV(OCIE2A);
}
if (output & TO_B) {
if (enable)
TIMSK2 |= _BV(OCIE2B);
TIMSK2 |= BV(OCIE2B);
else
TIMSK2 &= ~_BV(OCIE2B);
TIMSK2 &= ~BV(OCIE2B);
}
}
@ -71,41 +71,41 @@ static inline void t2_set_ocr(enum e_timer_output output, uint8_t value) {
static inline void t2_set_out_mode(enum e_timer_output output, enum e_timer_output_mode mode) {
if (output & TO_A) {
TCCR2A &= ~(_BV(COM2A1) | _BV(COM2A0));
TCCR2A &= ~(BV(COM2A1) | BV(COM2A0));
switch (mode) {
case (TOM_00): {
break;
}
case (TOM_10): {
TCCR2A |= (_BV(COM2A1));
TCCR2A |= (BV(COM2A1));
break;
}
case (TOM_01): {
TCCR2A |= (_BV(COM2A0));
TCCR2A |= (BV(COM2A0));
break;
}
case (TOM_11): {
TCCR2A |= (_BV(COM2A1) | _BV(COM2A0));
TCCR2A |= (BV(COM2A1) | BV(COM2A0));
break;
}
}
}
if (output & TO_B) {
TCCR2A &= ~(_BV(COM2B1) | _BV(COM2B0));
TCCR2A &= ~(BV(COM2B1) | BV(COM2B0));
switch (mode) {
case (TOM_00): {
break;
}
case (TOM_10): {
TCCR2A |= (_BV(COM2B1));
TCCR2A |= (BV(COM2B1));
break;
}
case (TOM_01): {
TCCR2A |= (_BV(COM2B0));
TCCR2A |= (BV(COM2B0));
break;
}
case (TOM_11): {
TCCR2A |= (_BV(COM2B1) | _BV(COM2B0));
TCCR2A |= (BV(COM2B1) | BV(COM2B0));
break;
}
}
@ -120,15 +120,15 @@ static inline void t2_set_out_mode(enum e_timer_output output, enum e_timer_outp
/*
// OC2B = PD3 → output
DDRD |= _BV(DDD3) | _BV(DDD5) | _BV(DDD6);
DDRD |= BV(DDD3) | BV(DDD5) | BV(DDD6);
// Fast PWM (8-bit): WGM22:0 = 0b011
TCCR2A = _BV(GM20) | _BV(GM21);
TCCR2A |= _BV(COM2B1);
TCCR2A = BV(GM20) | BV(GM21);
TCCR2A |= BV(COM2B1);
// 50% duty cycle
OCR0B = 128;
// Start timer, prescaler = 64
TCCR2B = _BV(CS22);
TCCR2B = BV(CS22);
*/