fix timer 3 unavailable on m324

use timer 0 (whith less precission)
master
Eggert Jung 7 months ago
parent c5fc30dfb3
commit 1fee740458

@ -80,30 +80,49 @@ void pwm_init (void)
TCCR1B = _BV(CS10) | _BV(WGM12); //fast PWM (8 bit), clk/1 source
//setup sample timer (CTC Mode)
#if defined(__AVR_ATmega1284P__)
TCCR3A = 0;
TCCR3B = _BV(CS30) | _BV(WGM32); // clk/1
#elif defined(__AVR_ATmega324PA__)
TCCR0A = _BV(WGM01);
TCCR0B = _BV(CS01); // clk/8
#endif
}
void pwm_deinit (void)
{
TCCR1A = 0;
TCCR1B = 0;
#if defined(__AVR_ATmega1284P__)
TCCR3A = 0;
TCCR3B = 0;
#elif defined(__AVR_ATmega324PA__)
TCCR0A = 0;
TCCR0B = 0;
#endif
}
void pwm_gen(uint16_t tone, uint16_t periods)
{
//calculate single sine sample duration
#if defined(__AVR_ATmega1284P__)
OCR3A = F_CPU / tone / sizeof(sine_lookup) - 1;
#elif defined(__AVR_ATmega324PA__)
OCR0A = F_CPU / 8 / tone / sizeof(sine_lookup) - 1;
#endif
for (uint16_t period=0; period < periods; ++period) {
for (uint8_t spl=0; spl < sizeof(sine_lookup); ++spl) {
//wait for timer ready for next spl
#if defined(__AVR_ATmega1284P__)
loop_until_bit_is_set(TIFR3, OCF1A);
TIFR3 = _BV(OCF1A); //clear OCF
#elif defined(__AVR_ATmega324PA__)
loop_until_bit_is_set(TIFR0, OCF0A);
TIFR0 = _BV(OCF0A); //clear OCF
#endif
OCR1A = pgm_read_byte(sine_lookup + spl) + 0x70;
}
}
}
}

Loading…
Cancel
Save