|
下面是重新修整過的代碼,此例的目地是讓他家了解一下LCD1602動(dòng)態(tài)顯示
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^5;
sbit RW = P2^6;
sbit EN = P2^7;
uchar Data2[15]="jinsheng7533967";
//---------自定義的字符數(shù)據(jù)------
uchar code CGCODE[]={
0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,//顯示年0x00
0x0F,0x09,0x0F,0x09,0x0F,0x09,0x13,0x00,//顯示月0x01
0x0F,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00 //顯示日0x02
};
//-----------函數(shù)的聲明------------------
void DelayMS(uint dly);
void User_def(void);
//----------液晶模塊的聲明---------------
bit BUSY(void);
void Write_cmd(uchar cmd);
void Write_dat(uchar dat);
void LCD_Init(void);
void main(void)
{
uchar i, k;
LCD_Init();
User_def(); //將自定義的字符寫入CGRAM
while(1)
{
Write_cmd(0x83);
Write_dat(0x32); // 2
Write_dat(0x30); // 0
Write_dat(0x31); // 1
Write_dat(0x38); // 8
Write_dat(0x00); //顯示 年
Write_dat(0x31); // 1
Write_dat(0x32); // 2
Write_dat(0x01); //顯示 月
Write_dat(0x31);
Write_dat(0x31);
Write_dat(0x02);
Write_cmd(0xc3);
Write_dat(0x02);
Write_dat(0x31);
Write_cmd(0xc1);
for(i=0;i<15;i++)
Write_dat(Data2);
Write_cmd(0x8b);
Write_dat(0x30+k/10);
Write_dat(0x31+k%10);
DelayMS(200); DelayMS(200); DelayMS(200);
if(k>30)k=0;
else k++;
}
}
void DelayMS(uint dly)
{
uint x,y;
for(x=dly;x>0;x--)
for(y=124;y>0;y--);
}
bit BUSY(void)
{
bit Bit;
RS = 0;
RW = 1;
EN = 1;
DelayMS(1);
Bit = (bit)(P0 & 0x80); //最高位為忙信號(hào)位
EN = 0;
return Bit;
}
void Write_cmd(uchar cmd)
{
while(BUSY()); //測忙
RS = 0;
RW = 0;
EN = 0;
P0 = cmd;
EN = 1;
DelayMS(1);
EN = 0;
}
void Write_dat(uchar dat)
{
while(BUSY()); //測忙
RS = 1;
RW = 0;
EN = 0;
P0 = dat;
EN = 1;
DelayMS(1);
EN = 0;
}
void LCD_Init(void)
{
Write_cmd(0x38); //功能設(shè)置
DelayMS(1);
Write_cmd(0x0c); //顯示開關(guān)控制
DelayMS(1);
Write_cmd(0x06); //輸入方式設(shè)置
DelayMS(1);
Write_cmd(0x01); //清除LCD的顯示內(nèi)容
DelayMS(1);
}
void User_def(void)
{
uchar i;
Write_cmd(0x40); //設(shè)置CGRAM地址
for(i=0;i<24;i++)
{
Write_dat(CGCODE);
}
}
|
評分
-
查看全部評分
|