Figure 6.17: PDL of the project
The program is called LED5.C, and the program listing is given in Figure 6.18. 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. The 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. Variables L and U store the lower and higher nibbles of the bit pattern to be sent to PORTC. The bit pattern to be sent to PORTC is then determined using the method described in the Project Hardware section and stored in variable R. This bit pattern is then sent to PORTC to display both dice numbers at the same time. The dice numbers are displayed for 3 seconds, after which the LEDs are turned OFF to indicate that the system is ready.
/************************************************************************
TWO DICE - USING FEWER I/O PINS
==============================
In this project LEDs are connected to PORTC of a PIC18F452 microcontroller
and 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: LED5.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,L,U,R,Seed = 1;
unsigned char DICE[] = {0,0x08,0x01,0x09,0x06,0x0E,0x07};
TRISC = 0; // PORTC are outputs
TRISB = 1; // RB0 input
PORTC = 0; // Turn OFF all LEDs
for(;;) // Endless loop
{
if (Switch == Pressed) // Is switch pressed ?
{
J = Number(6,seed); // Generate first dice number
L = DICE[J]; // Get LED pattern
J = Number(6,seed); // Generate second dice number
U = DICE[J]; // Get LED pattern
R = 16*U + L; // Bit pattern to send to PORTC
PORTC = R; // Turn on LEDs for both dice
Delay_ms(3000); // Delay 3 seconds
PORTC = 0; // Turn OFF all LEDs
}
}
}
Figure 6.18: Program listing
The program given in Figure 6.18 can made more efficient by combining the two dice nibbles into a single table value as described here.
There are thirty-six possible combinations of the two dice values. Referring to Table 6.4, Table 6.5, and Figure 6.16, we can create Table 6.6 to show all the possible two-dice values and the corresponding numbers to be sent to PORTC.
Table 6.6: Two-dice combinations and the number to be sent to PORTC
Dice numbers |
PORTC value |
Dice numbers |
PORTC value |
1,1 |
0x88 |
4,1 |
0x86 |
1,2 |
0x18 |
4,2 |
0x16 |
1,3 |
0x98 |
4,3 |
0x96 |
1,4 |
0x68 |
4,4 |
0x66 |
1,5 |
0xE8 |
4,5 |
0xE6 |
1,6 |
0x78 |
4,6 |
0x76 |
2,1 |
0x81 |
5,1 |
0x8E |
2,2 |
0x11 |
5,2 |
0x1E |
2,3 |
0x91 |
5,3 |
0x9E |
2,4 |
0x61 |
5,4 |
0x6E |
2,5 |
0xE1 |
5,5 |
0xEE |
2,6 |
0x71 |
5,6 |
0x7E |
3,1 |
0x89 |
6,1 |
0x87 |
3,2 |
0x19 |
6,2 |
0x17 |
3,3 |
0x99 |
6,3 |
0x97 |
3,4 |
0x69 |
6,4 |
0x67 |
3,5 |
0xE9 |
6,5 |
0xE7 |
3,6 |
0x79 |
6,6 |
0x77 |
The modified program (program name LED6.C) is given in Figure 6.19. In this program array DICE contains the thirty-six possible dice values. The program enters an endless for loop, and inside this loop the state of the push-button switch is checked. Also, a variable is incremented from 1 to 36. When the button is pressed, the value of this variable is used as an index to array DICE to determine the bit pattern to be sent to PORTC. As before, the program displays the dice numbers for 3 seconds and then turns OFF all LEDs to indicate that it is ready.
/*****************************************************************************
TWO DICE - USING FEWER I/O PINS
=============================
In this project LEDs are connected to PORTC of a PIC18F452 microcontroller
and 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: LED6.C
******************************************************************************************/
#define Switch PORTB.F0
#define Pressed 0
//
// Start of MAIN program
//
void main() {
unsigned char Pattern, J = 1;
unsigned char DICE[] = {0,0x88,0x18,0x98,0x68,0xE8,0x78,
0x81,0x11,0x91,0x61,0xE1,0x71,
0x89,0x19,0x99,0x69,0xE9,0x79,
0x86,0x16,0x96,0x66,0xE6,0x76,
0x8E,0x1E,0x9E,0x6E,0xEE,0x7E,
0x87,0x17,0x97,0x67,0xE7,0x77};
Читать дальше