TIMER (Please Help)
I'm trying to make a timer that counts while somepin is high, in this example its supposed to count up at a frequency determined by a constant named PRECITION and then get read and reset each time somepin goes low, the value the gets read is put on a memory address to get read by another cog. My problem is that it isn't counting up at all. Is there some error in this code or is the problem somewhere else? I can't find any tutorials for timers.
ctra[30..26] := %11010
ctra[5..0] := somepin
frqa := clkfreq/PRECITION
'some code
phsa~
'some more code
repeat
if not somepin
LONG[someaddress] := phsa
phsa~

Comments
You might want to look at the file for using counter modules. See attachment.
You might want to post your entire code, too, so people can see exactly what's going on.
What you want to do is read phsa only once for each time somepin goes high. In other words, look for a change in somepin from 1 to 0.
PRI Read(Pin, PositionAddr) dira[Pin]~ ctra[30..26] := %11010 ctra[5..0] := Pin frqa := clkfreq/PRECITION repeat while ina[Pin] phsa~ repeat if not ina[Pin] LONG[PositionAddr]:= phsa phsa~ repeat while not ina[Pin]Use PULSIN_Clk from "BS2 Functions" in the Object Exchange as an example of how to do this:
PUB PULSIN_Clk(Pin, State) : Duration {{ Reads duration of Pulse on pin defined for state, returns duration in 1/clkFreq increments - 12.5nS at 80MHz Note: Absence of pulse can cause cog lockup if watchdog is not used - See distributed example x := BS2.Pulsin_Clk(5,1) BS2.Debug_Dec(x) }} DIRA[pin]~ ctra := 0 if state == 1 ctra := (%11010 << 26 ) | (%001 << 23) | (0 << 9) | (PIN) ' set up counter, A level count else ctra := (%10101 << 26 ) | (%001 << 23) | (0 << 9) | (PIN) ' set up counter, !A level count frqa := 1 waitpne(State << pin, |< Pin, 0) ' Wait for opposite state ready phsa:=0 ' Clear count waitpeq(State << pin, |< Pin, 0) ' wait for pulse waitpne(State << pin, |< Pin, 0) ' Wait for pulse to end Duration := phsa ' Return duration as counts ctra :=0 ' stop counterYou'd simply call this likePRI Read(Pin, PositionAddr) repeat LONG[PositionAddr] := PULSIN_Clk(Pin, 1) / PRECISION