Simple PIC18 Projects:LED Dice

PROJECT 6.2—LED Dice

Project Description

This is a simple dice project based on LEDs, a push-button switch, and a PIC18F452 microcontroller operating with a 4MHz resonator. The block diagram of the project is shown in Figure 6.6.

Simple PIC18 Projects-0011

As shown in Figure 6.7, the LEDs are organized such that when they turn ON, they indicate numbers as on a real dice. Operation of the project is as follows: The LEDs are all OFF to indicate that the system is ready to generate a new number. Pressing the switch generates a random number between 1 and 6 which is displayed on the LEDs for 3 seconds. After 3 seconds the LEDs turn OFF again.

Simple PIC18 Projects-0012

Project Hardware

The circuit diagram of the project is shown in Figure 6.8. Seven LEDs representing the faces of a dice are connected to PORTC of a PIC18F452 microcontroller in current sourcing mode using 290-ohm current limiting resistors. A push-button switch is connected to bit 0 of PORTB (RB0) using a pull-up resistor. The microcontroller is operated from a 4MHz resonator connected between pins OSC1 and OSC2. The microcontroller is powered from a þ9V battery, and a 78L05-type voltage regulator IC is used to obtain the þ5V supply required for the microcontroller.

Simple PIC18 Projects-0013

Project PDL

The operation of the project is described in PDL in Figure 6.9. At the beginning of the program PORTC pins are configured as outputs and bit 0 of PORTB (RB0) is configured as input. The program then executes in a loop continuously and increments a variable between 1 and 6. The state of the push-button switch is checked and when the switch is pressed (switch output at logic 0), the current number is sent to the LEDs. A simple array is used to find out the LEDs to be turned ON corresponding to the dice number.

Simple PIC18 Projects-0014

Table 6.1 gives the relationship between a dice number and the corresponding LEDs to be turned ON to imitate the faces of a real dice. For example, to display number 1 (i.e., only the middle LED is ON), we have to turn on D4. Similarly, to display number 4, the LEDs to turn ON are D1, D3, D5, and D7.

Simple PIC18 Projects-0015

The relationship between the required number and the data to be sent to PORTC to turn on the correct LEDs is given in Table 6.2. For example, to display dice number 2, we have to send hexadecimal 0x22 to PORTC. Similarly, to display number 5, we have to send hexadecimal 0x5D to PORTC, and so on.

Simple PIC18 Projects-0016

Project Program

The program is called LED2.C, and the program listing is given in Figure 6.10. 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. Variable J is used as the dice number. Variable Pattern is the data sent to the LEDs. Program then enters an endless for loop where the value of variable J is incremented very fast between 1 and 6. When the push-button switch is pressed, the LED pattern corresponding to the current value of J is read from the array and sent to the LEDs. The LEDs remain in this state for 3 seconds (using function Delay_ms with the argument set to 3000ms), after which they all turn OFF. The system is then ready to generate a new dice number.

Using a Pseudorandom Number Generator

In the preceding project the value of variable J changes very fast among the numbers between 1 and 6, so we can say that the numbers generated are random (i.e., new numbers do not depend on the previous numbers).

A pseudorandom number generator function can also be used to generate the dice numbers. The modified program listing is shown in Figure 6.11. In this program a function called Number generates the dice numbers. The function receives the upper limit of the numbers to be generated (6 in this example) and also a seed value which

Simple PIC18 Projects-0017

Simple PIC18 Projects-0018

Simple PIC18 Projects-0019

defines the number set to be generated. In this example, the seed is set to 1. Every time the function is called, a number between 1 and 6 is generated.

The operation of the program is basically same as in Figure 6.10. When the push-button switch is pressed, function Number is called to generate a new dice number between

1 and 6, and this number is used as an index in array DICE in order to find the bit pattern to be sent to the LEDs.

Leave a comment

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