2

Do I Have to Change Internal Interrupt Setups when I Change Clock Speed? The reason I ask is because I am trying to modify this code:

https://github.com/DccPlusPlus/BaseStation/blob/master/DCCpp_Uno/DCCpp_Uno.ino (Beginning at Line 289)

so that if will work on an ATmega328P-derivative board running at 3.3V and 8 MHz. I need the waveforms to be generated at the same speed, so is there anything I have to change? The original code is written for a 16 MHz uno or mega.

2
  • 1
    Yes, you will have to recalculate all the timer compare values (halve them for half the clock speed?)
    – Majenko
    Commented Apr 11, 2017 at 21:07
  • 1
    Halve the values and round down: 3199 → 1599, etc. Commented Apr 12, 2017 at 7:43

1 Answer 1

1

The equation for timer period is given by the following formula, where N is the value of the prescaler, TOP is the value given by either MAX (i.e. 0xFF, 0xFFFF) or the value in register OCRnx, and f is the frequency of the I/O clock.

T = (N × (TOP+1)) ÷ f

If you're using phase-correct or phase- and frequency-correct mode, the factor (TOP+1) in the formula above should be replaced by 2 × TOP.

1
  • 1
    It's not TOP. It's either 2 × TOP (in the phase-correct and phase-and-frequency-correct PWM modes) or TOP + 1 (in any other mode). Commented Nov 2, 2017 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.