📄 stm32f10x_tim.c
字号:
* signal.
* Input : - TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM
* peripheral.
* - TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure
* that contains the configuration information for the specified
* TIM peripheral.
* Output : None
* Return : None
*******************************************************************************/
void TIM_PWMIConfig(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
{
u16 icoppositepolarity = TIM_ICPolarity_Rising;
u16 icoppositeselection = TIM_ICSelection_DirectTI;
/* Check the parameters */
assert_param(IS_TIM_123458_PERIPH(TIMx));
/* Select the Opposite Input Polarity */
if (TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)
{
icoppositepolarity = TIM_ICPolarity_Falling;
}
else
{
icoppositepolarity = TIM_ICPolarity_Rising;
}
/* Select the Opposite Input */
if (TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI)
{
icoppositeselection = TIM_ICSelection_IndirectTI;
}
else
{
icoppositeselection = TIM_ICSelection_DirectTI;
}
if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
{
/* TI1 Configuration */
TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
TIM_ICInitStruct->TIM_ICFilter);
/* Set the Input Capture Prescaler value */
TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
/* TI2 Configuration */
TI2_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
/* Set the Input Capture Prescaler value */
TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
}
else
{
/* TI2 Configuration */
TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
TIM_ICInitStruct->TIM_ICFilter);
/* Set the Input Capture Prescaler value */
TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
/* TI1 Configuration */
TI1_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
/* Set the Input Capture Prescaler value */
TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
}
}
/*******************************************************************************
* Function Name : TIM_BDTRConfig
* Description : Configures the: Break feature, dead time, Lock level, the OSSI,
* the OSSR State and the AOE(automatic output enable).
* Input :- TIMx: where x can be 1 or 8 to select the TIM
* - TIM_BDTRInitStruct: pointer to a TIM_BDTRInitTypeDef
* structure that contains the BDTR Register configuration
* information for the TIM peripheral.
* Output : None
* Return : None
*******************************************************************************/
void TIM_BDTRConfig(TIM_TypeDef* TIMx, TIM_BDTRInitTypeDef *TIM_BDTRInitStruct)
{
/* Check the parameters */
assert_param(IS_TIM_18_PERIPH(TIMx));
assert_param(IS_TIM_OSSR_STATE(TIM_BDTRInitStruct->TIM_OSSRState));
assert_param(IS_TIM_OSSI_STATE(TIM_BDTRInitStruct->TIM_OSSIState));
assert_param(IS_TIM_LOCK_LEVEL(TIM_BDTRInitStruct->TIM_LOCKLevel));
assert_param(IS_TIM_BREAK_STATE(TIM_BDTRInitStruct->TIM_Break));
assert_param(IS_TIM_BREAK_POLARITY(TIM_BDTRInitStruct->TIM_BreakPolarity));
assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(TIM_BDTRInitStruct->TIM_AutomaticOutput));
/* Set the Lock level, the Break enable Bit and the Ploarity, the OSSR State,
the OSSI State, the dead time value and the Automatic Output Enable Bit */
TIMx->BDTR = (u32)TIM_BDTRInitStruct->TIM_OSSRState | TIM_BDTRInitStruct->TIM_OSSIState |
TIM_BDTRInitStruct->TIM_LOCKLevel | TIM_BDTRInitStruct->TIM_DeadTime |
TIM_BDTRInitStruct->TIM_Break | TIM_BDTRInitStruct->TIM_BreakPolarity |
TIM_BDTRInitStruct->TIM_AutomaticOutput;
}
/*******************************************************************************
* Function Name : TIM_TimeBaseStructInit
* Description : Fills each TIM_TimeBaseInitStruct member with its default value.
* Input : - TIM_TimeBaseInitStruct : pointer to a TIM_TimeBaseInitTypeDef
* structure which will be initialized.
* Output : None
* Return : None
*******************************************************************************/
void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
{
/* Set the default configuration */
TIM_TimeBaseInitStruct->TIM_Period = 0xFFFF;
TIM_TimeBaseInitStruct->TIM_Prescaler = 0x0000;
TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct->TIM_RepetitionCounter = 0x0000;
}
/*******************************************************************************
* Function Name : TIM_OCStructInit
* Description : Fills each TIM_OCInitStruct member with its default value.
* Input : - TIM_OCInitStruct : pointer to a TIM_OCInitTypeDef structure
* which will be initialized.
* Output : None
* Return : None
*******************************************************************************/
void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct)
{
/* Set the default configuration */
TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStruct->TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStruct->TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStruct->TIM_Pulse = 0x0000;
TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct->TIM_OCNPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct->TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStruct->TIM_OCNIdleState = TIM_OCNIdleState_Reset;
}
/*******************************************************************************
* Function Name : TIM_ICStructInit
* Description : Fills each TIM_ICInitStruct member with its default value.
* Input : - TIM_ICInitStruct : pointer to a TIM_ICInitTypeDef structure
* which will be initialized.
* Output : None
* Return : None
*******************************************************************************/
void TIM_ICStructInit(TIM_ICInitTypeDef* TIM_ICInitStruct)
{
/* Set the default configuration */
TIM_ICInitStruct->TIM_Channel = TIM_Channel_1;
TIM_ICInitStruct->TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct->TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct->TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct->TIM_ICFilter = 0x00;
}
/*******************************************************************************
* Function Name : TIM_BDTRStructInit
* Description : Fills each TIM_BDTRInitStruct member with its default value.
* Input : - TIM_BDTRInitStruct : pointer to a TIM_BDTRInitTypeDef
* structure which will be initialized.
* Output : None
* Return : None
*******************************************************************************/
void TIM_BDTRStructInit(TIM_BDTRInitTypeDef* TIM_BDTRInitStruct)
{
/* Set the default configuration */
TIM_BDTRInitStruct->TIM_OSSRState = TIM_OSSRState_Disable;
TIM_BDTRInitStruct->TIM_OSSIState = TIM_OSSIState_Disable;
TIM_BDTRInitStruct->TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStruct->TIM_DeadTime = 0x00;
TIM_BDTRInitStruct->TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStruct->TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM_BDTRInitStruct->TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
}
/*******************************************************************************
* Function Name : TIM_Cmd
* Description : Enables or disables the specified TIM peripheral.
* Input : - TIMx: where x can be 1 to 8 to select the TIMx peripheral.
* - NewState: new state of the TIMx peripheral.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_TIM_ALL_PERIPH(TIMx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the TIM Counter */
TIMx->CR1 |= CR1_CEN_Set;
}
else
{
/* Disable the TIM Counter */
TIMx->CR1 &= CR1_CEN_Reset;
}
}
/*******************************************************************************
* Function Name : TIM_CtrlPWMOutputs
* Description : Enables or disables the TIM peripheral Main Outputs.
* Input :- TIMx: where x can be 1 or 8 to select the TIMx peripheral.
* - NewState: new state of the TIM peripheral Main Outputs.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_TIM_18_PERIPH(TIMx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the TIM Main Output */
TIMx->BDTR |= BDTR_MOE_Set;
}
else
{
/* Disable the TIM Main Output */
TIMx->BDTR &= BDTR_MOE_Reset;
}
}
/*******************************************************************************
* Function Name : TIM_ITConfig
* Description : Enables or disables the specified TIM interrupts.
* Input : - TIMx: where x can be 1 to 8 to select the TIMx peripheral.
* - TIM_IT: specifies the TIM interrupts sources to be enabled
* or disabled.
* This parameter can be any combination of the following values:
* - TIM_IT_Update: TIM update Interrupt source
* - TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
* - TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
* - TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
* - TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
* - TIM_IT_COM: TIM Commutation Interrupt source
* - TIM_IT_Trigger: TIM Trigger Interrupt source
* - TIM_IT_Break: TIM Break Interrupt source
* - NewState: new state of the TIM interrupts.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void TIM_ITConfig(TIM_TypeDef* TIMx, u16 TIM_IT, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_TIM_ALL_PERIPH(TIMx));
assert_param(IS_TIM_IT(TIM_IT));
assert_param(IS_TIM_PERIPH_IT((TIMx), (TIM_IT)));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the Interrupt sources */
TIMx->DIER |= TIM_IT;
}
else
{
/* Disable the Interrupt sources */
TIMx->DIER &= (u16)~TIM_IT;
}
}
/*******************************************************************************
* Function Name : TIM_GenerateEvent
* Description : Configures the TIMx event to be generate by software.
* Input : - TIMx: where x can be 1 to 8 to select the TIM peripheral.
* - TIM_EventSource: specifies the event source.
* This parameter can be one or more of the following values:
* - TIM_EventSource_Update: Timer update Event source
* - TIM_EventSource_CC1: Timer Capture Compare 1 Event source
* - TIM_EventSource_CC2: Timer Capture Compare 2 Event source
* - TIM_EventSource_CC3: Timer Capture Compare 3 Event source
* - TIM_EventSource_CC4: Timer Capture Compare 4 Event source
* - TIM_EventSource_Trigger: Timer Trigger Event source
* Output : None
* Return : None
*******************************************************************************/
void TIM_GenerateEvent(TIM_TypeDef* TIMx, u16 TIM_EventSource)
{
/* Check the parameters */
assert_param(IS_TIM_ALL_PERIPH(TIMx));
assert_param(IS_TIM_EVENT_SOURCE(TIM_EventSource));
assert_param(IS_TIM_PERIPH_EVENT((TIMx), (TIM_EventSource)));
/* Set the event sources */
TIMx->EGR = TIM_EventSource;
}
/*******************************************************************************
* Function Name : TIM_DMAConfig
* Description : Configures the TIMx抯 DMA interface.
* Input : - TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM
* peripheral.
* - TIM_DMABase: DMA Base address.
* This parameter can be one of the following values:
* - TIM_DMABase_CR, TIM_DMABase_CR2, TIM_DMABase_SMCR,
* TIM_DMABase_DIER, TIM1_DMABase_SR, TIM_DMABase_EGR,
* TIM_DMABase_CCMR1, TIM_DMABase_CCMR2, TIM_DMABase_CCER,
* TIM_DMABase_CNT, TIM_DMABase_PSC, TIM_DMABase_ARR,
* TIM_DMABase_RCR, TIM_DMABase_CCR1, TIM_DMABase_CCR2,
* TIM_DMABase_CCR3, TIM_DMABase_CCR4, TIM_DMABase_BDTR,
* TIM_DMABase_DCR.
* - TIM_DMABurstLength: DMA Burst length.
* This parameter can be one value between:
* TIM_DMABurstLength_1Byte and TIM_DMABurstLength_18Bytes.
* Output : None
* Return : None
*******************************************************************************/
void TIM_DMAConfig(TIM_TypeDef* TIMx, u16 TIM_DMABase, u16 TIM_DMABurstLength)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -