#include<reg52.h>
#include<intrins.h> //包含_nop_()函數(shù)的頭文件
sbit RS=P2^0; //將RS定義為P2^0
sbit RW=P2^1; //將RW定義為P2^1
sbit E=P2^2; //將E定義為P2^2
sbit BF=P0^7; //將BF定義為P0^7
unsigned char code User[]={0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00};//自定義字符℃
/************************************
函數(shù)功能:1ms基準(zhǔn)延時(shí)
入口參數(shù):n
************************************/
void delay1ms(unsigned int n )
{
unsigned char i;
while(n--)
for(i=0;i<115;i++);
}
/************************************
函數(shù)功能:判斷液晶的忙碌狀態(tài)
返回值:result=1,忙碌;result=0,不忙
************************************/
bit BusyTest()
{
bit result;
RS=0; //讀狀態(tài)時(shí),RS=0,RW=1,E=1
RW=1;
E=1;
_nop_(); //延時(shí)一個(gè)時(shí)鐘周期
_nop_();
_nop_();
_nop_(); //4個(gè)周期延時(shí),給硬件反應(yīng)時(shí)間
result=BF;
E=0; //將E置0,不允許讀
return result;
}
/************************************
函數(shù)功能:將模式設(shè)置指令或顯示地址送入液晶
入口參數(shù):dictate
************************************/
void Write_com(unsigned char dictate)
{
while(BusyTest()==1); //如果忙就等待
RS=0; //寫指令時(shí),RS=0.RW=0,E=負(fù)跳變
RW=0;
E=1; //先將E拉高
_nop_();
_nop_(); //給硬件反應(yīng)時(shí)間
P0=dictate; //將數(shù)據(jù)送入P0口,即指令或地址
_nop_();
_nop_();
_nop_();
_nop_(); //反應(yīng)時(shí)間
E=0; //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開始執(zhí)行命令
}
/************************************
函數(shù)功能:指定字符顯示的實(shí)際地址
入口參數(shù):x
************************************/
void WriteAddress(unsigned char x)
{
Write_com(x|0x80); //顯示位置的確定方法為“0x80+地址碼x”
}
/************************************
函數(shù)功能:將數(shù)據(jù)(ASKII碼)寫入液晶模塊
入口參數(shù):y(字符常量)
************************************/
void WriteData(unsigned char y)
{
while(BusyTest()==1); //如果忙碌就等待
RS=1; //寫數(shù)據(jù)時(shí)RS=1,RW=0,E=負(fù)跳變
RW=0;
E=1; //先將E拉高
P0=y;
_nop_();
_nop_();
_nop_();
_nop_(); //反應(yīng)時(shí)間
E=0; //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開始執(zhí)行命令
}
/************************************
函數(shù)功能:對(duì)LCD的顯示模式進(jìn)行初始化
************************************/
void LcdInt()
{
delay1ms(15); //首次寫指令時(shí)給LCD一段較長的反應(yīng)時(shí)間
Write_com(0x38); //顯示模式設(shè)置:16X2顯示,5X7點(diǎn)陣,8位數(shù)據(jù)總線
delay1ms(5);
Write_com(0x38);
delay1ms(5);
Write_com(0x38);
delay1ms(5); //3次寫 設(shè)置模式
Write_com(0x0f); //顯示模式設(shè)置:顯示開,有光標(biāo),光標(biāo)閃爍
delay1ms(5);
Write_com(0x06); //顯示模式設(shè)置:光標(biāo)右移,字符不移
delay1ms(5);
Write_com(0x01); //清屏
delay1ms(5);
}
//主函數(shù)
void main()
{
unsigned char i,j;
LcdInt(); //調(diào)用LCD初始化函數(shù)
delay1ms(10);
while(1)
{
Write_com(0x01); // 清屏
delay1ms(5);
Write_com(0x40); //設(shè)置CGRAM地址,索引值為(0x00 ~ 0x07),建立好字模后,往ddram中寫索引0x00,新建的字符就會(huì)顯示出來。
delay1ms(5);
for(j=0;j<15;j++)
{
WriteData(User[j]); //寫入℃
delay1ms(5);
}
WriteAddress(0x05); //設(shè)定屏幕上的顯示位置
WriteData(0x00); //因?yàn)閳?zhí)行過Write_com(0x40),默認(rèn)從CGRAM取出℃
delay1ms(1000);
}
}
仿真可以但是實(shí)物不行
想請(qǐng)大伙看看程序有什么問題
|