Sunday, March 14, 2021

ATMega32 interfaces to keypad and display

Introduction

A keypad is commonly found on telephone, ATM machine, fax machine, etc. It's arranged in matrix form, 3x4, 4x4, etc. The advantage of matrix arrangement of this digital input is saving the number of controller digital I/O. As an instance, a 4x4 keypad matrix requires only 8 pins of micro-controller, but it yields 16 difference keys.

ATMega32 interfaces to keypad and display
Simulation of this example program

Programming

Using 8-bit of a micro-controller's digital I/O port, this single port is divided into two nibbles. Lower nibble (column) is an output while higher nibble (row) is an input.

We use C loop of four repetition to generate key scanning routine. Whenever any key is pressed, the program determines which column and row are logic low that lead to key founding.

ATMega32 interfaces to keypad and display
Circuit diagram

As show in picture above, Port C connects to a 4x4 keypad matrix. These keypad gives key values of 0 to 9, and A to F, which is a hexadecimal representation.

Port D connects to a single seven segments display showing pressed key value. ATMega32 here uses its internal 8MHz oscillator.

I divide this program into C main file, and its keypad library I wrote for my own use.

C main file:

/*
 * _8_Keypad_C_SSD.c
 *
 * Created: 4/24/2018 8:51:03 PM
 *  Author: admin
 */ 


#include <avr/io.h>

#define F_CPU 8000000UL
#include <util/delay.h>

#include "keypad4x4.h"

int main(void)
{
DDRD=0xFF;
PORTD=0x00;
keyInit();
    while(1)
    {
        //TODO:: Please write your application code 
PORTD=scan();
_delay_us(100);
    }
}

C library header file:

/*
 * keypad4x4.h
 *
 * Created: 4/24/2018 8:52:11 PM
 *  Author: admin
 */ 


#ifndef KEYPAD4X4_H_
#define KEYPAD4X4_H_

#endif /* KEYPAD4X4_H_ */

extern unsigned char scan();
extern void keyInit();

C library file:

/*
 * keypad4x4.c
 *
 * Created: 4/24/2018 8:55:01 PM
 *  Author: admin
 */ 

#include <avr/io.h>
#include "keypad4x4.h"

#define KEY_DATA PORTC
#define KEY_DIR DDRC
#define KEY_PIN PINC

unsigned char keypad[4][4]={{0x07,0x7F,0x6F,0x71},
                            {0x66,0x6D,0x7D,0x79},
                            {0x06,0x5B,0x4F,0x5E},
                            {0x77,0x3F,0x7C,0x39}};

unsigned char output[4]={0xFE,0xFD,0xFB,0xF7};
unsigned char input[4]={0xEF,0xDF,0xBF,0x7F};
unsigned char key_value;

void keyInit(){
KEY_DATA=0xFF;
KEY_DIR=0x0F;
}

unsigned char scan(){
unsigned char i,j;
for (i=0;i<4;i++)
{
KEY_DATA=output[i];
for (j=0;j<4;j++)
{
KEY_DIR=input[j];
if ((KEY_PIN&0xF0)!=0xF0)
{
while((KEY_PIN&0xF0)!=0xF0);
key_value=keypad[j][i];
break;
}
}
}
return key_value;
}

If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.

 

Click here to download this example.


See Also,


No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)