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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/31/10 20:59
Read: times


 
#174692 - OT: use TR0 = 0; TMR0 -= offset; TR0 = 1;
Responding to: ???'s previous message
Andy Peters said:
So the idea is to add some new offset to the timer value. The timer value is read into the two bytes (two MOVs essentially) and then the offset is added to the 16-bit word, then the two bytes are written back to the timer (two more MOVs).
...
SDCC is little-endian.


Probably OT (but as you mentioned SDCC) instead of using the unions of AN155 and the lengthy (and not portable) stuff:

typedef union                          // union used for writing to TL0 & TH0
    {
        struct
        {
            unsigned char hi;
            unsigned char lo
        } b;
        unsigned int w;
    }udblbyte;

...
      TR0 = 0;                         // stop Timer0
      time.b.lo = TL0;                 // read lo byte first
      time.b.hi = TH0;                 // read hi byte second
      time.w = time.w - offset;        // calculate new time 
      TL0 = time.b.lo;                 // write lo byte first
      TH0 = time.b.hi;                 // write hi byte second
      TR0 = 1;                         // start Timer0

 

it also could be done like this:

      TR0 = 0;                         // stop Timer0
      TMR0 -= offset;
      TR0 = 1;                         // start Timer0

 

instead?

Compiles to:
                            609 ;       stepper.c:7: TR0 = 0;                         // stop Timer0
   0007 C2 8C               610         clr     _TR0
                            611 ;       stepper.c:8: TMR0 -= offset;
   0009 E5 8A               612         mov     a,_TMR0
   000B C3                  613         clr     c
   000C 95*00               614         subb    a,_offset
   000E F5 8A               615         mov     _TMR0,a
   0010 E5 8C               616         mov     a,(_TMR0 >> 8)
   0012 95*01               617         subb    a,(_offset + 1)
   0014 F5 8C               618         mov     (_TMR0 >> 8),a
                            619 ;       stepper.c:9: TR0 = 1;                         // start Timer0
   0016 D2 8C               620         setb    _TR0

 

Which is close to the metal.

If you want to reproduce, SDCC compilable code is:

#include "msc1210.h"
extern int offset;
void Timer_ISR (void) __interrupt 1
{
      TR0 = 0;                         // stop Timer0
      TMR0 -= offset;
      TR0 = 1;                         // start Timer0
}
 


List of 23 messages in thread
TopicAuthorDate
Unions and position of bytes            01/01/70 00:00      
   Syntax and strategy problem.            01/01/70 00:00      
      wow that was quick and excellent thanks            01/01/70 00:00      
      Works a treat            01/01/70 00:00      
   Note that this is heavily compiler-reliant            01/01/70 00:00      
      Code for transparency            01/01/70 00:00      
         Agreed - after bitter experience            01/01/70 00:00      
            Compiler Specific            01/01/70 00:00      
               How many compler brands/versions to test for?            01/01/70 00:00      
                  Create a Compiler header            01/01/70 00:00      
                     Didn't we talk about unions - your examples doesn't...            01/01/70 00:00      
                        Someone already wrote up a good way....            01/01/70 00:00      
                           Incomplete byte order. But pad is still dangerous            01/01/70 00:00      
                              why bother?            01/01/70 00:00      
                                 how would you know?            01/01/70 00:00      
                                    Exactly my point... you don't            01/01/70 00:00      
                                       Never give up on portability - just decide the amount            01/01/70 00:00      
               Yes, but            01/01/70 00:00      
               why bother?            01/01/70 00:00      
      unions and portability            01/01/70 00:00      
         OT: use TR0 = 0; TMR0 -= offset; TR0 = 1;            01/01/70 00:00      
         you're not?            01/01/70 00:00      
            It was just a warning that union type casts are dangerous            01/01/70 00:00      

Back to Subject List