

(图片与文章内容无关,来源于网络)
精确控制PWM个数采用的是主从方案, TIM3产生PWM,为TIM2提供时钟,然后TIM2计数

测试代码如下(待完善),仅供参考
主要的文件就是time.c文件
main.c文件
void Delay(unsigned int Time){if(Time == 0) return;while(Time--);}int main(){//NVIC_NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);TIM2_init(7199,999);/// 1/10=0.01TIM3_init(7199,99);/// 1/100=0.1//99usart1_init(115200);//SetPWMValue(TIM2,1,500);SetPWMValue(TIM3,1,50);UartPutChar(USART1,0X56);while(1){// UartPutChar(USART1,0X56);Delay(100);if(flag==1){TIM3_init(7199,(100+time_count*100)-1);SetPWMValue(TIM3,1,50);if(time_count==10){time_count=0;TIM_Cmd(TIM3,DISABLE);}flag=0;};}}
time.c文件
/************************T2的定时器初始化*************************/void TIM2_init(unsigned int pres,unsigned int period){TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypeStruct;NVIC_InitTypeDef NVIC_InitTypeStruct;TIM_OCInitTypeDef TIM_OCInitTypeStruct;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//TIM_TimeBaseInitTypeStruct.TIM_Prescaler=pres;TIM_TimeBaseInitTypeStruct.TIM_CounterMode=TIM_CounterMode_Up;//TIM_TimeBaseInitTypeStruct.TIM_Period=period;TIM_TimeBaseInitTypeStruct.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitTypeStruct);TIM_ITConfig(TIM2,TIM_IT_Trigger,ENABLE);NVIC_InitTypeStruct.NVIC_IRQChannel=TIM2_IRQn;NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority=2;NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority=2;NVIC_InitTypeStruct.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitTypeStruct);TIM_SelectInputTrigger(TIM2,TIM_TS_ITR2);TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_External1);TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);TIM_Cmd(TIM2,ENABLE);}/************************T3输出PWM控制T2的计数*************************/void TIM3_init(unsigned int pres,unsigned int period){GPIO_InitTypeDef GPIO_InitTypeStruct;TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypeStruct;TIM_OCInitTypeDef TIM_OCInitTypeStruct;NVIC_InitTypeDef NVIC_InitTypeStruct;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);GPIO_InitTypeStruct.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;GPIO_InitTypeStruct.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_InitTypeStruct.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitTypeStruct);TIM_TimeBaseInitTypeStruct.TIM_Prescaler=pres;TIM_TimeBaseInitTypeStruct.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInitTypeStruct.TIM_Period=period;TIM_TimeBaseInitTypeStruct.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitTypeStruct);// TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);TIM_OCInitTypeStruct.TIM_OCMode=TIM_OCMode_PWM1;TIM_OCInitTypeStruct.TIM_OutputState=TIM_OutputState_Enable;TIM_OCInitTypeStruct.TIM_Pulse=0;TIM_OCInitTypeStruct.TIM_OCPolarity=TIM_OCPolarity_High;TIM_OCInitTypeStruct.TIM_OCIdleState=TIM_OCIdleState_Set;TIM_OC1Init(TIM3,&TIM_OCInitTypeStruct);// //MasterTIM_CtrlPWMOutputs(TIM3,ENABLE);TIM_Cmd(TIM3,ENABLE);TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC1Ref );TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);}unsigned char time_count0=0;unsigned char time_count=0;unsigned char flag=0;void TIM2_IRQHandler(){TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypeStruct;if(TIM_GetITStatus(TIM2,TIM_IT_Trigger)!=RESET){/*T2 TIM_SlaveMode_Gatedtime_count0++;放在本函数前面//TIM2_init(7199,99);/// 1/10=0.01//TIM3_init(7199,99);/// 1/100=0.01count 1 /pwm 0 个脉冲count 2 /pwm 2 个脉冲count 3 /pwm 4 个脉冲count 4 /pwm 6 个脉冲//TIM2_init(7199,999);/// 1/10=0.1//TIM3_init(7199,99);/// 1/100=0.01count 1 /pwm 0 个脉冲count 2 /pwm 20 个脉冲count 3 /pwm 40 个脉冲count 4 /pwm ?个脉冲//TIM2_init(7199,99);/// 1/10=0.1//TIM3_init(7199,999);/// 1/100=0.01count 1 /pwm 0 个脉冲count 2 /pwm 2 个脉冲count 3 /pwm 4 个脉冲count 4 /pwm 6 个脉冲*//* T2 TIM_SlaveMode_Trigger模式T3每产生一次PWM脉冲将会使T2计数一次*/if(time_count0==11) //{time_count++;SetPWMValue(TIM3,1,0);flag=1;UartPutChar(USART1,time_count0);////time_count0=0;//TIM_Cmd(TIM2,DISABLE);}time_count0++;/*T2 TIM_SlaveMode_Gatedtime_count0++;放在本函数后面//TIM2_init(7199,99);/// 1/10=0.01//TIM3_init(7199,99);/// 1/100=0.01count 1 /pwm 2 个脉冲count 2 /pwm 4 个脉冲count 3 /pwm 6 个脉冲count 4 /pwm 8 个脉冲//TIM2_init(7199,999);/// 1/10=0.1//TIM3_init(7199,99);/// 1/100=0.01count 1 /pwm 20 个脉冲count 2 /pwm 40 个脉冲count 3 /pwm 60 个脉冲count 4 /pwm ?个脉冲//TIM2_init(7199,99);/// 1/10=0.1//TIM3_init(7199,999);/// 1/100=0.01count 1 /pwm 2 个脉冲count 2 /pwm 4 个脉冲count 3 /pwm 6 个脉冲count 4 /pwm 8 个脉冲*/TIM_ClearITPendingBit(TIM2,TIM_IT_Trigger);}}void SetPWMValue(TIM_TypeDef* TIMx,unsigned char channel,unsigned int data){if(channel==1){TIM_SetCompare1(TIMx,data);}else if(channel==2){TIM_SetCompare2(TIMx,data);}else if(channel==3){TIM_SetCompare3(TIMx,data);}else if(channel==4){TIM_SetCompare4(TIMx,data);}}
以上为相关程序代码,其他的.h文件可自行添加 很简单

本文分享自微信公众号 - 嵌入式软硬件爱好者(qrsfan)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
来源:oschina
链接:https://my.oschina.net/u/3970493/blog/4528958