MiscEl: MPU Timers

Back to MiscEl main page Write a message to author


This page is used to calculate values for MPU interrupt timer and software timers


Input values

ClockMain MPU clock or whatever clock is used for the interrupt timer
Timer N secondsPeriode time for this software timer, only fill either seconds or herts
Timer N hertzFrequence for this software timer, only fill either seconds or herts
Timer N checkmarkThis timer is important, try to reduce the timing error

Output values

RequestSame as "Timer N" value, but shown both as time and frequence
Best clock dividerOptimal divider value for system clock, includes information about errors
Actuel clock dividerUsed divider value for system clock, calculated from all chained timers, includes information about errors
Clock dividerBest and actuel divider is the same
---- div. byShow input source for this timer and divide factor
ResultWhat 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


Other pages