下面这个是产生1路pwm波的函数。我想知道如果要产生4路都需要什么呢。我想多加入几个参数,但是只让传入两个参数。void TIM2_CH1_PWM(uint32_t Freq, uint16_t Dutycycle){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t tim2_period;
uint16_t tim2_pulse;
tim2_period = (uint16_t)(TIM2_COUNTER_CLOCK/Freq - 1); //计算周期决定频率
tim2_pulse = (tim2_period + 1)*Dutycycle / 100; //计算脉宽决定占空比
/* TIM2ê±»ùμ¥ÔaÅäÖÃ */
TIM_TimeBaseStructure.TIM_Prescaler = TIM2_PRESCALER_VALUE;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = tim2_period;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM2í¨μà1
WM1Ä£ê½ÅäÖà */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = tim2_pulse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
还有就是为什么我在程序设置串口波特率为115200,但是用串口调试助手调试是,波特率设置为115200,不能正常的接收数据,但是设置为9600确能正确的接收数据。这是为什么啊,,求各位大佬给帮个忙,,谢谢啦。。。。
[mw_shl_code=c,true]TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //设置为pwm1输出模式
TIM_OCInitStructure.TIM_Pulse=INIT_PULSE; //设置占空比时间
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补 输出极性高
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能该通道输出
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //设定空闲状态时捕获比较输出状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR2上的预装载寄存器[/mw_shl_code]
谢谢啦。
一周热门 更多>