I'm currently using timer1 on the PIC16F877a but getting very confused with the math. The chip is hooked up to a 20mhz crystal.
I need to throw an interrupt every 10MS so I need to put an initial value into the TMR1L, TMR1H registers but not sure how to calculate this. Any help would be appreciated.
Thanks
Rich
have looked at previous threads + data sheet but still confused btw.
using 20MHz and 4 cycle per one count- review datasheet for pic16f877a- so
5MHz = 5*2^20 = 5242880
(1/10us)=100KHz
=> 5242880/100K = 52.4288 ~ 34 h
=>FFFF-34 = FFCB
so you have each time to load TMR1L,TMR1H with this value FFCB .
using 20MHz and 4 cycle per one count- review datasheet for pic16f877a- so
5MHz = 5*2^20 = 5242880
(1/10us)=100KHz
=> 5242880/100K = 52.4288 ~ 34 h
=>FFFF-34 = FFCB
so you have each time to load TMR1L,TMR1H with this value FFCB .
I know a Megabyte is 2^20 because it's the closest think to 1,000,000 in base 2, but I always thought Megahertz was excactly 1,000,000 cycles since we all live in a base 10 world.
using 20MHz and 4 cycle per one count- review datasheet for pic16f877a- so
5MHz = 5*2^20 = 5242880
(1/10us)=100KHz
=> 5242880/100K = 52.4288 ~ 34 h
=>FFFF-34 = FFCB
so you have each time to load TMR1L,TMR1H with this value FFCB .
What??? 5MHz is not 5*2^20. It is 5,000,000! In addition, timer 1 increments and overflows after FFFF so your math gives a 10us interrupt rate
20Mhz crystal = 5MHz instruction rate
(frequency * period) 5,000,000 * 0.01 = 50,000 so you would load Timer 1 to:
0x0000-0xC350 = 0x3CB0 (overflows on the transition from 0xFFFF to 0x0)
and it will take exactly 10ms to overflow.
Nigel is correct that Timer 2 is a better option for periodic timing. It has a period register that triggers an interrupt and resets the timer every time a match is made. After the initial setup, there is no code overhead to get periodic interrupts. If you use timer 1, you will have to immediately reload the timer registers (minus a few clock cycles) in order to have continued interrupts.