This was one of my early projects using pics, and I hope it will be helpful for any newbees. The programme is written in Mikrobasic, but should be easily changed to suit other basics.
program dice3
'simple dice programme, 6 leds are arranged in the normal dice pattern
'The leds are all strobed on at high speed, actually going throgh all the 6 possibilities but
'effectively looking as though they are all on .
'Pushing the button halts the leds at whatever number they were at when the button was pushed.
'The leds are then displayed flasing on and off for a few seconds, the flash rate
'gradually increases to indicate to the dice thrower that the leds will shortly finish
'with the number display and go back to waiting for a button press for the next
'throw.
'Written for pic16F684 but can be used on other pics, with possible port-letter changes
dim i,n,tim as byte
sub function throwc(dim numc as word) as word 'here the led combinations are set up
select case numc 'using both ports.
case 1 result = %11111110 'Bit patterns for port
case 2 result = %11111111
case 3 result = %11111110
case 4 result = %11110001
case 5 result = %11110000
case 6 result = %11110001
end select
end sub
sub function throwa(dim numa as word) as word
select case numa
case 1 result = %11111111
case 2 result = %11101001
case 3 result = %11101001
case 4 result = %11101110
case 5 result = %11101110
case 6 result = %11101000
end select
end sub
main:
intcon =0
trisa =%00001000
ansel =%00001000
trisc =0
portc =0
porta =0
loop:
for i = 1 to 6 'dice possibilities 1 to 6
if adc_read (3) < 60 then 'look for button push
gosub clearports 'clear the leds
tim = 60 'set the decaying blink rate timer
for n = 0 to 20
vdelay_ms(tim)
gosub clearports
vdelay_ms(tim)
porta = throwa(i) rem call the led pattern according to (I)
portc =throwc(i) rem call the led pattern according to (I)
tim = tim - 3
next n
gosub clearports
delay_ms(200)
end if
porta =throwa(i)
portc =throwc(i)
delay_us(50)
next i
goto loop
clearports: 'clears ports to no leds lit
porta = 0
portc = 0
return
end.
Rem, I must include a timer that switches off the display when not used for
' some time!
program dice3
'simple dice programme, 6 leds are arranged in the normal dice pattern
'The leds are all strobed on at high speed, actually going throgh all the 6 possibilities but
'effectively looking as though they are all on .
'Pushing the button halts the leds at whatever number they were at when the button was pushed.
'The leds are then displayed flasing on and off for a few seconds, the flash rate
'gradually increases to indicate to the dice thrower that the leds will shortly finish
'with the number display and go back to waiting for a button press for the next
'throw.
'Written for pic16F684 but can be used on other pics, with possible port-letter changes
dim i,n,tim as byte
sub function throwc(dim numc as word) as word 'here the led combinations are set up
select case numc 'using both ports.
case 1 result = %11111110 'Bit patterns for port
case 2 result = %11111111
case 3 result = %11111110
case 4 result = %11110001
case 5 result = %11110000
case 6 result = %11110001
end select
end sub
sub function throwa(dim numa as word) as word
select case numa
case 1 result = %11111111
case 2 result = %11101001
case 3 result = %11101001
case 4 result = %11101110
case 5 result = %11101110
case 6 result = %11101000
end select
end sub
main:
intcon =0
trisa =%00001000
ansel =%00001000
trisc =0
portc =0
porta =0
loop:
for i = 1 to 6 'dice possibilities 1 to 6
if adc_read (3) < 60 then 'look for button push
gosub clearports 'clear the leds
tim = 60 'set the decaying blink rate timer
for n = 0 to 20
vdelay_ms(tim)
gosub clearports
vdelay_ms(tim)
porta = throwa(i) rem call the led pattern according to (I)
portc =throwc(i) rem call the led pattern according to (I)
tim = tim - 3
next n
gosub clearports
delay_ms(200)
end if
porta =throwa(i)
portc =throwc(i)
delay_us(50)
next i
goto loop
clearports: 'clears ports to no leds lit
porta = 0
portc = 0
return
end.
Rem, I must include a timer that switches off the display when not used for
' some time!