📄 clock.c
字号:
/*********************************************************************************************************
**
** PC control microprocessor by serial port
**
** Copyright 2007, shiyueyong
**
** All Rights Reserved
**
**
**--------------文件信息--------------------------------------------------------------------------------
**文 件 名: main.c
**创 建 人: 史跃勇
**最后修改日期: 2007年9月4日
**描 述: 在MCS51上实现时钟的功能模块
** (通过的date,time命令对时间进行修改用dis命令调用每秒显示一次功能)
**------------------------------------------------------------------------------------------------------
** 修改人:
** 版 本:
** 日 期:
** 描 述:
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#define uchar unsigned char
extern bit display; /*声明使用的外部变量*/
extern uchar yearh;
extern uchar yearl;
extern uchar month;
extern uchar day;
extern uchar hour;
extern uchar min;
extern uchar sec;
extern int tcnt;
/*********************************************************************************************************
** 函数名称: send_char()和send_str()
** 功能描述: 分别实现发送一个字符和一个字符串的功能
** 输 入: 等待发送的字符或字符串
** 输 出: 无
** 全局变量:无
** 调用模块:无
**
** 作 者: 史跃勇
** 日 期: 2007年9月4日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void send_char(unsigned char txd)
// 传送一个字符
{
SBUF = txd;
while(!TI); // 等特数据传送
TI = 0; // 清除数据传送标志
}
void send_str(char *string)
// 传送字串
{
unsigned char i = 0;
while(string[i] != '\0')
{
SBUF = string[i];
while(!TI); // 等特数据传送
TI = 0; // 清除数据传送标志
i++; // 下一个字符
}
}
/*********************************************************************************************************
** 函数名称: _SECDIS_
** 功能描述: 对每秒钟时间调整后进行输出
(没有采printf函数直接输出的原因是这样能减少对整个系统的干扰)
** 输 入: yearh,yearl,month,day,date.hour,min,sec:时间参数
** 输 出: 无
** 全局变量:无
** 调用模块:send_char(),send_str()
**
** 作 者: 史跃勇
** 日 期: 2007年9月4日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void _SECDIS_(uchar L1,uchar L2,uchar L3,uchar L4,uchar L5,uchar L6,uchar L7)
{ uchar idata str0[]={"Date:"};
uchar idata str1[]={"Time:"};
uchar idata DateString[10];
uchar idata TimeString[8];
DateString[0] = L1/10 + '0'; //数字类型转化为字符类型
DateString[1] = L1%10 + '0';
DateString[2] = L2/10 + '0';
DateString[3] = L2%10 + '0';
DateString[4] = '-';
DateString[5] = L3/10 + '0';
DateString[6] = L3%10 + '0';
DateString[7] = '-';
DateString[8] = L4/10 + '0';
DateString[9] = L4%10 + '0';
DateString[10] = '\0';
send_str(" Date:");send_str(DateString);send_char(0x0d);
TimeString[0] = L5/10 + '0'; //数字类型转化为字符类型
TimeString[1] = L5%10 + '0';
TimeString[2] = ':';
TimeString[3] = L6/10 + '0';
TimeString[4] = L6%10 + '0';
TimeString[5] = ':';
TimeString[6] = L7/10 + '0';
TimeString[7] = L7%10 + '0';
TimeString[8] = '\0';
send_str(" Time:");send_str(TimeString);send_char(0x0d);
}
/*********************************************************************************************************
** 函数名称: clock
** 功能描述: 执行键盘输入的时间调整命令并输出调整结果
** 输 入: p:进行时钟控制的操作代码
num1:需要调整的时间参数
** 输 出: 无
** 全局变量:无
** 调用模块: 无
**
** 作 者: 史跃勇
** 日 期: 2007年9月4日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void clock(uchar sign,long idata m)
{
uchar idata getdate[7];
getdate[0]=(m%100000000)/10000000;
getdate[1]=(m%10000000)/1000000;
getdate[2]=(m%1000000)/100000;
getdate[3]=(m%100000)/10000;
getdate[4]=(m%10000)/1000;
getdate[5]=(m%1000)/100;
getdate[6]=(m%100)/10;
getdate[7]=(m%10);
switch(sign)
{
case 11:
{
yearh=10*getdate[0]+getdate[1];
yearl=10*getdate[2]+getdate[3];
month=10*getdate[4]+getdate[5];
day=10*getdate[6]+getdate[7];
printf("Date:%bd%bd%bd%bd",getdate[0],getdate[1],getdate[2],getdate[3]);
printf("-%bd%bd-%bd%bd\n",getdate[4],getdate[5],getdate[6],getdate[7]);
}break;
case 12:
{
hour=10*getdate[2]+getdate[3];
min=10*getdate[4]+getdate[5];
sec=10*getdate[6]+getdate[7];
printf("Time:%bd%bd:%bd%bd:%bd%bd\n",getdate[2],getdate[3],getdate[4],getdate[5],getdate[6],getdate[7]);
}break;
case 13:
display=1;
break;
default:
break;
}
}
/*********************************************************************************************************
** 函数名称: t0
** 功能描述: 定时器零溢出中断处理程序
** 输 入: 无
** 输 出: 无
** 全局变量:无
** 调用模块:secdis
**
** 作 者: 史跃勇
** 日 期: 2007年9月4日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void t0(void)interrupt 1 //t0的中断程序
{
uchar idata nextline=0x0d;
tcnt++;
if(tcnt==4000)
{
tcnt=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
day++;
if(month==2&&((yearl==0&&yearh%4==0)||(yearl!=0&&yearl%4==0))&&day==30)day=1;
else if(month==2&&day==29)day=1;
else if((month==4||month==6||month==9||month==11)&&day==31)day=1;
else if(day==32)day=1;
if(day==1)
{
month++;
if(month==13)
{
month=1;
yearl++;
if(yearl==100)
{
yearl=0;
yearh++;
if(yearh==100)
{
yearh=20;
}
}
}
}
}
}
}
if(display==1)
_SECDIS_(yearh,yearl,month,day,hour,min,sec);
}
}
/**********************end****************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -