'' ******************************* '' * RCTime * '' * (c) 2006 Parallax, Inc. * '' ******************************* '' '' Used to read an RC circuit. When start() method is used, the rctime() '' method is installed in its own cog and runs continuously until the '' stop() method is called. '' '' If no cog is available the rctime() method may be called as needed. '' '' '' State = 1 configuration: State = 0 configuration: '' '' Ý '' Pin %%%¸Ý²Ý%%%3%%%%%% ¹Ý¼Ý% '' ÂÝÐÝ¨Ý ¹Ý¼Ý% Pin %%%¸Ý²Ý%%%K%%% '' Ý Ý ÂÝÐÝ¨Ý '' Ý VAR long cogon, cog long rcstack[16] long rctemp long mode OBJ time : "Timing" PUB start(pin, state, rcvalueaddress) : okay '' Start background RCTIME driver - starts a cog '' -- returns false if no cog available stop okay := cogon := (cog := cognew(rctime(pin, state, rcvalueaddress), @rcstack)) > 0 mode := 1 PUB stop '' Stop rctime - frees a cog if cogon~ cogstop(cog) PUB rctime(pin, state, rcvalueaddress) '' Read RC circuit and move result to rcvalueaddress '' -- if mode 1, will run continuously in own cog '' -- if mode 0, will terminate and may be called again manually repeat outa[pin] := state ' set pin to state dira[pin] := 1 ' make pin an ouput time.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 if mode == 0 ' quit if called manually quit