📄 调时.c
字号:
#include <reg51.h>
#include <intrins.h>
unsigned char dis[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned char dis1[7]={0x4d,0x54,0x57,0x54,0x46,0x53,0x53};
unsigned char dis2[7]={0x4f,0x55,0x45,0x48,0x52,0x41,0x55};
unsigned char dis3[7]={0x4e,0x45,0x4e,0x55,0x49,0x54,0x4e};
unsigned int DisCash[10]={2,3,5,9,5,8,6,2007,11,4};
unsigned char FlashBit=0;//闪烁位数
bit InSet=0;//是否进入设置状态,等于1时为进入设置
bit turn=1;
unsigned TCount=0;
unsigned FCount=0;
int year,n,x,p;
sbit rs = P2^0; //
sbit rw = P2^1;
sbit ep = P2^2;
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef bit BOOL ;
lcd_pos(BYTE pos);
lcd_wdat(BYTE dat);
delay(BYTE ms);
delay(BYTE ms)
{ // 延时子程序
BYTE i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
BOOL lcd_bz()
{ // 测试LCD忙碌状态
BOOL result;
rs = 0;
rw = 1;
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (BOOL)(P0 & 0x80);
ep = 0;
return result;
}
lcd_wcmd(BYTE cmd)
{ // 写入指令数据到LCD
while(lcd_bz());
rs = 0;
rw = 0;
ep = 0;
_nop_();
_nop_();
P0= cmd;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
lcd_pos(BYTE pos)
{ //设定显示位置
lcd_wcmd(pos | 0x80);
}
lcd_wdat(BYTE dat)
{ //写入字符显示数据到LCD
while(lcd_bz());
rs = 1;
rw = 0;
ep = 0;
P0 = dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
lcd_init()
{ //LCD初始化设定
lcd_wcmd(0x38); //
delay(1);
lcd_wcmd(0x0c); //
delay(1);
lcd_wcmd(0x06); //
delay(1);
lcd_wcmd(0x01); //清除LCD的显示内容
delay(1);
}
/*函数is-leap-year()判断是否是闰年,如果是返回值为1,否则为0*/
int is_leap_year(int year)
{int leap;
if (year%4==0&&year%100!=0||year%400==0) leap=1;
else leap=0;
return leap;
}
/*函数len_of_month()的返回值为某年year的某月month的天数*/
int len_of_month(int year,int month)
{int month_days;
if (month==2)
if(is_leap_year(year))month_days=29;
else month_days=28;
else if (month==4||month==6||month==9||month==11)month_days=30;
else month_days=31;
return month_days;
}
//初始化定时器
//TMOD gate c/T M1 M0
void init()
{
TMOD=0x11;//T0,T1都使用16位定时器,不受外部外部引脚电平控制
TH0=0x4c;
TL0=0x01;
TH1=0x3c;
TL1=0xb0;//定时50ms
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
}
//使用定时器0进行计时
//0 外部中断0
//1 定时器中断0
//2 外部中断1
//3 定时器中断1
//4 串口中断
void time() interrupt 1 using 0
{
TH0=0x4c;
TL0=0x01;
TCount++;
if(TCount==20)
{
TCount=0;
DisCash[5]=DisCash[5]+1;
if(DisCash[5]>=10)
{
DisCash[5]=0;
DisCash[4]=DisCash[4]+1;
}
if(DisCash[4]>=6)
{
DisCash[3]=DisCash[3]+1;
DisCash[4]=0;
}
if(DisCash[3]>=10)
{
DisCash[2]=DisCash[2]+1;
DisCash[3]=0;
}
if(DisCash[2]>=6)
{
DisCash[1]=DisCash[1]+1;
DisCash[2]=0;
}
if((DisCash[1]>=10&&DisCash[0]<=1) || (DisCash[1]>=4&&DisCash[0]>=2))
{
DisCash[0]=DisCash[0]+1;
DisCash[1]=0;
}
if(DisCash[0]>=3)
{
DisCash[0]=0;
DisCash[9]=DisCash[9]+1;
DisCash[6]=DisCash[6]+1;
year=DisCash[7];
n=DisCash[8];
}
if(DisCash[6]>7)
{DisCash[6]=1;}
if(DisCash[9]>len_of_month(year,n))
{
DisCash[9]=1; /*日期*/
DisCash[8]=DisCash[8]+1;
}
if(DisCash[8]>12) /*月份*/
{
DisCash[8]=1;
DisCash[7]=DisCash[7]+1; /*年份*/
}
}
}
//使用定时器1进行闪烁
void flash() interrupt 3 using 1
{
TH0=0x3c;
TL0=0xb0;
FCount++;
if(FCount==10)
{
FCount=0;
if(InSet)//如果进入设置
{
turn=~turn;
}
}
}
void Display()
{ BYTE a,b,c,d,e,f,g,h;
unsigned char i,m;
for(i=0;i<6;i++)
{ if
(i>=2&&i<=3)
m=i+1;
else if(i>=4&&i<=5)
m=i+2;
else m=i;
lcd_pos(m); // 设置显示位置
if(i==FlashBit&&turn==0&&InSet==1)
lcd_wdat(0x20);
else
lcd_wdat(dis[DisCash[i]]);
}
lcd_pos(2);
lcd_wdat(0x3a);
lcd_pos(5);
lcd_wdat(0x3a);
a=DisCash[7]/1000;
b=(DisCash[7]/100)%10; /*数据处理*/
c=(DisCash[7]/10)%10;
d=DisCash[7]%10;
e=DisCash[8]/10;
f=DisCash[8]%10;
g=DisCash[9]/10;
h=DisCash[9]%10;
x=DisCash[7];
p=DisCash[8];
lcd_pos(0x4c);
lcd_wdat(dis1[(DisCash[6]-1)]);
lcd_pos(0x4d);
lcd_wdat(dis2[(DisCash[6]-1)]);
lcd_pos(0x4e);
lcd_wdat(dis3[(DisCash[6]-1)]);
lcd_pos(44);
lcd_wdat(0x2f);
lcd_pos(47);
lcd_wdat(0x2f);
lcd_pos(12);
lcd_wdat(0x5b);
lcd_pos(14);
lcd_wdat(0x5d);
if((FlashBit==7)&&(1&&turn==0&&InSet==1))
{
lcd_pos(40);
lcd_wdat(0x20);
lcd_pos(41);
lcd_wdat(0x20);
lcd_pos(42);
lcd_wdat(0x20);
lcd_pos(43);
lcd_wdat(0x20);}
else
{ lcd_pos(40);
lcd_wdat(dis[a]);
lcd_pos(41);
lcd_wdat(dis[b]);
lcd_pos(42);
lcd_wdat(dis[c]);
lcd_pos(43);
lcd_wdat(dis[d]);
}
if((FlashBit==8)&&(1&&turn==0&&InSet==1))
{ lcd_pos(45);
lcd_wdat(0x20);
lcd_pos(46);
lcd_wdat(0x20);
}
else
{lcd_pos(45);
lcd_wdat(dis[e]);
lcd_pos(46);
lcd_wdat(dis[f]);}
if((FlashBit==9)&&(1&&turn==0&&InSet==1))
{
lcd_pos(48);
lcd_wdat(0x20);
lcd_pos(49);
lcd_wdat(0x20);}
else
{
lcd_pos(48);
lcd_wdat(dis[g]);
lcd_pos(49);
lcd_wdat(dis[h]);}
if((FlashBit==6)&&(1&&turn==0&&InSet==1))
{lcd_pos(13);
lcd_wdat(0x20);}
else
{lcd_pos(13);
lcd_wdat(dis[DisCash[6]]);}
delay(6);
}
BYTE getkeyin(void)
{BYTE keyin;
BYTE temp,i;
P3=0x0f;
temp=P3;
if(temp==0x0f)
{
keyin=0xff;
}
if(temp!=0x0f)
{ //按键有松开过
for(i=10;i>0;i--); //键盘去抖延迟
P3=0xef; //P1^4=0
temp=P3;
temp=temp&0x0f;
if (temp!=0x0f)
{
switch(temp)
{
case 0x0e:keyin=0x0f;
break; //keyin='a'
case 0x0d:keyin=0x0b;
break; //keyin='b'
case 0x0b:keyin=8;break; //keyin='c'
case 0x07:keyin=4;break; //keyin='d'
}
return keyin;
}
P3=0xdf; //P1^5=0
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
switch(temp)
{
case 0x0e:keyin=0x0e;
break; //keyin='3'
case 0x0d:keyin=0x0a;
break; //keyin='6'
case 0x0b:keyin=7;break; //keyin='9'
case 0x07:keyin=3;break; //keyin='f'
}
return keyin;
}
P3=0xbf; //P1^6=0
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
switch(temp)
{
case 0x0e:keyin=0x0d;break; //keyin='2'
case 0x0d:keyin=0;break; //keyin='5'
case 0x0b:keyin=6;break; //keyin='8'
case 0x07:keyin=2;break; //keyin='0'
}
return keyin ;
}
P3=0x7f; //P1^7=0
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
switch(temp)
{
case 0x0e:keyin=0x0c;break; //keyin='1'
case 0x0d:keyin=9;break; //keyin='4'
case 0x0b:keyin=5;break; //keyin='7'
case 0x07:keyin=1;break; //keyin='e'
}
return keyin;
}
}
keyin=0x20;
}
void main()
{
unsigned char temp;
init();
lcd_init();
delay(10);
while(1)
{
P3=0x0f;
if((P3!=0x0f))
delay(12);
Display();
if(P3!=0x0f)
{ temp=getkeyin();
P3=0x0f;
while(P3!=0x0f)
{}
switch(temp)
{
case 0x0c:
if(InSet==1&&FlashBit>0)
FlashBit--;
else FlashBit=9;
break;
case 0x0d:
if(InSet==1&&FlashBit<9)
FlashBit++;
else FlashBit=0;
break;
case 0x0e:
InSet=1;
FlashBit=0;
TR1=1;
TR0=0;
break;
case 0x0f:
InSet=0;
init();
break;
case 0x0a:
if(InSet)
{
DisCash[FlashBit]=DisCash[FlashBit]+1;
if(((FlashBit==2)||(FlashBit==4))&&(DisCash[FlashBit]==6))
DisCash[FlashBit]=0;
if(((FlashBit==3)||(FlashBit==5))&&(DisCash[FlashBit]==10))
DisCash[FlashBit]=0;
if((FlashBit==0)&&(DisCash[FlashBit]==3))
DisCash[FlashBit]=0;
if((FlashBit==1)&&(DisCash[FlashBit-1]==2)&&(DisCash[FlashBit]==4))
DisCash[FlashBit]=0;
if((FlashBit==1)&&(DisCash[FlashBit-1]<2)&&(DisCash[FlashBit]==10))
DisCash[FlashBit]=0;
if((FlashBit==8)&&(DisCash[FlashBit]>12))
DisCash[FlashBit]=1;
if((FlashBit==9)&&(DisCash[FlashBit]>len_of_month(x,p)))
DisCash[9]=1;
if((FlashBit==6)&&(DisCash[FlashBit]>7))
DisCash[FlashBit]=1;
}
break;
case 0x0b:
if(InSet)
{
if(((FlashBit==2)||(FlashBit==4))&&(DisCash[FlashBit]==0))
DisCash[FlashBit]=6;
if(((FlashBit==3)||(FlashBit==5))&&(DisCash[FlashBit]==0))
DisCash[FlashBit]=10;
if((FlashBit==0)&&(DisCash[FlashBit]==0))
DisCash[FlashBit]=3;
if((FlashBit==1)&&(DisCash[FlashBit-1]==2)&&(DisCash[FlashBit]==0))
DisCash[FlashBit]=4;
if((FlashBit==1)&&(DisCash[FlashBit-1]<2)&&(DisCash[FlashBit]==0))
DisCash[FlashBit]=10;
if((FlashBit==8)&&(DisCash[FlashBit]==1))
DisCash[FlashBit]=13;
if((FlashBit==9)&&(DisCash[FlashBit]==1))
DisCash[9]=len_of_month(x,p)+1;
if((FlashBit==6)&&(DisCash[FlashBit]==1))
DisCash[FlashBit]=8;
DisCash[FlashBit]=DisCash[FlashBit]-1;
}
break;
}
}
}
Display();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -