keypad

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi.i am working with atmega 32 and i want connect keypad to it

i write my code and all thing is ok.but when,test my code to protous that,dont work key or display some number simillar 1 , 4 , 7 , * without i press him...
what is my wrong?

this is my code:
#include <mega32.h>
#include <delay.h>
#include <lcd.h>
#asm
.equ __lcd_port=0x1b
#endasm
#define c1 PORTB.4
#define c2 PORTB.5
#define c3 PORTB.6
#define c4 PORTB.7
flash char data_key[]={0x01 , 0x02 , 0x04, 0x08};
flash char key[]={
'1' , '2' , '3' , 'A' ,
'4' , '5' , '6' , 'B' ,
'7' , '8' , '9' , 'C' ,
'*' , '0' , '#' , 'D'};
unsigned char ac,table;
unsigned int r;
void keypad() {
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("keypad testing");
lcd_gotoxy(0,1);
lcd_putchar('~');
while(1) {
for(r=0 ; r<4 ; r++) {
delay_ms(100);
ac=4;
DDRB=0x0f;
PORTB=data_key[r];
if(c1==0) ac=0;
delay_ms(10);
if(!(c2==0)) ac=1;
delay_ms(10);
if(!(c3==0)) ac=2;
delay_ms(10);
if(!(c4==0)) ac=3;
delay_ms(10);
if(!(ac==4)) {
table=key[(r*4)+ac];
lcd_gotoxy(1,1);
lcd_putchar(table);
}
}
}
}
void main() {
keypad();
}

This topic has a solution.
Last Edited: Wed. Jan 31, 2018 - 05:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

How to properly post source code: http://www.avrfreaks.net/comment...

 

hadisamani1996 wrote:
when,test my code to protous that,dont work

So what debugging have you done to find out why that happens?

 

You're using Proteus, so you can step through your code and watch what's happening

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A schematic of your hookup would help us help you.

From the looks of your code, pressed keys are active low. Do you have pull up resistors on those column inputs?

(You could use the internal pull ups...)

David (aka frog_jr)

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

if you want to read pins of port, your definition of c1, c2, c3, c4 are wrong:
#define c1 PORTB.4
...
.
You should use PINx to read pins:
#define c1 PINB.4
...
.
follow #2 and #3

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

and note that "PORTB.4" is not standard 'C'

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

and note that "PORTB.4" is not standard 'C'

Which does not matter as the:

#include <mega32.h>

and especially the:

#asm
.equ __lcd_port=0x1b
#endasm

means this is almost certainly CodeVision.

 

BTW the lack of indentation in that code has come close to making my head explode so I used this:

 

https://www.tutorialspoint.com/o...

 

to get this:

#include <mega32.h>
#include <delay.h>
#include <lcd.h>
#asm
.equ __lcd_port=0x1b
#endasm
#define c1 PORTB.4
#define c2 PORTB.5
#define c3 PORTB.6
#define c4 PORTB.7
                flash char data_key[]= {0x01 , 0x02 , 0x04, 0x08};
flash char key[]= {
    '1' , '2' , '3' , 'A' ,
    '4' , '5' , '6' , 'B' ,
    '7' , '8' , '9' , 'C' ,
    '*' , '0' , '#' , 'D'
};
unsigned char ac,table;
unsigned int r;
void keypad() {
    lcd_init(16);
    lcd_gotoxy(0,0);
    lcd_putsf("keypad testing");
    lcd_gotoxy(0,1);
    lcd_putchar('~');
    while(1) {
        for(r=0 ; r<4 ; r++) {
            delay_ms(100);
            ac=4;
            DDRB=0x0f;
            PORTB=data_key[r];
            if(c1==0) ac=0;
            delay_ms(10);
            if(!(c2==0)) ac=1;
            delay_ms(10);
            if(!(c3==0)) ac=2;
            delay_ms(10);
            if(!(c4==0)) ac=3;
            delay_ms(10);
            if(!(ac==4)) {
                table=key[(r*4)+ac];
                lcd_gotoxy(1,1);
                lcd_putchar(table);
            }
        }
    }
}
void main() {
    keypad();
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh wait a minute that is reading PORTB.n to read the input states? Surely those should be PINB.n ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oohhhhhhhhhhhhh myyyyyyy goddd......
i have problem with this code of yesterday until today...

i fix my code again and again ,...

but i dont think,i have silly problem...

thank you...

you are very fine.

thank thank and thank of youheart

thank of all .

my problem solved

 

Last Edited: Wed. Jan 31, 2018 - 05:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

but it would become important if the OP ever wanted to move this to another compiler.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see comments like that about portability on here regularly but why on earth would anyone contemplate this? I started to use GCC for AVR in about 2005, never seen a reason why I'd suddenly jump ship?? I suppose if I'd ever have hit some unsolvable bug in the compiler it might have forced a switch, but seems unlikely.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But we do see, from time-to-time, people saying "why do I get an error on PORTB.3" - because they have not realised that it is a proprietary extension.