www.pudn.com > study_CIP51.rar > clock.c


#include 
#include 
#include 
 
#include "tdp.h" 
 
#ifdef evalboard 
extern void DINPUT(unsigned char aa, unsigned char bb); 
extern void DISPLY(unsigned char data *cc); 
static unsigned char data dg[] = {0,0,10,0,0,10,0,0}; 
#endif 
 
/*Global var. */ 
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 ) 
 
/*Clock Init*/ 
void clock_init(void) { 
	scan_flag = 0; 
	alm_flag = 0; 
	dayhsecs = 0L; 
	last_tick = timer0_count(); 
} 
 
/* Clock Update*/ 
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]; 
	dg[6] = buf[6]; 
	dg[4] = buf[4]; 
	dg[3] = buf[3]; 
	dg[1] = buf[1]; 
	dg[0] = buf[0]; 
	DISPLY(dg); 
#endif 
	 
	com_puts("\r\n"); 
	com_puts(buf); 
	com_puts("\r\n"); 
} 
 
/*set the screen to disply by "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 =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] = ':';	 
 
	com_puts("\r\n"); 
	com_puts(buf); 
	com_puts("\r\n"); 
 
}