Reputation: 1
Below is the avr code to 4X3 keypad interfacing to an ATmega. I can only see '#' on the display at the end. I am not able to figure out where i have been doing a mistake,any help is greatly appreciated.
#ifndef F_CPU
#define F_CPU 1000000UL // 1 MHz clock speed
#endif
#define D0 eS_PORTB0
#define D1 eS_PORTB1
#define D2 eS_PORTB2
#define D3 eS_PORTB3
#define D4 eS_PORTB4
#define D5 eS_PORTB5
#define D6 eS_PORTB6
#define D7 eS_PORTB7
#define RS eS_PORTD5
#define EN eS_PORTD7
#define RW eS_PORTD6
// #define KEYPAD PORTC //KEYPAD IS ATTACHED ON PORTC
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include "lcd.h"
uint8_t key,keypressed;
void main()
{
DDRB = 0xFF;
DDRD = 0xFF;
int i;
Lcd8_Init();
DDRC|= 0XF0;
PORTC|= 0X0F;
_delay_ms(5000);
while(1)
{
for(i=0;i<10;i++){
if (PINC!=0b11111111)//in any of column pins goes high execute the loop
{
if (PINC==0b00010001)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("1");//if row1 and column1 is high show “1”
}
if (PINC==0b00010010)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("4");// if row1 and column2 is high show “4”
}
if (PINC==0b00010100)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("7");// if row1 and column3 is high show “7”
}
if (PINC==0b00011000)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("*");//if row1 and column4 is high show “*”
}
if (PINC==0b00100001)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("2");// if row2 and column1 is high show “2”
}
if (PINC==0b00100010)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("5");// if row2 and column2 is high show “5”
}
if (PINC==0b00100100)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("8");// if row2 and column3 is high show “8”
}
if (PINC==0b00101000)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("0");// if row2 and column4 is high show “0”
}
if (PINC==0b01000001)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("3");
}
if (PINC==0b01000010)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("6");
}
if (PINC==0b01000100)
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("9");
}
if (PINC==0b01001000);
{
Lcd8_Set_Cursor(0,0);
Lcd8_Write_String("#");
}
}
else PINC == 0b11111111;
return 0;
}
}
}
changes made and the code i think is somewhat okay
#ifndef F_CPU
#define F_CPU 1000000UL // 1 MHz clock speed
#endif
#define D0 eS_PORTB0
#define D1 eS_PORTB1
#define D2 eS_PORTB2
#define D3 eS_PORTB3
#define D4 eS_PORTB4
#define D5 eS_PORTB5
#define D6 eS_PORTB6
#define D7 eS_PORTB7
#define RS eS_PORTD5
#define EN eS_PORTD7
#define RW eS_PORTD6
// #define KEYPAD PORTC //KEYPAD IS ATTACHED ON PORTC
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include "lcd.h"
void col_init(void)
{
DDRD = 0X0F;
PORTD = 0X70;
_delay_ms(5);
}
void row_init(void)
{
DDRD = 0X78;
PORTD = 0X87;
_delay_ms(5);
}
unsigned char read_key(void)
{
unsigned char value;
col_init();
value = 0;
if(!(PINC & 0X10))
{
value = 0X01;
}
else if(!(PINC & 0X20))
{
value = 0X02;
}
else if(!(PINC & 0X40))
{
value = 0X03;
}
row_init();
if(!(PINC & 0X01))
{
value += 0X00;
}
else if(!(PINC & 0X02))
{
value += 0X03;
}
else if(!(PINC & 0X04))
{
value += 0X06;
}
else if(!(PINC & 0X08))
{
value += 0X00;
}
_delay_ms(50);
return value;
}
int main (void)
{
unsigned char keypressed;
DDRB = 0xFF;
DDRD = 0xFF;
Lcd8_Init();
keypressed = 0X00;
col_init;
while(1)
{
if(!(PINC == 0X70))
{
keypressed = read_key();
}
Lcd8_Set_Cursor(1,1);
Lcd8_Write_String(keypressed);
}
}
Upvotes: 0
Views: 1551
Reputation: 6092
You should post a schematic. Its hard to help here. What I noticed:
1)
DDRC|= 0XF0;
PORTC|= 0X0F;
You only set 4 pins on port C as output. Judging from your if() conditions and that you have a 4x3 keypad (7 pins) this is probably wrong.
2) What is the for loop supposed to do?
3) I think you messed up your braces:
This
}
else PINC == 0b11111111;
return 0;
}
can be written this way:
} else {
PINC == 0b11111111;
}
return 0;
}
That is probably not what you intended.
Advice:
Reduce your code to a minimal example. Try to detect ONE button. Re-think your code.
Upvotes: 1