Lcd1602.c
/*
液晶显示相关代码
*/
#include <reg52.h>
#define LCD1602_DB P0
sbit LCD1602_RS=P1^0;
sbit LCD1602_RW=P1^1;
sbit LCD1602_E=P1^5;
void LcdWaitReady()//读状态
{
unsigned char sta;
LCD1602_DB=0XFF;//开始就进行读取
LCD1602_RS=0;
LCD1602_RW=1;
do{
LCD1602_E=1;
sta=LCD1602_DB;
LCD1602_E=0;
}while(sta&0x80);//判断高位为1,即为忙状态
}
void LcdWriteCmd(unsigned char cmd)//写入命令
{
LcdWaitReady();//写入之前需要判断状态
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_DB=cmd;
LCD1602_E=1;
LCD1602_E=0;
}
void LcdWriteDat(unsigned char dat)//写入数据
{
LcdWaitReady(); //进行状态的判断
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_DB=dat;
LCD1602_E=1;
LCD1602_E=0;
}
void LcdSetCursor(unsigned char x,unsigned char y)//进行光标位置的判断
{
unsigned char addr;
if(y)//y为1
addr=0x40+x;
else
addr=0x00+x;
LcdWriteCmd(addr|0x80);
}
void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str,unsigned char length)//对字符进行显示
{
LcdSetCursor( x, y);
while(length--)//每次固定输入指定的字符个数,形成一帧一帧的效果
{
LcdWriteDat(*str++);
}
}
void InitLcd1602()//进行液晶显示的初始化
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0c);
LcdWriteCmd(0x06);//文字不动,地址加1
LcdWriteCmd(0x01);//进行清屏显示
}
main.c
/*
实现液晶屏幕的整屏移动
相关的函数
液晶相关
需要中断计时,每隔0.5S放一帧
再前期需要制作帧
制作时间函数
*/
#include <reg52.h>
unsigned char flag500ms=0;//设置500ms定时标志
unsigned char str1[]="WangXiaobin ";
unsigned char str2[]="Happy Birthday";//保持两个字符串等长
unsigned char TORH=0;
unsigned char TORL=0;
extern void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str,unsigned char length);
extern void InitLcd1602();
void ConfigTimer0(unsigned int ms);
void main()
{
unsigned char cnt=0;
unsigned char pdata bufmove1[16+sizeof(str1)+16];
unsigned char pdata bufmove2[16+sizeof(str2)+16];
unsigned char i;
//定时器设置
EA=1;
ConfigTimer0(10);
InitLcd1602();
//进行帧的制作
for(i=0;i<16;i++) //制作帧前16字节
{
bufmove1[i]=' ';
bufmove2[i]=' ';
}
for(i=0;i<sizeof(str1)-1;i++)//制作帧中间字节
{
bufmove1[16+i]=str1[i];
bufmove2[16+i]=str2[i];
}
for(i=sizeof(str1)+15;i<sizeof(bufmove1);i++)//制作帧尾
{
bufmove1[i]=' ';
bufmove2[i]=' ';
}
while(1)
{
if(flag500ms)
{
flag500ms=0;
LcdShowStr(0,0,bufmove1+cnt,16);
LcdShowStr(0,1,bufmove2+cnt,16);
cnt++;
if(cnt>=sizeof(str1)+15)
cnt=0;
}
}
}
void ConfigTimer0(unsigned int ms)
{
unsigned long tmp;
tmp=11059200*ms/12000;
tmp=65536-tmp;
TORH= (unsigned char)(tmp>>8);
TORL= (unsigned char)(tmp);
TMOD&=0XF0;
TMOD|=0X01;
TH0=TORH;
TL0=TORL;
ET0=1;
TR0=1;
}
void InterruptTimer0() interrupt 1
{
static unsigned char ttt=0;
TH0=TORH;
TL0=TORL;
ttt++;
if(ttt>=50)
{
ttt=0;
flag500ms=1;
}
}