⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timers.c

📁 silicon wireless开发套件F920+Si4432原理图,源码
💻 C
字号:
/*
** ============================================================================
**
** FILE
**  timers.c
**
** DESCRIPTION
**  Contains the timer specific functions
**
** CREATED
**  Silicon Laboratories Hungary Ltd
**
** COPYRIGHT
**  Copyright 2008 Silicon Laboratories, Inc.  
**	http://www.silabs.com
**
** ============================================================================
*/
/*------------------------------------------------------------------------*/
/*						INCLUDE											  */
/*------------------------------------------------------------------------*/
#include "timers.h"


/*------------------------------------------------------------------------*/
/*						GLOBAL variables								  */
/*------------------------------------------------------------------------*/

/*------------------------------------------------------------------------*/
/*						LOCAL function prototypes						  */
/*------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  void StartTmr0( uint8 prescaler, uint16 period, uint8 it_enable)
  +
  + DESCRIPTION:    start TMR0 in the specified mode
  +
  +	INPUT:			prescaler: 		TMR0_48, TMR0_12, TMR0_4, TMR0_1
  +					period:			the duration of the timing	
  +					it_enable:		TRUE - it enables the IT; FALSE - it disables 
  +									the IT (status should be polled)	
  +
  + RETURN:         
  +
  + NOTES:          
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void StartTmr0(uint8 prescaler, UU16 period, bit it_enable)
{
	//setup timer
	TMOD = 0x11;						//TMR0 & TMR1 works in 16bit mode, independently of INT level
	CKCON = (CKCON & 0xF8) | prescaler;	//set prescaler
	

	//set time period
	period.U16 = 65535 - period.U16;
	TH0 = period.U8[MSB];
	TL0 = period.U8[LSB];

	//enable IT if needed
	if( it_enable == TRUE )
	{
		EnableTmr0It();
	}
	else
	{
		DisableTmr0It();
	}
	EnableTmr0();
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  UU16 GetTmr0(void)
  +
  + DESCRIPTION:    gives back the actual value of TMR0
  +
  +	INPUT:			None
  +
  + RETURN:         timer value
  +
  + NOTES:          
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
UU16 GetTmr0(void)
{
	UU16 Temp16;

	Temp16.U8[MSB] = TH0;
	Temp16.U8[LSB] = TL0;

	return Temp16;
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  bit Tmr0Expired(void)
  +
  + DESCRIPTION:    returns with TRUE if TMR0 already expired, 
  +					otherwise returns with FALSE
  +
  +	INPUT:			None
  +
  + RETURN:         TRUE / FALSE
  +
  + NOTES:          it clears the IT status flag!
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bit Tmr0Expired(void)
{
	if( TF0 == 0)
	{
		return FALSE;
	}
	else
	{	
		StopTmr0();
		return TRUE;
	}

}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  void StartTmr1(uint8 prescaler, UU16 period, bit it_enable)
  +
  + DESCRIPTION:    start TMR1 in the specified mode
  +
  +	INPUT:			prescaler: 		TMR1_48, TMR1_12, TMR1_4, TMR1_1
  +					period:			the duration of the timing	
  +					it_enable:		TRUE - it enables the IT; FALSE - it disables 
  +									the IT (status should be polled)	
  +
  + RETURN:         
  +
  + NOTES:          
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void StartTmr1(uint8 prescaler, UU16 period, bit it_enable)
{
	//setup timer
	//setup timer
	TMOD = 0x11;						//TMR0 & TMR1 works in 16bit mode, independently of INT level
	CKCON = (CKCON & 0xF4) | prescaler;	//set prescaler
	
	//set time period
	period.U16 = 65535 - period.U16;
	TH1 = period.U8[MSB];
	TL1 = period.U8[LSB];

	//enable IT if needed
	if( it_enable == TRUE )
	{
		EnableTmr1It();
	}
	else
	{
		DisableTmr1It();
	}
	EnableTmr1();
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  UU16 GetTmr1(void)
  +
  + DESCRIPTION:    gives back the actual value of TMR1
  +
  +	INPUT:			None
  +
  + RETURN:         Value of the timer
  +
  + NOTES:          
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
UU16 GetTmr1(void)
{
	UU16 Temp16;

	Temp16.U8[MSB] = TH1;
	Temp16.U8[LSB] = TL1;

	return Temp16;
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  bit Tmr1Expired(void)
  +
  + DESCRIPTION:    returns with TRUE if TMR1 already expired, 
  +					otherwise returns with FALSE
  +
  +	INPUT:			None
  +
  + RETURN:         TRUE / FALSE
  +
  + NOTES:          it clears the IT status flag!
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bit Tmr1Expired(void)
{
	if( TF1 == 0)
	{
		return FALSE;
	}
	else
	{
		StopTmr1();
		return TRUE;
	}
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  void StartTmr2(uint8 prescaler, UU16 period, bit it_enable)
  +
  + DESCRIPTION:    start TMR2 in the specified mode
  +
  +	INPUT:			prescaler:		TMR2_1, TMR2_12
  +					period:			the duration of the timing	
  +					it_enable:		TRUE - it enables the IT; FALSE - it disables 
  +									the IT (status should be polled)	
  +
  + RETURN:         
  +
  + NOTES:          
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void StartTmr2(uint8 prescaler, UU16 period, bit it_enable)
{
	//setup timer
	CKCON = (CKCON & 0xCF) | prescaler;
	TMR2CN = 0x00;
  	
	//set time period
	period.U16 = 65535 - period.U16;
	TMR2H = period.U8[MSB];
	TMR2L = period.U8[LSB];
	TMR2RLH = period.U8[MSB];	
	TMR2RLL = period.U8[LSB];

	//enable IT if needed
	if( it_enable == TRUE )
	{
		EnableTmr2It();
	}
	else
	{
		DisableTmr2It();
	}
	EnableTmr2();
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  UU16 GetTmr2(void)
  +
  + DESCRIPTION:    gives back the actual value of TMR2
  +
  +	INPUT:			
  +
  + RETURN:         
  +
  + NOTES:          
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
UU16 GetTmr2(void)
{
	UU16 Temp16;

	Temp16.U8[MSB] = TMR2H;
	Temp16.U8[LSB] = TMR2L;

	return Temp16;

}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  bit Tmr2Expired(void)
  +
  + DESCRIPTION:    returns with TRUE if TMR2 already expired, 
  +					otherwise returns with FALSE
  +
  +	INPUT:			None
  +
  + RETURN:         TRUE / FALSE
  +
  + NOTES:          it clears the IT status flag!
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bit Tmr2Expired(void)
{
	if( TF2H == 0)
	{
		return FALSE;
	}
	else
	{	
		StopTmr2();
		return TRUE;
	}

}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  void StartTmr3(uint8 prescaler, UU16 period, bit it_enable)
  +
  + DESCRIPTION:    start TMR3 in the specified mode
  +
  +	INPUT:			prescaler:		TMR3_1, TMR3_12
  +					period:			the duration of the timing	
  +					it_enable:		TRUE - it enables the IT; FALSE - it disables 
  +									the IT (status should be polled)	
  +
  + RETURN:         
  +
  + NOTES:          
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void StartTmr3(uint8 prescaler, UU16 period, bit it_enable)
{
	//setup timer
	CKCON = (CKCON & 0x3F) | prescaler;
	TMR3CN = 0x00;
  	
	//set time period
	period.U16 = 65535 - period.U16;
	TMR3H = period.U8[MSB];
	TMR3L = period.U8[LSB];
	TMR3RLH = period.U8[MSB];	
	TMR3RLL = period.U8[LSB];

	//enable IT if needed
	if( it_enable == TRUE )
	{
		EnableTmr3It();
	}
	else
	{
		DisableTmr3It();
	}
	EnableTmr3();
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  UU16 GetTmr3(void)
  +
  + DESCRIPTION:    gives back the actual value of TMR3
  +
  +	INPUT:			
  +
  + RETURN:         
  +
  + NOTES:          
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
UU16 GetTmr3(void)
{
	UU16 Temp16;

	Temp16.U8[MSB] = TMR3H;
	Temp16.U8[LSB] = TMR3L;

	return Temp16;

}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  bit Tmr3Expired(void)
  +
  + DESCRIPTION:    returns with TRUE if TMR3 already expired, 
  +					otherwise returns with FALSE
  +
  +	INPUT:			None
  +
  + RETURN:         TRUE / FALSE
  +
  + NOTES:          it clears the IT status flag!
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bit Tmr3Expired(void)
{
	if( (TMR3CN & 0x80) == 0)
	{
		return FALSE;
	}
	else
	{	
		StopTmr3();
		return TRUE;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -