PWM发脉冲,怎么精确控制发脉冲的个数呢

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

STM32的PWM发送脉冲,周期和脉宽都可调了,但是现在不知道如何精确的控制所发的脉冲个数。具体要求就是在一段时间内大概50ms内发送5000-1W个脉冲个数要很精确,误差2个以内可以接受该怎么控制呢?1.接上一个外部中断口,在中断中计数2.用一个定时器对发脉冲的时间进行控制各位大侠还有什么好的办法吗?求解啊!PWM溢出中断计数,是个不错的解决办法。如果一个上升沿算一个脉冲的话,控制周期(频率)不就行了开启PWM输出的溢出中断,进入一次中断,就是一个波原子哥是这里吗?设置这个寄存器或者只是简单的定时器溢出中断oidTIM3_IRQHandler(void){if(TIM3-SR&0X0001)//溢出中断{....//计数}TIM3-SR&=~(10);//清除中断标志位}看我们开发板:定时器中断例程。你好原子哥中断计数的话那怎么停止呢还有停止了还想让他循环发送呢有没有实现好的源程序呢我是STM32F103控制输出方波的脉冲数和周期好多人遇到这个问题,额!现在我用了两种方法实现,感觉都不好!方案1:定时器翻转IO,到达指定个数关闭TIM方案2:PWM,开启比较捕获中断,到达指定个数关闭TIM感觉都是频繁进中断,占用大量CPU资源。不能做其他事了voidTIM4_GPIO_Config(void){GPIO_InitTypeDefGPIO_InitStructure;/*TIM4clockenable*///PCLK1经过2倍频后作为TIM4的时钟源等于72MHzRCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);NVIC_Configuration(1);/*GPIOBclockenable*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);/*GPIOBConfiguration:TIM4channel1and2asalternatefunctionpush-pull*/GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);GPIO_ResetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7);}voidTim2_Slave_Init(void){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_DeInit(TIM2);RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);NVIC_Configuration(8);TIM_TimeBaseStructure.TIM_Period=XBUF[1]-1;TIM_TimeBaseStructure.TIM_Prescaler=0;//时钟预分频数TIM_TimeBaseStructure.TIM_ClockDivision=0;//采样分频TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上溢出TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);TIM_SelectInputTrigger(TIM2,TIM_TS_ITR3);//选择TIM2的输入触发源内部触发3TIM4TIM_InternalClockConfig(TIM2);TIM2-SMCR|=0x0007;//设定从模式控制寄存器,外部时钟模式1上升沿驱动计数//TIM_UpdateDisableConfig(TIM4,ENABLE);TIM_ARRPreloadConfig(TIM2,ENABLE);TIM_ClearFlag(TIM2,TIM_FLAG_Update);//清除溢出中断标志TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);TIM_Cmd(TIM2,ENABLE);//是否开启时钟(开启后每发送一个脉冲,定时器加一)}voidTim3_Slave_Init(void){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_DeInit(TIM3);RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);NVIC_Configuration(9);TIM_TimeBaseStructure.TIM_Period=XBUF[3]-1;TIM_TimeBaseStructure.TIM_Prescaler=0;//时钟预分频数TIM_TimeBaseStructure.TIM_ClockDivision=0;//采样分频TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上溢出TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);TIM_SelectInputTrigger(TIM3,TIM_TS_ITR3);//选择TIM2的输入触发源内部触发3TIM4TIM_InternalClockConfig(TIM3);TIM3-SMCR|=0x0007;//设定从模式控制寄存器,外部时钟模式1上升沿驱动计数//TIM_UpdateDisableConfig(TIM4,ENABLE);TIM_ARRPreloadConfig(TIM3,ENABLE);TIM_ClearFlag(TIM3,TIM_FLAG_Update);//清除溢出中断标志TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);TIM_Cmd(TIM3,ENABLE);//是否开启时钟(开启后每发送一个脉冲,定时器加一)}/**函数名:TIM4_Mode_Config*描述:配置TIM4输出的PWM信号的模式,如周期、极性、占空比*输入:无*输出:无*调用:内部调用*/voidTIM4_PWM_Init(u16T){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_OCInitTypeDefTIM_OCInitStructure;u16CCR1_Val=(T+1)/2;u16CCR2_Val=(T+1)/2;u16CCR3_Val=(T+1)/2;u16CCR4_Val=(T+1)/2;/*-----------------------------------------------------------------------TIM1Configuration:generate4PWMsignalswith4differentdutycycles:TIM1CLK=36MHz,Prescaler=0x0,TIM1counterclock=36MHzTIM1ARRRegister=999=TIM1Frequency=TIM1counterclock/(ARR+1)TIM1Frequency=36KHz.TIM1Channel1dutycycle=(TIM1_CCR1/TIM1_ARR)*100=50%TIM1Channel2dutycycle=(TIM1_CCR2/TIM1_ARR)*100=50%TIM1Channel3dutycycle=(TIM1_CCR3/TIM1_ARR)*100=50%TIM1Channel4dutycycle=(TIM1_CCR4/TIM1_ARR)*100=50%-----------------------------------------------------------------------*//*Timebaseconfiguration*/TIM_TimeBaseStructure.TIM_Period=T-1;//TusTIM_TimeBaseStructure.TIM_Prescaler=72-1;TIM_TimeBaseStructure.TIM_ClockDivision=0;TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);TIM4-CR1&=((u16)0x03FD);//UDISenable/*PWM1Modeconfiguration:Channel1*/TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse=CCR1_Val;TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;TIM_OC1Init(TIM4,&TIM_OCInitStructure);TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);/*PWM1Modeconfiguration:Channel2*/TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse=CCR2_Val;TIM_OC2Init(TIM4,&TIM_OCInitStructure);TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable);/*TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse=CCR3_Val;TIM_OC3Init(TIM4,&TIM_OCInitStructure);TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable);TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse=CCR4_Val;TIM_OC4Init(TIM4,&TIM_OCInitStructure);TIM_OC4PreloadConfig(TIM4,TIM_OCPreload_Enable);*/TIM_ARRPreloadConfig(TIM4,ENABLE);TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_OC1Ref);/*TIM1enablecounter*/TIM_Cmd(TIM4,ENABLE);/*MainOutputEnable*/TIM_CtrlPWMOutputs(TIM4,ENABLE);}voidTIM4_PWM_START(void){TIM4_GPIO_Config();Tim2_Slave_Init();Tim3_Slave_Init();TIM4_PWM_Init(XBUF[0]);}好像还有一种方法,就是利用定时器内部互联,一个定时器的给另一个定时器提供时钟,主从模式貌似,成功了!定时器内部互联,一个PWM输出脉冲给另一个定时提供时钟,每来一个脉冲,计数器值+1,当+到

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功