📄 tim.c
字号:
/****************************************Copyright (c)**************************************************
**
** STR710 development team
**
**
** http://www.appchip.com
**
**--------------文件信息--------------------------------------------------------------------------------
** 文 件 名: tim.c
** 创 建 人: lhl
** 创建日期: 2006年5月11日
** 描 述: 该文件包含了timer的基本功能函数
**
**--------------历史版本--------------------------------------------------------------------------------
** 创 建 人: lhl
** 版 本: V1.0
** 日 期: 2006年5月11日
** 描 述: 原始版本
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#include "tim.h"
#if EN_ARM_TIM > 0 //决定是否编译该文件
/******************************************* 全局变量定义 ***********************************************/
/******************************************* 局部函数声明 ***********************************************/
/*********************************************************************************************************
;** 函数名称: TIM_Reset
;** 功能描述: 复位指定的定时器
;**
;** 参 数: timer: 选择定时器类型(TIMER0, TIMER1, TIMER2, TIMER3)
;**
;** 返 回 值: None
;**
;** 作 者: lhl
;** 日 期: 2006年5月11日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void TIM_Reset(TIM_TYPES_T timer)
{
TIMx_CR1(timer) = 0x0000;
TIMx_CR2(timer) = 0x0000;
TIMx_SR(timer) = 0x0000;
}
/*********************************************************************************************************
;** 函数名称: TIM_ICAPModeConfig
;** 功能描述: 配置指定定时器输入捕捉模式
;**
;** 参 数: timer: 选择定时器类型(TIMER0, TIMER1, TIMER2, TIMER3)
;** channel: 选择定时器的通道(TIM_CHANNEL_A,TIM_CHANNEL_B)
;** edge : 输入捕捉触发类型(TIM_RISING, TIM_FALLING)
;**
;** 返 回 值: None
;**
;** 作 者: lhl
;** 日 期: 2006年5月11日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void TIM_ICAPModeConfig (TIM_TYPES_T timer, TIM_CHANNELS_T channel, TIM_CLOCK_EDGES_T edge)
{
switch (channel)
{
case TIM_CHANNEL_A :
if(edge == TIM_RISING)
TIMx_CR1(timer) |= TIM_IEDGA_MASK;
else
TIMx_CR1(timer) &= ~TIM_IEDGA_MASK;
break;
case TIM_CHANNEL_B :
if(edge == TIM_RISING)
TIMx_CR1(timer) |= TIM_IEDGB_MASK;
else
TIMx_CR1(timer) &= ~TIM_IEDGB_MASK;
break;
}
}
/*********************************************************************************************************
;** 函数名称: TIM_OCMPModeConfig
;** 功能描述: 配置指定定时器输出比较模式
;**
;** 参 数: timer: 选择定时器类型(TIMER0, TIMER1, TIMER2, TIMER3)
;** channel: 选择定时器的通道(TIM_CHANNEL_A,TIM_CHANNEL_B)
:** pulse_length: 脉冲长度
;** mode: 输出比较模式(TIM_TIMING, TIM_WAVE)
;** level: 输出比较电平类型(TIM_HIGH, TIM_LOW)
;**
;** 返 回 值: None
;**
;** 作 者: lhl
;** 日 期: 2006年5月11日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void TIM_OCMPModeConfig(TIM_TYPES_T timer,
TIM_CHANNELS_T channel,
UWORD16 pulse_length,
TIM_OC_MODES_T mode,
TIM_LOGIC_LEVELS_T level)
{
UWORD16 temp1 = 0x0000;
UWORD16 temp2 = TIMx_CR2(timer);
TIMx_CR2(timer) = 0x0000; /* Start The TIM Counter */
TIMx_CR1(timer) = TIM_EN_MASK; /* Update the CR2 Register */
TIMx_CR2(timer) = temp2;
switch (mode)
{
case TIM_TIMING : /*Output Compare Used only for Internal Timing Operation*/
temp1 = channel == TIM_CHANNEL_A ? temp1 & ~TIM_OCAE_MASK : temp1 & ~TIM_OCBE_MASK;
break;
case TIM_WAVE : /* Output Compare Used for external wave generation */
temp1 = channel == TIM_CHANNEL_A ? TIM_OCAE_MASK : TIM_OCBE_MASK;
if ( level == TIM_HIGH )
temp1 = channel == TIM_CHANNEL_A ? temp1 | TIM_OLVLA_MASK : temp1 | TIM_OLVLB_MASK;
else
temp1 = channel == TIM_CHANNEL_A ? temp1 & ~TIM_OLVLA_MASK : temp1 & ~TIM_OLVLB_MASK;
break;
}
if(channel == TIM_CHANNEL_A)
TIMx_OCAR(timer) = (pulse_length - 5);
else
TIMx_OCBR(timer) = (pulse_length - 5);
TIMx_CNTR(timer) = 0x0000;
TIMx_CR1(timer) |= temp1;
}
/*********************************************************************************************************
;** 函数名称: TIM_OPModeConfig
;** 功能描述: 配置指定定时器的单个脉冲模式 ,又单个脉冲模式通过ICAP A OCAP 共同实现
;**
;** 参 数: timer: 选择定时器类型(TIMER0, TIMER1, TIMER2, TIMER3)
:** pulse_length: 脉冲长度
;** level1: 脉冲持续期间电平类型(TIM_HIGH,TIM_LOW)
;** level2 脉冲产生后电平类型(TIM_HIGH,TIM_LOW)
;** activative_edge: 触发ICAP A的边沿类型(TIM_RISING,TIM_FALLING)
;**
;** 返 回 值: None
;**
;** 作 者: lhl
;** 日 期: 2006年5月11日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void TIM_OPModeConfig (TIM_TYPES_T timer,
UWORD16 pulse_length,
TIM_LOGIC_LEVELS_T level1,
TIM_LOGIC_LEVELS_T level2,
TIM_CLOCK_EDGES_T edge )
{
UWORD16 temp = 0;
if(level1 == TIM_HIGH)
temp |= TIM_OLVLB_MASK; /* Set the Level During the pulse */
if(level2 == TIM_HIGH)
temp |= TIM_OLVLA_MASK; /* Set the Level After the pulse */
if (edge == TIM_RISING)
temp |= TIM_IEDGA_MASK; /* Set the Activation Edge on the INCAP 1 */
temp |= TIM_OCAE_MASK; /* Set the Output Compare Function */
temp |= TIM_OMP_MASK; /* Set the One pulse mode */
TIMx_CR1(timer) = temp; /* Update the CR1 register Value */
TIMx_OCAR(timer) = pulse_length; /* Set the Pulse length */
}
/*********************************************************************************************************
;** 函数名称: TIM_PWMOModeConfig
;** 功能描述: 该函数配置指定定时器单个脉冲模式,又单个脉冲模式通过ICAP A OCAP 共同实现
;**
;** 参 数: timer: 选择定时器类型(TIMER0, TIMER1, TIMER2, TIMER3)
:** duty_cycle: 脉冲长度
;** level1: 脉冲持续期间电平类型(TIM_HIGH,TIM_LOW)
;** level2 脉冲产生后电平类型(TIM_HIGH,TIM_LOW)
;** full_period: 触发ICAP A的边沿类型(TIM_RISING,TIM_FALLING)
;**
;** 返 回 值: None
;**
;** 作 者: lhl
;** 日 期: 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
;**
;** 作 者: lhl
;** 日 期: 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周期
;**
;** 作 者: lhl
;** 日 期: 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
;**
;** 作 者: lhl
;** 日 期: 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;
}
}
#endif //EN_ARM_TIM > 0
/********************************************* end of file **********************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -