//LCD 4BIT DEMO
//#include <16F628.h>
#include <16F648A.h>
#use delay(clock=4000000)
#fuses XT,PUT,BROWNOUT,MCLR,NOWDT,NOPROTECT,NOLVP
//LCD Connection Pin
//E(6) RB0
//RS(4) RB1
//R/W(5) GND
//D0-D7(7-14) RB0-RB7
//Define LCD I/O PIN
//LCD Control
#define LCD_E PIN_B0
#define LCD_RS PIN_B1
//LCD Data
#define LCD_D4 PIN_B4
#define LCD_D5 PIN_B5
#define LCD_D6 PIN_B6
#define LCD_D7 PIN_B7
#define LCD_ROW1 0x00
#define LCD_ROW2 0x40
#define LCD_ROW3 0x14
#define LCD_ROW4 0x54
#define LCD_EPLUSE_TM 1
//*** 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_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);
}
void WriteLCD4Bit(BYTE DAT)
{
output_bit(LCD_D4,bit_test(DAT,4));
output_bit(LCD_D5,bit_test(DAT,5));
output_bit(LCD_D6,bit_test(DAT,6));
output_bit(LCD_D7,bit_test(DAT,7));
LCD_Epluse();
}
//Write 4 bit Data twice
void WriteLCDData(BYTE DAT)
{
output_bit(LCD_RS,1); //Data Mode
WriteLCD4Bit(DAT);
swap(DAT);
WriteLCD4Bit(DAT);
}
//Write 4 bit Command twice
void WriteLCDCmd(BYTE CMD)
{
output_bit(LCD_RS,0); //Command Mode
WriteLCD4Bit(CMD);
swap(CMD);
WriteLCD4Bit(CMD);
}
void InitialLCD(void)
{
BYTE BITS;
//Prepare LCD Control Logic
output_bit(LCD_E,0);
output_bit(LCD_RS,0); //Command Mode
//Initial LCD send Initial Code 0B00110000 3 time
WriteLCD4Bit(0B00110000);
LCD_Epluse();
WriteLCD4Bit(0B00110000);
LCD_Epluse();
WriteLCD4Bit(0B00110000);
LCD_Epluse();
//Set as 4 bit
WriteLCD4Bit(LCD_FUNCTION_SET|LCD4BIT_MODE);
}
//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(200);
InitialLCD();
//Set LCD
WriteLCDCmd(LCD_FUNCTION_SET|LCD4BIT_MODE|LCD_LINE2|LCD_FONT_5X7);
WriteLCDCmd(DISPLAY_LCD|LCD_ON|LCD_CURSOR_OFF);
WriteLCDCmd(LCD_ENTRY_MODE|INCREMENTS_LCD|NO_SCROLL_LCD);
WriteLCDCmd(LCD_CLEAR_SCREEN);
LCDGotoXY(2,1);
printf(WriteLCDData,"Hello");
LCDGotoXY(9,1);
printf(WriteLCDData,"LCD 2x16");
LCDGotoXY(3,2);
printf(WriteLCDData,"4bit");
LCDGotoXY(10,2);
printf(WriteLCDData,"Demo");
while(1);
}
|