📄 tim.c
字号:
;** 日 期: 2006年5月11日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void TIM_PWMOModeConfig( TIM_TYPES_T timer,
UWORD16 duty_cycle,
TIM_LOGIC_LEVELS_T level1,
UWORD16 full_period,
TIM_LOGIC_LEVELS_T level2
)
{
UWORD16 temp = TIMx_CR1(timer);
/* Set the Level During the pulse */
temp = level1 == TIM_HIGH ? temp | TIM_OLVLB_MASK : temp & ~TIM_OLVLB_MASK;
/* Set the Level after After the pulse */
temp = level2 == TIM_HIGH ? temp | TIM_OLVLA_MASK : temp & ~TIM_OLVLA_MASK;
temp |= TIM_OCAE_MASK; /* Set the OCAE */
temp |= TIM_PWM_MASK; /* Set the PWM Bit */
TIMx_CR1(timer) = temp; /* Update the CR1 */
if(duty_cycle < 5)
duty_cycle = 5;
TIMx_OCAR(timer) = duty_cycle - 5; /* Set the Duty Cycle value */
TIMx_OCBR(timer) = full_period - 5; /* Set the Full Period */
}
/*********************************************************************************************************
;** 函数名称: TIM_PWMIModeConfig
;** 功能描述: 该函数配置定时计数器(开始,停止,清除)
;**
;** 参 数: timer: 选择定时器类型(TIMER0, TIMER1, TIMER2, TIMER3)
;** edge: 输入触发类型(TIM_RISING,TIM_FALLING)
;**
;** 返 回 值: None
;**
;** 作 者: 罗辉联
;** 日 期: 2006年5月11日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void TIM_PWMIModeConfig (TIM_TYPES_T timer, TIM_CLOCK_EDGES_T edge)
{
UWORD16 temp = TIMx_CR1(timer);
/* Set the first edge Level */
temp = edge == TIM_RISING ? temp | TIM_IEDGA_MASK : temp & ~TIM_IEDGA_MASK;
/* Set the Second edge Level ( Opposit of the first level ) */
temp = edge == TIM_FALLING ? temp | TIM_IEDGB_MASK : temp & ~TIM_IEDGB_MASK;
temp |= TIM_PWMI_MASK; /* Set the PWM I Bit */
TIMx_CR1(timer) = temp; /* Update the CR1 */
}
/*********************************************************************************************************
;** 函数名称: TIM_PWMIValue
;** 功能描述: 该函数返回PWMI值
;**
;** 参 数: timer: 选择定时器类型(TIMER0, TIMER1, TIMER2, TIMER3)
;**
;** 返 回 值: pluse: 脉冲宽度
;** period: PWM周期
;**
;** 作 者: 罗辉联
;** 日 期: 2006年5月11日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
PWMI_PARAMETERS_T TIM_PWMIValue (TIM_TYPES_T timer)
{
PWMI_PARAMETERS_T temp;
temp.pulse = TIMx_ICBR(timer);
temp.period = TIMx_ICAR(timer);
return temp;
}
/*********************************************************************************************************
;** 函数名称: TIM_CounterConfig
;** 功能描述: 该函数配置定时计数器(开始,停止,清除)
;**
;** 参 数: timer: 选择定时器类型(TIMER0, TIMER1, TIMER2, TIMER3)
;** operation: 操作类型(TIM_START,TIM_STOP,TIM_CLEAR)
;**
;** 返 回 值: None
;**
;** 作 者: 罗辉联
;** 日 期: 2006年5月11日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void TIM_CounterConfig(TIM_TYPES_T timer, TIM_COUNTEROPERATIONS_T operation )
{
switch (operation)
{
case TIM_START :
TIMx_CR1(timer) |= TIM_EN_MASK;
break;
case TIM_STOP :
TIMx_CR1(timer) &= ~TIM_EN_MASK;
break;
case TIM_CLEAR :
TIMx_CNTR(timer) = 0x1234;
break;
}
}
/*********************************************************************************************************
;** 函数名称: TIM_FindFactors
;** 功能描述: Search for the best (a,b) values that fit n = a*b with the following constraints: 1<=a<=256, 1<=b<=65536
;**
;** 参 数: n: the number to decompose
;** a: a pointer to the first factor
;** b: a pointer to the second factor
;**
;** 返 回 值: 无
;**
;** 作 者: 罗辉联
;** 日 期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
static void TIM_FindFactors(UWORD32 n, UWORD16 *a, UWORD32 *b)
{
UWORD32 b0;
UWORD16 a0;
WORD32 err, err_min=n;
*a = a0 = ((n-1)/65536ul) + 1;
*b = b0 = n / *a;
for (; *a <= 256; (*a)++)
{
*b = n / *a;
err = (WORD32)*a * (WORD32)*b - (WORD32)n;
if (abs(err) > (*a / 2))
{
(*b)++;
err = (WORD32)*a * (WORD32)*b - (WORD32)n;
}
if (abs(err) < abs(err_min))
{
err_min = err;
a0 = *a;
b0 = *b;
if (err == 0) break;
}
}
*a = a0;
*b = b0;
}
/*********************************************************************************************************
;** 函数名称: TIM_OCMPTimerValueConfig
;** 功能描述: 对指定的定时器设置定时时间值(即设置prescaler的值与比较输出寄存器存储器的值)
;**
;** 参 数: timer: 定时器索引,channel: 定时器比较输出通道,time:需要的时间计数us为单位
;**
;** 返 回 值: 无
;**
;** 作 者: 罗辉联
;** 日 期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void TIM_OCMPTimerValueConfig (TIM_TYPES_T timer,TIM_CHANNELS_T channel,UWORD32 time)
{
UWORD16 a;
UWORD32 n, b;
n = time * (RCCU_AnyClockObtain(RCCU_PCLK) / 1000000);
TIM_FindFactors(n, &a, &b);
TIM_PrescalerConfig (timer, a - 1);
TIM_OCMPModeConfig(timer,channel,b - 1,TIM_TIMING,TIM_HIGH);
//uart_printf("RCCU_AnyClockObtain = %d",RCCU_AnyClockObtain(RCCU_PCLK));
//uart_printf("a = %d",a);
//uart_printf("b = %d",b);
}
#endif //EN_ARM_TIM > 0
/********************************************* end of file **********************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -