deligent
Joined: 30 Mar 2009 Posts: 2
|
Getting display on the LCD |
Posted: Sat Apr 11, 2009 10:17 am |
|
|
I have the following code it does not display the characters pressed on the keypad. Can anyone suggest the solution to display on the LCD the characters which are pressed on the keypad ?
Code: |
#include <16F88.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,INTRC_IO, NOPUT, MCLR, NOBROWNOUT, NOLVP, NOCPD, NOWRT, DEBUG, NOPROTECT, NOFCMEN, NOIESO
#use fast_io(A)
#use fast_io(B)
#define RS_PIN PIN_A4
#define E PIN_A7
#define row0 PIN_B4
#define row1 PIN_B5
#define row2 PIN_B6
#define row3 PIN_B7
#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define col3 PIN_B3
#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
char const KEYS[4][4] =
{{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
static char last_key;
unsigned char read_pad(void);
void SendLcdData(char data, char rs);
void SendLcdDataNibble(char data);
void InitLCD(void);
void main(){
unsigned char key;
setup_oscillator( OSC_8MHZ ); // This should be automatic but does not appear to work
setup_adc_ports(NO_ANALOGS);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
set_tris_a(0x00); // set all port A bits as outputs
set_tris_b(0xf0); // set port B bits 7-4 as row inputs, 3-0 outputs
port_b_pullups(TRUE); // turn on weak pullups on port B
output_a(0x00); // zero port A
output_b(0x0f); // set all port b outputs high
InitLCD(); // setup LCD
SendLcdData(0x3f,1); // Display '?'
while(TRUE) {
delay_ms(20);
output_toggle(PIN_A6);
last_key = read_pad();
SendLcdData(last_key,1);
} // end of while loop
} // end of main
short int ALL_ROWS (void)
{
if(input (row0) & input (row1) & input (row2) & input (row3))
return (0);
else
return (1);
}
unsigned char read_pad(void) {
//signed char key=-1;
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);
output_high(col3);
break;
case 1:
output_high(col0);
output_low(col1);
output_high(col2);
output_high(col3);
break;
case 2:
output_high(col0);
output_high(col1);
output_low(col2);
output_high(col3);
break;
case 3:
output_high(col0);
output_high(col1);
output_high(col2);
output_low(col3);
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==4)
col=0;
}
}
kbd_call_count=0;
}
return(last_key);
}
void SendLcdData(char data, char rs)
{
output_a(data >> 4);
output_bit(RS_PIN,rs);
output_bit(E,1);
delay_cycles( 1 ); // Same as a NOP
output_bit(E,0);
output_a(data & 0x0f);
output_bit(RS_PIN,rs);
output_bit(E,1);
delay_cycles( 1 ); // Same as a NOP
output_bit(E,0);
delay_us(40);
}
void SendLcdDataNibble(char data)
{
output_a(data>>4);
output_bit(E,1);
delay_cycles( 1 ); // Same as a NOP
output_bit(E,0);
delay_us(40);
}
void LCDReset(void){
delay_ms(50); // wait for LCD to power up
SendLcdDataNibble(0x30); // send 0x30 to LCD
delay_ms(10);
SendLcdDataNibble(0x30); // send 0x30 to LCD
delay_ms(5);
SendLcdDataNibble(0x30); // send 0x30 to LCD
delay_ms(5);
}
void InitLCD(void)
{
LCDReset();
SendLcdDataNibble(0x20); // setup 4 bit data
SendLcdData(0x28,0); // setup 4 bit data + 2 lines operation
SendLcdData(0x06,0); // set entry mode to incremental
SendLcdData(0x0C,0); // Display on, no cursor, no blink
SendLcdData(0x01,0); // Clear Screen
delay_ms(2);
SendLcdData(0x02,0); // Cursor Home
delay_ms(2);
} |
|
|