频率 10K,占空比可以达到1%变化的精度,100K只能是10%的变化精度。
#define PWM_CLK1M_ARR_10K 100-1
void IzADCPWMInit(uint8_t duty)//10%~90%
{
if(duty>90)
{
duty = 90;
}
if(duty<10)
{
duty = 10;
}
//reset
/* TIM4 enable counter */
TIM_Cmd(TIM4, DISABLE);
/* TIM4 Main Output Enable */
TIM_CtrlPWMOutputs(TIM4, DISABLE);
//end of reset
/* System Clocks Configuration */
RCCPWMADCConfiguration();
/* GPIO Configuration */
GPIOPWMADCConfiguration();
/* -----------------------------------------------------------------------
TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
clock at 24 MHz the Prescaler is computed as following:
- Prescaler = (TIM3CLK / TIM3 counter clock) - 1
SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
and Connectivity line devices and to 24 MHz for Low-Density Value line and
Medium-Density Value line devices
The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
= 24 MHz / 666 = 36 KHz
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
----------------------------------------------------------------------- */
/* Compute the prescaler value */
//PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;//24M
//TIM3 Frequency=1MHz/(9999+1) =100Hz
PrescalerValue = (uint16_t) (SystemCoreClock / 1000000) - 1; //TIM3 counter clock: 1MHz
/* Time base configuration */
//TIM_TimeBaseStructure.TIM_Period = 665;//(ARR)
//TIM3 Frequency=1MHz/(9999+1) =100Hz
//TIM_TimeBaseStructure.TIM_Period = 10000-1; //TIM3 counter clock/TIM3 Frequency
//TIM3 Frequency=1MHz/(99+1) =10 000Hz
TIM_TimeBaseStructure.TIM_Period = PWM_CLK1M_ARR_10K;//PWM_CLK1M_ARR_100; //TIM3 counter clock/TIM3 Frequency
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//CCR1_Val =TIM3_ARR*duty/100;
// TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
// TIM_OCInitStructure.TIM_Pulse = (uint16_t)(10000-1)*duty/100;
TIM_OCInitStructure.TIM_Pulse = (uint16_t)(PWM_CLK1M_ARR_10K)*duty/100;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// TIM_OC1Init(TIM3, &TIM_OCInitStructure);
// TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
//PB6: TM4 Channel1
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
//PB8: TM4 Channel3
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (uint16_t)(PWM_CLK1M_ARR_10K)*duty/100;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
//PB7: TM4 Channel2
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (uint16_t)(PWM_CLK1M_ARR_10K)*duty/100; ;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
#if 0
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
#endif
TIM_ARRPreloadConfig(TIM4, ENABLE);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
/* TIM4 Main Output Enable */
TIM_CtrlPWMOutputs(TIM4, ENABLE);
}
上一篇:对STM32的CAN1模块的总结
下一篇:ARM中的RO段、RW段和ZI段的区别
推荐阅读最新更新时间:2024-03-16 16:14
设计资源 培训 开发板 精华推荐
- PI LYTSwitch™-6系列IC 让你了解不知道的秘密看专题赢好礼!
- 【EEWORLD第二十九届】2011年08月社区明星人物揭晓!
- EEWorld十五周年,感恩同行!盘点那些难忘的小确幸~
- 【0元得开发板,还能赢T12焊台,报名倒计时】Follow me,与得捷电子一起解锁开发板超能力!
- 有奖直播:ADI在中国能源互联网应用中的技术及产品 1月8日上午10:00-11:30 准时开启!
- 免费申请 | Nordic Semiconductor nPM1300-EK PMIC 评估套件
- 抢楼有礼:看直播,深入了解ST最新 MEMS气压计原理、操作、防水结构设计
- Littelfuse第3期:新型SSA系列双端超低分流电阻器