========
In this project 7 LEDs are connected to PORTC of a PIC18F452 microcontroller
and 7 LEDs to PORTD. The microcontroller is operated from a 4MHz resonator.
The LEDs are organized as the faces of a real dice. When a push-button switch
connected to RB0 is pressed a dice pattern is displayed on the LEDs. The
display remains in this state for 3 seconds and after this period the LEDs
all turn OFF to indicate that the system is ready for the button to be pressed
again.
In this program a pseudorandom number generator function is
used to generate the dice numbers between 1 and 6.
Author: Dogan Ibrahim
Date: July 2007
File: LED4.C
***********************************************************************/
#define Switch PORTB.F0
#define Pressed 0
//
// This function generates a pseudo random integer number
// between 1 and Lim
//
unsigned char Number(int Lim, int Y) {
unsigned char Result;
static unsigned int Y;
Y = (Y * 32719 + 3) % 32749;
Result = ((Y % Lim) + 1);
return Result;
}
//
// Start of MAIN program
//
void main() {
unsigned char J,Pattern,Seed = 1;
unsigned char DICE[] = {0,0x08,0x22,0x2A,0x55,0x5D,0x77};
TRISC = 0; // PORTC are outputs
TRISD = 0; // PORTD are outputs
TRISB = 1; // RB0 input
PORTC = 0; // Turn OFF all LEDs
PORTD = 0; // Turn OFF all LEDs
for(;;) // Endless loop
{
if (Switch == Pressed) // Is switch pressed ?
{
J = Number(6,seed); // Generate first dice number
Pattern = DICE[J]; // Get LED pattern
PORTC = Pattern; // Turn on LEDs for first dice
J = Number(6,seed); // Generate second dice number
Pattern = DICE[J]; // Get LED pattern
PORTD = Pattern; // Turn on LEDs for second dice
Delay_ms(3000); // Delay 3 seconds
PORTC = 0; // Turn OFF all LEDs
PORTD = 0; // Turn OFF all LEDS
}
}
}
Figure 6.15: Program listing
PROJECT 6.4 — Two-Dice Project Using Fewer I/O Pins
Project Description
This project is similar to Project 3, but here LEDs are shared, which uses fewer input/output pins.
The LEDs in Table 6.1 can be grouped as shown in Table 6.3. Looking at this table we can say that:
• D4 can appear on its own
• D2 and D6 are always together
• D1 and D3 are always together
• D5 and D7 are always together
Table 6.3: Grouping the LEDs
| Required number | LEDs to be turned on |
|---|---|
| 1 | D4 |
| 2 | D2 D6 |
| 3 | D2 D6 D4 |
| 4 | D1 D3 D5 D7 |
| 5 | D1 D3 D5 D7 D4 |
| 6 | D2 D6 D1 D3 D5 D7 |
Thus, we can drive D4 on its own and then drive the D2, D6 pair together in series, the D1, D3 pair together in series, and also the D5, D7 pair together in series. (Actually, we could share D1, D3, D5, D7 but this would require 8 volts to drive if the LEDs are connected in series. Connecting them in parallel would call for even more current, and a driver IC would be required.) Altogether, four lines are needed to drive the seven LEDs of each dice. Thus, a pair of dice can easily be driven from an 8-bit output port.
Project Hardware
The circuit diagram of the project is shown in Figure 6.16. PORTC of a PIC18F452 microcontroller is used to drive the LEDs as follows:
• RC0 drives D2,D6 of the first dice
• RC1 drives D1,D3 of the first dice
• RC2 drives D5,D7 of the first dice
• RC3 drives D4 of the first dice
• RC4 drives D2,D6 of the second dice
• RC5 drives D1,D3 of the second dice
• RC6 drives D5,D7 of the second dice
• RC7 drives D4 of the second dice
Figure 6.16: Circuit diagram of the project
Since two LEDs are being driven on some outputs, we can calculate the required value of the current limiting resistors. Assuming that the voltage drop across each LED is 2V, the current through the LED is 10mA, and the output high voltage of the microcontroller is 4.85V, the required resistors are:
We will choose 100-ohm resistors.
We now need to find the relationship between the dice numbers and the bit pattern to be sent to the LEDs for each dice. Table 6.4 shows the relationship between the first dice numbers and the bit pattern to be sent to port pins RC0–RC3. Similarly, Table 6.5 shows the relationship between the second dice numbers and the bit pattern to be sent to port pins RC4–RC7.
Table 6.4: First dice bit patterns
| Dice number | RC3 | RC2 | RC1 | RC0 | Hex value |
|---|---|---|---|---|---|
| 1 | 1 | 0 | 0 | 0 | 8 |
| 2 | 0 | 0 | 0 | 1 | 1 |
| 3 | 1 | 0 | 0 | 1 | 9 |
| 4 | 0 | 1 | 1 | 0 | 6 |
| 5 | 1 | 1 | 1 | 0 | E |
| 6 | 0 | 1 | 1 | 1 | 7 |