初级会员
- 积分
- 96
- 金钱
- 96
- 注册时间
- 2019-9-29
- 在线时间
- 19 小时
|
楼主 |
发表于 2020-11-18 20:37:26
|
显示全部楼层
#include<reg52.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define lcd P0
sbit rs=P2^6; //数据命令选择
sbit rw=P2^5; //读写选择
sbit e=P2^7; //使能
uint t;
//uchar m_1,m_2,f_1,f_2,s_1,s_2;
uchar sj[3]={1,2,3};
uchar sz[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
void delayms(uchar c) //误差 0us
{
unsigned char a,b;
for(;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
//lcd
void lcd_xzl(uchar com) //写指令
{
delayms(10);
rs=0;
rw=0;
e=1;
delayms(1);
lcd=com;
delayms(1);
e=0;
//delayms(5);
}
void lcd_xsj(uchar dat) //写数据
{
delayms(10);
rs=1;
rw=0;
e=1;
delayms(1);
lcd=dat;
delayms(1);
e=0;
//delayms(5);
}
void lcd_init() //初始化
{
lcd=0x00;
lcd_xzl(0x38); //设置 8 位格式,2 行,5x7
lcd_xzl(0x38);
lcd_xzl(0x38);
lcd_xzl(0x06); //设定输入方式,增量不位移
lcd_xzl(0x0c); //整体显示,关光标,不闪烁
lcd_xzl(0x01); //清屏
}
//计时
void InitTimer0(void)
{
TMOD = 0x01;
TH0 = 0x0FC;
TL0 = 0x18;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void sj_js()
{
if(t==1000)
{
t=0;
sj[0]++;
if(sj[0]==60)
{
sj[1]++;
sj[0]=0;
if(sj[1]==60)
{
sj[2]++;
sj[1]=0;
if(sj[2]==24)
sj[2]=0;
}
}
}
}
void lcd_szxs()
{
uchar m_1,m_2,f_1,f_2,s_1,s_2;
//sj_js();
m_1=sj[0]/10;
m_2=sj[0]%10;
f_1=sj[1]/10;
f_2=sj[1]%10;
s_1=sj[2]/10;
s_2=sj[2]%10;
lcd_xzl(0x00+0x80);
lcd_xsj(0x20);
lcd_xsj(0x20);
lcd_xsj(0x20);
lcd_xsj(0x20);
lcd_xsj(sz[s_1]);
lcd_xsj(sz[s_2]);
lcd_xsj(0x3a);
lcd_xsj(sz[f_1]);
lcd_xsj(sz[f_2]);
lcd_xsj(0x3a);
lcd_xsj(sz[m_1]);
lcd_xsj(sz[m_2]);
}
void main()
{
lcd_init();
InitTimer0();
while(1)
{
sj_js();
lcd_szxs();
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x0FC;
TL0 = 0x18;
t++;
}
|
|