2009年12月1日 星期二

Timer tutorial include prescalers

timer tutorial (incl prescalers)

Delays@
Andrew Warren [fastfwd at ix.netcom.com] of Fast Forward Engineering San Diego, California says:
timer-0 has four components: The clock input (either from the TMR0 pin or from the internal instruction clock), the TMR0 prescaler, the TMR0 register, and the TMR0 interrupt flags (GIE, T0IE, and T0IF).

CLOCK INPUT:

You'll probably use the internal instruction clock as your TMR0 input; I'll assume that your PIC is running at 4 MHz. Since there's an internal divide-by-4 between the oscillator frequency and the instruction clock, this means that instruction clocks occur at a 1 MHz rate.

prescaler:

The TMR0 prescaler is set (via 4 bits in the OPTION register) to divide-by-1, -2, -4, -8, -16, -32, -64, -128, or -256. The TMR0 input clock (1 MHz in your case) is passed to the prescaler, whose divided-down output is then fed to the TMR0 register.
For example, if the TMR0 prescaler is set to divide-by-4 and the PIC is running at 4 MHz, the prescaler will send a 250 KHz clock to the TMR0 register.

TMR0 REGISTER:

The TMR0 register can be preloaded with any 8-bit value that you like.
Each clock pulse from the prescaler increments the contents of the TMR0 register. When the value in the TMR0 register rolls over from 0xFF to 0x00, the T0IF flag is set (the TMR0 register continues to be incremented on every pulse from the prescaler, though).

INTERRUPT FLAGS:

If the GIE and T0IE flags are set when the T0IF flag is set, an interrupt is generated (the GIE bit is automatically cleared (to temporarily prevent further interrupts while your interrupt routine is executing), and the PIC jumps to the "interrupt vector" at location 0x04. Your interrupt-service routine at that location should check the T0IF flag to determine the source of the interrupt, then must clear the T0IF flag to prevent the PIC from immediately jumping back to the interrupt routine when interrupts are re-enabled.
At this point in the interrupt routine, you can re-load the RTCC with any appropriate value.
When you're finished handling the interrupt, your code should execute a RETFIE instruction, which will automatically set the GIE bit to re-enable interrupts and return to your main program.
Luis F says:
If you want the delay in seconds:
Delay = (256 - InitTMR0 * prescaler)
            -----------------------------------------
                    Frequency / 4
Or if you want the value to put in TMR0 to get a determinate DELAY
InitTMR0 = 256 - ( DELAY * Frequency ) / ( 4* prescaler)

沒有留言:

張貼留言