Thursday, April 29, 2010

--> Seven segment Electronic Dice with microcontroller

Another possible alternative to dice , not much different from our actual implementation, except that each LED is driven independently by an output pin of the processor. That only puts more demands on the processor resources. For this simple project using an PIC16f84, that may not be a big deal, as the extra required output pins are available, but if you want to port it to a different processor with fewer pins, that may not be possible. Also, this scheme, like the last one, requires extra resistors.
 
Program  code is proton basic compiler for pic microcontrollers
Device 16F84
Xtal = 4
symbol pb1=PORTa.0 'define pin for push button
TRISA = %11111111 ' Make PORTB all outputs
TRISB = 000000
dim edge1 as bit 'define a single bit of ram
dim digit as byte
dim number as word
dim raw as word
edge1=0
raw = 0
portb = 255
digit = 8
loop:
if pb1=0 and edge1=0 then 'check if button pressed for rising edge
edge1=1
portb = 255
delayms 1000
number = (RANDOM)+(raw //6)
'Generate a random number which is an integr
digit = number// 6 'creat a number in the range of 0 to 5
raw = 0
else
inc raw
if raw > 65000 then
raw = 0
digit = 8
portb = 255
delayms 1000 ' after waiting a while the display is turned off
endif
endif
SELECT digit
CASE 0 ' Is digit equal to 1 ?
portb = 249
CASE 1 ' Is digit equal to 2 ?
portb = 164
CASE 2 ' Is digit equal to 3 ?
portb = 176
CASE 3 ' Is digit equal to 4 ?
portb = 153
CASE 4 ' Is digit equal to 5 ?
portb = 146
CASE 5 ' Is digit equal to 5 ?
portb = 131
CASE ELSE ' display is off
portb = %10111111
ENDSELECT
edge1=0
goto loop 'return back to check again

 
 




IN this experiment we learnt how to generate random number in basic language for microcontrollers.

----------------------------------------------------------------

No comments:

Post a Comment