11.8 PROJECT 11.8 – The High/Low Game

This project uses a 4 × 4 keypad to create the classical a High/Low game. For those of you who are not familiar with the game, here are the rules for this version of the game:

  • The computer will generate a secret random number between 1 and 32 767.
  • The top row of the LCD will display ‘Guess Now . . .’
  • The player will try to guess what the number is, by entering a number on the keypad and then pressing the ENTER key.
  • If the guessed number is higher than the secret number, the bottom row of the LCD will display ‘HIGH – Try Again’.
  • If the guessed number is lower than the secret number, the bottom row of the LCD will display ‘LOW – Try Again’.
  • If the player guesses the number, then the bottom row will display ‘Well Done . . .’
  • The program waits for 5 seconds and the game re-starts automatically.

Before going into the design of the project, it is worthwhile to learn a bit more about how the keypads work and the mikroC Pro for PIC commands available to use a keypad.

11.8.1 Keypads

A 4 × 4 keypad consists of 16 keys with internal mechanical switches at each key position. Figure 11.32 shows a typical 4 × 4 keypad connected to PORT D of a PIC microcontroller.

Figure 11.32 A 4 × 4 keypad connected to a microcontroller

img

The keypad operates on the principle of ‘scan and detect’, where in Figure 11.32, RD0, RD1, RD2 and RD3 are configured as inputs and connected to columns of the keypad via pull-down resistors. RD4, RD5, RD6 and RD7 pins are configured as outputs and connected to rows of the keypad. The program sends logic 1 to each row of the keypad in turn and checks the columns by reading the logic state at the column. Pressing any key will cause logic 1 to be applied to input pins of the microcontroller. The software detects which key is pressed by scanning the inputs. For example, assume that while sending out logic 1 to row 2 where numbers 4, 5, 6, B are, we detect that RD2 input is logic 1. In this case, the pressed key must be number 5, and so on.

11.8.2 mikroC Pro for PIC Keypad Library Functions

mikroC Pro for PIC provides a built-in library called ‘Keypad Library’, which contains functions to help us use keypads in our programs. These functions are given below:

  • Keypad_Init
  • Keypad_Key_Press
  • Keypad_Key_Click

11.8.2.1 Keypad_Init

This function initialises the keypad library. The port where the keypad is connected must be declared before this function is called using the reserved name ‘keypadPort’. For example, if the keypad is connected to PORT D, then at the beginning of the program we must declare:

img

11.8.2.2 Keypad_Key_Press

This function reads the key from the keypad when it is pressed. The code of the key is returned as a number between 1 and 16. If no key is pressed, a 0 is returned.

11.8.2.3 Keypad_Key_Click

This function is similar to Keypad_Key_Press, but this is a blocking function. The function waits until a key is pressed. If more than one key is pressed, the function will wait for all the keys to be released and then return code of the first key pressed.

11.8.3 Generating a Random Number

In our program, we will be generating a random integer number using the mikroC Pro for PIClibrary functions ‘srand’ and ‘rand’. Function ‘srand’ must be called with an integer argument (or ‘seed’) to prepare the random number generator library. Then, every time function ‘rand’ is called, a new random number will be generated between 1 and 32 767. The set of numbers generated are the same if the program is re-started with the same ‘seed’ applied to function ‘srand’. Thus, if the game is re-started after resetting the microcontroller, the same set of numbers will be generated.

11.8.3.1 Block Diagram

The block diagram of the project is shown in Figure 11.33. The keypad is organised, as shown in Figure 11.34.

Figure 11.33 Block diagram of the project

img

Figure 11.34 Key configuration on the keypad

img

The numbers returned by the mikroC Pro for PICkeypad library when a key is pressed is as follows:

Key pressed Number returned
1 1
2 2
3 3
A 4
4 5
5 6
6 7
B 8
7 9
8 10
9 11
C 12
* 13
0 14
# 15
D 16

We will be using key ‘D’ as the ENTER key in our program. Also, we will be correcting the key numbering in our program so that, for example, when ‘7’ is pressed on the keypad, a 7 is returned and not a 9 as in the above table.

Figure 11.35 shows a typical 4 × 4 keypad, manufactured by mikroElektronika (http://www.mikroe.com). This keypad can be directly plugged into the PORT D connector at the edge of the EasyPIC 7 development board.

Figure 11.35 A typical 4 × 4 keypad

img

11.8.3.2 Circuit Diagram

The circuit diagram of the project is shown in Figure 11.36. The LCD is connected to PORT B, as in the earlier projects. The rows and columns of the keypad are connected to upper and lower nibbles of PORT D, respectively.

Figure 11.36 Circuit diagram of the project

img

11.8.3.3 Project PDL

The PDL of this project is given in Figure 11.37.

Figure 11.37 PDL of the project

img

11.8.3.4 Project Program

The program is named LCD8.C and the program listing of the project is shown in Figure 11.38.

Figure 11.38 Program listing of the project

img

img

At the beginning of the program, keypadPORT is declared as PORTD and some other variables used in the program are also declared. Then PORTB is configured as digital I/O, keypad library is initialised, the LCD is initialised and message ‘High/Low Game’ is displayed on the LCD. After a 2 second delay, the program continues in an endless loop.

If this is a new game, the LCD is cleared and message ‘Guess Now . . . ’ is displayed on the first row of the LCD. Then a random number is generated between 1 and 32 767 by calling library function ‘rand’ and this number is stored in variable ‘GuessNumber’. Notice that the ‘srand’ library function must be called with an integer number before calling ‘rand’.

The keypad is then checked and numbers are received until the ENTER key (key D) is pressed. The key numbers are then adjusted such that if, for example, 4 is pressed, number 4 is used by the program instead of 5. Similarly, if key 0 is pressed, number 0 is used by the program instead of 14 returned by the keypad library routine. The numbers entered by the player are displayed on the second row of the LCD, as they are entered so that the player can see what he/she has entered. After the player presses the ENTER key, a 1 second delay is introduced. The number entered by the player is stored in variable ‘PlayerNumber’ in decimal format.

The program then calculates the difference between the secret number in ‘GuessNumber’ and the number entered by the player (in PlayerNumber). This difference is stored in variable ‘Diff’.

If ‘Diff’ is positive, that is if the number entered by the player is greater than the secret number, then the program displays message ‘HIGH - Try Again’, waits for 1 second, and clears the second row of the LCD, ready for the player to try another number.

If ‘Diff’ is negative, that is if the number entered by the player is less than the secret number, then the program displays message ‘LOW - Try Again’, waits for 1 second, and clears the second row of the LCD, ready for the player to try another number.

If ‘Diff’ is 0, that is if the number entered by the player is equal to the secret number, then the program displays message ‘Well Done . . . ’ waits for 5 seconds, and sets the ‘new_game’ flag so that a new secret number can be generated by the program. The game continues as before.

Figures 11.39 to 11.41 show various displays from the game. Notice that the keypad keys are not debounced in the keypad library and sometimes you may get double key strokes, even though you press a key once. You should be firm and quick when pressing a key to avoid this to happen.

Figure 11.39 Display from the game – start of the game

img

Figure 11.40 Display from the game – user guessed 258

img

Figure 11.41 Display from the game – the guess was low

img
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.157.39