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 ─────| MT123010AR |── 220ohm───| P10 ─────| |── 220ohm───| P11 ─────| |── 220ohm───| ------------- ------------- P12 ─────| LED_n LED_p |── 220ohm───--- 3.3V P13 ─────| |── 220ohm───| P14 ─────| |── 220ohm───| P15 ─────| |── 220ohm───| P16 ─────| |── 220ohm───| P17 ─────| |── 220ohm───| P18 ─────| |── 220ohm───| P19 ─────| |── 220ohm───| P20 ─────| |── 220ohm───| P21 ─────| MT123010AR |── 220ohm───| P22 ─────| |── 220ohm───| P23 ─────| |── 220ohm───| ------------- -- Curcuit for demo5 -- column row ------------------------ ------------ P0 ── 220ohm ───| 5 LED_p LED_n_com 13 |─────| Qa RESET |--- 3.3V P1 ── 220ohm ───| 2 3 |─────| Qb | P2 ── 220ohm ───| 7 4 |─────| Qc A |--- P8 P3 ── 220ohm ───| 1 10 |─────| Qd SCLK |--- P9 P4 ── 220ohm ───| 12 6 |────-| Qe LCLK |--- P10 P5 ── 220ohm ───| 8 11 |─────| Qf | P6 ── 220ohm ───| 14 15 |─────| Qg OE |--- GND P7 ── 220ohm ───| 9 16 |─────| Qh | ------------------------ ------------ TOM-1588BH 74HC595 2013/10/16 22:13:46 } \ 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 d1000 u/ constant 1msec \ Rollover when "clkfreq dd3857 u/" on forth-word"drive_LED_bar" clkfreq d40 u/ constant 25msec 8 constant data 9 constant sclk d10 constant lclk variable scale -4 allot d10 c, d20 c, d30 c, d40 c, d48 c, d55 c, d60 c, d64 c, d60 c, d55 c, d48 c, d40 c, d30 c, d20 c, \ ******************* \ Variables \ ******************* variable dT variable cycle wvariable flag variable LED_level d20 allot variable dX wvariable ++ variable LED_matrix d60 allot \ =========================================================================== \ Set I/O \ =========================================================================== : data_l data pinlo ; : data_h data pinhi ; : sclk_l sclk pinlo ; : sclk_h sclk pinhi ; : lclk_l lclk pinlo ; : lclk_h lclk pinhi ; \ ******************* \ 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(24elements) \ ( n1 -- ) n1:first pin of LED group : drive_LED_bar 1msec d64 u/ dT L! 1 frqa COG! 0 phsa COG! cnt COG@ 1msec + swap begin \ Run 24-element d24 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 1msec waitcnt \ Wait until cnt+1msec swap dup i + pinin \ Set target-port to input 0 ctra COG! \ Reset counter loop \ 0 until fkey? swap drop until 2drop ; \ Display test pattern1 for LED_bar \ ( -- ) : LED_bar_test1 LED_level scale 3 0 do 8 0 do 2dup i + C@ swap i + C! loop swap 8 + swap loop 2drop LED_level d24 dump pwm drive_LED_bar ; \ Display test pattern2 for LED_bar \ ( -- ) : LED_bar_test2 LED_level scale d14 0 do 2dup C@ swap C! 1+ swap 1+ swap loop drop scale 8 0 do 2dup i + C@ swap i + C! loop 2drop LED_level d24 dump pwm drive_LED_bar ; \ Display like_a_wave on LED_bar \ ( -- ) : demo4 c" pwm drive_LED_bar" 0 cogx LED_level scale cnt COG@ 25msec + rot2 \ ( cnt LED_level scale ) begin 2dup d24 0 do 2dup C@ swap C! \ Store brightness data to LED_level array 1+ dup scale d14 + = if drop scale then \ scale table end? swap 1+ dup LED_level d24 + = if drop LED_level then \ LED_level array end? swap loop 2drop 1+ dup scale d14 + = if drop scale then rot \ ( LED_level scale cnt ) 25msec waitcnt rot2 \ LED_level d24 dump fkey? swap drop until 3drop 0 cogreset ; lockdict create a_drive_LED_bar forthentry $C_a_lxasm w, h142 h113 1- tuck - h9 lshift or here W@ alignl h10 lshift or l, z2Wi]uB l, z1SyLI[ l, z2WiaBB l, z1SyLI[ l, z2WiaJB l, z2Wyb01 l, z2Wyb80 l, z2WyxCv l, z20ixFk l, z2WyP[s l, z2WyPb0 l, z2WyPO1 l, zfyPSq l, z2Wya4q l, z2WixmB l, z1byavr l, z1byavx l, z2WyPnt l, z20iPmD l, ziaRE l, z1Sy]nl l, z2[iQBG l, z2WibBH l, z3ryxCv l, z1[ixmB l, z2Wyar0 l, z20yPb1 l, zfyPO1 l, z20ya01 l, z3[yP[X l, z1SV04S l, z2WyPvw l, z2WyQ00 l, z20yQ4u l, z3[yPvn l, z1SV000 l, 0 l, 0 l, 0 l, 0 l, 0 l, zJY0 l, zJY l, zG0000 l, z7r l, z7t l, z7v l, freedict : test1 LED_level scale 3 0 do 8 0 do 2dup i + C@ swap i + C! loop swap 8 + swap loop 2drop LED_level d24 dump LED_level d24 0 a_drive_LED_bar ; { \ Assembler of drive_LED_bar \ ( n1 n2 n3 -- ) n1:address of LED_bar array n2:number of leds n3:first led $C_treg1 - counter for lednum $C_treg2 - index for LED_bar array $C_treg3 - address to get brightness data $C_treg4 - counter to multiple $C_treg5 - result for multiple $C_treg6 - $C_treg5's negative value fl build_BootOpt :rasm \ Get first_pin-number mov __pin , $C_stTOS spop \ Get number of leds mov __lednum , $C_stTOS spop \ Get address of LED_bar array mov __array , $C_stTOS mov __frqa , # 1 mov __phsa , # 0 mov cnt , # __1msec add cnt , cnt \ initial __1 mov $C_treg1 , # __lednum mov $C_treg2 , # 0 \ pin_mask mov $C_stTOS , # 1 shl $C_stTOS , # __pin mov __pin_num , # __pin __2 mov dira , $C_stTOS or __ctra , # __pin_num or __ctra , # __pwm mov $C_treg3 , # __array add $C_treg3 , $C_treg2 rdbyte __bright , $C_treg3 jmpret __multi_ret , # __multi neg $C_treg6 , $C_treg5 mov __phsa , $C_treg6 waitcnt cnt , # __1msec andn dira , $C_stTOS mov __ctra , # 0 \ next pin add $C_treg2 , # 1 shl $C_stTOS , # 1 add __pin_num , # 1 djnz $C_treg1 , # __2 jmp # __1 \ multiple __bright by __dT __multi mov $C_treg4 , # __dT mov $C_treg5 , # 0 __3 add $C_treg5 , # __bright djnz $C_treg4 , # __3 __multi_ret ret \ variables __pin 0 __pin_num 0 __lednum 0 __array 0 __bright 0 \ constants __1msec d80000 __dT d1250 __pwm h10000000 __ctra h1F8 __frqa h1FA __phsa h1FC ;asm a_drive_LED_bar }