****************************************************************************************/
#include "pwm.h"
#define ARR ((uint8_t)499)
static void Delay(u16 nCount);
/**************************************************************************
* 函数名:PWM_Init
* 描述 :PWM初始化
* 输入 :无
*
* 输出 :无
* 返回 :无
* 调用 :外部调用
*************************************************************************/
void PWM_Init(void)
{
TIM3_PSCR =0x04;
/* Set the Autoreload value */
TIM3_ARRH = (uint8_t)(ARR >> 8);
TIM3_ARRL = (uint8_t)(ARR);
/*TIM2 Frequency=16M/16/(499+1)=2K*/
/* PWM1 Mode configuration: Channel1
TIM2 Channel1 duty cycle = [TIM2_CCR1/(TIM2_ARR + 1)] * 100 = 50%*/
#if TIM3_Channel==TIM3_Channel1
/*TIM2 Frequency = TIM2 counter clock/(ARR + 1) */
TIM3_OC1Init(TIM3_OCMODE_PWM1, TIM3_OUTPUTSTATE_ENABLE,250, TIM3_OCPOLARITY_HIGH);
#elif TIM3_Channel==TIM3_Channel2
TIM3_OC2Init(TIM3_OCMODE_PWM2, TIM3_OUTPUTSTATE_ENABLE,500, TIM3_OCPOLARITY_HIGH);
#endif
TIM3_CR1 |= (uint8_t)MASK_TIM3_CR1_CEN;
}
/**************************************************************************
* 函数名:TIM3_OC1Init
* 描述 :TIM3 第一通道PWM初始化
* 输入 :TIM3_OCMode 常用参数TIM3_OCMODE_PWM1:0x60 ,TIM3_OCMODE_PWM2:0x70
TIM3_OutputState TIM3_OUTPUTSTATE_ENABLE:0x11
TIM3_Pulse 占空比
TIM3_OCPolarity TIM3_OCPOLARITY_HIGH:0x00 ,TIM3_OCPOLARITY_LOW:0x22
*
* 输出 :无
* 返回 :无
* 调用 :外部调用
*************************************************************************/
void TIM3_OC1Init(uint8_t TIM3_OCMode,uint8_t TIM3_OutputState,uint16_t TIM3_Pulse,uint8_t TIM3_OCPolarity )
{
/* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
TIM3_CCER1 &= (uint8_t)(~( MASK_TIM3_CCER1_CC1E | MASK_TIM3_CCER1_CC1P));
/* Set the Output State & Set the Output Polarity */
TIM3_CCER1 |= (uint8_t)((uint8_t)(TIM3_OutputState & MASK_TIM3_CCER1_CC1E ) |
(uint8_t)(TIM3_OCPolarity & MASK_TIM3_CCER1_CC1P));
/* Reset the Output Compare Bits & Set the Ouput Compare Mode */
TIM3_CCMR1 = (uint8_t)((uint8_t)(TIM3_CCMR1 & (uint8_t)(~(uint8_t)MASK_TIM3_CCMR1_OC1M)) |
(uint8_t)TIM3_OCMode);
/* Set the Pulse value */
TIM3_CCR1H = (uint8_t)(TIM3_Pulse >> 8);
TIM3_CCR1L = (uint8_t)(TIM3_Pulse);
TIM3_CCMR1 |= (uint8_t)MASK_TIM3_CCMR1_OC1PE;
}
/**************************************************************************
* 函数名:TIM3_OC2Init
* 描述 :TIM3 第二通道PWM初始化
* 输入 :TIM3_OCMode 常用参数TIM3_OCMODE_PWM1:0x60 ,TIM3_OCMODE_PWM2:0x70
TIM3_OutputState TIM3_OUTPUTSTATE_ENABLE:0x11
TIM3_Pulse 占空比
TIM3_OCPolarity TIM3_OCPOLARITY_HIGH:0x00 ,TIM3_OCPOLARITY_LOW:0x22
*
* 输出 :无
* 返回 :无
* 调用 :外部调用
*************************************************************************/
void TIM3_OC2Init(uint8_t TIM3_OCMode,uint8_t TIM3_OutputState,uint16_t TIM3_Pulse,uint8_t TIM3_OCPolarity )
{
/* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
TIM3_CCER1 &= (uint8_t)(~(MASK_TIM3_CCER1_CC2E | MASK_TIM3_CCER1_CC2P));
/* Set the Output State & Set the Output Polarity */
TIM3_CCER1 |= (uint8_t)((uint8_t)(TIM3_OutputState & MASK_TIM3_CCER1_CC2E ) |
(uint8_t)(TIM3_OCPolarity & MASK_TIM3_CCER1_CC2P));
/* Reset the Output Compare Bits & Set the Ouput Compare Mode */
TIM3_CCMR2 = (uint8_t)((uint8_t)(TIM3_CCMR1 & (uint8_t)(~(uint8_t)MASK_TIM3_CCMR2_OC2M )) |
(uint8_t)TIM3_OCMode);
/* Set the Pulse value */
TIM3_CCR2H = (uint8_t)(TIM3_Pulse >> 8);
TIM3_CCR2L = (uint8_t)(TIM3_Pulse);
TIM3_CCMR2 |= (uint8_t)MASK_TIM3_CCMR2_OC2PE;
}
void SetTIM3_PWM_Frequency(uint16_t TIM3_Period)
{
/* Set the Autoreload value */
TIM3_ARRH = (uint8_t)(TIM3_Period >> 8);
TIM3_ARRL = (uint8_t)(TIM3_Period);
}
void SetTIM3_PWM_DutyCycle( uint16_t TIM3_Pulse)
{
#if TIM3_Channel==TIM3_Channel1
/* Set the Pulse value */
TIM3_CCR1H = (uint8_t)(TIM3_Pulse >> 8);
TIM3_CCR1L = (uint8_t)(TIM3_Pulse);
#elif TIM3_Channel==TIM3_Channel2
TIM3_CCR2H = (uint8_t)(TIM3_Pulse >> 8);
TIM3_CCR2L = (uint8_t)(TIM3_Pulse);
#endif
}
void TestPWM_LED(void)
{
u16 Duty_Val;
for(Duty_Val=0;Duty_Val<499;Duty_Val++)
{
SetTIM3_PWM_DutyCycle(Duty_Val);
Delay(0x3fff);
}
}
static void Delay(u16 nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
} |