CON _10us = 1_000_000 / 10 ' Divisor for 10 us _1ms = 1_000_000 / 1_000 ' Divisor for 1 ms _1s = 1_000_000 / 1_000_000 ' Divisor for 1 s VAR long cogon, cog long rcstack[16] long rctemp long delay long clkcycles PUB start(pin, state, rcvalueaddress) return cognew(rctime(pin, state, rcvalueaddress), @rcstack) PUB rctime(pin, state, rcvalueaddress) repeat outa[pin] := state ' set pin to state dira[pin] := 1 ' make pin an ouput pause1ms(1) ' allow cap to (dis)charge dira[pin] := 0 ' make pin an input rctemp := cnt ' get current counter waitpeq(1-state,|< pin, 0) ' wait for state change on pin rctemp := || (cnt - rctemp) ' calculate duration rctemp := rctemp - 1600 ' adjust for instructions (for zero) rctemp := rctemp >> 4 ' scale value (divide by 16) long [rcvalueaddress] := rctemp ' move result to target PUB pause1ms(period) '' Pause execution for period (in units of 1 ms). clkcycles := ((clkfreq / _1ms * period) - 4296) #> 381 ' Calculate 1 ms time unit waitcnt(clkcycles + cnt) ' Wait for designated time { PUB pause10us(period) '' Pause execution for period (in units of 10 us) clkcycles := ((clkfreq / _10us * period) - 4296) #> 381 ' Calculate 10 us time unit waitcnt(clkcycles + cnt) ' Wait for designated time PUB pause1s(period) '' Pause execution for period (in units of 1 sec). clkcycles := ((clkfreq / _1s * period) - 4296) #> 381 ' Calculate 1 s time unit waitcnt(clkcycles + cnt) ' Wait for designated time }