fl { LED_sensor PropForth 5.5 P0 ─ 220ohm──── LED ───── P1 ledp ledn 2013/10/04 22:29:21 } \ a cog special register [ifndef ctra h1F8 wconstant ctra ] [ifndef frqa h1FA wconstant frqa ] [ifndef phsa h1FC wconstant phsa ] \ ******************* \ Constants \ ******************* 0 constant ledp 1 constant ledn 1 ledp lshift constant ledpm 1 ledn lshift constant lednm lednm ledpm or constant ledm \ ******************* \ main \ ******************* \ Read_Hi-pulse \ After adding revaerse bias, measuring Hi-time of N-side for LED \ ( -- n1) n1:phsa value : readLED \ Reverse bias for LED outa COG@ ledpm andn lednm or outa COG! \ Set outa for ledp=0 ledn=1 dira COG@ ledm or dira COG! \ Set ledn and ledp to output cnt COG@ cnt COG@ drop drop \ Wait 5usec 0 phsa COG! dira COG@ lednm andn dira COG! \ Set ledn to input \ Wait until ledn become to Lo begin ina COG@ lednm and \ Get ledn phsa COG@ d1480000 > \ Time out 18msec if drop 1 else 0= then until phsa COG@ ; \ Print out phsa value as '*' on TeraTerm \ ( -- ) : test h20000000 ledn or ctra COG! \ Set mode to "POS detector" 1 frqa COG! clkfreq 5 u/ cnt COG@ + \ loop time 200msec begin readLED \ . cr d10000 u/ 0 do h2A emit loop cr clkfreq 5 u/ waitcnt fkey? swap drop until drop ; : test1 h20000000 ledn or ctra COG! \ Set mode to "POS detector" 1 frqa COG! clkfreq cnt COG@ + \ loop time 1sec begin readLED \ . d10000 u/ 0 do h2A emit loop cr clkfreq waitcnt fkey? swap drop until drop ; \ LED_sensor demo \ ( -- ) : demo1 h20000000 ledn or ctra COG! \ Set mode to "POS detector" 1 frqa COG! clkfreq 5 u/ cnt COG@ + \ loop time 200msec begin readLED dup . dira COG@ lednm andn dira COG! \ Set ledn to output cnt COG@ cnt COG@ drop drop \ On LED if d500000 d500000 > \ Compare threshold level if ." LED on" cr outa COG@ lednm andn ledpm or outa COG! \ Set outa for ledp=1 ledn=0 dira COG@ ledm or dira COG! \ Set ledn and ledp to output then clkfreq 5 u/ waitcnt \ loop time 200msec fkey? swap drop until drop ;