Skip to content

Commit

Permalink
Merge pull request #202 from tomcombriat/AVR_misFreq
Browse files Browse the repository at this point in the history
Fix frequency offset for AVR
  • Loading branch information
tomcombriat authored Oct 24, 2023
2 parents 349b8ad + 523ba59 commit cf9e173
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions MozziGuts_impl_AVR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cf9e173

Please sign in to comment.