Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

4x4 and 3x3 keypad interfacing with ATmega128 problem?

Status
Not open for further replies.

nihal_2000

Member level 1
Joined
Aug 31, 2009
Messages
33
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
India
Activity points
1,496
Hi friends...I am just trying to connect key pad. There are two keypads connected to controller one is on PORTD which is 4x4 and other on PORTC 3x3, in following code just trying to get key number and display using LED's on PORTA. FCPU is 12MHz and Controller is ATmega128.
i am using Code-vision compiler...i have tried my level best to get the result .....but all the LED's on PORTA are constantly glowing irrespective to pressed key .... Please help me!

Code:
#include <mega16.h>
#include <delay.h>
#include <stdlib.h>


int key(void);

void main(void)
{

unsigned char i=0;
DDRA=0xff;
DDRB=0x0F;
DDRC=0x07;
DDRD=0x0F;

while(1){
i=key();
PORTA=i;

            }

}


int key(void){
char KEY = 1 ;
while(KEY){

PORTD.0 = 1 ;
PORTD.1 = 0 ;
PORTD.2 = 0 ;
PORTD.3 = 0 ;
if(PIND.4 == 1){return 7 ; KEY = 0;delay_ms(50);}
if(PIND.5 == 1){return 8 ; KEY = 0;delay_ms(50);}
if(PIND.6 == 1){return 9 ; KEY = 0;delay_ms(50);}
if(PIND.7 == 1){return 10; KEY = 0;delay_ms(50);}
//==========================================
PORTD.0 = 0 ;
PORTD.1 = 1 ;
PORTD.2 = 0 ;
PORTD.3 = 0 ;
if(PIND.4 == 1){return 4 ; KEY = 0;}
if(PIND.5 == 1){return 5 ; KEY = 0;}
if(PIND.6 == 1){return 6 ; KEY = 0;}
if(PIND.7 == 1){return 11; KEY = 0;}
//==========================================
PORTD.0 = 0 ;
PORTD.1 = 0 ;
PORTD.2 = 1 ;
PORTD.3 = 0 ;
if(PIND.4 == 1){return 1 ; KEY = 0;}
if(PIND.5 == 1){return 2 ; KEY = 0;}
if(PIND.6 == 1){return 3 ; KEY = 0;}
if(PIND.7 == 1){return 12; KEY = 0;}
//==========================================
PORTD.0 = 0 ;
PORTD.1 = 0 ;
PORTD.2 = 0 ;
PORTD.3 = 1 ;
if(PIND.4 == 1){return 15; KEY = 0;}
if(PIND.5 == 1){return 0 ; KEY = 0;}
if(PIND.6 == 1){return 14; KEY = 0;}
if(PIND.7 == 1){return 13; KEY = 0;}

//================================================== =====
PORTC.0 = 1 ;
PORTC.1 = 0 ;
PORTC.2 = 0 ;
if(PINC.5 == 1){return 16 ; KEY=0;}
if(PINC.6 == 1){return 17; KEY=0;}
if(PINC.7 == 1){return 18 ; KEY=0;}
//================================================== ===
PORTC.0 = 0 ;
PORTC.1 = 1 ;
PORTC.2 = 0 ;
if(PINC.5 == 1){return 19 ; KEY=0;}
if(PINC.6 == 1){return 20 ; KEY=0;}
if(PINC.7 == 1){return 21 ; KEY=0;}
//================================================== ===
PORTC.0 = 0 ;
PORTC.1 = 0 ;
PORTC.2 = 1 ;
if(PINC.5 == 1){return 22 ; KEY=0;}
if(PINC.6 == 1){return 23 ; KEY=0;}
if(PINC.7 == 1){return 24 ; KEY=0;}

KEY = 1 ;
}
}
 

I am sure you brainy guys there ...........Please try this one?
 

Anything AFTER the 'return' statement will never be reached, since the CPU will return back to the main function before it ever reaches it.


example: if(PIND.6 == 1){return 9 ; KEY = 0;delay_ms(50);}
 

Modificaion could be like:
Code:
if(PINC.5 == 1){return 22 ; KEY=0;}
to
Code:
if(PINC.5 == 1){KEY=0;return 22;}

Change all of your conditions to have the KEY assignment (and delay if necessary) before the return statement.

Hope this helps.
Tahmid.
 

Thanks Tahmid,
I really appreciate your efforts. Thanks for the advice it worked with that way.
Now i have tried to display the pressed number on LCD. Facing a new problem before even i press any button a string of random number is getting printed on that constantly. Please help me.

Code:
#include <mega16.h>
#include <delay.h>
#include <lcd.h>
#include <stdlib.h>
#include <math.h>

#asm
   .equ __lcd_port=0x18
#endasm

int key();

char y=0 , lcd[25], z ;
//#############################################
void main(void){

DDRB=0x0F;
DDRC=0x07;
DDRD=0x0F;

lcd_init(16);
lcd_clear();
key();
z=10;                          //Just to check LCD is working printing z on lcd and cleared it before  it enters while loop
itoa(z,lcd);
lcd_puts(lcd);
delay_ms(100);
lcd_clear();
while (1){
      y=key();
      itoa(y,lcd);
      lcd_puts(lcd);
      delay_ms(50);
}
}
//######################################################### 
 

int key(void){
char KEY = 255 ;
   while(KEY==255){
        PORTD.0 = 1 ;
        PORTD.1 = 0 ;
        PORTD.2 = 0 ;
        PORTD.3 = 0 ;
        if(PIND.4 == 1)
            KEY = 7;     
        if(PIND.5 == 1)
            KEY = 8;    
        if(PIND.6 == 1)
            KEY = 9;   
        if(PIND.7 == 1)
            KEY = 10;       
        //==========================================
        PORTD.0 = 0 ;
        PORTD.1 = 1 ;
        PORTD.2 = 0 ;
        PORTD.3 = 0 ;
        if(PIND.4 == 1)
            KEY = 4;     
        if(PIND.5 == 1)
            KEY = 5;    
        if(PIND.6 == 1)
            KEY = 6;
        if(PIND.7 == 1)
            KEY = 11;       
        //==========================================
        PORTD.0 = 0 ;
        PORTD.1 = 0 ;
        PORTD.2 = 1 ;
        PORTD.3 = 0 ;
        if(PIND.4 == 1)
            KEY = 1;         
        if(PIND.5 == 1)
            KEY = 2;       
        if(PIND.6 == 1)
            KEY = 3;      
        if(PIND.7 == 1)
            KEY = 12;         
        //==========================================
        PORTD.0 = 0 ;
        PORTD.1 = 0 ;
        PORTD.2 = 0 ;
        PORTD.3 = 1 ;
        if(PIND.4 == 1)
            KEY = 15;        
        if(PIND.5 == 1)
            KEY = 0;        
        if(PIND.6 == 1)
            KEY = 14;       
        if(PIND.7 == 1)
            KEY = 13;
        //======================================================= 
    }
    delay_ms(50);     
    return KEY;
}

//############################################################

Here is lcd.h which is a code vision lcd driver routine


Code:
CodeVisionAVR C Compiler
  (C) 1998-2003 Pavel Haiduc, HP InfoTech S.R.L.

  BEFORE #include -ING THIS FILE YOU
  MUST DECLARE THE I/O ADDRESS OF THE
  DATA REGISTER OF THE PORT AT WHICH
  THE LCD IS CONNECTED!

  EXAMPLE FOR PORTB:

    #asm
        .equ __lcd_port=0x18
    #endasm
    #include <lcd.h>

*/

  
#ifndef _LCD_INCLUDED_
#define _LCD_INCLUDED_

#pragma used+

void _lcd_ready(void);
void _lcd_write_data(unsigned char data);
// write a byte to the LCD character generator or display RAM
void lcd_write_byte(unsigned char addr, unsigned char data);
// read a byte from the LCD character generator or display RAM
unsigned char lcd_read_byte(unsigned char addr);
// set the LCD display position  x=0..39 y=0..3
void lcd_gotoxy(unsigned char x, unsigned char y);
// clear the LCD
void lcd_clear(void);
void lcd_putchar(char c);
// write the string str located in SRAM to the LCD
void lcd_puts(char *str);
// write the string str located in FLASH to the LCD
void lcd_putsf(char flash *str);
// initialize the LCD controller
unsigned char lcd_init(unsigned char lcd_columns);

#pragma used-
#pragma library lcd.lib

#endif
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top