Simple PIC18 Projects:Two-Dice Project

PROJECT 6.3—Two-Dice Project

Project Description

This project is similar to Project 2, but here a pair of dice are used—as in many dice games such as backgammon—instead of a single dice.

The circuit shown in Figure 6.8 can be modified by adding another set of seven LEDs for the second dice. For example, the first set of LEDs can be driven from PORTC, the second set from PORTD, and the push-button switch can be connected to RB0 as before. Such a design requires fourteen output ports just for the LEDs. Later on we will see how the LEDs can be combined in order to reduce the input/output requirements. Figure 6.12 shows the block diagram of the project.

Project Hardware

The circuit diagram of the project is shown in Figure 6.13. The circuit is basically same as in Figure 6.8, with the addition of another set of LEDs connected to PORTD.

Simple PIC18 Projects-0020

The operation of the project is very similar to that for Project 2. Figure 6.14 shows the PDL for this project. At the beginning of the program the PORTC and PORTD pins are configured as outputs, and bit 0 of PORTB (RB0) is configured as input. The program then executes in a loop continuously and checks the state of the push-button switch. When the switch is pressed, two pseudorandom numbers between 1 and 6 are

Simple PIC18 Projects-0021

generated, and these numbers are sent to PORTC and PORTD. The LEDs remain at this state for 3 seconds, after which all the LEDs are turned OFF to indicate that the push-button switch can be pressed again for the next pair of numbers.

Project Program

The program is called LED4.C, and the program listing is given in Figure 6.15. At the beginning of the program Switch is defined as bit 0 of PORTB, and Pressed is defined as 0. The relationships between the dice numbers and the LEDs to be turned on are stored in an array called DICE, as in Project 2. Variable Pattern is the data sent to the LEDs. Program enters an endless for loop where the state of the push-button switch is checked continuously. When the switch is pressed, two random numbers are generated by calling function Number. The bit patterns to be sent to the LEDs are then determined and sent to PORTC and PORTD. The program then repeats inside the endless loop, checking the state of the push-button switch.

Leave a comment

Your email address will not be published. Required fields are marked *