📄 stm32f10x_tim1.c
字号:
u16 ExtTRGFilter)
{
/* Configure the ETR Clock source */
ETR_Config(TIM1_ExtTRGPrescaler, TIM1_ExtTRGPolarity, ExtTRGFilter);
/* Enable the External clock mode2 */
*(vu32 *) SMCR_ECE_BB = SMCR_ECE_Set;
}
/*******************************************************************************
* Function Name : TIM1_ITRxExternalClockConfig
* Description : Configures the Internal Trigger as External Clock
* Input : - TIM1_ITRSource: Internal Trigger source.
* This parameter can be one of the following values:
* - TIM1_TS_ITR0: Internal Trigger 0
* - TIM1_TS_ITR1: Internal Trigger 1
* - TIM1_TS_ITR2: Internal Trigger 2
* - TIM1_TS_ITR3: Internal Trigger 3
* Output : None
* Return : None
*******************************************************************************/
void TIM1_ITRxExternalClockConfig(u16 TIM1_ITRSource)
{
/* Select the Internal Trigger */
TIM1_SelectInputTrigger(TIM1_ITRSource);
/* Select the External clock mode1 */
TIM1->SMCR |= TIM1_SlaveMode_External1;
}
/*******************************************************************************
* Function Name : TIM1_TIxExternalClockConfig
* Description : Configures the Trigger as External Clock
* Input : - TIM1_TIxExternalCLKSource: Trigger source.
* This parameter can be one of the following values:
* - TIM1_TS_TI1F_ED: TI1 Edge Detector
* - TIM1_TS_TI1FP1: Filtered TIM1 Input 1
* - TIM1_TS_TI2FP2: Filtered TIM1 Input 2
* - TIM1_ICPolarity: specifies the TIx Polarity.
* This parameter can be:
* - TIM1_ICPolarity_Rising
* - TIM1_ICPolarity_Falling
* - ICFilter : specifies the filter value.
* This parameter must be a value between 0x0 and 0xF.
* Output : None
* Return : None
*******************************************************************************/
void TIM1_TIxExternalClockConfig(u16 TIM1_TIxExternalCLKSource,
u8 TIM1_ICPolarity, u8 ICFilter)
{
/* Configure the TIM1 Input Clock Source */
if (TIM1_TIxExternalCLKSource == TIM1_TIxExternalCLK1Source_TI2)
{
TI2_Config(TIM1_ICPolarity, TIM1_ICSelection_DirectTI, ICFilter);
}
else
{
TI1_Config(TIM1_ICPolarity, TIM1_ICSelection_DirectTI, ICFilter);
}
/* Select the Trigger source */
TIM1_SelectInputTrigger(TIM1_TIxExternalCLKSource);
/* Select the External clock mode1 */
TIM1->SMCR |= TIM1_SlaveMode_External1;
}
/*******************************************************************************
* Function Name : TIM1_SelectInputTrigger
* Description : Selects the Input Trigger source
* Input : - TIM1_InputTriggerSource: The Trigger source.
* This parameter can be one of the following values:
* - TIM1_TS_ITR0: Internal Trigger 0
* - TIM1_TS_ITR1: Internal Trigger 1
* - TIM1_TS_ITR2: Internal Trigger 2
* - TIM1_TS_ITR3: Internal Trigger 3
* - TIM1_TS_TI1F_ED: TI1 Edge Detector
* - TIM1_TS_TI1FP1: Filtered Timer Input 1
* - TIM1_TS_TI2FP2: Filtered Timer Input 2
* - TIM1_TS_ETRF: External Trigger input
* Output : None
* Return : None
*******************************************************************************/
void TIM1_SelectInputTrigger(u16 TIM1_InputTriggerSource)
{
u32 tmpsmcr = 0;
tmpsmcr = TIM1->SMCR;
/* Select the Tgigger Source */
tmpsmcr &= SMCR_TS_Mask;
tmpsmcr |= TIM1_InputTriggerSource;
TIM1->SMCR = tmpsmcr;
}
/*******************************************************************************
* Function Name : TIM1_UpdateDisableConfig
* Description : Enables or Disables the Update event.
* Input : - Newstate: new state of the TIM1 peripheral Preload register
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void TIM1_UpdateDisableConfig(FunctionalState Newstate)
{
/* Set or Reset the UDIS Bit */
*(vu32 *) CR1_UDIS_BB = Newstate;
}
/*******************************************************************************
* Function Name : TIM1_UpdateRequestConfig
* Description : Configures the Update Request Interrupt source.
* Input : - TIM1_UpdateSource: specifies the Update source.
* This parameter can be one of the following values:
* - TIM1_UpdateSource_Regular
* - TIM1_UpdateSource_Global
* Output : None
* Return : None
*******************************************************************************/
void TIM1_UpdateRequestConfig(u8 TIM1_UpdateSource)
{
/* Set or Reset the URS Bit */
*(vu32 *) CR1_URS_BB = TIM1_UpdateSource;
}
/*******************************************************************************
* Function Name : TIM1_SelectHallSensor
* Description : Enables or disables the TIM1抯 Hall sensor interface.
* Input : - Newstate: new state of the TIM1 Hall sensor interface
* Output : None
* Return : None
*******************************************************************************/
void TIM1_SelectHallSensor(FunctionalState Newstate)
{
/* Set or Reset the TI1S Bit */
*(vu32 *) CR2_TI1S_BB = Newstate;
}
/*******************************************************************************
* Function Name : TIM1_SelectOPM
* Description : Enables or disables the TIM1抯 One Pulse Mode.
* Input : - TIM1_OPMode: specifies the OPM Mode to be used.
* This parameter can be one of the following values:
* - TIM1_OPMode_Single
* - TIM1_OPMode_Repetitive
* Output : None
* Return : None
*******************************************************************************/
void TIM1_SelectOnePulseMode(u16 TIM1_OPMode)
{
/* Set or Reset the OPM Bit */
*(vu32 *) CR1_OPM_BB = TIM1_OPMode;
}
/*******************************************************************************
* Function Name : TIM1_SelectOutputTrigger
* Description : Selects the TIM1 Trigger Output Mode.
* Input : - TIM1_TRGOSource: specifies the Trigger Output source.
* This paramter can be one of the following values:
* - TIM1_TRGOSource_Reset
* - TIM1_TRGOSource_Enable
* - TIM1_TRGOSource_Update
* - TIM1_TRGOSource_OC1
* - TIM1_TRGOSource_OC1Ref
* - TIM1_TRGOSource_OC2Ref
* - TIM1_TRGOSource_OC3Ref
* - TIM1_TRGOSource_OC4Ref
* Output : None
* Return : None
*******************************************************************************/
void TIM1_SelectOutputTrigger(u16 TIM1_TRGOSource)
{
u32 tmpcr2 = 0;
tmpcr2 = TIM1->CR2;
/* Reset the MMS Bits */
tmpcr2 &= CR2_MMS_Mask;
/* Select the TRGO source */
tmpcr2 |= TIM1_TRGOSource;
TIM1->CR2 = tmpcr2;
}
/*******************************************************************************
* Function Name : TIM1_SelectSlaveMode
* Description : Selects the TIM1 Slave Mode.
* Input : - TIM1_SlaveMode: specifies the TIM1 Slave Mode.
* This paramter can be one of the following values:
* - TIM1_SlaveMode_Reset
* - TIM1_SlaveMode_Gated
* - TIM1_SlaveMode_Trigger
* - TIM1_SlaveMode_External1
* Output : None
* Return : None
*******************************************************************************/
void TIM1_SelectSlaveMode(u16 TIM1_SlaveMode)
{
u32 tmpsmcr = 0;
tmpsmcr = TIM1->SMCR;
/* Reset the SMS Bits */
tmpsmcr &= SMCR_SMS_Mask;
/* Select the Slave Mode */
tmpsmcr |= TIM1_SlaveMode;
TIM1->SMCR = tmpsmcr;
}
/*******************************************************************************
* Function Name : TIM1_SelectMasterSlaveMode
* Description : Sets or Resets the TIM1 Master/Slave Mode.
* Input : - TIM1_MasterSlaveMode: specifies the TIM1 Master Slave Mode.
* This paramter can be one of the following values:
* - TIM1_MasterSlaveMode_Enable: synchronization between
* the current timer and its slaves (through TRGO).
* - TIM1_MasterSlaveMode_Disable: No action
* Output : None
* Return : None
*******************************************************************************/
void TIM1_SelectMasterSlaveMode(u16 TIM1_MasterSlaveMode)
{
/* Set or Reset the MSM Bit */
*(vu32 *) SMCR_MSM_BB = TIM1_MasterSlaveMode;
}
/*******************************************************************************
* Function Name : TIM1_EncoderInterfaceConfig
* Description : Configures the TIM1 Encoder Interface.
* Input : - TIM1_EncoderMode: specifies the TIM1 Encoder Mode.
* This parameter can be one of the following values:
* - TIM1_EncoderMode_TI1: Counter counts on TI1FP1 edge
* depending on TI2FP2 level.
* - TIM1_EncoderMode_TI2: Counter counts on TI2FP2 edge
* depending on TI1FP1 level.
* - TIM1_EncoderMode_TI12: Counter counts on both TI1FP1 and
* TI2FP2 edges depending on the level of the other input.
* - TIM1_IC1Polarity: specifies the IC1 Polarity
* This parmeter can be one of the following values:
* - TIM1_ICPolarity_Falling
* - TIM1_ICPolarity_Rising
* - TIM1_IC2Polarity: specifies the IC2 Polarity
* This parmeter can be one of the following values:
* - TIM1_ICPolarity_Falling
* - TIM1_ICPolarity_Rising
* Output : None
* Return : None
*******************************************************************************/
void TIM1_EncoderInterfaceConfig(u16 TIM1_EncoderMode, u8 TIM1_IC1Polarity,
u8 TIM1_IC2Polarity)
{
u32 tmpsmcr = 0;
u32 tmpccmr1 = 0;
tmpsmcr = TIM1->SMCR;
tmpccmr1 = TIM1->CCMR1;
/* Set the encoder Mode */
tmpsmcr &= SMCR_SMS_Mask;
tmpsmcr |= TIM1_EncoderMode;
/* Select the Capture Compare 1 and the Capture Compare 2 as input */
tmpccmr1 &= CCMR_CC13S_Mask & CCMR_CC24S_Mask;
tmpccmr1 |= CCMR_TI13Direct_Set | CCMR_TI24Direct_Set;
/* Set the TI1 and the TI2 Polarities */
*(vu32 *) CCER_CC1P_BB = TIM1_IC1Polarity;
*(vu32 *) CCER_CC2P_BB = TIM1_IC2Polarity;
TIM1->SMCR = tmpsmcr;
TIM1->CCMR1 = tmpccmr1;
}
/*******************************************************************************
* Function Name : TIM1_PrescalerConfig
* Description : Configures the TIM1 Prescaler.
* Input : - Prescaler: specifies the Prescaler Register value
* - TIM1_PSCReloadMode: specifies the TIM1 Prescaler Reload mode.
* This parmeter can be one of the following values:
* - TIM1_PSCReloadMode_Update: The Prescaler is loaded at
* the update event.
* - TIM1_PSCReloadMode_Immediate: The Prescaler is loaded
* immediatly.
* Output : None
* Return : None
*******************************************************************************/
void TIM1_PrescalerConfig(u16 Prescaler, u16 TIM1_PSCReloadMode)
{
/* Set the Prescaler value */
TIM1->PSC = Prescaler;
/* Set or reset the UG Bit */
*(vu32 *) EGR_UG_BB = TIM1_PSCReloadMode;
}
/*******************************************************************************
* Function Name : TIM1_CounterModeConfig
* Description : Specifies the Counter Mode to be used.
* Input : - TIM1_CounterMode: specifies the Counter Mode to be used
* This parameter can be one of the following values:
* - TIM1_CounterMode_Up: TIM1 Up Counting Mode
* - TIM1_CounterMode_Down: TIM1 Down Counting Mode
* - TIM1_CounterMode_CenterAligned1: TIM1 Center Aligned Mode1
* - TIM1_CounterMode_CenterAligned2: TIM1 Center Aligned Mode2
* - TIM1_CounterMode_CenterAligned3: TIM1 Center Aligned Mode3
* Output : None
* Return : None
*******************************************************************************/
void TIM1_CounterModeConfig(u16 TIM1_CounterMode)
{
u32 tmpcr1 = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -