Can I do this in spin?
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?
PUB timeSwitch(onTime, offTime) ' 5 Stack Longs '' ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ '' │ Switches the return value back and forth between true and false depending on the elasped time. │ '' │ │ '' │ Returns true when in on time and false when in off time. │ '' │ │ '' │ OnTime = On time for returning true condition in milliseconds. │ '' │ OffTime = Off time for returning false condition in milliseocnds. │ '' └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ result := (cnt - timeSwitchCounter) timeSwitchCounter := cnt if(result < 0) result += $FFFF_FFFF if(timeSwitchBuffer) if(result => (onTime * (clkfreq / 1000))) timeSwitchBuffer~ else if(result => (offTime * (clkfreq / 1000))) timeSwitchBuffer~~ return timeSwitchBuffer VAR byte timeSwitchBuffer long timeSwitchCounterThis 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:
/** * Remember the current time and calculate the stop time. */ public void mark(int timeout_ticks) { start = currentTick(); stop = start + timeout_ticks; }/** * Get the number of passed ticks since the last call to mark(). * * @return Passed time in ticks (unsigned) */ public int passedTicks() { return currentTick() - start; }/** * Test for timeout using the start and stop time remembered * by the last call to mark(). * * @return true if the timeout period has expired. */ public boolean timeout() { int curr = currentTick(); return (start < stop) ? ((curr >= stop) || (curr < start)) : ((curr >= stop) && (curr < start)); }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
PUB elapsed_time_msec(p_TimeVar) | RightNow, ClockTicks 'can measure up to 26843 Milliseconds '(2147483647 CounterTicks / 80.000.000 Hz = 26843 Milliseconds) RightNow := cnt if RightNow < p_TimeVar ClockTicks := ||($FFFFFFFF - RightNow + p_TimeVar) else ClockTicks := ||(RightNow - p_TimeVar) result := ClockTicks / (clkfreq / 1_000) 'calculate milliseconds '##################################################################################### 'explanatio how it works ' SPIN treats longs as SIGNED longs meaning bit 32 represents the sign "+-" ' the systemcounter thinks of the 32bits as UNsigned meaning bit 32 is ' just another bit of the number and NOT a sign "+-" ' if one or both values are negative it could happen ' that the result is negative too ' the command "||" calculates the absolute value of the SIGNED longs 'if the systemcounter has wrapped around since snapshot of 'systemcounter (value of parameter p_TimeVar), 'this method calculates the timedifference in the right way 'wrap-around example with easy numbers: counter maxvalue 1000 'p_TimeVar containing value 980 'time goes on counter wraps around from 1000 to 0 'time goes on 'RightNow containing 300. This means 20 ticks until maximum 1000 and 300 ticks 'since wrapping from 1000 to 0 in summary 320 ticks of time has gone 'this is calculated by MaxCounter - p_TimeVar + RightNow 'in numbers 1000 - 980 + 300 = 320 'the real systemcounter has 32bits max value 2^32 -1 = 4294967295 'hexadecimal $FFFFFFFF '#####################################################################################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