📄 clock.c
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (c) 2007 by Qu chun lei watt@vip.163.com
*
* Filename: CLOCK.C
* Module: 1302C
* Language: ANSI C
* $Revision: 35 $
*
* Source code for clock functions which are described in CLOCK.H.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef CLOCK_C
#define CLOCK_C
#include "clock.h"
/*------------------*
| Constants
*------------------*/
#define ONE_OCLOCK (UINT16)60
#define TWELVE_HOUR_OFFSET (UINT16)720
#define TWELVE_PM (UINT16)720
#define MINUTES_MAXIMUM_12_HOURS (UINT16)719
#define MINUTES_12_HOURS (UINT16)720
#define MINUTES_MAXIMUM_24_HOURS (UINT16)1439
/*----------------*
| Private data
*----------------*/
static UINT16 clock; /* This is the clock */
#if CLOCK_DAY_OF_WEEK
static TClockDayOfWeek clockDayOfWeek; /* Current day of week */
#endif
#if CLOCK_OPTIMIZE_ROM
#if CLOCK_CONTROL
static BOOLEAN clockOn; /* True when clock is running */
#endif
#if CLOCK_CONFIGURABLE
static BOOLEAN clockFormat; /* 12/24-hour mode */
#endif
#else
#if CLOCK_CONTROL || CLOCK_CONFIGURABLE
static struct
{
BOOLEAN clockOn :1; /* True when clock is running */
BOOLEAN clockFormat :1; /* 12/24-hour mode */
}clockSettings;
#endif
#endif
/*-----------------------------*
| Private Function Prototypes
*-----------------------------*/
static BOOLEAN ClockValid(UINT16 checkTime);
/*--------------------*
| Public Functions
*--------------------*/
#if CLOCK_DAY_OF_WEEK
/*---------------------------------------------------------------------------*
| ClockDayOfWeek
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
TClockDayOfWeek ClockDayOfWeek(void)
{
return clockDayOfWeek;
}
#endif
#if CLOCK_DAY_OF_WEEK
/*---------------------------------------------------------------------------*
| ClockDayOfWeekSet
|
| Sets the current day of week
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
BOOLEAN ClockDayOfWeekSet(TClockDayOfWeek day)
{
BOOLEAN result;
result = FALSE;
if(day < 7)
{
clockDayOfWeek = day;
result = TRUE;
}
return result;
}
#endif
#if CLOCK_BINARY
/*---------------------------------------------------------------------------*
| ClockBinary
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
UINT16 ClockBinary(void)
{
return clock;
}
#endif
#if CLOCK_BINARY_SET
/*---------------------------------------------------------------------------*
| ClockBinarySet
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
BOOLEAN ClockBinarySet(UINT16 newTime)
{
BOOLEAN status;
if(ClockValid(newTime))
{
clock = newTime;
status = TRUE;
}
else
status = FALSE;
return status;
}
#endif
#if CLOCK_BCD
/*---------------------------------------------------------------------------*
| ClockBcd
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
TTimeOfDay ClockBcd(void)
{
TTime convertedClock;
#if CLOCK_CONFIGURABLE
#if CLOCK_OPTIMIZE_ROM
if(clockFormat == TIME_12_HOUR)
#else
if(clockSettings.clockFormat == TIME_12_HOUR)
#endif
convertedClock = Time12FromBinary(clock);
else
convertedClock = TimeFromBinary(clock);
#else
#if CLOCK_24
convertedClock = TimeFromBinary(clock);
#else
convertedClock = Time12FromBinary(clock);
#endif
#endif
return convertedClock;
}
#endif
#if CLOCK_BCD_SET
/*---------------------------------------------------------------------------*
| ClockBcdSet
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
BOOLEAN ClockBcdSet(TTimeOfDay newTime)
{
BOOLEAN status;
#if CLOCK_CONFIGURABLE
#if CLOCK_OPTIMIZE_ROM
if(TimeOfDayValid(newTime, clockFormat))
{
if(clockFormat == TIME_12_HOUR)
clock = Time12ToBinary(newTime);
#else
if(TimeOfDayValid(newTime, clockSettings.clockFormat))
{
if(clockSettings.clockFormat == TIME_12_HOUR)
clock = Time12ToBinary(newTime);
#endif
else
clock = TimeToBinary(newTime);
status = TRUE;
}
#else
if(TimeOfDayValid(newTime, CLOCK_24))
{
#if CLOCK_24
clock = TimeToBinary(newTime);
#else
clock = Time12ToBinary(newTime);
#endif
status = TRUE;
}
#endif
else
status = FALSE;
return status;
}
#endif
/*---------------------------------------------------------------------------*
| ClockStart
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
#if CLOCK_CONTROL
void ClockStart(void)
{
#if CLOCK_OPTIMIZE_ROM
clockOn = TRUE;
#else
clockSettings.clockOn = TRUE;
#endif
}
#endif
/*---------------------------------------------------------------------------*
| ClockStop
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
#if CLOCK_CONTROL
void ClockStop(void)
{
#if CLOCK_OPTIMIZE_ROM
clockOn = FALSE;
#else
clockSettings.clockOn = FALSE;
#endif
}
#endif
#if CLOCK_CONTROL
/*---------------------------------------------------------------------------*
| ClockActive
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
BOOLEAN ClockActive(void)
{
#if CLOCK_OPTIMIZE_ROM
return clockOn;
#else
return clockSettings.clockOn;
#endif
}
#endif
#if CLOCK_CONFIGURABLE
/*---------------------------------------------------------------------------*
| ClockConfig
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
TTimeOfDayFormat ClockConfig(void)
{
#if CLOCK_OPTIMIZE_ROM
return clockFormat;
#else
return clockSettings.clockFormat;
#endif
}
#endif
#if CLOCK_CONFIGURABLE
/*---------------------------------------------------------------------------*
| ClockConfigSet
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
void ClockConfigSet(TTimeOfDayFormat newClockFormat)
{
#if CLOCK_OPTIMIZE_ROM
if((clockFormat != TIME_12_HOUR) && (newClockFormat ==
TIME_12_HOUR))
{
if(clock > MINUTES_MAXIMUM_12_HOURS)
clock -= MINUTES_12_HOURS;
}
clockFormat = newClockFormat;
#else
if((clockSettings.clockFormat != TIME_12_HOUR) && (newClockFormat ==
TIME_12_HOUR))
{
if(clock > MINUTES_MAXIMUM_12_HOURS)
clock -= MINUTES_12_HOURS;
}
clockSettings.clockFormat = newClockFormat;
#endif
}
#endif
#if CLOCK_AM_PM_BINARY
/*---------------------------------------------------------------------------*
| ClockAmPmBinary
|
| Returns proper binary time of day in proper configuration and am/pm
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
UINT16 ClockAmPmBinary(TClockAmPm *amPm)
{
UINT16 convertedClock;
#if (CLOCK_CONFIGURABLE || (!CLOCK_CONFIGURABLE && CLOCK_24))
if(clock < TWELVE_PM)
{
*amPm = CLOCK_AM;
convertedClock = clock;
}
else
{
*amPm = CLOCK_PM;
convertedClock = clock - TWELVE_HOUR_OFFSET;
}
#else /* else not CLOCK_CONFIGURABLE and CLOCK_24 is
TIME_12_HOUR */
*amPm = CLOCK_AM;
convertedClock = clock;
#endif
return convertedClock;
}
#endif
#if CLOCK_AM_PM_BCD
/*---------------------------------------------------------------------------*
| ClockAmPmBcd
|
| Returns proper BCD time of day in proper configuration and am/pm
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
TTimeOfDay ClockAmPmBcd(TClockAmPm *amPm)
{
TTime convertedClock;
#if (CLOCK_CONFIGURABLE || (!CLOCK_CONFIGURABLE && CLOCK_24 ))
if(clock < TWELVE_PM)
{
*amPm = CLOCK_AM;
convertedClock = Time12FromBinary(clock);
}
else
{
*amPm = CLOCK_PM;
convertedClock = Time12FromBinary((UINT16)(clock - TWELVE_HOUR_OFFSET));
}
#else /* else not CLOCK_CONFIGURABLE and CLOCK_24 is
TIME_12_HOUR */
*amPm = CLOCK_AM;
convertedClock = Time12FromBinary(clock);
#endif
return convertedClock;
}
#endif
/*---------------------------------------------------------------------------*
| ClockProcess
|
| For details see CLOCK.H
*---------------------------------------------------------------------------*/
void ClockProcess(void)
{
#if CLOCK_CONTROL
#if CLOCK_OPTIMIZE_ROM
if(clockOn)
#else
if(clockSettings.clockOn)
#endif
#endif
{
clock++;
if(!ClockValid(clock))
{
clock = 0;
#if CLOCK_DAY_OF_WEEK && CLOCK_24
clockDayOfWeek++;
if(clockDayOfWeek > 6)
clockDayOfWeek = 0;
#endif
}
}
}
/*-------------------*
| Private Function
*-------------------*/
/*---------------------------------------------------------------------------*
| ClockValid
|
| Checks to see that the clock input is valid for the current clock
| configuration
|
| On Entry: Binary time in minutes since midnight to be checked
|
| On Exit: True if valid, false if invalid
*---------------------------------------------------------------------------*/
static BOOLEAN ClockValid(UINT16 checkTime)
{
BOOLEAN result;
result = TRUE;
#if CLOCK_CONFIGURABLE
#if CLOCK_OPTIMIZE_ROM
if(clockFormat == TIME_12_HOUR)
{
#else
if(clockSettings.clockFormat == TIME_12_HOUR)
{
#endif
if(checkTime > MINUTES_MAXIMUM_12_HOURS)
result = FALSE;
}
else
{
if(checkTime > MINUTES_MAXIMUM_24_HOURS)
result = FALSE;
}
#else
#if CLOCK_24
if(checkTime > MINUTES_MAXIMUM_24_HOURS)
result = FALSE;
#else
if(checkTime > MINUTES_MAXIMUM_12_HOURS)
result = FALSE;
#endif
#endif
return result;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -