key encoder interfacing with AVR(74922 with AVR) This tutorial is taken from the book
MY EXPERIENCE IN PROGRAMMING AVR MICROCONTROLLER
IN C USING WINAVR/AVRGCC.
you can download it free from the files
section(FILE-AVRBOOK.RAR) of yahoo group-booksbybibinhttp://groups.yahoo.com/group/booksbybibin/
comments about this book on
www.booksbybibin.blogspot.comKEY ENCODER INTERFACING
Mostly we use Keypad to interface with microcontroller, but keypad has a problem of key debouncing. 74922,74923 avoid this problem and give proper output, if you have problem with keypad interfacing, go for this circuit
connected with LCD
connected with keypad(if you want to use keypad)
/*----------------------------------------------------------------
-----------------HEADER FILES-------------------------------------
-----------------------------------------------------------------*/
#include "lcd.c"
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN 74922 AND ATMEGA32---------------
-----------------------------------------------------------------*/
#define IC_DDR DDRC
#define IC_PORT PORTC
#define IC_PIN PINC
#define Pin_A 0
#define Pin_B 1
#define Pin_C 2
#define Pin_D 3
#define Pin_DA 4
#define Pin_OE 5
#define IC_Mask 0x10 //OE for output remaining for input
#define Output_DDR DDRD
#define Output_PORT PORTD
#define Output_Enable IC_PORT&=~_BV(Pin_OE) //for enabling the IC
/*----------------------------------------------------------------
-----------------FUNCTIONS---------------------------------------
-----------------------------------------------------------------*/
void Init_Ports922(void);
void main()
{
unsigned int i;
Init_Ports();
Output_Enable;
//lcd initializationsa
Init_Ports();
Init_Lcd();
do
{
i=IC_PIN&0X0F;
if(i<10) i+=0x30; //in case of decimal values
if(i>9) i+=55 ;//for showing alphabets
Lcd_Send(i);
while(!((IC_PIN&_BV(Pin_DA))>>Pin_DA));
}
while(1);
}
/*----------------------------------------------------------------
-----------------FUNCTIONS TO INITIALIZE PORTS--------------------
-----------------------------------------------------------------*/
void Init_Ports922(void)
{
IC_DDR=IC_Mask; //setting OE pins for output,remaining for input
Output_DDR=0XFF;
}
LCD.C
/*----------------------------------------------------------------
-----------------HEADER FILES-------------------------------------
-----------------------------------------------------------------*/
#include<avr/io.h>
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN LCD AND ATMEGA32-----------------
-----------------------------------------------------------------*/
#define DATA_DDR DDRA
#define DATA_PORT PORTA
#define CONTROL_DDR DDRD
#define CONTROL_PORT PORTD
#define Enable_Pin 6
#define RegSelect_Pin 4
#define ReadWrite_Pin 5
#define CONTROL_MASK 0X70
/*----------------------------------------------------------------
-------------CONTROL BITS OF LCD --------------------------------
-----------------------------------------------------------------*/
#define Set_Enable CONTROL_PORT|=_BV(Enable_Pin)
#define Clear_Enable CONTROL_PORT&=~_BV(Enable_Pin)
#define Write_Lcd CONTROL_PORT&=~_BV(ReadWrite_Pin)
#define Read_Lcd CONTROL_PORT|=_BV(ReadWrite_Pin)
#define Select_InstructionRegister CONTROL_PORT&=~_BV(RegSelect_Pin)
#define Select_DataRegister CONTROL_PORT|=_BV(RegSelect_Pin)
#define Data_Lcd(a) DATA_PORT=a
#define delay(a) delay_ms(a)
/*----------------------------------------------------------------
-----------------FUNCTIONS---------------------------------------
-----------------------------------------------------------------*/
void Init_Ports(void);
void Init_Lcd(void);
void delay_ms(unsigned char time_ms);
void Lcd_Send(unsigned char a);
/*----------------------------------------------------------------
-----------------SEND A CHARACTER TO LCD-------------------------
-----------------------------------------------------------------*/
void Lcd_Send(unsigned char a)
{
Select_DataRegister;
Write_Lcd;
Data_Lcd(a);
Set_Enable;
delay(30);
Clear_Enable;
delay(30);
}
/*----------------------------------------------------------------
-----------------FUNCTIONS TO INITIALIZE PORTS--------------------
-----------------------------------------------------------------*/
void Init_Ports(void)
{
DATA_DDR=0XFF; //Setting data port for output
CONTROL_DDR=CONTROL_MASK;//setting selected pins of control port for output
CONTROL_PORT&=~(_BV(Enable_Pin)|_BV(RegSelect_Pin )|_BV(ReadWrite_Pin)); //setting values to 0 at starting
}
/*----------------------------------------------------------------
------------FUNCTION TO INITIATE LCD -----------------------------
-----------------------------------------------------------------*/
void Init_Lcd(void)
{
char init[10];
int i;
init[0] = 0x01;
init[1] = 0x38;
init[2] = 0x0c;
init[3] = 0x06;
init[4] = 0x80;
Select_InstructionRegister;
Write_Lcd;
for(i=0;i<5;i++)
{
Data_Lcd(init[i]);
Set_Enable;
delay(30);
Clear_Enable;
delay(30);
}
}
/*----------------------------------------------------------------
--------FUNCTION TO GENERATE DELAY IN MILLISECONDS----------------
-----------------------------------------------------------------*/
void delay_ms(unsigned char time_ms)
{
unsigned short delay_count = F_CPU / 400;
unsigned short cnt;
asm volatile ("\n"
"L_dl1%=:\n\t"
"mov %A0, %A2\n\t"
"mov %B0, %B2\n"
"L_dl2%=:\n\t"
"sbiw %A0, 1\n\t"
"brne L_dl2%=\n\t"
"dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt)
:"r"(time_ms), "r"((unsigned short) (delay_count))
);
}
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN 74922 AND ATMEGA32---------------
-----------------------------------------------------------------*/
#define IC_DDR DDRC
#define IC_PORT PORTC
#define IC_PIN PINC
#define Pin_A 0
#define Pin_B 1
#define Pin_C 2
#define Pin_D 3
#define Pin_DA 4
#define Pin_OE 5
#define IC_Mask 0x10 //OE for output remaining for input
#define Output_DDR DDRD
#define Output_PORT PORTD
#define Output_Enable IC_PORT&=~_BV(Pin_OE) //for enabling the IC
Here I am using a single port for connecting the pins of 74922. You can see that all pins are input except Ouput Enable. So you have to configure that pin for output andit should be active low.IC_DDR=IC_Mask; //setting OE pins for output,remaining for inputHere I take input from PORTC and outputting the key pressed to PORTD. See thecapacitor values otherwise keydebouncing will occurs, it should be C and 10Crespectively. See the datasheet for more reference.Now let's see the keypressed on LCD. So I am using the previous program in thefolder 'lcd' to a normal .c file after removing the main() function. Put that .c file insame directory and send the value to the LCD. Before sending we should make itASCII value for displaying on LCD. '0' in hexadecimal corresponds to 48 in ASCII. Wehave to do same with alphabets also. See the code, you will understand better. See the
diagram below for the circuit.
Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.