| ??? 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
}
|



