'* picaxe18m2 multiplex program By A.J,Cartwright 5/2/13
'* two 7 seg displays multiplexed via a cmos 4511
'* using the pinsC to send data to cmos 4511
'* using B pins to switch displays and receive button inputs
'* multiplexing is simply having a number of displays turn on and off individually
'* in this example just two 7 seg displays are used
'* to change the displays output or to add a countdown or countup facility
'* this program is just to show the multiplexing as simply as possible
'* the display_units and display_tens may seems long but it should be easy to follow
'* for the newcomer.
'* learn the basics start simple then improve code efficency when everything works
setfreq m8 'set frequency to 8mhz speed things up a bit
symbol tens =b0 'tens storage variable
symbol units = b1 'units storage variable
symbol flag =b2
symbol pressed =b3
let tens =0 'can pre load the tens value here or set at 0
let units =0 'can pre load the units value here or set at 0
let flag =0 'used to choose which display to show
let pressed =0
main:
if pinB.7 = 1 then ' reset routine this only gets done if button pressed
do while pinB.7 =1 ' reset button pressed
gosub display_units ' these act like interups
gosub display_tens ' othewise display flashes between button presses
loop
units =0
tens =0
end if
'**********************************************
if pinB.3 = 1 then ' units down routine this only gets done if button pressed
do while pinB.3 =1 ' units down button pressed
gosub display_units ' these act like interups
gosub display_tens ' othewise display flashes between button presses
loop
units = units -1 ' subtract 1 from units
if units =255 then ' test if units has gone below 0 (note no negative numbers so
units = 9 ' no -1) it goes to 255.So set to 9 here
end if
end if
'*********************************************
if pinB.2 = 1 then ' units up routine
do while pinB.2 =1 ' units up button pressed
gosub display_units ' these act like interups
gosub display_tens ' othewise display flashes between button presses
loop
units = units +1 ' add 1 to units
if units >9 then ' if units more than 9 change to 0
units = 0
end if
end if
'*********************************************
if pinB.4 = 1 then ' tens down routine this only gets done if button pressed
do while pinB.4 =1 ' tens down button pressed
gosub display_units ' these act like interups
gosub display_tens ' othewise display flashes between button presses
loop
tens = tens -1 ' subtract 1 from tens
if tens =255 then ' test if tens has gone below 0 (note no negative numbers so
tens = 9 ' no -1) it goes to 255.So set to 9 here
end if
end if
'*********************************************
if pinB.5 = 1 then ' tens up routine this only gets done if button pressed
do while pinB.5 =1 ' tens down button pressed
gosub display_units ' these act like interups
gosub display_tens ' othewise display flashes between button presses
loop
tens = tens +1 ' add 1 to tens
if tens >9 then 'if tens more than 9 change to 0
tens = 0
end if
end if
'*********************************************
if flag =0 then gosub display_units 'this displays the units if flag is equal to 0
if flag =1 then gosub display_tens ' this displays the tens if flag is equal to 1
goto main ' end of main program
end 'end of main program code
'****************************************************
display_units:
select case units
case 0
let dirsC = %00000000 ' display 0
let pinsC = %00000000
case 1
let dirsC = %00000010 ' display 1
let pinsC = %00000010
case 2
let dirsC = %00000001 ' display 2
let pinsC = %00000001
case 3
let dirsC = %00000011 ' display 3
let pinsC = %00000011
case 4
let dirsC = %10000000 ' display 4
let pinsC = %10000000
case 5
let dirsC = %10000010 ' display 5
let pinsC = %10000010
case 6
let dirsC = %10000001 ' display 6
let pinsC = %10000001
case 7
let dirsC = %10000011 ' display 7
let pinsC = %10000011
case 8
let dirsC = %01000000 ' display 8
let pinsC = %01000000
case 9
let dirsC = %01000010 ' display 9
let pinsC = %01000010
end select
high B.0 'turn on units 7 seg display
pause 5 'short delay
low B.0 'turn off the uits display
let dirsC =%00000000 'switch c pins low
flag =1 'set flag to 1 so that, in main program display_tens will get processed
return
display_tens:
select case tens
case 0
let dirsC = %00000000 ' display 0
let pinsC = %00000000
case 1
let dirsC = %00000010 ' display 1
let pinsC = %00000010
case 2
let dirsC = %00000001 ' display 2
let pinsC = %00000001
case 3
let dirsC = %00000011 ' display 3
let pinsC = %00000011
case 4
let dirsC = %10000000 ' display 4
let pinsC = %10000000
case 5
let dirsC = %10000010 ' display 5
let pinsC = %10000010
case 6
let dirsC = %10000001 ' display 6
let pinsC = %10000001
case 7
let dirsC = %10000011 ' display 7
let pinsC = %10000011
case 8
let dirsC = %01000000 ' display 8
let pinsC = %01000000
case 9
let dirsC = %01000010 ' display 9
let pinsC = %01000010
end select
high B.1 'turn on tens 7 seg display
pause 5 ' short delay
low B.1 'turn off the tens display
let dirsC =%00000000 ' switch c pins low
flag =0 'set flag to 0 so that, in main program display_units will get processed
return