Microcontroller › AVR › Help in need for a Keypad PROBLEM :(
- This topic has 2 replies, 3 voices, and was last updated 9 years, 3 months ago by Ashutosh Bhatt.
-
AuthorPosts
-
June 24, 2015 at 4:00 am #3722LamdaParticipant
Hi there, EGarage,
I wrote this keypad code couple of days back and i achieved the expected behavior form the keypad but i couldn't complete expected task that i had in my mind when i started writing this code … could you guys help me to understand what went wrong …
Simply saying, these are the steps which i have followed when reading a letter from the keypad.
Steps
1. used a timer to get an interrupt trigger within 30ms from last execute.
2. when interrupt triggered => starts scanning the keypad (drive as 1,2,4,8 (the lower nibble) and read the PIN value).
3. stay while the user release the key.
4. match the PIN value with the key table and get the pressed key.
I got the problem after the 4th step that how to make the character array from the keypad numbers. i tried using pointers, character arrays and int type variable for separate executions but i've failed.
I attached my Keypa d.c code and the keypad.h code so that you guys could get an idea what i tried to achieve.
Please help me to figure out my code …
Thanks in Advance …
This is my Keypad.c Code
#include "keypad.h"
#include "lcd/hd44780.h"volatile int16_t counter = 10; // assuming a interrupt happens after 30ms with 125kHz of clock frequency,prescaller is 64
volatile int8_t isStarPressed = 0;
int8_t isPolling = 0;
volatile int8_t isHashPressed = 0;
volatile int a=0;volatile int b=0;volatile int c=0;void initKeyPad() {
//innitializing keypad pin direction
KEYPADD = 15;
//innitializing keypad * function to indicate the start scanning keypad
}void enableStart(void){
//GICR = 1 << INT2;
}void disableStart(void){
//GICR = 0 << INT2;
}void KeypadDisabled() {
TCCR0&=0xF8;
isPolling = 0;
}void KeypadPoll() {
TCCR0 |= _BV(WGM01) | _BV(CS00);
OCR0 |= 250;
TIMSK |= _BV(OCIE0);
isPolling = 1;
}static int8_t getKey(int8_t pin) {
int8_t num = 0;
switch (pin) {
case 0x11: {num = '1';//17(0x09) case 15 => changed to suit the case
break;
}
case 0x21: {num = '2'; //33(0x0A) case 14 => changed to suit the case
break;
}
case 0x41: {num = '3'; //65
break;
}
case 0x12: {num = '4'; //18
break;
}
case 0x22: {num = '5'; //34(0x12) case 19 => changed to suit the case
break;
}
case 0x42: {num = '6'; //66
break;
}
case 0x14: {num = '7'; //20
break;
}
case 0x24: {num = '8'; //36
break;
}
case 0x44: {num = '9'; //68
break;
}
case 0x28: {num = '0'; //40
break;
}
case 0x38:{num='0';
break;
}
case 0x18: {
num = '*'; //24
isStarPressed=1;
break;
}
case 0x48:
{
num = '#'; //72
isHashPressed = 1;
break;
}
default:{break;}
}
return num;
}int pos=0;
char arr[8];
static void driveKeypad(void) {
for(int8_t row=0;row<4;row++){
_delay_ms(1);
KEYPADP = _BV(row);
_delay_ms(1);
int pin=PINB;
_delay_ms(1);
if(pin>=17 && (pin!=0x18||pin!=0x48)){
while(PINB>15){}
_delay_ms(20);
int8_t key=getKey(pin)&0xCF;
if(key<=9){
arr[pos]=key;
pos++;
break;
}
}
KEYPADP = 0b00000000;
_delay_ms(1);
}
}char *getCode(void){
char *b=&arr;
return b;
}ISR(TIMER0_COMP_vect) {
counter–;
if (counter == 0) {
if(!isHashPressed){
driveKeypad();
}else{
KeypadDisabled();
}
counter = 10;
}
}This is my keypad.h code
#ifndef KEYPAD_H
#define KEYPAD_H
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include "ssd.h"
#include "bank.h"//define PIN for keypad
#define KEYPADD DDRB
#define KEYPADP PORTBextern int8_t isPolling;
extern char arr[8];
extern volatile int8_t isHashPressed;
extern volatile int8_t isStarPressed;extern void initKeyPad(void);
extern void disableStart(void);
extern void KeypadDisabled(void);
extern void KeypadPoll(void);
extern void KeypadSysIndicator(void);extern char *getCode(void);
#endifJune 25, 2015 at 7:32 am #13011Vara AshishkumarParticipantYou store the key in char arr but you have problem to char *getCode(void).does u want to combine all key (which is store in arr) into single number or string?
June 28, 2015 at 7:28 pm #13022Ashutosh BhattParticipantwhat do u want to do exactly?
do u want to store all the numbers of pressed key in one array and make a string?
-
AuthorPosts
- You must be logged in to reply to this topic.