fl { 7-Segment Display Dynamic driver PropForth 5.5 2013/08/25 16:39:37 7-Segment Dynamic drive cuicuit Propeller 1 VDD ---- 3.3V 2 GND ---- GND 3 Serial data ---- P0 4 ShiftClock ---- P1 5 LatchClock ---- P2 } \ =========================================================================== \ Constants \ =========================================================================== 0 wconstant DATA 1 wconstant SCLK 2 wconstant LCLK clkfreq d400 u/ constant 2.5msec \ Hex data for 7-Segment Data wvariable SegData1 -2 allot hFC c, h60 c, hDA c, hF2 c, h66 c, hB6 c, hBE c, hE4 c, hFE c, hF6 c, hEE c, h3E c, h1A c, h7A c, h9E c, h8E c, h00 c, \ Decimal data for 7-Segment Data wvariable SegData2 -2 allot hFC c, h60 c, hDA c, hF2 c, h66 c, hB6 c, hBE c, hE4 c, hFE c, hF6 c, h02 c, h3E c, h1A c, h7A c, h9E c, h8E c, h00 c, \ =========================================================================== \ Variables \ =========================================================================== variable value \ Variable for 7-Segment Data address wvariable SegData \ =========================================================================== \ 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 \ =========================================================================== \ Initialize shift_register \ ( -- ) : init_sr DATA 3 0 do dup pinout 1+ loop drop ; \ Shift out 8bit-data \ ( n -- ) n:8bit data : shift_data 1 8 0 do 2dup and if data_h else data_l then sclk_h sclk_l 1 lshift loop 2drop ; \ Set shift-register to output \ ( -- ) : latch_out lclk_h lclk_l ; \ Clear shift-register \ ( -- ) : clr_sr hFF hFF shift_data shift_data latch_out ; \ Test Hardware \ ( -- ) : hw_test 8 0 do 1 i lshift hFF xor 8 0 do \ Send data for dynamic-drive dup shift_data \ Send 7Segment-LED's each element(a,b,cd,e,f,g.dp) 1 7 i - lshift shift_data \ Display 7Segment-LED's each element(a,b,cd,e,f,g.dp) latch_out d100 delms loop drop loop ; wvariable z_supless \ 7-Segment Drive(400Hz) \ ( -- ) : 7SEG_drive init_sr SegData1 SegData W! \ Using SegData1 ( 0-9,a-F) begin 1 z_supless W! value L@ hF0000000 cnt COG@ 2.5msec + rot2 8 0 do 1 i lshift hFF xor shift_data \ Drive LED-common siganal 2dup and d28 i 4 u* - rshift dup if 0 z_supless W! else z_supless W@ if lasti? 0= if drop d16 then then \ Replace to blank if not last number then SegData W@ + C@ \ Get 7-Segment data shift_data latch_out \ Display 7Segment-LED's each element(a,b,cd,e,f,g.dp) 4 rshift rot 2.5msec waitcnt rot2 loop 3drop \ fkey? swap drop 0 until ; variable tmp wvariable result variable work \ Convert binary to BCD with sign (No sign less than d-1000_0000) \ ( n1 -- ) n1:bainary : bin_to_bcd 0 work L! dup 0 <> if 0 result W! SegData2 SegData W! \ Using SegData2 ( 0-9,blank,-) dup \ ( n1 n1 ) \ Check if minus h80000000 and if negate 0 else 1 then swap \ ( 1/0 n1 ) d10000000 tmp L! 8 0 do work L@ 4 lshift work L! dup tmp L@ >= if result W@ 0= if swap 0= if work L@ 4 rshift d10 or 4 lshift work L! \ "-" then then 1 result W! tmp L@ u/mod work L@ or work L! then tmp L@ d10 u/ tmp L! loop drop else drop then ; : demo init_sr hw_test c" 7SEG_drive" 0 cogx \ hex count d-1000 value L! d2000 0 do value L@ 1+ value L! d10 delms loop \ decimal count d1001 d2001 0 do 1- dup bin_to_bcd work L@ value L! 5 delms loop drop ;