In example , there is such code in T0Delay () void T0Delay() { T0CON=0x01; TMR0H
ID: 1846610 • Letter: I
Question
In example , there is such code in T0Delay ()
void T0Delay()
{
T0CON=0x01;
TMR0H=0x85;
TMR0L=0xEE;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}
There is a very weird while loop different from what you learned in ECE 114:
while(INTCONbits.TMR0IF==0);
The while loop you have usually is this way:
bool done = false;
while (!done) {
// some C code for processing)
if (some magic happens)
done = true; // exit the while loop by setting done true
}
(a) When will this weird while loop continue execution?
(b) When will the code T0CONbits.TMR0ON=0; right after the while loop be executed?
(c) How can this while loop stop due to timer control?
(d) interrupt is different from polling. Explain briefly what interrupt is, what polling is, and what their difference is. Explain what interrupt service routine is.
Explanation / Answer
void T0Delay()
{
T0CON=0x01; // this line will set the prescalar for timer0 to on and prescalar value is 1:4 . timer 0 is configured for 16 bits
TMR0H=0x85;
TMR0L=0xEE;
// these two lines have made effectively the TMR0 initial count to 0x85EE or decimal 34286
T0CONbits.TMR0ON=1; // this line will start the timer
while(INTCONbits.TMR0IF==0); // this line will be executed untill TMR0IF bit becomes 1. I.e. Timer 0 has overflowed
T0CONbits.TMR0ON=0; // Turn Timer 0 off
INTCONbits.TMR0IF=0; // Clear the TImer0 overflow bit.
}
a) this line will be executed till TMR0H =0XFF and TMR0L=0xFF i.e. the total count has reached 0XFFFF decimal 65535.
at this stage TMR0IF bit will become 1 and While loop will exit.
b) let the XTAL is X Mhz
so now count tis incresed from 0x85EE to 0xFFFF ---> 65535-34286 =31249 counts
Now we find timer0 increase rate = (X/4)/prescalar =(X/4)/4=X/16 Mhz
time = 31249/(X/16) = 31249*16/X micro second = 499.984/X ms
suppose X was a 10 Mhz then we would get a delay of 499.984/10 ms =49.9984ms o 50ms.
hence the T0CONbits.TMR0ON=0; will be executed after 499.984/X ms where X is XTAL frequency.
c) This while loop stops because it depends on value of TMR0IF bit which is set by timer.
d)
I will explain you all the three with a real world example. Suppose you want to get up next morning at 4:00am (remember i said suppose :)) and finish your homework.
so one way is you keep looking at the clock. check the time if time is 4:00am start doing homework else look again.
this is polling
other way is to put an alarm of 4:00am . you will respond to the alarm . ie. you continue what you are doing and alarm interrupted you. this is interrrupt.
now suppose you were sleeping . alarm interrupted you . you woke up completed the homework and went back to sleep. so sleeping is normal operation and doing homework was something which had to be done after alarm interrupted you. Thus doing home work was interrupt service routine. Any job which is done once interrupt is received is called interrupt service routine.