#define DPORT PORTB // 定义数据端口为B端口
#define DTRIS TRISB // 定义数据端口的方向
#define CTRIS_E TRISC0 // 定义端口的方向
#define CTRIS_RS TRISC1 // 定义端口的方向
#define RS RC1
#define E RC0
void LCD_WriteComm(unsigned int TheComm){
// 定义向LCD写一个指令字的函数
CTRIS_RS = 0; // 定义端口为写状态
CTRIS_E = 0;
DTRIS = 0x00;
DPORT = TheComm; // 把数据输出到端口,实际上只是高4位
RS = 0;
E = 0;
E = 0; // 多了一个指令,是为了让信号电平有足够得时间稳定
E = 1;
DPORT = TheComm<<4; // 将数据的低4位移位到高4位,输出到端口
RS = 0;
E = 0;
E = 0;
E = 1;
}
void LCD_WriteDate(unsigned int Hang,unsigned int No,unsigned char TheDate){
// 定义向LCD的第Hang行的第No个位置,写字符TheDate
// Hang:0,1; No:0-15;
unsigned char tmpi;
tmpi = Hang*0x40+No+0x80; // 计算地址指针
LCD_NoBusy();
CTRIS_RS = 0;
CTRIS_E = 0;
DTRIS = 0x00;
DPORT = tmpi;
RS = 0;
E = 0;
E = 0;
E = 1;
DPORT = tmpi<<4;
E = 0;
E = 0;
E = 1;
LCD_NoBusy();
DTRIS = 0x00;
DPORT = TheDate;
RS = 1;
E = 0;
E = 0;
E = 1;
DPORT = TheDate<<4;
E = 0;
E = 0;
E = 1;
}
void LCD_NoBusy(){
delay(2);
}
void LCD_init()
// 初始化LCD显示屏
{
delay(20); //1、延时大约20ms
CTRIS_RS = 0;
CTRIS_E = 0;
DTRIS = 0x00;
DPORT = 0x30;
RS = 0;
E = 0;
E = 0;
E = 1;
delay(5);
DPORT = 0x30;
RS = 0;
E = 0;
E = 0;
E = 1;
delay(2);
DPORT = 0x30;
RS = 0;
E = 0;
E = 0;
E = 1;
delay(2);
DPORT = 0x20;
RS = 0;
E = 0;
E = 0;
E = 1;
delay(2);
LCD_WriteComm(0x28);
delay(4);
LCD_WriteComm(0x08); //4、写指令08H,关闭LCD
LCD_NoBusy();
LCD_WriteComm(0x01); //5、写指令01H,清屏LCD
LCD_NoBusy();
LCD_WriteComm(0x06); //6、写指令06H
LCD_NoBusy();
LCD_WriteComm(0x0C); //7、写指令0CH
};
200字以内,仅用于支线交流,主线讨论请采用回复功能。