微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 定时器产生PWM彻底应用

STM32 定时器产生PWM彻底应用

时间:12-02 来源:互联网 点击:

AF_PP;

GPIO_Init(GPIOB,&GPIO_InitStructure);

}

管脚配置

void NVIC_Configuration(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM

NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);

#else

NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);

#endif

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

中断配置

void TIM_Configuration(void)

{

TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

TIM_InternalClockConfig(TIM3);

TIM_BaseInitStructure.TIM_Prescaler=3;//4分频,18M

TIM_BaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;

TIM_BaseInitStructure.TIM_Period=65535;

TIM_BaseInitStructure.TIM_ClockDivision=0;

TIM_BaseInitStructure.TIM_RepetitionCounter=0;

TIM_TimeBaseInit(TIM3,&TIM_BaseInitStructure);

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;

TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse=CCR1;

TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;

TIM_OC1Init(TIM3,&TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Disable);

TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;

TIM_OCInitStructure.TIM_Pulse=CCR2;

TIM_OC2Init(TIM3,&TIM_OCInitStructure);

TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Disable);

TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;

TIM_OCInitStructure.TIM_Pulse=CCR3;

TIM_OC3Init(TIM3,&TIM_OCInitStructure);

TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Disable);

TIM_ClearITPendingBit(TIM3,TIM_IT_CC3);

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;

TIM_OCInitStructure.TIM_Pulse=CCR4;

TIM_OC4Init(TIM3,&TIM_OCInitStructure);

TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Disable);

TIM_ClearITPendingBit(TIM3,TIM_IT_CC4);

TIM_Cmd(TIM3,ENABLE);

TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);

TIM_ITConfig(TIM3,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);

}

void TIM3_IRQHandler(void)

{

if(TIM_GetITStatus(TIM3,TIM_IT_CC1)!=RESET)

{

TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);

Cnt[0]=(~Cnt[0])&0x01;

if(Cnt[0]==0x01)

R[0]+=Rl[0];

else

R[0] += Rh[0];

if(R[0]>65535)

R[0]=R[0]-65535;

CCR1=R[0];

TIM_SetCompare1(TIM3,CCR1);

}

if(TIM_GetITStatus(TIM3,TIM_IT_CC2)!=RESET)

{

TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);

Cnt[1]=(~Cnt[1])&0x01;

if(Cnt[1]==0x01)

R[1]+=Rl[1];

else

R[1] += Rh[1];

if(R[1]>65535)

R[1]=R[1]-65535;

CCR2=R[1];

TIM_SetCompare2(TIM3,CCR2);

}

if(TIM_GetITStatus(TIM3,TIM_IT_CC3)!=RESET)

{

TIM_ClearITPendingBit(TIM3,TIM_IT_CC3);

Cnt[2]=(~Cnt[2])&0x01;

if(Cnt[2]==0x01)

R[2]+=Rl[2];

else

R[2] += Rh[2];

if(R[2]>65535)

R[2]=R[2]-65535;

CCR3=R[2];

TIM_SetCompare3(TIM3,CCR3);

}

if(TIM_GetITStatus(TIM3,TIM_IT_CC4)!=RESET)

{

TIM_ClearITPendingBit(TIM3,TIM_IT_CC4);

Cnt[3] = (~Cnt[3])&0x01;

if(Cnt[3]==0x01)

R[3]+=Rl[3];

else

R[3] += Rh[3];

if(R[3]>65535)

R[3]=R[3]-65535;

CCR4=R[3];

TIM_SetCompare4(TIM3,CCR4);

}

}

中断函数

其余就是按键扫描函数,通过改变周期数组中的值和占空比寄存器中的值就能改变PWM波的频率和占空比,当然按键可以设置为4个(一个按键对应一个通道),如果IO够用也可以设置8个,没两个按键对应一个通道分别改变频率和占空比。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top