WebTruck Moving out
Guest

IR sensor jal code

-- check reverse sensor
-- generate signal to measure, enter u/d on serial port to change speed
--
-- all this asm nop stuff comes up to 33.4 kHz,
-- for the sensor TSOP1733
-- target configuration: 16f877 with 20 Mhz Xtal

include f877_20
include jlib
include f877_modules
include upins
include const
include routines

const byte irloop = 1
const byte tsop1722const = 8
const byte irgenloop = 80
const byte revsensconst = irgenloop - irgenloop / 4 -- how many hits at least to have seen something


function revsens return byte is
ret = 0
for irgenloop loop
irledr = on
for tsop1722const - 2 loop
asm nop
end loop
if irsensor == low then
ret = ret + 1
end if
irledr = off
for tsop1722const + 3 loop
asm nop
end loop
end loop
if noserial == 0 & debugirrev == 1 then
com("r") com("s") com3(ret)
end if
return (ret)
end function

function frontsens return byte is
ret = stop
t = 0
t2 = 0
for irgenloop loop
irledf = on
for tsop1722const - 2 loop
asm nop
end loop
if irsensorl == low then
t = t + 1
end if
if irsensorr == low then
t2 = t2 + 1
end if
irledf = off
for tsop1722const + 3 loop
asm nop
end loop
end loop
if t > 0 & t2 > 0 then
ret = smiddle
elsif t2 > 0 then
ret = shright
elsif t > 0 then
ret = shleft
end if
if noserial == 0 & ret != stop & debugirfront == 1 then
com("r") com("s") com3(t) com3(t2) com3(ret)
end if
return (ret)
end function


-- test routine

-- f877_serial_setup ( 192 ) -- 19200 baud, 8,n,1 format.



-- var bit oldstate
-- var bit newstate
-- var byte irret

-- com("o") com("k") com(13) com(10)

-- forever loop

-- irret = frontsens

-- newstate = irret > revsensconst
-- if newstate == oldstate then
-- else
-- oldstate = newstate
-- end if
-- if newstate == high then
-- com("1") com(" ")
-- breakled = on
-- else
-- breakled = off
-- end if
-- if irret > 0 then
-- for irret / 2 loop
-- com("-")
-- end loop
-- com3(irret)
-- com(13) com(10)
-- end if
-- end loop