|
|
View previous topic :: View next topic |
Author |
Message |
sharji
Joined: 14 Feb 2011 Posts: 3
|
KeyPad problem with 16f877 PIC |
Posted: Mon Feb 14, 2011 12:40 pm |
|
|
Hello
I am new at this forum so please help me.
I have a project and I have to take the value of the keypad and send the value by using rs232 or display it on LCD screen.
The problem I have is the LCD or RS232 is working fine without adding KBD code. After I write the code, the LCD is not displaying anything and the RS232 is stop transmitting!!
I am using flex drivers for the LCD and KBD and my controller is 16F877.
This is the main code:
Code: | #include "LCD.c"
#include "Flex_KBD.c"
void main()
{
char k;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
lcd_init();
kbd_init();
lcd_putc("\fReady...\n");
delay_ms(100);
while(1)
{
K=kbd_getc();
while (K=='\0') { //Loop waiting for the key
K=kbd_getc();
delay_ms(10);
}
printf(lcd_putc, " char: %c\n", K );
delay_ms(100);
} // end of while
}// end of main
|
this is the keypad driver
Code: | ///////////////////////////////////////////////////////////////////////////
//// Flex_KBD.C ////
//// Generic keypad scan driver ////
//// ////
//// kbd_init() Must be called before any other function. ////
//// ////
//// c = kbd_getc(c) Will return a key value if pressed or /0 if not ////
//// This function should be called frequently so as ////
//// not to miss a key press. ////
//// ////
///////////////////////////////////////////////////////////////////////////
//Keypad connection:
#define col0 PIN_E0
#define col1 PIN_E1
#define col2 PIN_E2
#define row0 PIN_D0
#define row1 PIN_D1
#define row2 PIN_D2
#define row3 PIN_D3
// Keypad layout:
char const KEYS[4][3] = {{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}};
#define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where
// n is the number of times you expect
// to call kbd_getc each second
void kbd_init() {
}
short int ALL_ROWS (void)
{
if (input (row0) & input (row1) & input (row2) & input (row3))
return (0);
else
return (1);
}
char kbd_getc( ) {
static byte kbd_call_count;
static short int kbd_down;
static char last_key;
static byte col;
byte kchar;
byte row;
kchar='\0';
if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
switch (col) {
case 0 : output_low(col0);
output_high(col1);
output_high(col2);
break;
case 1 : output_high(col0);
output_low(col1);
output_high(col2);
break;
case 2 : output_high(col0);
output_high(col1);
output_low(col2);
break;
}
if(kbd_down) {
if(!ALL_ROWS()) {
kbd_down=false;
kchar=last_key;
last_key='\0';
}
} else {
if(ALL_ROWS()) {
if(!input (row0))
row=0;
else if(!input (row1))
row=1;
else if(!input (row2))
row=2;
else if(!input (row3))
row=3;
last_key =KEYS[row][col];
kbd_down = true;
} else {
++col;
if(col==3)
col=0;
}
}
kbd_call_count=0;
}
return(kchar);
}
|
I want to know if my keypad is working fine or not and how to take the value from bottoms.
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 14, 2011 6:44 pm |
|
|
We can see the pins you are using for the keypad, but what pins are you
using for the LCD and the RS-232 ? |
|
|
sharji
Joined: 14 Feb 2011 Posts: 3
|
|
Posted: Tue Feb 15, 2011 12:42 am |
|
|
Code: |
//LCD connection
// These pins are for the PIC development board:
// B1 enable
// B2 rs
// B3 rw
// B4 D4
// B5 D5
// E1 D6
// E0 D7
#define LCD_DB4 PIN_D0
#define LCD_DB5 PIN_D1
#define LCD_DB6 PIN_D2
#define LCD_DB7 PIN_D3
#define LCD_E PIN_A1
#define LCD_RS PIN_A3
#define LCD_RW PIN_A2
|
connection of an RS-232 host through the DB9 connector.
I mentioned that the code of LCD is working fine without the line in the main code
once i commented it, the LCD works fine
I have used different pins for LCD and KBD as you see on the code but it did not work. |
|
|
sharji
Joined: 14 Feb 2011 Posts: 3
|
|
Posted: Tue Feb 15, 2011 8:01 am |
|
|
Actually, I found that the pins that are used in LCD connections is same to the pins that are used in KBD connections, so I changed the pins that are connected to the KBD as following:
Code: |
//Keypad connection:
#define col0 PIN_E0
#define col1 PIN_E1
#define col2 PIN_E2
#define row0 PIN_B0
#define row1 PIN_B1
#define row2 PIN_B2
#define row3 PIN_B3
|
And the problem still the same, when I run the program without the following line, the program work fine and the LCD display what I want, but when I add the following line, the LCD does not display anything and sometimes display random things.
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 15, 2011 11:30 pm |
|
|
1. Your while() loop in main() has large delays in it. You don't need them.
Look at the CCS example file, ex_kblcd.c, which shows how to use a
keypad and an LCD together in the same program:
Quote: |
c:\program files\picc\examples\ex_lcdkb.c
|
Remove the main() code from your program in your first post in this
thread. Use the main() code from ex_lcdkb.c instead.
Also delete the 7 lines of setup code below from main(). They are not needed:
Quote: |
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
|
2. You need pull-up resistors on the Row pins for the keypad. You have
used PortB for the Row pins, so you can use the built-in pullups in the
16F877. To do this, edit the Flex keypad driver file and add the line
shown in bold below to the kbd_init() function:
Quote: |
void kbd_init()
{
port_b_pullups(TRUE);
}
|
If you do these changes, I think it has a chance to work. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|