Can I do this in spin?
Kye
Posts: 2,200
Looking to see if I can preform sigend math with the cnt register for counter wrap arround.
The idea is that result holds the delta since the last time the method was called and "timeSwitchCounter" is a global variable.
But, I'm not sure if I can compute the wrap arround propellerly since the spin does signed math only.
Any help?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
result := (cnt - timeSwitchCounter) timeSwitchCounter := cnt if(result < 0) result += $FFFF_FFFF
The idea is that result holds the delta since the last time the method was called and "timeSwitchCounter" is a global variable.
But, I'm not sure if I can compute the wrap arround propellerly since the spin does signed math only.
Any help?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Need to make your prop design easier or secure? Get a PropMod has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module with uSD reader, and RTC options.
$FFFF_FFFF is the same as -1 so I doubt that adding it to result will help much.
What do you want to do with result?
This is the full piece of code.
The code allows you to use the system counter to make a return value go from true to false on a set on seconds and offseconds·timer.
The:
· result := (cnt - timeSwitchCounter)
· timeSwitchCounter := cnt
· if(result < 0)
··· result += $FFFF_FFFF
part is supposed to produce the true delta when looking at the delta that can occur in a circular counter, I then compare the delta with the time specified in milliseconds and flip the switch back and forth.
I'm not trying to add -1, I'm trying to add (2^32 -1).
Using this piece of code, it would be easy to make a blinking LED or feed a logical condition to any other method, the logical condition would then alternate within a specified time in ms.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
Post Edited (Kye) : 4/3/2009 1:38:21 AM GMT
Adding $FFFF_FFFF is no solution.
Here is an analysis for 16bit signed values, but the results also apply
to 32bit values.
You need several functions:
Sorry about the java but you should understand it.
Replace currentTick() by CNT.
Start and stop are global vars.
regards peter
Post Edited (Peter Verkaik) : 4/3/2009 2:21:00 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
some time ago I coded a elapsed_time-function
somebody else posted a VERY SHORT an elegant derivate for the calculating but I don't remember the name
if you bitshift the countervalue one bit to the right the precision goes from one clocktick to two clockticks but then
the bit no 32 = sign-bit is ALWAYS zero and all values stay positive
the method above can handle the wrap around to zero
best regards
Stefan