thannara123
Advanced Member level 5
- Joined
- Jan 7, 2010
- Messages
- 1,602
- Helped
- 122
- Reputation
- 244
- Reaction score
- 116
- Trophy points
- 1,353
- Activity points
- 10,625
uint8_t GetKeyPressed()
{
uint8_t r,c;
KEYPAD_PORT|= 0X0F;
for(c=0;c<4;c++)
{
KEYPAD_DDR&=~(0XFF);
KEYPAD_DDR|=(0X80>>c);
for(r=0;r<4;r++)
{
if(!(KEYPAD_PIN & (0X08>>r)))
{
return (r*4+c);
}
}
}
return 0XFF;//Indicate No key pressed
}
uint8_t GetKeyPressed()
{
uint8_t r,c;
[COLOR="#FF0000"]KEYPAD_PORT|= 0X0F;[/COLOR]
for(c=0;c<4;c++)
{
[COLOR="#FF0000"]KEYPAD_DDR&=~(0XFF);[/COLOR]
KEYPAD_DDR|=(0X80>>c);
for(r=0;r<4;r++)
{
if(!(KEYPAD_PIN & (0X08>>r)))
{
return (r*4+c);
}
}
}
return 0XFF;//Indicate No key pressed
}
uint8_t GetKeyPressed()
{
uint8_t r,c;
KEYPAD_PORT|= 0X0F;
[COLOR="#FF0000"]for(c=0;c<4;c++)[/COLOR]
{
KEYPAD_DDR&=~(0XFF);
[COLOR="#FF0000"] KEYPAD_DDR|=(0X80>>c);[/COLOR]
for(r=0;r<4;r++)
{
if(!(KEYPAD_PIN & (0X08>>r)))
{
return (r*4+c);
}
}
}
return 0XFF;//Indicate No key pressed
}
uint8_t GetKeyPressed()
{
uint8_t r,c;
KEYPAD_PORT|= 0X0F;
for(c=0;c<4;c++)
{
KEYPAD_DDR&=~(0XFF);
KEYPAD_DDR|=(0X80>>c);
[COLOR="#FF0000"]for(r=0;r<4;r++)[/COLOR]
{
[COLOR="#FF0000"]if(!(KEYPAD_PIN & (0X08>>r)))[/COLOR]
{
[COLOR="#FF0000"]return (r*4+c);[/COLOR]
}
}
}
return 0XFF;//Indicate No key pressed
}
while(!system_reset)
{
x=0;
keyCode=GetKeyPressed(); //Get the keycode of pressed key
if(keyCode==prev_key)
continue;
else if(keyCode==0xFF)
{
prev_key=0xFF;
continue;
}
else
{
prev_key=keyCode;
}
if(pos==3)
{
return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3])); //save the password in 16 bit integer
//return(1234);
break;
//Wait for enter key or reset key
//Convert pass to a single integer
//
}
//
switch (keyCode) //generating key characeter to display on LCD
{
case (11):
string("1");
dis_cmd(k++);
key[x]=1;
++x;
pos++;
break;
case (10):
//dis_cmd(k++);
string("2");
dis_cmd(k++);
key[x]=2;
++x;
pos++;
break;
case (9):
string("3");
dis_cmd(k++);
key[x]=3;
++x;
pos++;
break;
case (7):
string("nubere");
dis_cmd(k++);
key[x]=4;
++x;
pos++;
break;
case (6):
//dis_cmd(k++);
string("5");
dis_cmd(k++);
key[x]=5;
++x;
pos++;
break;
case (5):
string("6");
dis_cmd(k++);
key[x]=6;
++x;
pos++;
break;
case (3):
string("7");
dis_cmd(k++);
key[x]=7;
++x;
pos++;
break;
case (2):
string("8");
dis_cmd(k++);
key[x]=8;
++x;
pos++;
break;
case (1):
string("9");
dis_cmd(k++);
key[x]=9;
++x;
pos++;
break;
case (15):
string("0");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (14):
string("#");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (13):
system_reset=1;
break;
default: ;
}
_delay_loop_2(1600);
}
//key[0]=1;key[1]=2;key[2]=3;key[3]=4;
}
switch (keyCode) //generating key characeter to display on LCD
{
case (11):
string("1");
dis_cmd(k++);
key[x]=1;
++x;
pos++;
break;
case (10):
//dis_cmd(k++);
string("2");
dis_cmd(k++);
key[x]=2;
++x;
pos++;
break;
case (9):
string("3");
dis_cmd(k++);
key[x]=3;
++x;
pos++;
break;
case (7):
string("nubere");
dis_cmd(k++);
key[x]=4;
++x;
pos++;
break;
case (6):
//dis_cmd(k++);
string("5");
dis_cmd(k++);
key[x]=5;
++x;
pos++;
break;
case (5):
string("6");
dis_cmd(k++);
key[x]=6;
++x;
pos++;
break;
case (3):
string("7");
dis_cmd(k++);
key[x]=7;
++x;
pos++;
break;
case (2):
string("8");
dis_cmd(k++);
key[x]=8;
++x;
pos++;
break;
case (1):
string("9");
dis_cmd(k++);
key[x]=9;
++x;
pos++;
break;
case (15):
string("0");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (14):
string("#");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (13):
system_reset=1;
break;
default: ;
}
x = 0;
switch (keyCode) //generating key characeter to display on LCD
{
case (11):
string("1");
dis_cmd(k++);
key[x]=1;
pos++;
break;
case (10):
//dis_cmd(k++);
string("2");
dis_cmd(k++);
key[x]=2;
pos++;
break;
case (9):
string("3");
dis_cmd(k++);
key[x]=3;
pos++;
break;
case (7):
string("nubere");
dis_cmd(k++);
key[x]=4;
pos++;
break;
case (6):
//dis_cmd(k++);
string("5");
dis_cmd(k++);
key[x]=5;
pos++;
break;
case (5):
string("6");
dis_cmd(k++);
key[x]=6;
pos++;
break;
case (3):
string("7");
dis_cmd(k++);
key[x]=7;
pos++;
break;
case (2):
string("8");
dis_cmd(k++);
key[x]=8;
pos++;
break;
case (1):
string("9");
dis_cmd(k++);
key[x]=9;
pos++;
break;
case (15):
string("0");
dis_cmd(k++);
key[x]=0;
pos++;
break;
case (14):
string("#");
dis_cmd(k++);
key[x]=0;
pos++;
break;
case (13):
system_reset=1;
break;
default: ;
}
x++;
You can use value = value*10 + key; for getting the integer value of keypresses.
I am not getting the correct value .why so ?
Code:switch (keyCode) //generating key characeter to display on LCD { case (11): string("1"); dis_cmd(k++); key[x]=1; ++x; pos++; break; case (10): //dis_cmd(k++); string("2"); dis_cmd(k++); key[x]=2; ++x; pos++; break; case (9): string("3"); dis_cmd(k++); key[x]=3; ++x; pos++; break; case (7): string("nubere"); dis_cmd(k++); key[x]=4; ++x; pos++; break; case (6): //dis_cmd(k++); string("5"); dis_cmd(k++); key[x]=5; ++x; pos++; break; case (5): string("6"); dis_cmd(k++); key[x]=6; ++x; pos++; break; case (3): string("7"); dis_cmd(k++); key[x]=7; ++x; pos++; break; case (2): string("8"); dis_cmd(k++); key[x]=8; ++x; pos++; break; case (1): string("9"); dis_cmd(k++); key[x]=9; ++x; pos++; break; case (15): string("0"); dis_cmd(k++); key[x]=0; ++x; pos++; break; case (14): string("#"); dis_cmd(k++); key[x]=0; ++x; pos++; break; case (13): system_reset=1; break; default: ; }
when i press 1,2,3,4 that is switch case 11,10,9,7 but not giving 1234 value .what is the reason
asigined int key[]={0,0,0,0} please help me
consumed too time .....
is there any array value return problem ?
key[]={0,0,0,0};
Key pad matrix i used 12 keys ,How may keys does your keypad have, 12, 16, etc? What is the layout of your keypad? When you press 1, 2 ,3, 4, what is the resulting sequence of keyCode(s)?
BigDog
yes sir i have debugging cappabilities but never used (dont know well how to ) .I am using AVR Studio6 .If you have hardware debugging capabilities, i.e. breakpoints, set a breakpoints before entering the switch/case statement and record the value of keyCode after each press of a key.
Or
If you do not have hardware debugging capabilities, you could modify the above routine to store each sequential keypress into an array of appropriate size and type.
BigDog
string("1");... string("9");
dis_cmd(k++);
//Global variables
unsigned char keycodechar;
keycode = GetKeyPressed();
keycodechar = keycode + 48;
dis_data(keycodechar);
Yes getting the right value only on LCD not in return function ;If you are getting 1, 2, 3, 4 when you press 1, 2, 3, 4 then what is your problem? You said you are not getting the right vaues. When you display 1, 2, 3, 4 on lcd it its value will be 0x31, 0x32, 0x33, 0x34 or d49, d50, d51, d52 because they are characters.
[/QUOTE]Can I see the code for GetKeyPressed() function?
/*
* keypad.h
*
* Created: 12/19/2012 10:49:06 PM
* Author: Gelectorn
*/
#ifndef KEYPAD_H_
#define KEYPAD_H_
#define KEYPAD_PORT PORTD
#define KEYPAD_DDR DDRD
#define KEYPAD_PIN PIND
uint8_t GetKeyPressed()
{
uint8_t r,c;
KEYPAD_PORT|= 0X0F;
for(c=0;c<3;c++)
{
KEYPAD_DDR&=~(0X7F);
KEYPAD_DDR|=(0X40>>c);
for(r=0;r<4;r++)
{
if(!(KEYPAD_PIN & (0X08>>r)))
{
return (r*3+c);
}
}
}
return 0XFF;//Indicate No key pressed
}
#endif /* KEYPAD_H_ */
return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3]))
uint16_t InputNumber(char *msg)
uint16_t retval = 0;
retval = InputNumber();
unsigned int retval = 0;
unsigned char sretval[5];
retval = InputNumber(char *msg);
sretval[0] = (retval/1000) + 48;
sretval[1] = (retval/100)%10 + 48;
sretval[2] = (retval/10)%10 + 48;
sretval[3] = (retval/1)%10 + 48;
sretval[4] = '\0';
string(sretval);
#include <avr/io.h>
#include <inttypes.h>
#include<util/delay.h>
#include "keypad.h"
#include "user_interface.h"
#include "lcd.h"
unsigned int retval = 0;
unsigned char sretval[5];
uint16_t InputNumber(char *msg)
{
uint8_t keyCode,x=0,pos = 0;
uint8_t prev_key=0xFF;
uint8_t k=0xc1;
uint8_t system_reset = 0;
uint8_t key[]="####";
dis_cmd(0x81);
//string("Enter Password");
while(*msg) dis_data(*msg++);
LINE2;
while(!system_reset)
{
keyCode=GetKeyPressed(); //Get the keycode of pressed key
if(keyCode==prev_key)
continue;
else if(keyCode==0xFF)
{
prev_key=0xFF;
continue;
}
else
{
prev_key=keyCode;
}
if(pos==3)
{
retval = (1000*key[0])+(100*key[1])+(10*key[2])+1*key[3]);
return 0;
//return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3])); //save the password in 16 bit integer
//return(1234);
break;
//Wait for enter key or reset key
//Convert pass to a single integer
}
sretval[0] = (retval/1000) + 48;
sretval[1] = (retval/100)%10 + 48;
sretval[2] = (retval/10)%10 + 48;
sretval[3] = (retval/1)%10 + 48;
sretval[4] = '\0';
string(sretval);
switch (keyCode) //generating key characeter to display on LCD {
case (11):
string("1");
dis_cmd(k++);
key[x]=1;
++x;
pos++;
break;
case (10):
string("2");
dis_cmd(k++);
key[x]=2;
++x;
pos++;
break;
case (9):
string("3");
dis_cmd(k++);
key[x]=3;
++x;
pos++;
break;
case (7):
string("nubere");
dis_cmd(k++);
key[x]=4;
++x;
pos++;
break;
case (6):
string("5");
dis_cmd(k++);
key[x]=5;
++x;
pos++;
break;
case (5):
string("6");
dis_cmd(k++);
key[x]=6;
++x;
pos++;
break;
case (3):
string("7");
dis_cmd(k++);
key[x]=7;
++x;
pos++;
break;
case (2):
string("8");
dis_cmd(k++);
key[x]=8;
++x;
pos++;
break;
case (1):
string("9");
dis_cmd(k++);
key[x]=9;
++x;
pos++;
break;
case (15):
string("0");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (14):
string("#");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (13):
system_reset=1;
break;
default:
;
}
_delay_loop_2(1600);
}
}
key[0]=1, key[1]=2, key[2]=3, key[3]=4
( (1000*key[0]) + (100*key[1]) + (10*key[2]) + key[3] );
#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>
#include "eeprom.h"
#include "lcd.h"
//#include "keypad.h"
#include "user_interface.h"
int main(void)
{
DDRB=0xFF;
lcd_init();
uint16_t password;
uint8_t keyCode,x,pos;
uint8_t prev_key=0xFF;
int ct=1000;
int pass=0;
uint16_t newpassword;
//if(ReadPassFromEEPROM()==255)
//{
//Password is blank so store a default password
WritePassToEEPROM(1234);
//}
LCD_clear;
LINE1;
dis_cmd(0x84);
string("Drive");
_delay_ms(200);
while(1){
LCD_clear;
[COLOR=#ff0000] password= InputNumber("Enter Password");//Get the keycode of pressed key[/COLOR] /// worng
if(password==ReadPassFromEEPROM())
{
LCD_clear;
string(" Unlocked");
_delay_ms(500);
while(GetKeyPressed()==255)
{
_delay_loop_2(10);
}
}
else if(keyCode=0)
{ LINE2;
LCD_clear;
//string("");
newpassword = InputNumber("Enter new password");
WritePassToEEPROM(newpassword);
string("changed");
_delay_ms(1000);
}
else
{
LCD_clear;
string("Wrong Password");
_delay_ms(1000);
}
}
}
When you enter 1234 what is the return value you are getting from return((1000*key[0])+(100*key[1])+(10*key[2])+key[3]);?
Assign your return value to a int varible likeCode:unsigned int retval = 0; unsigned char sretval[5]; retval = InputNumber(char *msg);
Then convert retval to string likeCode:sretval[0] = (retval/1000) + 48; sretval[1] = (retval/100)%10 + 48; sretval[2] = (retval/10)%10 + 48; sretval[3] = (retval/1)%10 + 48; sretval[4] = '\0'; string(sretval);
used it and displayed the string as 00001 when i pressed "1" key only one time . 4 zeros extra:???:
screen shoot
#include <avr/io.h>
#include <inttypes.h>
#include<util/delay.h>
#include "keypad.h"
#include "user_interface.h"
#include "lcd.h"
unsigned int retval = 0;
unsigned char sretval[5];
uint16_t InputNumber(char *msg)
{
uint8_t keyCode,x=0,pos = 0;
uint8_t prev_key=0xFF;
uint8_t k=0xc1;
uint8_t system_reset = 0;
uint8_t key[]="####";
dis_cmd(0x81);
//string("Enter Password");
while(*msg) dis_data(*msg++);
LINE2;
while(!system_reset)
{
keyCode=GetKeyPressed(); //Get the keycode of pressed key
if(keyCode==prev_key)
continue;
else if(keyCode==0xFF)
{
prev_key=0xFF;
continue;
}
else
{
prev_key=keyCode;
}
if(pos==3)
{
retval = (1000*key[0])+(100*key[1])+(10*key[2])+1*key[3]);
return 0;
//return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3])); //save the password in 16 bit integer
//return(1234);
//Wait for enter key or reset key
//Convert pass to a single integer
sretval[0] = (retval/1000) + 48;
sretval[1] = (retval/100)%10 + 48;
sretval[2] = (retval/10)%10 + 48;
sretval[3] = (retval/1)%10 + 48;
sretval[4] = '\0';
string(sretval);
break;
}
switch (keyCode) //generating key characeter to display on LCD {
case (11):
string("1");
dis_cmd(k++);
key[x]=1;
++x;
pos++;
break;
case (10):
string("2");
dis_cmd(k++);
key[x]=2;
++x;
pos++;
break;
case (9):
string("3");
dis_cmd(k++);
key[x]=3;
++x;
pos++;
break;
case (7):
string("nubere");
dis_cmd(k++);
key[x]=4;
++x;
pos++;
break;
case (6):
string("5");
dis_cmd(k++);
key[x]=5;
++x;
pos++;
break;
case (5):
string("6");
dis_cmd(k++);
key[x]=6;
++x;
pos++;
break;
case (3):
string("7");
dis_cmd(k++);
key[x]=7;
++x;
pos++;
break;
case (2):
string("8");
dis_cmd(k++);
key[x]=8;
++x;
pos++;
break;
case (1):
string("9");
dis_cmd(k++);
key[x]=9;
++x;
pos++;
break;
case (15):
string("0");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (14):
string("#");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (13):
system_reset=1;
break;
default:
;
}
_delay_loop_2(1600);
}
}
#include <avr/io.h>
#include <inttypes.h>
#include<util/delay.h>
#include "keypad.h"
#include "user_interface.h"
#include "lcd.h"
unsigned int retval = 0;
unsigned char sretval[5];
uint16_t InputNumber(char *msg)
{
uint8_t keyCode,x=0,pos = 0;
uint8_t prev_key=0xFF;
uint8_t k=0xc1;
uint8_t system_reset = 0;
uint8_t key[4]; //="####";
dis_cmd(0x81);
//string("Enter Password");
while(*msg) dis_data(*msg++);
LINE2;
while(!system_reset)
{
keyCode=GetKeyPressed();
//Get the keycode of pressed key
if(keyCode==prev_key)
continue;
else if(keyCode==0xFF)
{
prev_key=0xFF;
continue;
}
else
{
prev_key=keyCode;
}
if(pos==3)
{
retval = (1000*key[0])+(100*key[1])+(10*key[2])+1*key[3]);
//return 0;
//return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3]));
//save the password in 16 bit integer
//return(1234);
//Wait for enter key or reset key
//Convert pass to a single integer
sretval[0] = (retval/1000) + 48;
sretval[1] = (retval/100)%10 + 48;
sretval[2] = (retval/10)%10 + 48;
sretval[3] = (retval/1)%10 + 48;
sretval[4] = '\0';
string(sretval);
break;
}
switch (keyCode) //generating key characeter to display on LCD
{
case (11):
string("1");
dis_cmd(k++);
key[x]=1;
++x;
pos++;
break;
case (10):
string("2");
dis_cmd(k++);
key[x]=2;
++x;
pos++;
break;
case (9):
string("3");
dis_cmd(k++);
key[x]=3;
++x;
pos++;
break;
case (7):
string("nubere");
dis_cmd(k++);
key[x]=4;
++x;
pos++;
break;
case (6):
string("5");
dis_cmd(k++);
key[x]=5;
++x;
pos++;
break;
case (5):
string("6");
dis_cmd(k++);
key[x]=6;
++x;
pos++;
break;
case (3):
string("7");
dis_cmd(k++);
key[x]=7;
++x;
pos++;
break;
case (2):
string("8");
dis_cmd(k++);
key[x]=8;
++x;
pos++;
break;
case (1):
string("9");
dis_cmd(k++);
key[x]=9;
++x;
pos++;
break;
case (15):
string("0");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (14):
string("#");
dis_cmd(k++);
key[x]=0;
++x;
pos++;
break;
case (13):
system_reset=1;
break;
default:
;
}
_delay_loop_2(1600);
}
}
yes sir i have debugging cappabilities but never used (dont know well how to ) .I am using AVR Studio6 .
where can i record the keycode value and how to see it ?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?