PropBasic-calculating difference between readings of cnt register
tritonium
Posts: 543
in Propeller 1
Hi
I've been measuring the number of cnts between successive reads of the cnt register in PropBasic.
My reasoning went-
scnt= start reading
fcnt= finish reading
the difference (dcnt) is fcnt which will be bigger minus scnt the first reading.....
But
cnt can roll over so the finish can be smaller than the start-
so
if scnt > fnct then the difference dcnt is-
dcnt=$FF_FF_FF_FF - scnt (to see how far short of rollover it is)
dcnt=dcnt+fcnt (add smaller value fcnt)
and by jove it works!
Then after further consideration I wondered why- because Basic treats these 32 bit longs as signed, so after $7F_FF_FF_FF numbers are negative, so in fact going from $7F_FF_FF_FF to $80_00_00_00 is a big difference.
This seems to work but why?
My brain hurts- please put me out of my misery
Dave
I've been measuring the number of cnts between successive reads of the cnt register in PropBasic.
My reasoning went-
scnt= start reading
fcnt= finish reading
the difference (dcnt) is fcnt which will be bigger minus scnt the first reading.....
But
cnt can roll over so the finish can be smaller than the start-
so
if scnt > fnct then the difference dcnt is-
dcnt=$FF_FF_FF_FF - scnt (to see how far short of rollover it is)
dcnt=dcnt+fcnt (add smaller value fcnt)
and by jove it works!
Then after further consideration I wondered why- because Basic treats these 32 bit longs as signed, so after $7F_FF_FF_FF numbers are negative, so in fact going from $7F_FF_FF_FF to $80_00_00_00 is a big difference.
This seems to work but why?
gpol 0 'wait for zero edge scnt=cnt gpol 1 'wait for one edge gpol 0 'wait for zero edge fcnt=cnt if fcnt>scnt then dcnt=fcnt-scnt else dcnt=$ff_ff_ff_ff - scnt 'maxcnt-start cnt dcnt=dcnt+fcnt endif
My brain hurts- please put me out of my misery
Dave
Comments
small enough to be represented. It can only be wrong by multiples of 2^32, like any modulo
operation.
Or in other words: is all you need. It works up to half the range of the CNT register (27 seconds @ 80MHz).
dcnt=$ff_ff_ff_ff - scnt 'maxcnt-start cnt
dcnt=dcnt+fcnt
is one LSB displaced. Correct would be: dcnt = 0 - scnt, but then the two lines do exactly the same as: dcnt=fcnt-scnt.
Andy
Thanks for your answers- somehow I missed Ariba's when he gave it 4 months ago!
Ok now I've got to think about this again. Time for some paper and pencil examples.
Dave