Electronic Dice Using PIC Microcontroller Circuit
Introduction To PIC Microcontroller Circuit
This PIC Microcontroller Circuit is a good project for those who have yet to have any practical hands-on of using a microcontroller in their electronic project. Once you are familiar with how a microcontroller works, you will be able to do any project using any brand of microcontroller. The price of microcontroller has dropped significantly as the demand for its use increases day by day. They are used in computers, home appliances, mobile phone, control circuitry...... In other words, the use of microcontroller will become more common in our daily lives.
In this project, we want to generate two random numbers between 1 and 6 everytime the Roll switch is pressed and to display
the numbers on the 7 segment LED displays for a few seconds. Then we want to be able to press Roll again and get another
pair of numbers.
If the Roll is not pressed for about a minute then we want to reduce the power consumption of the circuit.
This is done by using the sleep mode available in the PIC IC to save power and hence a longer battery life. Pressing the
switch for about a second should wake the PIC from sleep mode and be ready to roll the 2 dice again. A slide switch is
built into the circuit so that the unit can be turned completely off when it is not in use. The PIC Microcontroller Circuit diagram is as
shown below.
PIC Microcontroller Circuit Hardware Description
There are two I/O ports in the 16C54 - one 8 bit & the other 4 bit. The 8 bit port, Port B, is connected to the 7 segment
display. Three pins of the 4 bit Port A are used. The other pin is tied high. Pin 4 of the PIC is also tied high to give a
power-on reset.
A 150 ohm resistor acts as a current limiting resistor for each display. PIC ports have a maximum sourcing
current of 40mA. The maximum current for the LED display is 200mA so it is the PIC that R1 and R2 is protecting,
not the display.
The PIC uses its internal RC clock oscillator option (with the 10K resistor, R3, and the 330pF capacitor, C2) and runs
at about 330kHz. The switch is connected to input RA0. Normally this line is pulled high via R4 to +5V, but when the switch
is closed the input is pulled low. The software detects the falling edge as a dice roll.
Software Description
When power is first applied, the PIC does a power-on reset. It goes to Start. The first task is to initialize the various
internal options such as the direction of the I/O ports and the status of any outputs (high or low.) The watchdog timer is
switched on and the initial value of some RAM locations are set.
The dice now settles down in the main program loop, Main. It continuously loops about 4000 times a second to see if the
switch has been pressed, flash the decimal point, generate the next random number and reset the watchdog timer. Then when
a Roll (switch press) is detected, the program jumps through to Main02. The two dice are rolled and displayed sequentially
in Output. Note how the second random number is generated by the length of time the roll switch is depressed. Display
outputs the random numbers to the 7 segment LED displays by multiplexing to them. At the end of Output the program returns
to Main.
Various subroutines - Display, Delay, Roll, Flash, Random and two look-up tables for the display, Look_Up_Seg and
Look_Up_Digit are listed in the final part of the program. Note the Rand01 code segment in Random and why it is there.
If there is no keypress for 50 flashes then the PIC goes into sleep mode at the end of the Flash subroutine. The
oscillator is turned off and ports A & B are configured as inputs.
Now with the PIC in sleep mode the watchdog timer is not being reset anymore. It was not turned off when the PIC
went to sleep. It now times out about every 1.3 seconds to check if the key is being pressed. If no keypress is found
then it goes back to sleep. But if it is then the PIC wakesup, resets the ports, starts to flash the decimal point and it
is ready to roll again on the next keypress.
The Source Code can be downloaded
here.
Parts List
The parts list of the microcontroller project is as shown below.


|