home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1996 September
/
pcwk_09_96.iso
/
demo
/
wgelectr
/
pk51demo
/
files.2
/
EXAMPLES
/
TDP
/
CLOCK.C
next >
Wrap
C/C++ Source or Header
|
1995-06-08
|
6KB
|
224 lines
/*------------------------------------------------------------------------------
CLOCK.C: Routines to maintain a TOD clock.
Copyright 1995 KEIL Software, Inc.
------------------------------------------------------------------------------*/
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include "tdp.h"
/*------------------------------------------------------------------------------
Global Variable Declarations
------------------------------------------------------------------------------*/
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)
/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
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;
unsigned secs;
unsigned mins;
unsigned 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';
com_puts ("\r\n");
com_puts (buf);
com_puts ("\r\n");
}
/*------------------------------------------------------------------------------
TIME STR: 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");
}
/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/