用于WSN的PDA搜救器
源代码在线查看: tpm.c
/* Description : TIME/PWM functions.
*
* $Author: 王琢玉
* $Date: 2007/02/23
* $Name: TPM.c
*/
#define TPM_GLOBALS
#include "include.h"
extern UINT8 AskForEntironment;
/********************************************************************************
MC13213 有两个 TIMER,采用 TIMER2 做计数器。
TIMER2 有一个计数器,4个定时/比较器,计数器时钟采用 BUS CLOCK 进行分频到 62.5k,
MC13213的是一个16位的计数器,周期为1.04秒,因此定义一个16位的全局变量 UINT16 time2Ext,
将计数器扩展为32位。
********************************************************************************/
/************************************************************
Function: void InitMcuTimer2(void)
Parameter:
Return:
Description: Init the timer2
************************************************************/
void InitMcuTimer2(void)
{
/* ### Init_TPM init code */
TPM2MOD = 0xf424;
/* TPM2SC: TOF=0,TOIE=1,CPWMS=0,CLKSB=0,CLKSA=1,PS2=1,PS1=1,PS0=1 */
TPM2SC = 0x56; //计时器的频率为 62.5k (use XCLK, device = 64)
TPM2CNTH = 0;
TPM2CNTL = 0; //计数器清零
TPM2C0SC = 0x10;
time2Ext = 0;
}
/************************************************************
Function: TIME TimeGet(void)
Parameter:
Return: current time
Description: Get current time
************************************************************/
TIME TimeGet(void)
{
TIME Time;
SaveStatusReg();
Time.Byte.b1 = TPM2CNTH;
Time.Byte.b0 = TPM2CNTL;
Time.Byte.b3 = (UINT8)(time2Ext >> 8);
Time.Byte.b2 = (UINT8)time2Ext;
RestoreStatusReg();
CLRWDT();
return Time;
}
/************************************************************
Function: void OneMilliSecondDelay(void)
Parameter:
Return:
Description: Delay one second
************************************************************/
void OneMilliSecondDelay(void)
{
TIME currentTime;
currentTime = TimeGet();
while(TimeGetDiff(TimeGet(),currentTime)< ONE_MILLISECOND)CLRWDT();
}
/************************************************************
Function: void N_milliSecondsDelay(UINT32 MilliSeconds)
Parameter: UINT32 MilliSeconds how many millisenconds you want to delay
Return:
Description: Delay N millisenconds
************************************************************/
void N_milliSecondsDelay(UINT32 MilliSeconds)
{
while(MilliSeconds > 0)
{
OneMilliSecondDelay();
MilliSeconds --;
}
}
/************************************************************
Function: interrupt void VTPM2Ovf(void)
Parameter:
Return:
Description:
************************************************************/
interrupt void VTPM2Ovf(void)
{
TPM2SC = TPM2SC & 0x7f;
time2Ext ++;
AskForEntironment = 1;
}
/************************************************************
Function: interrupt void TPM2C0(void)
Parameter:
Return:
Description:
************************************************************/
interrupt void TPM2C0(void)
{
TPM2C0SC &= 0x3f;
Sci2Info.bits.RxOnePacket = 1;
}