Выбрать главу

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 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.

/*********************************************************************

                            SIMPLE DICE

                            ===========

In this project 7 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:   LED3.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 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 a number between 1 and 6

   Pattern = DICE[J];   // Get LED pattern

   PORTC = Pattern;     // Turn on LEDs

   Delay_ms(3000);      // Delay 3 second

   PORTC = 0;           // Turn OFF all LEDs

  }

 }

}

Figure 6.11: Dice program using a pseudorandom number generator

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.

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.

Figure 6.12: 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.

Figure 6.13: Circuit diagram of the project

Project PDL

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 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.

START

 Create DICE table

 Configure PORTC as outputs

 Configure PORTD as outputs

 Configure RB0 as input

 DO FOREVER

  IF button pressed THEN

   Get a random number between 1 and 6

   Find bit pattern

   Turn ON LEDs on PORTC

   Get second random number between 1 and 6

   Find bit pattern

   Turn on LEDs on PORTD

   Wait 3 seconds

   Turn OFF all LEDs

  ENDIF

 ENDDO

END

Figure 6.14: PDL of the project

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.

/*********************************************************************

                               TWO DICE