⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.c

📁 C51单片机的C语言例程,可实现定时器中断及其时钟功能的源代码
💻 C
字号:
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include "tdp.h"
#ifdef evalboard           /* 采用硬件目标板时需要使用以下外部函数 */
extern void DINPUT(unsigned char aa, unsigned char bb);
extern void DISPLY(unsigned char data *cc);
#endif

/* 全局变量定义  */
static xdata unsigned long dayhsecs;
static xdata unsigned last_tick;
static xdata unsigned char scan_flag;
static xdata unsigned char alm_flag;
static xdata unsigned almmins;

#define MAX_HSEC_DAY	(100 * 60 * 60 * 24)
#ifdef evalboard           /* 采用硬件目标板时需要使用以下数组 */
static unsigned char data dg[]={0,0,10,0,0,10,0,0};
#endif

/* 时钟初始化函数 */
void clock_init (void) {
	scan_flag = 0;
	alm_flag = 0;
	dayhsecs = 0L;
	last_tick = timer0_count ();
}

/* 时钟更新函数 */
void clock_update (void) {
	static xdata unsigned long last_daysecs;
	dayhsecs += timer0_elapsed_count (last_tick);
	last_tick = timer0_count ();
	while (dayhsecs >= MAX_HSEC_DAY)
	  dayhsecs -= MAX_HSEC_DAY;
	if ((dayhsecs / 100) == last_daysecs)
	  return;
	last_daysecs = dayhsecs / 100;
	if (alm_flag != 0)
	  if ((dayhsecs / (100 * 60)) == almmins)
    	com_putchar ('\x7');
	if (scan_flag != 0)  {
	  clock_out_time ();
	  cmdb_prompt ();
	}
}

/* 时钟设置函数 */
void clock_set ( unsigned long sethsec)	 {
	dayhsecs = sethsec;
	last_tick = timer0_count ();
	clock_update ();
}

/* 时钟扫描函数 */
void clock_scan (unsigned char flag)  {
	scan_flag = flag;
}

/* 时间输出函数 */
void clock_out_time (void)  {
	xdata char buf [21];
	unsigned hsecs,  secs,  mins,  hours;
	unsigned long t;

	t = dayhsecs;
	hsecs = t % 100;
	t /= 100;
	secs  = t % 60;
	t /= 60;
	mins  = t % 60;
	t /= 60;
	hours = t % 24;

	buf [0]  = (hours / 10) + '0';
	buf [1]  = (hours % 10) + '0';
	buf [2]  = ':';
	buf [3]  = (mins / 10) + '0';
	buf [4]  = (mins % 10) + '0';
	buf [5]  = ':';
	buf [6]  = (secs / 10) + '0';
	buf [7]  = (secs % 10) + '0';
	buf [8]  = '.';
	buf [9]  = (hsecs / 10) + '0';
	buf [10] = (hsecs % 10) + '0';
	buf [11] = '\0';

	#ifdef evalboard    /* 采用硬件目标板时需要使用以下数组 */
	dg[7]=buf[7];       /* 准备硬件目标板LED 显示数据 */
	dg[6]=buf[6];
	dg[4]=buf[4];
	dg[3]=buf[3];
	dg[1]=buf[1];
	dg[0]=buf[0];                        
	DISPLY(dg);       /* 点亮硬件目标板LED */
	#endif

	com_puts ("\r\n");
	com_puts (buf);
	com_puts ("\r\n");
}

/* 设置屏幕时间按"HHMMSS"格式显示 */
char strtotm ( unsigned long *t, char *s)  {
	char *s2;
	unsigned char tmp;
	if (strlen (s) != 6)
	  return (-1);

	for (s2 = s; *s2 != '\0'; s2++)  {
	  if (!isdigit (*s2))
    	return (-1);
    }

	tmp = ((s[0] - '0') * 10) + (s[1] - '0');
	if (tmp >= 24)
	  return (-1);
	*t = tmp;
	tmp = ((s[0] - '0') * 10) + (s[1] - '0');
	if (tmp >= 24)
	  return (-1);
	*t = tmp;
	tmp = ((s[2] - '0') * 10) + (s[3] - '0');
	if (tmp >= 60)
	  return (-1);
	*t = (60 * *t) + tmp;
	tmp = ((s[4] - '0') * 10) + (s[5] - '0');
	if (tmp >= 60)
	  return (-1);
	*t = (60 * *t) + tmp;
	return (0);
}

/* 闹钟设置函数 */
void alarm_set (unsigned setmins) {
	almmins = setmins;
	alm_flag = 1;
}

/* 闹钟清零函数 */
void alarm_clr (void) {
	alm_flag = 0;
}

/* 闹钟时间输出函数 */
void alarm_out_time (void) {
	xdata char buf [21];
	unsigned mins;
	unsigned hours;
	unsigned t;

	if (alm_flag == 0)   {
	  com_puts ("\r\nNone\r\n");
	  return;
    }
	t = almmins;
	mins  = t % 60;
	t /= 60;
	hours = t % 24;

	buf [0] = (hours / 10) + '0';
	buf [1] = (hours % 10) + '0';
	buf [2] = ':';
	buf [3] = (mins / 10) + '0';
	buf [4] = (mins % 10) + '0';
	buf [5] = '\0';
	
	com_puts ("\r\n");
	com_puts (buf);
	com_puts ("\r\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -