/*
* @brief Simple calulate square number.
* @param1 Base number
* @param2 Exponent number
* @retval Number of base^number
* @author: icerol
*/
unsigned long pow(uint base , uint exponent)
{
uint temp = 1;
for(uint i=0; i< exponent; i++)
{
temp *= base;
}
return temp;
}
/*
* @brief Initial timer2 for base timer.
* @param1 Timeout of microsecond
* @retval None
* @author: icerol
*/
#define F_MASTER 16000000 //CPU主频率
#define Second_to_us(times) ((unsigned long)(((times)*1000000)))
#define Millisecond_to_us(times) ((unsigned long)((((unsigned long)times)*1000)))
void timer2_Init(ulong microSecond)
{
unsigned char pscr;
double arr;
double f_tim;
for(pscr=0;pscr<0x10;pscr++)
{
f_tim = F_MASTER/pow(2,pscr);
arr = microSecond/(1000000/f_tim);
if(arr<0xffff)
break;
}
TIM2_PSCR_PSC = pscr; //分频
TIM2_ARRH = (uint)arr/256;
TIM2_ARRL = (uint)arr%256; //每time中断一次
TIM2_IER = 0x01; //允许更新中断
TIM2_CR1 = 0x01; //计数器使能,开始计数
}
#pragma vector = TIM2_OVR_UIF_vector
__interrupt void TIM2_OVR_UIF(void)
{
TIM2_SR1_UIF = 0;
//user code
PD_ODR_ODR1 = !PD_ODR_ODR1;
}