📄 1602时钟.c
字号:
#include"reg51.h" //函数名 不能超多8个字母
#define uchar unsigned char
#define uint unsigned int
//*************定义位************
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
//*************函数声明************
void delay(uint timer);
void lcdinit(void);
void command(uchar c);
void wtdata(uchar d);
void busy(void);
void putstr(uchar *str);
void putchar(uchar ch);
//*************主函数************
void main()
{ uchar timer[6];
uchar sec=0,min=0,hour=0;
lcdinit();
command(0x80);
putstr("DATA:2008/04/13");
command(0xc0);
putstr("TIMER: ");
command(0x04); //左移
command(0xcd);
putchar('0');
putchar('0');
putchar(':');
putchar('0');
putchar('0');
putchar(':');
putchar('0');
putchar('0'); //用此方法编写的时钟 能实现时钟的基本 运行 但是
while(1) //在 转接(59不会直接到0的显示 而是显示60 再显示0)
{ //因为是 顺序执行的 只能顺序的运行 这个是 一大 缺陷
command(0xcd); //还得 加以 改进
timer[0]=sec%10;
timer[0]+='0';
wtdata(timer[0]);
timer[1]=sec/10;
timer[1]+='0';
wtdata(timer[1]);
putchar(':');
sec++;
if(sec>=60)
{
sec=0;
min++;
timer[2]=min%10;
timer[2]+='0';
wtdata(timer[2]);
timer[3]=min/10;
timer[3]+='0';
wtdata(timer[3]);
putchar(':');
if(min>=60)
{
min=0;
hour++;
timer[4]=hour%10;
timer[4]+='0';
wtdata(timer[4]);
timer[5]=hour/10;
timer[5]+='0';
wtdata(timer[5]);
if(hour>=24)
hour=0;
}
}
}
}
//*************子函数************
//*************1602初始设置************
void lcdinit()
{ //01(清0) 38(8位数据线 字符5*7)
command(0x01);//清0
command(0x38);//8位总线 5*7 双行显示
command(0x0c);//0c(开显示 无光标) 0e(开显示 有光标 光标不闪烁) 0f(开显示 有光标 光标闪烁)
command(0x06);//06光标右移 04光标左移
}
//*************判断忙************
void busy()
{
E=0;
RS=0;
RW=1;
E=1;
while(P1&0x80);
E=0;
delay(5);
}
//*************写指令************
void command(uchar c)
{
busy();
E=0;
RS=0;
RW=0;
E=1;
P1=c;
E=0;
delay(5);
}
//*************写数据************
void wtdata(uchar d)
{
busy();
E=0;
RS=1;
RW=0;
E=1;
P1=d;
E=0;
delay(5);
}
//*************延时************
void delay(uint timer)
{
uchar i;
for(;timer>0;timer--)
for(i=0;i<120;i++)
{;}
}
//*************字符串输入************
void putstr(uchar *str)
{
while(*str!='\0')
{
wtdata(*str);
str++;
}
}
void putchar(uchar ch)
{
wtdata(ch);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -