diff --git a/MozziGuts_impl_AVR.hpp b/MozziGuts_impl_AVR.hpp index 05067eb1b..dd0f474dc 100644 --- a/MozziGuts_impl_AVR.hpp +++ b/MozziGuts_impl_AVR.hpp @@ -168,7 +168,11 @@ static uint8_t mozzi_TCCR4A, mozzi_TCCR4B, mozzi_TCCR4C, mozzi_TCCR4D, static void startAudio() { backupPreMozziTimer1(); Timer1.initializeCPUCycles( - F_CPU / AUDIO_RATE, + (F_CPU/AUDIO_RATE)-1, // the -1 here is a result of empirical tests + // that showed that it brings the resulting frequency + // closer to what is expected. + // see: https://github.com/sensorium/Mozzi/pull/202 + PHASE_FREQ_CORRECT); // set period, phase and frequency correct TIMSK1 = _BV(TOIE1); // Overflow Interrupt Enable (when not using // Timer1.attachInterrupt()) @@ -198,10 +202,16 @@ static void startAudio() { // pinMode(AUDIO_CHANNEL_2_PIN, OUTPUT); // set pin to output for audio # if (AUDIO_MODE == STANDARD) Timer1.initializeCPUCycles( - F_CPU / AUDIO_RATE, + (F_CPU/AUDIO_RATE)-1,// the -1 here is a result of empirical tests + // that showed that it brings the resulting frequency + // closer to what is expected. + // see: https://github.com/sensorium/Mozzi/pull/202 PHASE_FREQ_CORRECT); // set period, phase and frequency correct # else // (AUDIO_MODE == STANDARD_PLUS) - Timer1.initializeCPUCycles(F_CPU / PWM_RATE, + Timer1.initializeCPUCycles((F_CPU/PWM_RATE)-1, // the -1 here is a result of empirical tests + // that showed that it brings the resulting frequency + // closer to what is expected. + // see: https://github.com/sensorium/Mozzi/pull/202 FAST); // fast mode enables higher PWM rate # endif Timer1.pwm(AUDIO_CHANNEL_1_PIN, @@ -267,7 +277,7 @@ static void startAudio() { pinMode(AUDIO_CHANNEL_1_lowByte_PIN, OUTPUT); // set pin to output for audio, use 499k resistor Timer1.initializeCPUCycles( - F_CPU / 125000, + F_CPU/125000, FAST); // set period for 125000 Hz fast pwm carrier frequency = 14 bits Timer1.pwm(AUDIO_CHANNEL_1_highByte_PIN, 0); // pwm pin, 0% duty cycle, ie. 0 signal