Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/16/06 07:32
Read: times


 
#118392 - Why it doesn't work
Responding to: ???'s previous message
In your non-interrupt based code, the timer gets reloaded thus it keeps time. In your interrupt code, you only load the timer once, after that it just rolls over every 65536 ticks. If you reload the timer in the interrupt routine ,you'll find it will keep good time.

As for 16 bit auto-reload for timer0 - I don't believe the 8051 has that feature - only on timer2 if my memory serves me correctly. Timer 0,1 only have auto reload in 8 bit mode. So, if you can work it out using an 8 bit timer then you're home free or try timer2 if you processor actually has it.

Other things to watch - the time will drift as you have to contend with the delay before the interrupt actually executes - this adds a few microseconds here and there as the timer will roll over, cause an interrupt and some time later you interrupt code actually executes and then you reload the timer.

The other critical item is 'atomicity'. You have a shared variable between the interrupt code and the main routine. When the main code accesses this variable, you must disable interrupts whilst you alter it then re-enable them. This is to stop an interrupt occuring whilst in the middle of doing a multibyte operation. The variable should also be declared 'volatile'.

For example:

volatile int seconds = 0;

main
{
int copy_seconds;
..
...
..
..
..

// to read the seconds value

EA = 0;
copy_seconds = seconds;
EA = 1;
// use the variable copy_seconds

//to write the seconds value
EA = 0; //no interruptions!
seconds = copy_seconds;
EA = 1;

In PC speak, this is called a 'critical section' or an 'atomic' operation.





}






List of 19 messages in thread
TopicAuthorDate
Timer0_ISR delay troubles            01/01/70 00:00      
   Why it doesn't work            01/01/70 00:00      
      Extra Atomic            01/01/70 00:00      
   Been there Done That            01/01/70 00:00      
   Yes,very atomic            01/01/70 00:00      
      nuclear explosions            01/01/70 00:00      
   Also            01/01/70 00:00      
      Thanks everyone            01/01/70 00:00      
         learn to swim before diving in            01/01/70 00:00      
         using 1            01/01/70 00:00      
            Some code from the prof broke interrupt            01/01/70 00:00      
               Nevermind            01/01/70 00:00      
               which it is not            01/01/70 00:00      
                  Explain?            01/01/70 00:00      
   What happend to common sense?            01/01/70 00:00      
      specific examples are dangerous            01/01/70 00:00      
      Uncommon Sense            01/01/70 00:00      
         Practical programming            01/01/70 00:00      
            Charles            01/01/70 00:00      

Back to Subject List