|
//***************************************************************************************
//硬件連接:1602VDD接5V,VO接地,BL1接5V,BL2接地,8根數(shù)據(jù)線接P0
//口,RS RW E分別接P2.0、P2.1、P.4口
//***************************************************************************************
#include <REG52.h>
#include <string.h>
#define Busy 0x80 //用于檢測(cè)LCM狀態(tài)字中的Busy標(biāo)識(shí)
#define LCM_Data P0
sbit LCM_RS = P3^7; //寄存器選擇
sbit LCM_RW = P3^6; //讀/寫控制
sbit LCM_E = P3^5; //讀/寫使能
int i,j;
//自定義字符列表
//=====================================================================================
unsigned char character0[8] = {0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02}, //年
character1[8] = {0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11}, //月
character2[8] = {0x0f,0x09,0x09,0x09,0x0f,0x09,0x09,0x0f}, //日
characterN[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //日
//=====================================================================================
//=====================================================================================
//延時(shí)程序
//=====================================================================================
void Delay5Ms(void)
{
unsigned long int TempCyc = 5552;
while(TempCyc--);
}
//========================================第5頁========================================
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
};
}
//=====================================================================================
//讀寫子程序
//=====================================================================================
//讀數(shù)據(jù)
unsigned char ReadDataLCM(void)
{
LCM_RS = 1;
LCM_RW = 1;
LCM_E = 1;
LCM_E = 0;
for(i=0;i<100;i++);
LCM_E = 1;
return(LCM_Data);
}
//讀狀態(tài)
unsigned char ReadStatusLCM(void)
{
LCM_Data = 0xFF;
LCM_RS = 0;
LCM_RW = 1;
LCM_E = 1;
LCM_E = 0;
for(i=0;i<100;i++);
LCM_E = 1;
while (LCM_Data & Busy); //檢測(cè)忙信號(hào)
return(LCM_Data);
}
//寫數(shù)據(jù)
//========================================第6頁========================================
void WriteDataLCM(unsigned char WDLCM)
{
ReadStatusLCM(); //檢測(cè)忙
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 1;
LCM_E = 0; //若晶振速度太高可以在這后加小的延時(shí)
for(i=0;i<100;i++);//延時(shí)
LCM_E = 1;
}
//寫指令
void WriteCommandLCM(unsigned char WCLCM,BuysC) //BuysC為0時(shí)忽略忙檢測(cè)
{
if (BuysC) ReadStatusLCM(); //根據(jù)需要檢測(cè)忙
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 1;
LCM_E = 0;
for(i=0;i<100;i++);
LCM_E = 1;
}
//=====================================================================================
//初始化子程序
//=====================================================================================
void LCMInit(void) //LCM初始化
{
LCM_Data = 0;
WriteCommandLCM(0x38,0); // 三次顯示模式設(shè)置,不檢測(cè)忙信號(hào)
Delay5Ms();
WriteCommandLCM(0x38,0);
Delay5Ms();
WriteCommandLCM(0x38,0);
Delay5Ms();
WriteCommandLCM(0x38,1); // 顯示模式設(shè)置,開始要求每次檢測(cè)忙信號(hào)
Delay5Ms();
WriteCommandLCM(0x08,1); // 關(guān)閉顯示
//========================================第7頁========================================
Delay5Ms();
WriteCommandLCM(0x01,1); // 清屏
Delay5Ms();
WriteCommandLCM(0x06,1); // 顯示光標(biāo)移動(dòng)設(shè)置
Delay5Ms();
WriteCommandLCM(0x0c,1); // 顯示開及光標(biāo)設(shè)置
Delay5Ms();
}
//=====================================================================================
//按指定位置顯示一個(gè)字符
//=====================================================================================
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //當(dāng)要顯示第二行時(shí)地址碼+0x40;
X |= 0x80; //算出指令碼
WriteCommandLCM(X, 0); //這里不檢測(cè)忙信號(hào),發(fā)送地址碼
WriteDataLCM(DData);
}
//=====================================================================================
//按指定位置顯示一串字符
//void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
//說明: x(0-15):x參數(shù) y(0-1):y參數(shù) DData(字符串):要顯示的內(nèi)容(英文、數(shù)字、符號(hào))
//=====================================================================================
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength,j;
ListLength = strlen(DData);
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
//========================================第8頁========================================
if (X <= 0xF) //X坐標(biāo)應(yīng)小于0xF
{
for(j=0;j<ListLength;j++)
{
DisplayOneChar(X, Y, DData[j]); //顯示單個(gè)字符
X++;
}
}
}
//=====================================================================================
//顯示自定義字符
//void mychar(char xx,char yy,unsigned char *character,unsigned char saveto)
//說明:xx(0-15):為x參數(shù).yy(0-1):y參數(shù).character:要顯示的字符的列表地址,在程序前面有定義
//saveto(1-7)為字符保存的RAM,每屏最多顯示7個(gè)自定義字符
//(0x00-0x0h是自定義字符)
//=====================================================================================
void mychar(char xx,char yy,unsigned char *character,unsigned char saveto)
{
unsigned char add = (saveto<<3) | 0x40;
unsigned char t; //臨時(shí)變量,每一行的值
t=*(character+0);
WriteCommandLCM(add, 0); //第1行
WriteDataLCM(t);
t=*(character+1);
WriteCommandLCM(add+1, 0); //第2行
WriteDataLCM(t);
t=*(character+2);
WriteCommandLCM(add+2, 0); //第3行
WriteDataLCM(t);
t=*(character+3);
WriteCommandLCM(add+3, 0); //第4行
WriteDataLCM(t);
t=*(character+4);
WriteCommandLCM(add+4, 0); //第5行
//========================================第9頁========================================
WriteDataLCM(t);
t=*(character+5);
WriteCommandLCM(add+5, 0); //第6行
WriteDataLCM(t);
t=*(character+6);
WriteCommandLCM(add+6, 0); //第7行
WriteDataLCM(t);
t=*(character+7);
WriteCommandLCM(add+7, 0); //第8行
WriteDataLCM(t);
for(i = 0;i<8;i++)
{
WriteCommandLCM(add+i, 0);
WriteDataLCM(*(character+i));
}
DisplayOneChar(xx,yy,saveto); //顯示字符
}
//=====================================================================================
//主函數(shù)
//=====================================================================================
main()
{
Delay400Ms();
LCMInit();
Delay400Ms(); //1602初始化
while(1)
{
DisplayListChar(0,0,"This is ListChar");
DisplayListChar(0,1,"!");
for(j=0;j<30;j++)for(i=0;i<30000;i++);
WriteCommandLCM(0x01,1); //清屏
Delay5Ms();
DisplayListChar(0,0,"This is OneChar:");
DisplayOneChar(0,1,0x4f);
DisplayOneChar(1,1,0x6e);
DisplayOneChar(2,1,0x65);
//========================================第10頁========================================
DisplayOneChar(3,1,0x21);
for(j=0;j<30;j++)for(i=0;i<30000;i++);
WriteCommandLCM(0x01,1); //清屏
Delay5Ms();
DisplayListChar(0,0,"This is MyChar:");
mychar(0,1, character0,1);
mychar(1,1, character1,2);
mychar(2,1, character2,3);
for(j=0;j<30;j++)for(i=0;i<30000;i++);
WriteCommandLCM(0x01,1); //清屏
Delay5Ms();
}
}
|
評(píng)分
-
查看全部評(píng)分
|