Table 6.5: Second 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 |
We can now find the 8-bit number to be sent to PORTC to display both dice numbers as follows:
• Get the first number from the number generator, call this P
• Index the DICE table to find the bit pattern for low nibble (i.e., L = DICE[P])
• Get the second number from the number generator, call this P
• Index the DICE table to find the bit pattern for high nibble (i.e., U = DICE[P])
• Multiply high nibble by 16 and add low nibble to find the number to be sent to PORTC (i.e., R = 16*U + L), where R is the 8-bit number to be sent to PORTC to display both dice values.
Project PDL
The operation of this project is very similar to that of Project 2. Figure 6.17 shows the PDL of the project. At the beginning of the program the 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 checks the state of the push-button switch. When the switch is pressed, two pseudorandom numbers between 1 and 6 are generated, and the bit pattern to be sent to PORTC is found by the method just described. This bit pattern is then sent to PORTC to display both dice numbers at the same time. The display shows the dice numbers for 3 seconds, and then all the LEDs turn OFF to indicate that the system is waiting for the push-button to be pressed again to display the next set of numbers.
START
Create DICE table
Configure PORTC as outputs
Configure RB0 as input
DO FOREVER
IF button pressed THEN
Get a random number between 1 and 6
Find low nibble bit pattern
Get second random number between 1 and 6
High high nibble bit pattern
Calculate data to be sent to PORTC
Wait 3 seconds
Turn OFF all LEDs
ENDIF
ENDDO
END
Figure 6.17: PDL of the project
Project Program
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