A keypad is the most widely used input devices of a microcontroller. In order to use it effectively, we need a basic understanding of them. In this section, we discuss keyboard fundamentals, along with key press and key detection mechanisms, Then we show how a keyboard is interfaced to an AT89S51 microcontroller.
At the lowest level, keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and column through ports; therefore, with a port of microcontroller, a 4X3 matrix of keys can be connected. When a key pressed, a row and column make a connection; otherwise, there is no connection between row and column.
Scanning and Identifying the Key Pressed
Figure1 shows a 4X3 matrix connected to port 1. The rows (R1 through R4) are connected to an output port and the columns (C1 through C3) are connected to an input port. Note that, we only use 7 pins of port 1.
To detect a pressed key, first, the microcontroller set pin 0 through 7 of port 1 and initiate the variable ‘Digit’ to zero. The value of ‘Digit’ represents the digit of key pressed.
Then it sends 0111 to R1 R2 R3 R4 and it reads the columns. If the data read from the columns is C1 C2 C3 =111, no key has been pressed and the process continues to next step.
If the data read from the columns is C1 C2 C3 =011, this means that a key in the R1 row and C1 column has been pressed. That is ‘1’.
In the subroutine ‘CheckColumn’, the value of ‘Digit’ is increased. The value of ‘Digit’ represents the key pressed. Before leave this subroutine, microcontroller set ‘KeyPressed’ to indicate there is a key pressed.
If the data read from the columns is C1 C2 C3 =101, this means that a key in the R1 row and C2 column has been pressed. That is ‘2’. Table 1 represents the meaning of each combination of data received at C1 C2 C3.
Someone can not press two key at the same time. There is time different between press a key with another key. ‘KeyPressed’ indicates there is a key pressed in this time. Before leave the entire subroutine, if ‘KeyPressed’ has been set, the microcontroller set the variable ‘KeyAlreadyPressed’. This variable will not be cleared until this subroutine detect that there is no key pressed. So, if press more than two key, only the first key will be read.
To see if any key is pressed, the columns are scanned over and over in an infinite loop until one of them has a 0 on it. The listing below is only for example. In fact, it’s no efficient if we scan the keypad without any intervals of time. We usually use the intervals time for scanning more than 10 ms. This interval can be generated by timer.
Listing Program.
Program for counter
Sometimes we need a counter to count an event at digital scope. Counter can be made by JK flip-flop. There are some integrated chip contains two or more JK flip-flop that can be operated as a counter. A flip-flop can act as one bit counter. So, if we need 4 bit counter, that can count 0 to 16, we need four flip-flops.
A programmable counter can be made by a microcontroller. AT89S51 have two timers that can be operated as independent counter. To do this, we must set c/t bit at TMOD register. This sample will operate timer 0 as mode 2 counter. By loading 155des at TL0 and TH0, timer 0 act as 100des counter.
A simple frequency counter can be made by this counter.
This is the sample program for counting negative edge of input clock at T0 (p3.4). The number of clock will displayed at 2 digit of 7-segment LED common anode.
This program also acts as frequency divider. The clock frequency will be divided by 100 and emitted to P3.2. Press INT0 for pause and INT0 again to resume.
Here is the listing program.
A programmable counter can be made by a microcontroller. AT89S51 have two timers that can be operated as independent counter. To do this, we must set c/t bit at TMOD register. This sample will operate timer 0 as mode 2 counter. By loading 155des at TL0 and TH0, timer 0 act as 100des counter.
A simple frequency counter can be made by this counter.
This is the sample program for counting negative edge of input clock at T0 (p3.4). The number of clock will displayed at 2 digit of 7-segment LED common anode.
This program also acts as frequency divider. The clock frequency will be divided by 100 and emitted to P3.2. Press INT0 for pause and INT0 again to resume.
Here is the listing program.
Displaying 2 digits to 7-segment LED
The 2 digit 7-segment display can seem a little complicated. The main idea for this system is to connect both 7-segment cells together in parallel, but in the same time, only one that be powered.
Here is the simple sequence to show the ones at the right cell and tens at the left cell.
1. Sends data ‘ones’ though the ‘PortLED’.
2. Energizes the right cell while left cell is off by resetting P1.0.
3. Wait for a short time delay. In this project, we paused for 8.192 ms that was setted by interrupt from timer 1.
4. Sends data ‘tens’ though the ‘PortLED’.
5. Energizes the left cell while right cell is off by setting P1.0.
6. Wait for 8.192 ms too.
Here is a sample program for displaying 00~99 to 7-Segment LED common anode.Each digit activated by multiplexing every 8128 ms.
The ones will increment in 1 second.
Subscribe to:
Posts (Atom)