PDA

View Full Version : Using TMR0 to keep time



BraveZero
- 23rd November 2011, 03:46
I'm trying to use TMR0 for one second periods, but can't seem to get it. I'm using a PIC16F887 with a 4MHz clock. From what I understand you take (((4MHz / 4) / 256) / prescaler) which will give you the total overflows per second. I know that it won't be an exact second every time it advances, but it should correct itself over time. I'm using a prescaler of two and this is what I have so far:

(1MHz / 256) / 2 = 1953.125
1953.125 * 8 = 15625



While (INTCON.2 == 0) : WEND
INTCON.2 = 0
Time = Time + 8
if (Time > 15625) Then
Time = Time - 15625
gosub Second
endif


Is there something wrong with what I did? I can't seem to get the clock to be very accurate.

prstein
- 23rd November 2011, 04:12
Download and use Mister_E's PicMultiCalc from http://www.picbasic.co.uk/forum/content.php?r=159-New-PIC-Utility.-PICMultiCalc.

It totally rocks and will calculate the settings you want.

Best Regards,
Paul

BraveZero
- 23rd November 2011, 05:25
Download and use Mister_E's PicMultiCalc from http://www.picbasic.co.uk/forum/content.php?r=159-New-PIC-Utility.-PICMultiCalc.

It totally rocks and will calculate the settings you want.

Best Regards,
Paul

Alright, so the number I got for each interrupt was 512 uS. With the program I got 513-527 uS. I realized I didn't include the instruction cycle in the equation which adds on a uS per cycle, any idea on how I could figure out the instruction cycle number?