View previous topic :: View next topic |
Author |
Message |
Legatus
Joined: 23 Oct 2008 Posts: 4
|
PIC16F877A and a 4X4 Keypad |
Posted: Thu Oct 23, 2008 4:24 am |
|
|
A relatively simple question for you guys.
How can I integrate a 4x4 keypad with a pic16f877a microcontroller using ccs?
|
|
|
Kabukiman Guest
|
|
Posted: Thu Oct 23, 2008 6:18 am |
|
|
You can adjust the default driver kbd.c to your keypad 4x4 or
write your own function to read keypad. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Belial Guest
|
|
Posted: Thu Oct 23, 2008 1:03 pm |
|
|
I make a function to read a keypad 4x4 myself, all keys are connected to
PORTC you can connect at any PORT with 8 pins
I don't know it's very efficient but works good luck.
Code: |
SET_TRIS_C(0b11110000);//you mus set in main function before infinite loop port configuration
void main{
while(1){
key = readkey(); // Call the function
if(key!='\0') // if key different '\0' show it else do nothing
{
if(x > 16 && y == 1){
x=0;
y=2;
}else if(x>16 && y==2)
{
printf(lcd_putc,"\f");
x=0;y=1;
}
x++;
lcd_gotoxy(x,y);
printf(lcd_putc,"%c",key);
delay_ms(300);
}
}
}
|
Code: | char readkey()
{
output_low(PIN_C1);
output_low(PIN_C2);
output_low(PIN_C3);
output_high(PIN_C0);
if(input_state(PIN_C4)==1)
{
return '1';
}
if(input_state(PIN_C5)==1)
{
return '4';
}
if(input_state(PIN_C6)==1)
{
return '7';
}
if(input_state(PIN_C7)==1)
{
return '*';
}
output_low(PIN_C0);
output_low(PIN_C0);
output_low(PIN_C2);
output_low(PIN_C3);
output_high(PIN_C1);
if(input_state(PIN_C4)==1)
{
return '2';
}
if(input_state(PIN_C5)==1)
{
return '5';
}
if(input_state(PIN_C6)==1)
{
return '8';
}
if(input_state(PIN_C7)==1)
{
return '0';
}
output_low(PIN_C1);
output_low(PIN_C0);
output_low(PIN_C1);
output_low(PIN_C3);
output_high(PIN_C2);
if(input_state(PIN_C4)==1)
{
return '3';
}
if(input_state(PIN_C5)==1)
{
return '6';
}
if(input_state(PIN_C6)==1)
{
return '9';
}
if(input_state(PIN_C7)==1)
{
return '#';
}
output_low(PIN_C2);
output_low(PIN_C0);
output_low(PIN_C1);
output_low(PIN_C2);
output_high(PIN_C3);
if(input_state(PIN_C4)==1)
{
return 'A';
}
if(input_state(PIN_C5)==1)
{
return 'B';
}
if(input_state(PIN_C6)==1)
{
return 'C';
}
if(input_state(PIN_C7)==1)
{
return 'D';
}
output_low(PIN_C3);
return '\0';
} |
|
|
|
Legatus
Joined: 23 Oct 2008 Posts: 4
|
|
Posted: Thu Oct 23, 2008 7:59 pm |
|
|
Sorry, I should've searched the forums before posting. |
|
|
Legatus
Joined: 23 Oct 2008 Posts: 4
|
|
Posted: Thu Oct 23, 2008 8:02 pm |
|
|
Quote: |
if(x > 16 && y == 1){
x=0;
y=2;
}else if(x>16 && y==2)
{
printf(lcd_putc,"\f");
x=0;y=1;
}
x++;
lcd_gotoxy(x,y);
printf(lcd_putc,"%c",key);
delay_ms(300); |
Please do correct me if i'm mistaken, these are for printing the pressed key to the lcd. |
|
|
Guest
|
|
Posted: Fri Oct 24, 2008 8:19 am |
|
|
Yes this is for print to lcd |
|
|
Legatus
Joined: 23 Oct 2008 Posts: 4
|
|
Posted: Wed Oct 29, 2008 3:40 am |
|
|
Quote: | I make a function to read a keypad 4x4 myself, all keys are connected to PORTC you can connect at any PORT with 8 pins |
How would I know which keypad pin is connected to which port pin?
Thanks.
|
|
|
kabukiman85
Joined: 30 Oct 2008 Posts: 2 Location: Barcelona Spain
|
Keypad |
Posted: Thu Oct 30, 2008 5:30 am |
|
|
You can connect your keypad to any port with 8 pins in my example, I use 8 pins on PORTC.
This is my own made keypad 4x4 with mikroe schematics
I'm using it on EasyPic5 mikroe dev Board
Here are the schematics of this keypad
http://www.mikroe.com/pdf/keypad_board_schematic.pdf
|
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Thu Oct 30, 2008 8:19 am |
|
|
kabukiman85 said:
You can connect your keypad to any port with 8 pins in my example, I use 8 pins on PORTC.
In fact the schematic shows PORTB, and I think it's right. If you use PORTB, you have internal pullups available, so you can be sure the state of any input is high, unless it's pulled down by one of the outputs. Anyway, on a lot of the processors, PORTC has all the communication pins. You wouldn't want to tie up the whole port with the keypad. |
|
|
|