📄 73x_tim.c
字号:
if (TIM_InitStruct->TIM_Period_Level == TIM_High)
TIMx->CR1 |= TIM_OLVLA_Set_Mask;
/* Set the Activation Edge on the INCAP 1 */
if (TIM_InitStruct->TIM_INPUT_Edge == TIM_Rising)
TIMx->CR1 |= TIM_IEDGA_ENABLE_Mask;
/* Set the Output Compare Function */
TIMx->CR1 |= TIM_OCA_ENABLE_Mask;
/* Set the One pulse mode */
TIMx->CR1 |= TIM_OMP_Mask;
/* Set the Pulse length */
TIMx->OCAR = TIM_InitStruct->TIM_Pulse_Length_A;
}
break;
/*********************************Input capture channel A*************/
case TIM_ICAP_CHANNELA:
{
if (TIM_InitStruct->TIM_ICAPA_Modes == TIM_Rising)
{
TIMx->CR1 |= TIM_IEDGA_ENABLE_Mask;
}
else
{
TIMx->CR1 &= TIM_IEDGA_DISABLE_Mask;
}
}
break;
/*********************************Input capture channel B**************/
case TIM_ICAP_CHANNELB:
{
if (TIM_InitStruct->TIM_ICAPB_Modes == TIM_Rising)
{
TIMx->CR1 |= TIM_IEDGB_ENABLE_Mask;
}
else
{
TIMx->CR1 &= TIM_IEDGB_DISABLE_Mask;
}
}
break;
/*********************************Input capture channel A & B *********/
case TIM_ICAP_CHANNELAB:
{
if (TIM_InitStruct->TIM_ICAPB_Modes == TIM_Rising)
{
TIMx->CR1 |= TIM_IEDGB_ENABLE_Mask;
}
else
{
TIMx->CR1 &= TIM_IEDGB_DISABLE_Mask;
}
if (TIM_InitStruct->TIM_ICAPA_Modes == TIM_Rising)
{
TIMx->CR1 |= TIM_IEDGA_ENABLE_Mask;
}
else
{
TIMx->CR1 &= TIM_IEDGA_DISABLE_Mask;
}
}
break;
}
}
/*******************************************************************************
* Function Name : TIM_CounterCmd
* Description : Enables or disables TIMx Counter peripheral.
* Input : - TIMx: where x can be 0, 1 or 9 to select the TIM peripheral.
* - TIM_operation: new state of the TIMx Counter peripheral
* (TIM_operation can be TIM_START, TIM_STOP or TIM_CLEAR)
* Output : None
* Return : None
*******************************************************************************/
void TIM_CounterCmd(TIM_TypeDef *TIMx, TIM_CounterOperations TIM_operation)
{
switch ( TIM_operation )
{
case TIM_START :
TIMx->CR1 |= TIM_ENABLE_Mask;
break;
case TIM_STOP :
TIMx->CR1 &= TIM_DISABLE_Mask;
break;
case TIM_CLEAR :
TIMx->CNTR = 0x1234;
break;
}
}
/*******************************************************************************
* Function Name : TIM_ClockSourceConfig
* Description : This routine is used to configure the TIM clock source
* Input : - TIMx: where x can be 0, 1..9 to select the TIM peripheral
* : - TIM_Clock : Specifies the TIM source clock
* - TIM_CLK_INTERNAL : The TIM is clocked by the APB frequency
* divided by the prescaler value.
* - TIM_CLK_EXTERNAL : The TIM is clocked by an external Clock
- TIM_ICAP_x : where x can be 0,1..9 to select the ICAPx
input pin that recieves the external clock.
* Output : None
* Return : None
*******************************************************************************/
void TIM_ClockSourceConfig ( TIM_TypeDef *TIMx, u16 TIM_Clock )
{
if (TIM_Clock == TIM_CLK_INTERNAL)
{
TIMx->CR1 &= TIM_ECK_DISABLE_Mask;
}
else
{
if (TIM_Clock & 0x8000)
{
TIMx->CR1 |= TIM_ECK_ENABLE_Mask;
CFG->TIMSR = (CFG->TIMSR & 0x0000)|TIM_ICAP_NONE;
}
else
{
TIMx->CR1 |= TIM_ECK_ENABLE_Mask;
CFG->TIMSR = (CFG->TIMSR & 0x0000)|TIM_Clock;
}
/**/
}
}
/*******************************************************************************
* Function Name : TIM_GetClockStatus
* Description : This routine is used to get the TIM clock source status
* Input : TIMx: where x can be 0, 1 or 9 to select the TIM peripheral.
* Output : None
* Return : The TIM source clock
* - TIM_CLK_INTERNAL : The TIM is clocked by the APB frequency
* divided by the prescaler value.
* - TIM_CLK_EXTERNAL : The TIM is clocked by an external Clock
*******************************************************************************/
u16 TIM_GetClockStatus(TIM_TypeDef *TIMx)
{
if ((TIMx->CR1 & TIM_ECK_ENABLE_Mask) == 0)
{
return TIM_CLK_INTERNAL;
}
else
{
return TIM_CLK_EXTERNAL;
}
}
/*******************************************************************************
* Function Name : TIM_PrescalerConfig
* Description : This routine is used to configure the TIM prescaler value
* (when using an internal clock).
* Input : - TIMx: where x can be 0, 1 or 9 to select the TIM peripheral
* : - TIM_Prescaler: Prescaler division factor
* Output : None
* Return : None
*******************************************************************************/
void TIM_PrescalerConfig(TIM_TypeDef *TIMx, u8 TIM_Prescaler)
{
TIMx->CR2 = TIMx->CR2 & 0xFF00;
TIMx->CR2 |= TIM_Prescaler;
}
/*******************************************************************************
* Function Name : TIM_GetPrescalerValue
* Description : This routine is used to get the TIM prescaler value
* ( when using an internal clock )
* Input : TIMx: where x can be 0, 1 or 9 to select the TIM peripheral.
* Output : None
* Return : Prescaler Register
*******************************************************************************/
u8 TIM_GetPrescalerValue(TIM_TypeDef *TIMx)
{
return TIMx->CR2 & 0x00FF;
}
/*******************************************************************************
* Function Name : TIM_GetICAPAValue
* Description : This routine is used to get the Input Capture A value.
* Input : TIMx: where x can be 0, 1 or 9 to select the TIM peripheral
* Output : None
* Return : Input Capture A Register
*******************************************************************************/
u16 TIM_GetICAPAValue(TIM_TypeDef *TIMx)
{
return TIMx->ICAR;
}
/*******************************************************************************
* Function Name : TIM_GetICAPBValue
* Description : This routine is used to get the Input Capture B value.
* Input : TIMx: where x can be 0, 1 or 9 to select the TIM peripheral
* Output : None
* Return : Input Capture B Register
*******************************************************************************/
u16 TIM_GetICAPBValue ( TIM_TypeDef *TIMx)
{
return TIMx->ICBR;
}
/*******************************************************************************
* Function Name : TIM_FlagStatus
* Description : Checks whether the specified TIM flag is set or not.
* Input : - TIMx: where x can be 0, 1 or 9 to select the TIM peripheral.
* - TIM_Flag: flag to check(TIM_Flag can be TIM_FLAG_ICA, TIM_FLAG_ICB
* TIM_FLAG_OCA, TIM_FLAG_OCB, TIM_FLAG_TO)
* Output : None
* Return : The NewState of the TIM_Flag (SET or RESET).
*******************************************************************************/
FlagStatus TIM_FlagStatus ( TIM_TypeDef *TIMx, u16 TIM_Flag )
{
if((TIMx->SR & TIM_Flag) == RESET)
{
return RESET;
}
else
{
return SET;
}
}
/*******************************************************************************
* Function Name : TIM_FlagClear
* Description : Clears the TIM Flag passed as a parameter
* Input : - TIMx: where x can be 0, 1 or n to select the TIM peripheral.
* - TIM_Flag: flag to clear (TIM_Flag can be TIM_FLAG_ICA, TIM_FLAG_ICB
* TIM_FLAG_OCA, TIM_FLAG_OCB, TIM_FLAG_TO)
* Output : None
* Return : None
*******************************************************************************/
void TIM_FlagClear ( TIM_TypeDef *TIMx, u16 TIM_Flag )
{
/* Clear TIM_Flag */
TIMx->SR &= ~TIM_Flag;
}
/*******************************************************************************
* Function Name : TIM_GetPWMIPulse
* Description : This routine is used to get the Pulse value in PWMI Mode.
* Input : TIMx: where x can be 0, 1 or 9 to select the TIM peripheral
* Output : None
* Return : Pulse
*******************************************************************************/
u16 TIM_GetPWMIPulse(TIM_TypeDef *TIMx)
{
return TIMx->ICBR;
}
/*******************************************************************************
* Function Name : TIM_GetPWMIPeriod
* Description : This routine is used to get the Period value in PWMI Mode.
* Input : TIMx: where x can be 0, 1 or 9 to select the TIM peripheral
* Output : None
* Return : Period
*******************************************************************************/
u16 TIM_GetPWMIPeriod(TIM_TypeDef *TIMx)
{
return TIMx->ICAR;
}
/*******************************************************************************
* Function Name : TIM_ITConfig
* Description : Configures the Timer interrupt source.
* Input : - TIMx: where x can be 0, 1 or 9 to select the TIM peripheral.
* - TIM_IT: specifies the TIM interrupt source to be enabled.
* This parameter can be one of the following values:
* - TIM_IT_ICA: Input Capture A Interrupt source
* - TIM_IT_OCA: Output Compare A Interrupt source
* - TIM_IT_TO: Timer Overflow Interrupt source
* - TIM_IT_ICB: Input Capture B Interrupt source
* - TIM_IT_OCB: Output Compare B Interrupt source
* Output : None
* Return : None
*******************************************************************************/
void TIM_ITConfig(TIM_TypeDef *TIMx, u16 TIM_IT, FunctionalState NewState)
{
if(NewState == ENABLE)
{
TIMx->CR2 = (TIMx->CR2 & 0x00FF) | TIM_IT;
}
else
{
TIMx->CR2 &= ~TIM_IT;
}
}
/*******************************************************************************
* Function Name : TIM_DMAConfig
* Description : Configures the Timer DMA source.
* Input : - TIMx: where x can be 0, 1 or 9 to select the TIM peripheral.
* - TIM_DMA_Souces: specifies the TIM DMA source to be selected.
* This parameter can be one of the following values:
* - TIM_DMA_ICA: Input Capture A DMA source
* - TIM_DMA_OCA: Output Compare A DMA source
* - TIM_DMA_TO: Timer Overflow DMA source
* - TIM_DMA_ICB: Input Capture B DMA source
* - TIM_DMA_OCB: Output Compare B DMA source
* Output : None
* Return : None
*******************************************************************************/
void TIM_DMAConfig(TIM_TypeDef *TIMx, u16 TIM_DMA_Sources)
{
/* Reset the DMAS[1:0] bits */
TIMx->CR1 &= 0xCFFF;
/* Set the DMAS[1:0] bits according to TIM_DMA_Sources parameter */
TIMx->CR1 |= TIM_DMA_Sources;
}
/*******************************************************************************
* Function Name : TIM_DMACmd
* Description : Enables or disables TIMx DMA peripheral.
* Input : - TIMx: where x can be 0, 1 or 9 to select the TIM peripheral.
* - Newstate: new state of the TIMx DMA peripheral
* (Newstate can be ENABLE or DISABLE)
* Output : None
* Return : None
*******************************************************************************/
void TIM_DMACmd(TIM_TypeDef *TIMx, FunctionalState Newstate )
{
if (Newstate == ENABLE)
{
TIMx->CR2 |= TIM_DMA_Enable;
}
else
{
TIMx->CR2 &= TIM_DMA_Disable;
}
}
/******************* (C) COPYRIGHT 2005 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -