This page is used to calculate values for MPU interrupt timer and software timers
Input values
| Clock | Main MPU clock or whatever clock is used for the interrupt timer |
| Timer N seconds | Periode time for this software timer, only fill either seconds or herts |
| Timer N hertz | Frequence for this software timer, only fill either seconds or herts |
| Timer N checkmark | This timer is important, try to reduce the timing error |
| | |
Output values
| Request | Same as "Timer N" value, but shown both as time and frequence |
| Best clock divider | Optimal divider value for system clock, includes information about errors |
| Actuel clock divider | Used divider value for system clock, calculated from all chained timers, includes information about errors |
| Clock divider | Best and actuel divider is the same |
| ---- div. by | Show input source for this timer and divide factor |
| Result | What the actuel time and frequence will be with the calculated values |
Programming hints
To get exact timer interrupts it is required to compensate for interrupt timer programming time on most MPU's. This is most easily done with a timer/counter running at full clock speed. Use the following sequence at the start of the interrupt routine:
read timer
add timer initialization value minus time for this code
write timer
Here is an actuel example with AVR assembler code:
.def StatusIntrSave = r1
.def IR1 = r25
.equ TimerValue = 256-120
Timer0Intr:
in StatusIntrSave,sreg ;Store Status Register
in IR1,tcnt0 ;Compensate for responce time
subi IR1,-(TimerValue+3) ;This may only be used with x1 timer
out tcnt0,IR1
;user stuff
out sreg,StatusIntrSave
reti
The total overhead for this interrupt routine is 16 clocks + user stuff, this code will on the average be running once every 120 clock cycles (Defined in the .equ line). The routine will compensate for slow interrupt responce (up to about 110 clocks delayed), this may be due to disabled interrupt or slow instructions.
For timers with a prescaler the programming must be syncronized with the prescaler, this means that some extra (and time wasting code) must be added:
wait until timer change
read timer
add timer initialization value minus time for this code (compensated for prescaler)
write timer
Be very carefull with the wait part, this will probally take a variable amount of time after the prescaler changes, and ALL possiblities has to have the same amount of compensation in the "add" code!
Timers that does automatic reload from a initialization register does not need this stuff, they will allways be exact!
The assumptions and rules used are
- No clocks are lost
- When possible prescaler values of 2, 4 and 8 are avoided (will lose cycles on some MPU's)
- No interrupt faster than 32 clock are possible
- Timers running at interrupt frequence are 8 bit
- Timers running below interrupt frequence can be 16 to 32 bits
- A extra timer will be used when fastest timer requires more than 8 bits
Other pages
- MPU Support list where MiscEl can help when working with microprocessors