My questions are:
g) how do I know how many cycles this or that instruction needs?
h) how do I calculate the time for a TMR0 overflow?
g) Unfortunately the answer is “it depends.” Best way is if you think you are cramped for instruction cycles is to use a spare pin, set it before some code, reset after. Then you can use an oscilloscope to time how long it takes. You could also do this with the simulator in MPLAB, but the first way is probably quicker to do.
h) TMR0 overflows when the count goes from 255 back to 0. If you originally reset the timer, then hit it with the sys clock at the instruction rate, through the prescaler, the overflow time is:
instruction rate (sec) * prescaler (pure number) * 256 (pure number) = overflow rate
So with your 4MHz sys clock (1us rate) you’ll overflow:
1us * 4 * 256 = 1024 us = 1.024 ms
If instead you set TMR0 to some value, you can change this overflow time. If you set TMR0 to N, the time becomes:
instruction rate * prescaler * (256 – N) = overflow rate
So if you wanted a delay of 1.000 ms, you could try presetting N to 6:
1us * 4 * (256 - 6) = 1us * 4 * 250 = 1000 us = 1.000 ms
The exact time may be off a instruction or so as TMR0 needs some time to be updated (so don’t try this if you are accumulating the time in something like a real time clock).
Bookmarks