//LCD 8BIT DEMO
//#include <16F628.h>
#include <16F648A.h>
#use delay(clock=4000000)
#fuses XT,PUT,BROWNOUT,MCLR,NOWDT,NOPROTECT,NOLVP
//LCD Connection Pin
//RS(4) RA1(pin18)
//R/W(5) GND
//E(6) RA0(pin17)
//D0-D7(7-14) RB0-RB7
//Define LCD I/O PIN
#define LCD_E PIN_A0
#define LCD_RS PIN_A1
#define LCDdata(x) output_b(x)
#define LCD_EPLUSE_TM 1
#define LCD_ROW1 0x00
#define LCD_ROW2 0x40
#define LCD_ROW3 0x14
#define LCD_ROW4 0x54
//*** START LCD Command ***
#define LCD_CLEAR_SCREEN 0B00000001
#define LCD_RETURN_HOME 0B00000010
//Entry mode set (BIT2=1)
#define LCD_ENTRY_MODE 0B00000100
#define INCREMENTS_LCD 0B00000010
#define DECREMENTS_LCD 0B00000000
#define NO_SCROLL_LCD 0B00000000
#define SCROLL_LCD 0B00000001
//Display ON/OFF (BIT3=1)
#define DISPLAY_LCD 0B00001000
#define LCD_ON 0B00000100
#define LCD_OFF 0B00000000
#define LCD_CURSOR_ON 0B00000010
#define LCD_CURSOR_OFF 0B00000000
#define LCD_BLINK_ON 0B00000001
#define LCD_BLINK_OFF 0B00000000
//Scroll Display/Shift Cursor (BIT4=1)
#define LCD_SCROLL_SHIFT 0B00010000
#define LCD_SHIFT_CURSOR 0B00000000
#define LCD_SHIFT_DISPLAY 0B00001000
#define LCD_SHIFT_LEFT 0B00000000
#define LCD_SHIFT_RIGHT 0B00000100
//Function set (BIT5=1)
#define LCD_FUNCTION_SET 0B00100000
#define LCD4BIT_MODE 0B00000000
#define LCD8BIT_MODE 0B00010000
#define LCD_LINE1 0B00000000
#define LCD_LINE2 0B00001000
#define LCD_FONT_5X7 0B00000000
#define LCD_FONT_5X10 0B00000100
//Move To CGRAM Address (BIT6=1)
#define LCD_CGRAM_ADDRESS 0B01000000
//Move To DDRAM Address (BIT7=1)
#define LCD_DDRAM_ADDRESS 0B10000000
//*** END LCD Command ***
void InitialChip(void)
{
setup_comparator(NC_NC_NC_NC); //Input Digital
set_tris_a(0B00000000);
set_tris_b(0B00000000);
output_a(0);
output_b(0);
}
void LCD_Epluse()
{
output_bit(LCD_E,1);
delay_ms(LCD_EPLUSE_TM);
output_bit(LCD_E,0);
delay_ms(5); //Wait for LCD Ready
}
//RS=0 command mode
void WriteLCDCmd(BYTE CMD)
{
output_bit(LCD_RS,0);
LCDdata(CMD);
LCD_Epluse();
}
//RS=1 data mode
void WriteLCDData(char c)
{
output_bit(LCD_RS,1); //Data Mode
LCDdata(c);
LCD_Epluse();
}
void InitialLCD(void)
{
//Prepare LCD Control Logic
output_bit(LCD_E,0);
output_bit(LCD_RS,0); //Command Mode
//Initial LCD send Initial Code 0B00110000 3 time
LCDdata(0B00110000);
LCD_Epluse();
LCDdata(0B00110000);
LCD_Epluse();
LCDdata(0B00110000);
LCD_Epluse();
}
//DD-RAM command
void LCDGotoXY(BYTE X,BYTE Y)
{
X--; //Adjust to Count number 1...n
switch(Y)
{
case 1:
WriteLCDCmd((LCD_ROW1|LCD_DDRAM_ADDRESS)+X); //0x00|0x80
break;
case 2:
WriteLCDCmd((LCD_ROW2|LCD_DDRAM_ADDRESS)+X); //64|0x80
break;
}
}
main()
{
InitialChip();
delay_ms(50);
InitialLCD();
//Set LCD
WriteLCDCmd(LCD_ENTRY_MODE|INCREMENTS_LCD|NO_SCROLL_LCD);
WriteLCDCmd(DISPLAY_LCD|LCD_ON|LCD_CURSOR_OFF|LCD_BLINK_OFF);
WriteLCDCmd(LCD_FUNCTION_SET|LCD8BIT_MODE|LCD_LINE2|LCD_FONT_5X7);
WriteLCDCmd(LCD_CLEAR_SCREEN);
LCDGotoXY(2,1);
printf(WriteLCDData,"Hello");
LCDGotoXY(9,1);
printf(WriteLCDData,"LCD 2x16");
LCDGotoXY(3,2);
printf(WriteLCDData,"8bit");
LCDGotoXY(10,2);
printf(WriteLCDData,"Demo");
while(1); //Loop here
}
|