Hi all,
I'm new to microcontroller programming. I would like to ask how do I display multiple keys on my LCD display. An example is I want to show "123456". Below is the codes I'm using. I am using PIC18F4520 and this is my LCD datasheet. Thanks in advance!
I'm new to microcontroller programming. I would like to ask how do I display multiple keys on my LCD display. An example is I want to show "123456". Below is the codes I'm using. I am using PIC18F4520 and this is my LCD datasheet. Thanks in advance!
Code:
#include <xc.h>
void Init_LCD(void); // Initialize the LCD
void LCD_sendCW(char); // 8-bit Control word for LCD
void LCD_sendData(char); // 8-bit Text Data for LCD
char key_set();
#define LCD_DATA PORTD"
#define LCD_RS PORTBbits.RB1 // RS signal for LCD
#define LCD_E PORTBbits.RB2 // E signal for LCD
#define A PORTAbits.RA0
#define B PORTAbits.RA1
#define C PORTAbits.RA2
#define D PORTAbits.RA3
#define MAX_DATA_LENGTH 6
char DataEntered[MAX_DATA_LENGTH];
char DataPointer = 0;
void main()
{
int i,x;
ADCON1 = 0x0F;
TRISA = 0xFF;
TRISD = 0;
Init_LCD(); // Initialize LCD 8-bit interface,multiple line
while (1)
{
for (x=0;x<6;x++)
{
DataEntered[DataPointer] = get_key();
if (DataPointer < MAX_DATA_LENGTH)
{
DataPointer++;
}
}
LCD_sendCW(0b11000000);
for (i=0; DataEntered[DataPointer]!=0; i++)
LCD_sendData(DataEntered[DataPointer]);
}
}
char key_set(){
char dataIn;
if (PORTAbits.RA4==1)
{
dataIn = PORTA;
switch(dataIn)
{
case 0: LCD_sendData('1');
break;
case 1: LCD_sendData('2');
break;
case 2: LCD_sendData('3');
break;
case 3: LCD_sendData('A');
break;
case 4: LCD_sendData('4');
break;
case 5: LCD_sendData('5');
break;
case 6: LCD_sendData('6');
break;
case 7: LCD_sendData('B');
break;
case 8: LCD_sendData('7');
break;
case 9: LCD_sendData('8');
break;
case 10: LCD_sendData('9');
break;
case 11: LCD_sendData('C');
break;
case 12: LCD_sendData('*');
break;
case 13: LCD_sendData('0');
break;
case 14: LCD_sendData('#');
break;
case 15: LCD_sendData('D');
}
}
}
void Init_LCD(){
LCD_sendCW(0b00111000); // Function Set - 8-bit, 2 lines, 5X7
LCD_sendCW(0b00001100); // Display on, cursor on
LCD_sendCW(0b00000111); // Entry mode - inc addr, no shift
LCD_sendCW(0b00000001); // Clear display
LCD_sendCW(0b00000010); // Return cursor to home position
}
/* Write control word to LCD */
void LCD_sendCW(char x){
LCD_RS = 0;
LCD_E = 1;
LCD_DATA = x;
LCD_E = 0;
_delay(1000); // 2ms delay
}
/* Write text data to LCD */
void LCD_sendData(char x){
LCD_RS = 1;
LCD_E = 1;
LCD_DATA = x;
LCD_E = 0;
_delay(500); // 1ms delay
}