• No results found

Using Matrix Keypad

In document PIC for Beginners (Page 85-88)

rows to reduce the short circuit among row and column pins. Just connect the keypad connector on the PORTB connector starting from RB0. also connect the LCD to see the results returned by keypad.

Proton Basic provides an InKey command to scan the keypad. Before using the Inkey Command the

com-piler needs to be informed about the port on which keypad has been connected.

The x=Inkey command will scan the keypad, and return a number in variable x. this command will not wait for a key to be pressed. If no key is being pressed it will return a value of 16. other values will depend how

keypad has been attached. If properly connected following codes should be returned.

Notice numbers 003, 007 and 015 are missing, these are for column 4 keys if you have 4 x 4 column key-pad. If you do not get these numbers in this order, reverse the keypad connector, so that rows are connected to lower port bits and columns on upper port pins.

Mapping The KeyPad Labels

The keypad is basically a matrix of switches, it may not always be numeric keypad. There can be various symbols or some other labels, as per requirements of the project. Although you as a programmer know which key has been pressed by knowing its code, you can however your life easier by mapping various la-bels to the returned values. This can be implemented using If statement, the Proton Basic however provides a useful command called LookUp. The lookup command accepts a variable, and a set of labels, which are

returned based upon index value given.

This program scans the keypad and reads the keypad value into variable x. the next line uses x as an index to locate a related value in the list. We have put 255 in places where a key does not exist, and the last 255 for number 16 if no key is pressed. The variable ‘a’ will now contain the matching numbers from the list.

Note the * and # signs have been enclosed within inverted comas, this will return their ASCII Codes.

Reading Keypad to get a Number

Well so far we have practiced with keypad to read in individual keys. What if we want to read in a value, Device = 18F452

Print At 1,1,"Key Code:", DEC3 x GoTo loop

a= LookUp x, [1,2,3,255,4,5,6,255,7,8,9,255,"*",0,"#",255,255]

Print At 1,1,"Key Code:", DEC3 x Print At 2,1,"Key Lbl :", DEC3 a GoTo loop

and store it in a variable like an integer. Suppose we want to make a password protected device to open a lock when the right password has been entered. The first task would be to read the keypad keys and make a one number. Like if we press 6712 and then * the variable should contain number 6712 which can then be used in any other calculation, comparison or what not. The bare idea is to scan the keypad, and read the dig-its as 0-9 ignoring the idle state. When a number is read the number is added to the variable. When * is

pressed the routine finishes.

The key point in this routine is a variable mynumber, which is word sized variable, so it can hold a maxi-mum value of 65534. its initial value is 0, each time a key is pressed, its value is multiplied by 10, that shifts the present value left by 1 digit, and new digit is added in the units place.

Device = 18F452 XTAL = 20

ALL_DIGITAL=true KEYPAD_PORT PORTB PORTB_PULLUPS true LCD_DTPIN PORTD.4 LCD_RSPIN PORTD.3 LCD_ENPIN PORTD.2 Print Cls

Dim x As Byte

Dim mynumber As Word start:

Print Cls mynumber=0 loop:

x=InKey

x= LookUp x, [1,2,3,255,4,5,6,255,7,8,9,255,"*",0,"#",255,255]

If x="*" Then GoTo Exit_loop If x <> 255 Then

DelayMS 500

mynumber=mynumber *10 + x Print Cls, At 1,1, Dec mynumber EndIf

GoTo loop Exit_loop:

Print Cls

Print At 1,1,"You Entered:"

Print At 2,1,Dec mynumber DelayMS 5000

GoTo start

I

t is rightly said that we live in Analog World, but we process our information in digital world. Most of the real world data is in Analog form, like temperature, pressure, humidity, altitude, distance, speed, force, voltage, light, radiation, direction, depth and hundreds of more parameters, they are all analog.

In order to interact with these analog signals, we have to transform them some how into digital equivalents. The first step in this regards is an appropriate sensor that should be able to detect the particular modality, like temperature and convert the physical world entity into a corresponding electrical signal. The strength of signal in turn corresponds to the measured value.

Most of the sensors return the sensed data as voltage. The strength of physical parameter measured is re-flected in the level of voltage returned. In order to use this analog data (voltage) in digital world, it has to be converted into digital equivalent. This process is called Analog to Digital Conversion or ADC. The ADC device has complex design of resistor ladders and networks that sequentially divide the input voltage into discreet levels and then return the value as digital number.

Since interaction with digital world is quite common for microcontrollers, PIC 18F452 has 8 channels of ADC input. The pins associated with analog inputs are also used for other purposes, in order to use them as analog certain registers have to be set. They enable microcontroller to recognize not only whether some pin is driven to logic zero or one (0 or +5V), but to precisely measure its voltage and convert it into numerical value, i.e. digital format. The whole procedure takes place in A/D converter module which has the follow-ing features:

• The converter generates a 10-bit binary result using the method of successive approximation and stores the conversion results into the ADC registers (ADRESL and ADRESH).

• There are 8 separate analog inputs.

• The A/D converter allows conversion of an analog input signal to a 10-bit binary representation of that signal.

• By selecting voltage references Vref- and Vref+, the minimal resolution or quality of conversion may be adjusted to various needs.

Chapter 13

In document PIC for Beginners (Page 85-88)