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.

Keypad interfacing problem with pic16f877a.

Status
Not open for further replies.

HerohetoChakma

Junior Member level 3
Joined
Apr 19, 2013
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Chittagong, Bangladesh.
Activity points
2,338
This is an open letter to all member of this forum that i have designed a digital temperature monitoring system in which i can set two digits set point using a 4*3 keypad. My microcontroller is pic16f877a and LM35 sensor. I can not interface my keypad correctly. Could anybody help me by giving code in hi-tech c . It is very much need to me. I will be grateful for ever to u if u can solve this problem. This is an open request to you all. Iam also giving u my code. Some it work by sometimes this is not work. it say that simulation can't run due to excessive load. After trying 10- to 15 mins i can set only one time that also by trying more and more. Please help me.
//==========================================================================
// Author :Heroheto Chakma
// Project : Temperature Monitoring System using LM35
// Project description : This project(DTMScode) will use PIC16F877A to measure temperature // from the sensor and display on the LCD. If the temperature reaches // certain temp, it will trigger the LEDs and buzzer.
//==========================================================================
//==================include=================================
#include <pic.h>
#include <math.h>
//===============configuration==============================
__CONFIG (0x3F32);
//===============define IO port=============================
#define lcd PORTD
#define RS RB4
#define E RB5
#define led1 RB6
#define redled RB7
#define CHANNEL0 0b10000001 // AN0
#define CHANNEL1 0b10001001 // AN1
#define buzzer RB2
#define orangeled RB3
#define greenled RB0
//==============FUNCTION PTOTOTYPE=========================
void e_pulse(void);
void delay(unsigned short i);
void send_char(unsigned char data);
void send_string(const char *s);
void send_config(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void dis_num(unsigned long data);
void increment(unsigned long data);
void read_adc(void);
unsigned short read_temp(void);
void blink_LED1(unsigned char n);
void blink_redled(unsigned char n);
void blink_orangeled(unsigned char n);
void clearrow1(void);
void clearrow2(void);
void clearrow3(void);
void clearrow4(void);
void scancolumn1(void);
void scancolumn2(void);
void scancolumn3(void);
void scancolumn4(void);
void beep_once(void);
//====================MAIN================================
unsigned short result;
unsigned short temp;
float tempB,tempA;
unsigned char keyin[3]; //Declare an array to stall the keyin values
unsigned char num_count=0;
void main(void)
{
ADRESH=0; //clear A/D result
ADRESL=0; //clear A/D result
//setting ADCON1 Register
ADCON1=0b11000000; //A/D result right justified, //configure RA2 and RA5 as digital I/O
TRISA=0b11111111; //configure RA4 & RA5 as output
TRISB=0b00000000; //configure PORTB as output
TRISC=0b00001111; //set bit0-3 portC as input and bit4-7 as output
TRISD=0b00000000; //configure PORTD as output
TRISE=0b00000000; //configure PORTE as output
PORTA=0;
PORTB=0;
// setting the PWM
CCP1CON==0b00001100; //PWM mode
PR2=0xFF; //PWM Period setting/PWM frequency set as 4.88kHz
T2CON=0b00000101; //Timer2 on, prescale 4
CCPR1L=0;
//LCD Settings
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //Lcd Return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //diplay on, cursor off and cursor blink off
send_config(0b00111000); //function set
lcd_clr(); //clear LCD
delay(1000); //delay
lcd_goto(0); //initial display
send_string("PLEASE ENTER"); //Display "PLEASE ENTER" on lcd
lcd_goto(20); //Display on 2nd line
send_string("2 DIGIT SPEC");
while(1)
{ //keypad scanning
clearrow1(); //Clear 1st output pin and set the others
scancolumn1(); //scan column 1-4
clearrow2(); //Clear 2nd output pin and set the others
scancolumn2(); //scan column
clearrow3(); //Clear 3rd output pin and set the others
scancolumn3(); //scan column
clearrow4(); //Clear 4th output pin and set the others
scancolumn4(); //scan column
if(num_count==2)
{
delay(8000);
lcd_goto(0); //cursor start from beginning
//display character on LCD
send_char(' ');
send_char('S');
send_char('P');
send_char('E');
send_char('C');
send_char('.');
send_char('.');
send_char('=');
lcd_goto(20); //cursor go to 2nd line of the LCD
//display character on LCD
send_char(' ');
send_char('M');
send_char('E');
send_char('A');
send_char('S');
send_char('U');
send_char('R');
send_char('E');
send_char('D');
send_char('=');
while(1) //infinity loop
{
//keypad value
ADCON0=CHANNEL1;
lcd_goto(9);
keyin[2]=0;
tempA=(keyin[0]-0x30)*10+(keyin[1]-0x30);
dis_num((unsigned long)round(tempA));
send_char(0b11011111); //to set in degree celcius
send_char('C');
send_char(' ');
send_char(' ');
//sensor B the LM35 sensor
ADCON0=CHANNEL0; //CHANNEL1=0b10001001
lcd_goto(31);
read_adc();
temp=read_temp();
tempB=(((float)temp)*0.48876);
dis_num((unsigned long)round(tempB));
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');
if(tempB<tempA) //when temperature is below spec
{
if((tempB+5)>=tempA) //when temperature is within 5 //degrees of spec
{
led1=1;
blink_LED1(10);
greenled=1;
orangeled=1;
blink_orangeled(3);
redled=0;
buzzer=0;
}
else
{
led1=1;
blink_LED1(10);
greenled=1;
orangeled=0;
redled=0;
buzzer=0;
}
}
else if(tempB>tempA) //when temperature over spec
{
led1=1; //turn on led1
//blink LED1 for 3 times
blink_LED1(3);
greenled=0;
orangeled=1;
blink_orangeled(3);
redled=1;
//blink redled for 3 times
blink_redled(3);
buzzer=1;
//beep_once();
}
delay(2000);
}
}
}
}
//=============================================================================
// Keypad scanning functions
//=============================================================================
void clearrow1(void) //clear the 1st row and set the others
{
RC7=0; //RC7,RC6,RC5 and RC4 are the output pins from PIC which //connect to 4 pins of keypad
RC6=1;
RC5=1;
RC4=1;
}
void clearrow2(void) //clear the 2nd row and set the others
{
RC7=1;
RC6=0;
RC5=1;
RC4=1;
}
void clearrow3(void) //clear the 3rd row and set the others
{
RC7=1;
RC6=1;
RC5=0;
RC4=1;
}
void clearrow4(void) //clear the 4th roW and set the others
{
RC7=1;
RC6=1;
RC5=1;
RC4=0;
}
void scancolumn1(void)
{
if(RC0==0) //if key '1' is being pressed
{
while(RC0==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st word
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of nuum_count //variable
send_char('1'); //Display the symbol '1' at LCD
keyin[num_count]='1'; //Stall the '1' value at the keyin array
num_count+=1; //increase the num_count variable's value by 1 //and the result stall back to the variable
delay(200);
}
else if(RC1==0) //if key '2' is being pressed
{
while(RC1==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('2'); //Display the symbol '2' at LCD
keyin[num_count]='2'; //Stall the '2' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
else if(RC2==0) //if key '3' is being pressed
{
while(RC2==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('3'); //Display the symbol '3' at LCD
keyin[num_count]='3'; //Stall the '3' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
}
void scancolumn2(void)
{
if(RC0==0) //if key '4' is being pressed
{
while(RC0==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('4'); //Display the symbol '4' at LCD
keyin[num_count]='4'; //Stall the '4' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
else if(RC1==0) //if key '5' is being pressed
{
while(RC1==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('5'); //Display the symbol '5' at LCD
keyin[num_count]='5'; //Stall the '5' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
else if(RC2==0) //if key '6' is being pressed
{
while(RC2==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('6'); //Display the symbol '6' at LCD
keyin[num_count]='6'; //Stall the '6' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
}
void scancolumn3(void)
{
if(RC0==0) //if key '7' is being pressed
{
while(RC0==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('7'); //Display the symbol '7' at LCD
keyin[num_count]='7'; //Stall the '7' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
else if(RC1==0) //if key '8' is being pressed
{
while(RC1==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('8'); //Display the symbol '8' at LCD
keyin[num_count]='8'; //Stall the '8' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
else if(RC2==0) //if key '9' is being pressed
{
while(RC2==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('9'); //Display the symbol '9' at LCD
keyin[num_count]='9'; //Stall the '9' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
}
void scancolumn4(void)
{
if(RC0==0) //if key '*' is being pressed
{
while(RC0==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('*'); //Display the symbol '*' at LCD
keyin[num_count]='*'; //Stall the '*' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
else if(RC1==0) //if key '0' is being pressed
{
while(RC1==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('0'); //Display the symbol '0' at LCD
keyin[num_count]='0'; //Stall the '0' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
else if(RC2==0) //if key '#' is being pressed
{
while(RC2==0)continue; //waiting the key to be released
if(num_count==0)lcd_clr(); //Clear the LCD if the key is the 1st password
lcd_goto(num_count); //The cursor of LCD points to the column //equivalent to the value of password_count //variable
send_char('#'); //Display the symbol '#' at LCD
keyin[num_count]='#'; //Stall the '#' value at the keyin_char array
num_count+=1; //increase the Password_count variable's value //by 1 and the result stall back to the variable
delay(200);
}
}
//==================subroutine LCD setting ==========================
void send_config(unsigned char data)
{
RS=0;
lcd=data;
delay(500);
e_pulse();
}
void e_pulse(void)
{
E=1;
delay(500);
E=0;
delay(500);
}
void send_char(unsigned char data)
{
RS=1;
lcd=data;
delay(500);
e_pulse();
}
void lcd_goto(unsigned char data)
{
if(data<16)
{
send_config(0x80+data);
}
else
{
data=data-20;
send_config(0xc0+data);
}
}
void lcd_clr(void)
{
RS=0;
send_config(0x01);
delay(600);
}
void send_string(const char *s)
{
unsigned char i=0;
while (s && *s)send_char (*s++);
}
void dis_num(unsigned long data)
{
unsigned char hundred_thousand;
unsigned char ten_thousand;
unsigned char thousand;
unsigned char hundred;
unsigned char tenth;
hundred_thousand = data/100000;
data = data % 100000;
ten_thousand = data/10000;
data = data % 10000;
thousand = data / 1000;
data = data % 1000;
hundred = data / 100;
data = data % 100;
tenth = data / 10;
data = data % 10;
if(hundred_thousand>0)
{
send_char(hundred_thousand + 0x30); //0x30 added to become ASCII code
send_char(ten_thousand + 0x30);
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(ten_thousand>0)
{
send_char(ten_thousand + 0x30); //0x30 added to become ASCII code
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(thousand>0)
{
send_char(thousand + 0x30); //0x30 added to become ASCII code
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(hundred>0)
{
send_char(hundred + 0x30); //0x30 added to become ASCII code
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(tenth>0)
{
send_char(tenth + 0x30); //0x30 added to become ASCII code
send_char(data + 0x30);
}
else send_char(data + 0x30); //0x30 added to become ASCII code
}
void increment(unsigned long data)
{
unsigned short j;
for(j=10;j>0;j--)
{ lcd_goto(32);
data=data+1;
dis_num(data);
delay(10000);
}
}
//==================subroutine ADC=========================
void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1) //looping 2000 times for getting average value
{
ADGO = 1; //ADGO is the bit 2 of the ADCON0 register
while(ADGO==1); //ADC start, ADGO=0 after finish ADC progress
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC
result_temp+=result;
}
result = result_temp/2000; //getting average value
}
unsigned short read_temp(void)
{
unsigned short temp;
temp=result;
return temp;
}
//==================subroutine DELAY==========================
void delay(unsigned short i)
{
for(;i>0;i--);
}
//==================led blinking============================
void blink_LED1(unsigned char n)
{
//loop for n times
for(n+=1;n>0;n-=1)
{
//set LED1 to 1
led1=1;
//short delay
for(unsigned int i=0;i<20000;i+=1); //max value is 65535
//set LED1 to 1
led1=0;
//short delay
for(unsigned int i=0;i<20000;i+=1); //max value is 65535
}
}
//function to blink redled for n times
void blink_redled(unsigned char n)
{
//loop for n times
for(n+=1;n>0;n-=1)
{
//set redled to 1
redled=1;
//short delay
for(unsigned int i=0;i<20000;i+=1); //max value is 65535
//set redled to 1
redled=0;
//short delay
for(unsigned int i=0;i<20000;i+=1); //max value is 65535
}
}
//function to blink redled for n times
void blink_orangeled(unsigned char n)
{
//loop for n times
for(n+=1;n>0;n-=1)
{
//set redled to 1
orangeled=1;
//short delay
for(unsigned int i=0;i<20000;i+=1); //max value is 65535
//set redled to 1
orangeled=0;
//short delay
for(unsigned int i=0;i<20000;i+=1); //max value is 65535
}
}
//==================Buzzer Beeping============================
void beep_once(void)
{
buzzer=1;
delay(10);
buzzer=0;
delay(20);
buzzer=1;
delay(10);
buzzer=0;
}

schemeetic.jpg
 

Hi,

Here is a code for 4*4 keypad

Code:
char scan_keypad()
        {
        PORTD=0xEf;
		Delay5mS();
		
        switch(PORTD)
                {
                case 0xEE:while(PORTD==0xEE); return  '1'; break;
                
		case 0xed:while(PORTD==0xed); return  '2'; break;
                
		case 0xeb:while(PORTD==0xeb); return  '3'; break;
                
		case 0xE7:while(PORTD==0xE7); return  'A'; break;
                }
        
		PORTD=0xdf;
		
		Delay5mS();

        switch(PORTD)
                {
                case 0xde:while(PORTD==0xde); return  '4'; break;
                
		case 0xDD:while(PORTD==0xDD); return  '5'; break;
                
		case 0xdB:while(PORTD==0xdB); return  '6'; break;
                
		case 0xd7:while(PORTD==0xd7); return  'B'; break;
                }
        
		PORTD=0xbf;

		Delay5mS();
				
                switch(PORTD)
                {
                case 0xbE:while(PORTD==0xbE); return  '7'; break;
                
		case 0xbD:while(PORTD==0xbD); return  '8'; break;
                
		case 0xBB:while(PORTD==0xBB); return  '9'; break;
                
		case 0xb7:while(PORTD==0xb7); return  'C'; break;
                }
        
		PORTD=0x7f;
		
		Delay5mS();
		
        switch(PORTD)
                {
                case 0x7E:while(PORTD==0x7E); return  '*'; break;
                
		case 0x7D:while(PORTD==0x7D); return  '0'; break;
                
		case 0x7B:while(PORTD==0x7B); return  '#'; break;
                
		case 0x77:while(PORTD==0x77); return  '.'; break;
               }
return 'z'; // return nothing

        }
 

Hi,

Try this for your self. The forum is not a code sharing website. It is just to discuss knowledge and help out individuals by the experience one gained by practicing electronics.
I shared a chunk of code as I have seen many written in poor format and one cannot understand it clearly what and how are they doing.

Enjoy!
 

i don't need ur help my code is write? "A little knowledge is dangerous things" it can be apply for u. m i right????????????????/
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top