/*=========================================================
1602液晶顯示的實驗例子
-------------------------------------------------
| DB4-----P0.4 | RW-------P2.1
| DB5-----P0.5 | RS-------P2.2
| DB6-----P0.6 | E--------P2.0
| DB7-----P0.7 |
---------------------------------------------------
LCD1602的簡簡單顯示
=========================================================*/
#include
#include
#define LCM_RW P2_1 //定義引腳
#define LCM_RS P2_2
#define LCM_E P2_0
#define LCM_Data P0
#define Busy 0x80 //用于檢測LCM狀態(tài)字中的Busy標識
/*------------------------------------------
子函數(shù)聲明
------------------------------------------*/
void Write_Data_LCM(unsigned char WDLCM);
void Write_Command_LCM(unsigned char WCLCM,BuysC);
void Read_Status_LCM(void);
void LCM_Init(void);
void Set_xy_LCM(unsigned char x, unsigned char y);
void Display_List_Char(unsigned char x, unsigned char y, unsigned char *s);
void main(void)
{
LCM_Init(); //LCM初始化
delay_nms(5);//延時片刻(可不要)
while(1)
{
Display_List_Char(0, 0, "www.CSMZXY.COM");
Display_List_Char(1, 0, "Robot-AT89S52");
}
}
/*=====================================
函數(shù)名:Read_Status_LCM()
功 能: 忙檢測函數(shù)
======================================*/
void Read_Status_LCM(void)
{
unsigned char read=0;
LCM_RW = 1;
LCM_RS = 0;
LCM_E = 1;
LCM_Data = 0xff;
do
{
read = LCM_Data;
}
while(read & Busy);
LCM_E = 0;
}
/*-------------------------------------------
函數(shù)名:Write_Data_LCM ( )
功 能: 對LCD 1602寫數(shù)據(jù)
--------------------------------------------*/
void Write_Data_LCM(unsigned char WDLCM)
{
Read_Status_LCM(); //檢測忙
LCM_RS = 1;
LCM_RW = 0;
LCM_Data &= 0x0f;
LCM_Data |= WDLCM&0xf0;
LCM_E = 1; //若晶振速度太高可以在這后加小的延時
LCM_E = 1; //延時
LCM_E = 0;
WDLCM = WDLCM<<4;
LCM_Data &= 0x0f;
LCM_Data |= WDLCM&0xf0;
LCM_E = 1;
LCM_E = 1; //延時
LCM_E = 0;
}
/*-------------------------------------------
函數(shù)名:Write_Command_ LCM ( )
功 能: 對LCD 1602寫指令
--------------------------------------------*/
void Write_Command_LCM(unsigned char WCLCM,BuysC) //BuysC為0時忽略忙檢測
{
if (BuysC)
Read_Status_LCM(); //根據(jù)需要檢測忙
LCM_RS = 0;
LCM_RW = 0;
LCM_Data &= 0x0f;
LCM_Data |= WCLCM&0xf0;//傳輸高四位
LCM_E = 1;
LCM_E = 1;
LCM_E = 0;
WCLCM = WCLCM<<4; //傳輸?shù)退奈?/p>
LCM_Data &= 0x0f;
LCM_Data |= WCLCM&0xf0;
LCM_E = 1;
LCM_E = 1;
LCM_E = 0;
}
/*-------------------------------------------
函數(shù)名:LCM_Init()
功 能: 對LCD 1602初始化
--------------------------------------------*/
void LCM_Init(void) //LCM初始化
{
LCM_Data = 0;
Write_Command_LCM(0x28,0); //三次顯示模式設(shè)置,不檢測忙信號
delay_nms(15);
Write_Command_LCM(0x28,0);
delay_nms(15);
Write_Command_LCM(0x28,0);
delay_nms(15);
Write_Command_LCM(0x28,1); //顯示模式設(shè)置,開始要求每次檢測忙信號
Write_Command_LCM(0x08,1); //關(guān)閉顯示
Write_Command_LCM(0x01,1); //顯示清屏
Write_Command_LCM(0x06,1); //顯示光標移動設(shè)置
Write_Command_LCM(0x0C,1); //顯示開及光標設(shè)置
}
/*-------------------------------------------
函數(shù)名:Set_xy_LCM ()
功 能:設(shè)定顯示坐標位置
--------------------------------------------*/
void Set_xy_LCM(unsigned char x, unsigned char y)
{
unsigned char address;
if( x == 0 )
address = 0x80+y;
else
address = 0xc0+y;
Write_Command_LCM(address,1);
}
/*-------------------------------------------
函數(shù)名:Display_List_Char()
功 能:按指定位置顯示一串字符
--------------------------------------------*/
void Display_List_Char(unsigned char x, unsigned char y, unsigned char *s)
{
Set_xy_LCM(x,y);
while(*s)
{
LCM_Data = *s;
Write_Data_LCM(*s);
s++;
}
}