题目:在LCD
1602显示屏上显示满足条件:
1、在第一行显示“000-999 CLOCK”
2、在第二行末尾显示“BY XIAO WU”
3、在第二行起首用三位显示000-999跑表
4、循环下去
解决方案:(C语言编程)
#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="000-999 CLOCK";
uchar code table1[]="BY XIAO WU";
uchar code tabledu[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,
0x37,0x38,0x39}; //数据0~9
uchar bai,shi,ge,num,flag;
uint shu;
sbit lcdrs=P1^7; //端口定义
sbit lcdrw=P1^6;
sbit lcde=P2^4;
sbit le=P3^6;
sbit leden=P2^5;
sbit dkle=P3^7;
void delay(uint a) ;//函数声明
void display();
void write_com(uchar com) //写命令子函数
{
lcde=0;
lcdrw=0;
lcdrs=0;
delay(1);
lcde=1;
delay(1);
P0=com;
delay(1);
lcde=0;
}
void write_data(uchar dat)//写数据子函数
{
lcde=0;
lcdrw=0;
lcdrs=1;
delay(1);
lcde=1;
delay(1);
P0=dat;
delay(1);
lcde=0;
}
void init()
{
shu=0; //计数值初始化
bai=0;
shi=0;
ge=0;
TMOD=0x01;//定时器初始化
TH0=0xd8;
TL0=0xf0;
EA=1;
ET0=1;
TR0=1;
lcde=0; //LCD1602初始化
le=0;
leden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x01);
write_com(0x80);
for(num=0;num<13;num++) //在第一行显示“000-999 CLOCK”
{
write_data(table[num]);
}
write_com(0x80+0x45);
for(num=0;num<10;num++) //在第二行末尾显示“BY XIAO WU”
{
write_data(table1[num]);
}
}
void main() //main函数
{
init();
write_com(0x80+0x40);
delay(100);//延时1s
while(1)
{
if(flag==1)
{
flag=0;
display();
}
}
}
void time0() interrupt 1//中断子函数
{
TH0=0xd8;
TL0=0xf0;
flag=1;
}
void display()//显示子函数
{
shu++;
if(shu==999)
shu=0;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
write_data(tabledu[bai]);
write_data(tabledu[shi]);
write_data(tabledu[ge]);
write_com(0x80+0x40);
}
void delay(uint a) { //延时子函数
uint i,j;
for(j=a;j>0;j--)
for(i=2500;i>0;i--) ;
}