📄 realtime.c
字号:
/*****************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC1010 *
* *** + + *** FHP - RealTime *
* *** +++ *** *
* *** *** *
* *********** *
* ********* *
* *
*****************************************************************************
* This file contains the implementations of all the functions from the *
* Real Time Clock library, which is found in Cul.h, as well as *
* some internal utility functions. *
*****************************************************************************
* Author: OGR *
*****************************************************************************
* Revision history: *
* *
* $Log: RealTime.c,v $
* Revision 1.1 2003/07/29 11:24:50 tos
* Initial version in CVS.
*
*
* *
****************************************************************************/
#include <chipcon/hal.h>
#include <chipcon/cul.h>
// When changing between clock sources, the timer is stopped.
// The following constants reflect the time in milliseconds that
// is spent during these transitions. They are calculated for
// the evaluation board clock frequency 14.7456 MHz. If using
// another clock frequency, they must be recalibrated. See the
// example program RTCcalibrate.
#define TIME_LOST_DURING_POWER_UP 7240 //27372
#define TIME_LOST_DURING_POWER_DOWN 41225 //21040
// These constants must be set to 12000/clkFreq with large precision.
// XOSC_FACTOR is currently calculated for the evaluation board frequency of 14.7456 MHz.
// X32_FACTOR is calculated for the 32.768 kHz oscillator frequency.
#define XOSC_FACTOR 0.81380208333333333333333333333333
#define X32_FACTOR 366.2109375
#define RT_FACTOR (usingXOSC?XOSC_FACTOR:X32_FACTOR)
xdata bool usingXOSC = TRUE;
xdata ulong RTmodulus = 0;
xdata ulong status = 0;
xdata word overflows = 0;
//----------------------------------------------------------------------------
// void reset()
//
// Description:
// Resets the timer registers to zero.
//
// Arguments:
// None.
//
// Return value:
// void
//----------------------------------------------------------------------------
void reset() {
TL0=0;
TH0=0;
overflows = 0;
} // end reset
//----------------------------------------------------------------------------
// ulong getTime()
//
// Description:
// Reads the current time.
//
// Arguments:
// None.
//
// Return value:
// ulong
// The current time in microseconds.
//----------------------------------------------------------------------------
ulong getTime() {
volatile byte high=TH0, low=TL0;
volatile ulong ans = overflows;
ans <<= 16;
ans += (((ulong)high)<<8) | low;
ans *= RT_FACTOR;
ans += status;
if (RTmodulus)
ans %= RTmodulus;
return ans;
} // end getTime
//----------------------------------------------------------------------------
// void setTime(...)
//
// Description:
// Sets the real time clock to the given time.
//
// Arguments:
// ulong time
// The current time in microseconds.
//
// Return value:
// void
//----------------------------------------------------------------------------
void setTime(ulong time) {
status = time;
reset();
} // end setTime
//----------------------------------------------------------------------------
// void setModulus(...)
//
// Description:
// Specifies that whenever the time reaches the given value,
// it is reset to zero. A modulus of 0 cancels this effect.
//
// Arguments:
// ulong modulus
// The time will be measured modulo this value.
//
// Return value:
// void
//----------------------------------------------------------------------------
void setModulus(ulong modulus) {
RTmodulus = modulus;
} // end setModulus
//----------------------------------------------------------------------------
// void useX32()
//
// Description:
// Switches to the 32 kHz crystal oscillator as main clock source.
// The oscillator must be powered up prior to calling this function.
// It is absolutely necessary to use this function when switching
// to the 32 kHz oscillator if the real time clock is to work properly.
//
// Arguments:
// None.
//
// Return value:
// void
//----------------------------------------------------------------------------
void useX32() {
ulong wait = 7;
if (!usingXOSC) return;
TR0 = 0;
INT_GLOBAL_ENABLE(FALSE);
status = getTime()+TIME_LOST_DURING_POWER_DOWN;
reset();
usingXOSC = FALSE;
MAIN_CLOCK_SET_SOURCE(CLOCK_X32);
while (wait--);
TR0 = 1;
INT_GLOBAL_ENABLE(TRUE);
} // end useX32
//----------------------------------------------------------------------------
// void useXOSC()
//
// Description:
// Switches to the main crystal oscillator as main clock source.
// The oscillator must be powered up prior to calling this function.
// It is absolutely necessary to use this function when switching
// to the main oscillator if the real time clock is to work properly.
//
// Arguments:
// None.
//
// Return value:
// void
//----------------------------------------------------------------------------
void useXOSC() {
if (usingXOSC) return;
TR0 = 0;
INT_GLOBAL_ENABLE(FALSE);
MAIN_CLOCK_SET_SOURCE(CLOCK_XOSC);
status = getTime()+TIME_LOST_DURING_POWER_UP;
reset();
usingXOSC = TRUE;
TR0 = 1;
INT_GLOBAL_ENABLE(TRUE);
} // end useXOSC
//----------------------------------------------------------------------------
// void overflow() interrupt INUM_TIMER0
//
// Description:
// Called when the timer overflows.
// Simply increments a counter.
//
// Arguments:
// None.
//
// Return value:
// void
//----------------------------------------------------------------------------
void overflow() interrupt INUM_TIMER0 {
++overflows;
} // end overflow
//----------------------------------------------------------------------------
// void RTstart()
//
// Description:
// Starts the real time clock.
//
// Arguments:
// None.
//
// Return value:
// void
//----------------------------------------------------------------------------
void RTstart() {
usingXOSC = !(X32CON&0x01);
TMOD=(TMOD&0xF0)|(0x01);
CKCON=(CKCON&~0x0F);
TR0=1;
TF0=0;
ET0=1;
PT0=INT_HIGH;
EA=1;
} // end RTstart
//----------------------------------------------------------------------------
// void RTstop()
//
// Description:
// Stops the real time clock.
//
// Arguments:
// None.
//
// Return value:
// void
//----------------------------------------------------------------------------
void RTstop() {
ET0=0;
TR0=0;
} // end RTstop
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -