fl { LED_pwm PropForth 5.5 -- Curcuit for demo1 and demo2/demo3 -- P0 ─ 220ohm──── LED_n LED_p ───── 3.3V -- Curcuit for demo4 -- ------------- P0 ─────| LED_n LED_p |── 220ohm───--- 3.3V P1 ─────| |── 220ohm───| P2 ─────| |── 220ohm───| P3 ─────| |── 220ohm───| P4 ─────| |── 220ohm───| P5 ─────| |── 220ohm───| P6 ─────| |── 220ohm───| P7 ─────| |── 220ohm───| P8 ─────| |── 220ohm───| P9 ─────| |── 220ohm───| P10 ─────| |── 220ohm───| P11 ─────| |── 220ohm───| ------------- MT123010AR 2013/10/09 21:44:48 } \ a cog special register [ifndef ctra h1F8 wconstant ctra ] [ifndef frqa h1FA wconstant frqa ] [ifndef phsa h1FC wconstant phsa ] \ ******************* \ Constants \ ******************* 0 constant pwm 1 pwm lshift constant pwmm clkfreq d10000 u/ constant 100usec clkfreq d500 u/ constant 2msec \ ******************* \ Variables \ ******************* variable dT variable cycle wvariable flag variable LED_level 8 allot variable dX wvariable ++ \ ******************* \ main \ ******************* \ Blinking LED \ pwm output Cycle=1second On-time=875msec Off-time=125msec \ ( -- ) : demo1 clkfreq 8 u/ dT L! \ dT=125msec h10000000 pwm or ctra COG! \ Set mode to "NCO/PWM single-ended" 1 frqa COG! dira COG@ pwmm or dira COG! \ Set pwm to output cnt COG@ clkfreq + \ cnt +1second begin dT L@ negate phsa COG! \ Set -dT to phsa clkfreq waitcnt \ Wait until isec pass fkey? swap drop \ Stop if any key until drop 0 ctra COG! \ Reset counter dira COG@ pwmm andn dira COG! \ Set pwm to input ; \ Changing On-time for LED from 100% to 0% [1second] \ ( -- ) : demo2 clkfreq d100 u/ dT L! \ cycle time=10msec h10000000 pwm or ctra COG! \ Set mode to "NCO/PWM single-ended" 1 frqa COG! dira COG@ pwmm or dira COG! \ Set pwm ti output cnt COG@ dT L@ + \ cnt + 10msec 0 begin dup negate phsa COG! \ Set -value to phsa swap dT L@ waitcnt \ Wait until 10msec pass swap 100usec + \ Add ticks(100usec) dup dT L@ > if drop 0 then fkey? swap drop \ Stop if any key until 2drop 0 ctra COG! dira COG@ pwmm andn dira COG! \ Set pwm to input ; \ Changing On-time for LED from 100% to 0% and 0% to 100% [2second] \ ( -- ) : demo3 clkfreq d100 u/ dT L! \ cycle time=10msec h10000000 pwm or ctra COG! \ Set mode to "NCO/PWM single-ended" 1 frqa COG! dira COG@ pwmm or dira COG! \ Set pwm ti output cnt COG@ dT L@ + \ cnt + 10msec 0 begin dup negate phsa COG! \ Set -value to phsa swap dT L@ waitcnt \ Wait until 10msec pass swap 100usec flag W@ \ Check if incremnt or decrement for Hi-term if - else + then \ Subtract 100usec if flag=1 dup dT L@ > if 1 flag W! 100usec - else dup 0< \ Check if minus if drop 0 0 flag W! then \ then fkey? swap drop \ Stop if any key until 2drop 0 ctra COG! dira COG@ pwmm andn dira COG! \ Set pwm to input ; \ Drive LED-bar(12elements) \ ( n1 -- ) n1:first pin of LED group : drive_LED_bar 2msec d100 u/ dT L! 1 frqa COG! 0 phsa COG! cnt COG@ 2msec + swap begin \ Run 12-element d12 0 do dup i + pinout \ Set target-port to output dup i + h10000000 or ctra COG! \ PWM/NCO mode on LED pin i LED_level + C@ \ Get level for target LED dT L@ u* negate phsa COG! swap 2msec waitcnt \ Wait until cnt+2msec swap dup i + pinin \ Set target-port to input 0 ctra COG! \ Reset counter loop \ 0 until fkey? swap drop until 2drop ; : test LED_level d10 0 do dup i 1+ d10 u* swap C! 1+ loop drop d90 LED_level d10 + C! d80 LED_level d11 + C! LED_level 20 dump 0 drive_LED_bar ; \ Display LED-bar-elements by pwm \ ( -- ) : demo4 \ Clear LED_level array LED_level 3 0 do dup 0 swap L! 4 + loop drop c" pwm drive_LED_bar" 0 cogx d10 dX L! \ Set increment to d10 1 ++ W! \ Set ++ to increment 0 \ ( n1 ) initial value d500 0 do dup \ ( n1 n1 ) \ Save data to 12-elements d12 0 do dup \ ( n1 n1 n1 ) i LED_level + C! \ Save data to LED_level+n dup d100 = \ Check if switching to decremant if d-10 dX L! \ Set increment to d-10 else dup 0= \ Check if switching to incremant if d10 dX L! \ Set increment to d10 then then dX L@ + \ ( n1 n1 ) loop drop \ ( n1 ) LED_level 12 dump d50 delms \ Change first elemnt ++ W@ \ Check if increment or decrement if d10 dX L! else d-10 dX L! then dX L@ + dup \ ( n1 n1 ) d100 > ++ W@ and \ Check if n1 > d100 if drop d90 \ Place 90 0 ++ W! \ Set ++ to decrement d-10 dX L! \ Set increment to d-10 else dup 0< ++ W@ 0= and \ Check if n1 < 0 if drop 0 \ Place 0 1 ++ W! \ Set ++ to increment d10 dX L! \ Set increment to d10 then then \ ( n1) loop drop 0 cogreset ;