精準控制PWM脈沖的頻率和數量
在一些項目中,我們經常要控制PWM脈沖的頻率和數量,比如步進電機的控制等,下面分享一個程序是關於這方面的,程序的思想就是通過STM32的定時器來輸出PWM波,並開啟定時器中斷,在中斷裏面計數脈沖的數量,一旦脈沖數量達到了目標值,就關閉定時器,實現精確控制脈沖的數量。
下面是程序分析:
定時器4配置
#include"stm32f10x.h"
#include"pwm.h"
/*?¨ê±?÷4ê?3?4?·2?í?????±è??3?
ó2?tá??óè???
CH1--PB6
CH2--PB7
CH3--PB8
CH4--PB9
*/
void PWM_Init(u16 arr,u16 psc)
{
u16 val1=arr/5;
/*u16 val2=arr/4;
u16 val3=arr/6;
u16 val4=arr/8;*/
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//ê±?ó????
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 ,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO ,ENABLE);
//???ú3?ê??ˉ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//?¨ê±?÷4?ù±?????
TIM_TimeBaseInitStructure.TIM_Period=arr;
TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
TIM_TimeBaseInitStructure.TIM_ClockDivision=0;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
//PWM?£ê?????
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //?????¨ê±?÷?£ê?:TIM??3??í?èμ÷???£ê?1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è??ê?3?ê1?ü
TIM_OCInitStructure.TIM_Pulse = 0; //éè??′y×°è?2???±è????′??÷μ???3??μ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //ê?3???D?:TIMê?3?±è????D???
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //ê1?üTIMx?úCCR1é?μ??¤×°????′??÷
/*TIM_OCInitStructure.TIM_Pulse=val2;
TIM_OC2Init(TIM4,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable );
TIM_OCInitStructure.TIM_Pulse=val3;
TIM_OC3Init(TIM4,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable );
TIM_OCInitStructure.TIM_Pulse=val4;
TIM_OC4Init(TIM4,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4,TIM_OCPreload_Enable );*/
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
TIM_ITConfig(TIM4,TIM_IT_CC1,ENABLE);
//TIM_Cmd(TIM4,ENABLE);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
主函數:
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "pwm.h"
#include "usart.h"
u32 step=0;
int main(void)
{
SystemInit();
uart_init(115200);
delay_init();
LED_Init();
NVIC_Config();
PWM_Init(719,100);
TIM_Cmd(TIM4,ENABLE);
while(1)
{
// delay_ms(100);
// LED1=!LED1;
// delay_ms(100);
TIM4->CCR1 = 300;
}
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_CC1)!=RESET)
{
TIM_ClearITPendingBit(TIM4,TIM_IT_CC1);
step++;
printf("%d\r\n",step);
if(step==80000)
{
TIM_Cmd(TIM4,DISABLE);
}
}
}
以上就是完整程序。歡迎同學交流,改正。
精準控制PWM脈沖的頻率和數量