" i would like to write a program using TMRO overflow interrupt to blink portb LED's at 5 second interval"
my question is how to calculate(write) 5 sec delay code.pls help me
hi,
You set the tmr0 and its prescaler to give an integer roller interrupt value for timer0, increment a software counter on the interrupt.
Then you repeat that tmr0 loop the number of times that give 5 seconds.
Its also possible to do the same action without using the interrupt, just test the Tmr0 interrupt flag.
hi,
Assume you have a 4MHz crystal, giving a 1uSec cycle time.
So 256uSec to fill the prescaler,,, the tmr0 clock input would occur every 256uSec, as tmr0 is 8bit, [256 counts] = 65.536 mSec.
So the loop count would 65.536mSec *76= 4.98Seconds.
A simplier way is:
If you used a 3.2768MHz crystal, set the prescaler for 128, then tmr0 would roll over every 40mSec [tmr0 interrupt flag set],
do this 25 times and you get 1second, so 25 * 5 = 125 times around the loop will give exactly 5seconds.
hi,
Now that you can calculate the required time, its important to realise that when using a 'polling' method for timing using tmr0 in this way, that the MCU is not doing any other action.!
A more general method would be to use a interrupt service subroutine, in that way every 40mSec the program would jump to the ISR for doing the count.
The MCU could perform other functions while waiting for the tmr0 interrupt.
Have you considered using timer1 for the timing interrupt.?
Please use Code tags when posting code here in the forums! Simply click the # icon immediately before pasting your code, or type [code] before your code and [/code] after it. This tells the forum software to not strip the formatting from your code and prevents it from becoming the unreadable lump seen in the above post.