📄 75x_pwm.c
字号:
* Return : None
*******************************************************************************/
void PWM_SetPeriod(u16 Period)
{
PWM->ARR = Period;
}
/*******************************************************************************
* Function Name : PWM_SetPulse
* Description : Sets the PWM pulse value.
* Input : - PWM_Channel: specifies the PWM channel to be used.
* This parameter can be one of the following values:
* - PWM_Channel_1: PWM Channel 1 is used
* - PWM_Channel_2: PWM Channel 2 is used
* - PWM_Channel_3: PWM Channel 3 is used
* - PWM_Channel_ALL: PWM Channel 1, Channel 2 and 3 are used
* - Pulse: PWM pulse new value.
* Output : None
* Return : None
*******************************************************************************/
void PWM_SetPulse(u16 PWM_Channel, u16 Pulse)
{
/* Sets Channel 1 pulse value */
if(PWM_Channel == PWM_Channel_1)
{
PWM->OCR1 = Pulse;
}
/* Sets Channel 2 pulse value */
else if(PWM_Channel == PWM_Channel_2)
{
PWM->OCR2 = Pulse;
}
/* Sets Channel 3 pulse value */
else if(PWM_Channel == PWM_Channel_3)
{
PWM->OCR3 = Pulse;
}
/* Sets Channel 1, Channel 2 and Channel 3 pulse values */
else if(PWM_Channel == PWM_Channel_ALL)
{
PWM->OCR1 = Pulse;
PWM->OCR2 = Pulse;
PWM->OCR3 = Pulse;
}
}
/*******************************************************************************
* Function Name : PWM_SetPulse1
* Description : Sets the PWM Channel 1 pulse value.
* Input : - Pulse: PWM Channel 1 pulse new value.
* Output : None
* Return : None
*******************************************************************************/
void PWM_SetPulse1(u16 Pulse)
{
PWM->OCR1 = Pulse;
}
/*******************************************************************************
* Function Name : PWM_SetPulse2
* Description : Sets the PWM Channel 2 pulse value.
* Input : - Pulse: PWM Channel 2 pulse new value.
* Output : None
* Return : None
*******************************************************************************/
void PWM_SetPulse2(u16 Pulse)
{
PWM->OCR2 = Pulse;
}
/*******************************************************************************
* Function Name : PWM_SetPulse3
* Description : Sets the PWM Channel 3 pulse value.
* Input : - Pulse: PWM Channel 3 pulse new value.
* Output : None
* Return : None
*******************************************************************************/
void PWM_SetPulse3(u16 Pulse)
{
PWM->OCR3 = Pulse;
}
/*******************************************************************************
* Function Name : PWM_DebugCmd
* Description : Enables or disables PWM peripheral Debug control.
* Input : Newstate: new state of the PWM Debug control.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void PWM_DebugCmd(FunctionalState Newstate)
{
if(Newstate == ENABLE)
{
PWM->CR |= PWM_DBGC_Set;
}
else
{
PWM->CR &= PWM_DBGC_Reset;
}
}
/*******************************************************************************
* Function Name : PWM_CounterModeConfig
* Description : Specifies the Counter Mode to be used.
* Input : PWM_CounterMode: specifies the Counter Mode to be used
* This parameter can be one of the following values:
* - PWM_CounterMode_Up: PWM Up Counting Mode
* - PWM_CounterMode_Down: PWM Down Counting Mode
* - PWM_CounterMode_CenterAligned1: PWM Center Aligned1 Mode
* - PWM_CounterMode_CenterAligned2: PWM Center Aligned2 Mode
* - PWM_CounterMode_CenterAligned3: PWM Center Aligned3 Mode
* Output : None
* Return : None
*******************************************************************************/
void PWM_CounterModeConfig(u16 PWM_CounterMode)
{
/* Counter mode configuration */
PWM->CR &= PWM_CounterMode_Mask;
PWM->CR |= PWM_CounterMode;
}
/*******************************************************************************
* Function Name : PWM_ForcedOCConfig
* Description : Forces the PWM output waveform to active or inactive level.
* Input : - PWM_Channel: specifies the PWM channel to be used.
* This parameter can be one of the following values:
* - PWM_Channel_1: PWM Channel 1 is used
* - PWM_Channel_2: PWM Channel 2 is used
* - PWM_Channel_3: PWM Channel 3 is used
* - PWM_Channel_ALL: PWM Channel 1, Channel 2 and 3 are used
* - PWM_ForcedAction: specifies the forced Action to be set to the
* output waveform.
* This parameter can be one of the following values:
* - PWM_ForcedAction_Active: Force active level on OCxREF
* - PWM_ForcedAction_InActive: Force inactive level on
* OCxREF
* Output : None
* Return : None
*******************************************************************************/
void PWM_ForcedOCConfig(u16 PWM_Channel, u16 PWM_ForcedAction)
{
/* Channel 1 Forced Output Compare mode configuration */
if(PWM_Channel == PWM_Channel_1)
{
PWM->OMR1 &= PWM_OC1C_Mask;
PWM->OMR1 |= PWM_ForcedAction;
}
/* Channel 2 Forced Output Compare mode configuration */
else
{
if(PWM_Channel == PWM_Channel_2)
{
PWM->OMR1 &= PWM_OC2C_Mask;
PWM->OMR1 |= (PWM_ForcedAction<<8);
}
else
{
/* Channel 3 Forced Output Compare mode configuration */
if(PWM_Channel == PWM_Channel_3)
{
PWM->OMR2 &= PWM_OC3C_Mask;
PWM->OMR2 |= PWM_ForcedAction;
}
/* Channel 1, Channel 2 and Channel 3 Forced Output Compare mode
configuration */
else
{
PWM->OMR1 &= PWM_OC1C_Mask;
PWM->OMR1 |= PWM_ForcedAction;
PWM->OMR1 &= PWM_OC2C_Mask;
PWM->OMR1 |= (PWM_ForcedAction<<8);
PWM->OMR2 &= PWM_OC3C_Mask;
PWM->OMR2 |= PWM_ForcedAction;
}
}
}
}
/*******************************************************************************
* Function Name : PWM_SetDeadTime
* Description : Inserts dead time between the OCx and OCNx.
* Input : DeadTime: PWM Dead Time value.
* Output : None
* Return : None
*******************************************************************************/
void PWM_SetDeadTime(u16 DeadTime)
{
/* Sets the dead time value */
PWM->DTR &= PWM_DTR_Mask;
PWM->DTR |= DeadTime;
}
/*******************************************************************************
* Function Name : PWM_ResetCounter
* Description : Re-intializes the PWM counter and generates an update of the
* registers.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void PWM_ResetCounter(void)
{
/* Resets the PWM counter */
PWM->CR |= PWM_COUNTER_Reset;
}
/*******************************************************************************
* Function Name : PWM_TRGOSelection
* Description : Sets the PWM Master Mode selection bits.
* Input : PWM_TRGOMode: specifies the TRGO source.
* This parameter can be one of the following values:
* - PWM_TRGOMode_Enable: The CNT_EN bit is used as TRGO
* - PWM_TRGOMode_Update: The Update event is used as TRGO
* - PWM_TRGOMode_Reset: The CNT_RST bit is used as TRGO
* - PWM_TRGOMode_OC: The OC1 signal is used as TRGO
* Output : None
* Return : None
*******************************************************************************/
void PWM_TRGOSelection(u16 PWM_TRGOMode)
{
/* Sets the synchronization action */
PWM->CR &= PWM_MasterModeSelection_Mask;
PWM->CR |= PWM_TRGOMode;
}
/*******************************************************************************
* Function Name : PWM_GetFlagStatus
* Description : Checks whether the specified PWM flag is set or not.
* Input : PWM_FLAG: specifies the flag to check.
* This parameter can be one of the following values:
* - PWM_FLAG_OC1: Output Compare 1 Flag
* - PWM_FLAG_OC2: Output Compare 2 Flag
* - PWM_FLAG_OC3: Output Compare 3 Flag
* - PWM_FLAG_Update: PWM update Flag
* - PWM_FLAG_Emergency: PWM Emergency Flag
* Output : None
* Return : The new state of the PWM_FLAG(SET or RESET).
*******************************************************************************/
FlagStatus PWM_GetFlagStatus(u16 PWM_FLAG)
{
if((PWM->ISR & PWM_FLAG) != RESET )
{
return SET;
}
else
{
return RESET;
}
}
/*******************************************************************************
* Function Name : PWM_ClearFlag
* Description : Clears the PWM抯 pending flags.
* Input : PWM_FLAG: specifies the flag to clear.
* This parameter can be any combination of the following values:
* - PWM_FLAG_OC1: Output Compare 1 flag
* - PWM_FLAG_OC2: Output Compare 2 flag
* - PWM_FLAG_OC3: Output Compare 3 flag
* - PWM_FLAG_Update: PWM update flag
* - PWM_FLAG_Emergency: PWM Emergency flag
* Output : None
* Return : None
*******************************************************************************/
void PWM_ClearFlag(u16 PWM_FLAG)
{
/* Clears the flags */
PWM->ISR &= ~PWM_FLAG;
}
/*******************************************************************************
* Function Name : PWM_GetITStatus
* Description : Checks whether the PWM interrupt has occurred or not.
* Input : PWM_IT: specifies the PWM interrupt source to check.
* This parameter can be one of the following values:
* - PWM_IT_OC1: PWM Output Compare 1 Interrupt source
* - PWM_IT_OC2: PWM Output Compare 2 Interrupt source
* - PWM_IT_OC3: PWM Output Compare 3 Interrupt source
* - PWM_IT_Update: PWM update Interrupt source
* - PWM_IT_Emergency: PWM Emergency interrupt source
* - PWM_IT_GlobalUpdate: PWM global update Interrupt
* source
* Output : None
* Return : The new state of the PWM_IT(SET or RESET).
*******************************************************************************/
ITStatus PWM_GetITStatus(u16 PWM_IT)
{
u16 PWM_IT_Check = 0;
/* Calculates the pending bits to be checked */
PWM_IT_Check = PWM_IT & PWM_IT_Clear_Mask;
if((PWM->ISR & PWM_IT_Check) != RESET )
{
return SET;
}
else
{
return RESET;
}
}
/*******************************************************************************
* Function Name : PWM_ClearITPendingBit
* Description : Clears the PWM's interrupt pending bits.
* Input : PWM_IT: specifies the pending bit to clear.
* This parameter can be any combination of the following values:
* - PWM_IT_OC1: PWM Output Compare 1 Interrupt source
* - PWM_IT_OC2: PWM Output Compare 2 Interrupt source
* - PWM_IT_OC3: PWM Output Compare 3 Interrupt source
* - PWM_IT_Update: PWM update Interrupt source
* - PWM_IT_Emergency: PWM Emergency interrupt source
* - PWM_IT_GlobalUpdate: PWM global update Interrupt
* source
* Output : None
* Return : None
*******************************************************************************/
void PWM_ClearITPendingBit(u16 PWM_IT)
{
u16 PWM_IT_Clear = 0;
/* Calculates the pending bits to be cleared */
PWM_IT_Clear = PWM_IT & PWM_IT_Clear_Mask;
/* Clears the pending bits */
PWM->ISR &= ~PWM_IT_Clear;
}
/*******************************************************************************
* Function Name : OCM_ModuleConfig
* Description : Output Compare Module configuration.
* Input : PWM_InitStruct: pointer to a PWM_InitTypeDef structure that
* contains the configuration information for the PWM peripheral.
* Output : None
* Return : None
*******************************************************************************/
static void OCM_ModuleConfig(PWM_InitTypeDef* PWM_InitStruct)
{
u16 PWM_OCControl = 0x0000;
u16 DTR_REG = 0x0000;
if(PWM_InitStruct->PWM_Mode == PWM_Mode_OCTiming)
{
PWM_OCControl = PWM_OCControl_OCTiming;
}
else
{
if(PWM_InitStruct->PWM_Mode == PWM_Mode_OCActive)
{
PWM_OCControl = PWM_OCControl_OCActive;
}
else
{
if(PWM_InitStruct->PWM_Mode == PWM_Mode_OCInactive)
{
PWM_OCControl = PWM_OCControl_OCInactive;
}
else
{
if(PWM_InitStruct->PWM_Mode == PWM_Mode_OCToggle)
{
PWM_OCControl = PWM_OCControl_OCToggle;
}
else
{
PWM_OCControl = PWM_OCControl_PWM;
}
}
}
}
/* Read DTR register */
DTR_REG = PWM->DTR & 0x8000;
/*Channel 1 Configuration-----------------------------------------------------*/
if(PWM_InitStruct->PWM_Channel == PWM_Channel_1)
{
/* PWM Output Complementary Configuration */
if(PWM_InitStruct->PWM_Complementary == PWM_Complementary_Enable)
{
/* Configures Channel 1 on Output Compare mode */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -