想用TIM2生成4路PWM波,看来看去不知道问题在哪。希望有人指点一下。(我是24K纯菜鸟)。
这是pwm.c设置
#include "pwms.h"
#include "usart.h"
#include"key.h"
//TIM14 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM2_PWM_Init(u32 arr,u32 psc)
{
//此部分需手动修改IO口设置
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM2时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //使能PORTF时钟
GPIO_PinAFConfig(GPIOF,GPIO_PinSource9|GPIO_PinSource10|GPIO_PinSource10|GPIO_PinSource8|GPIO_PinSource7,GPIO_AF_TIM2); //GPIOF9复用为定时器2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_7|GPIO_Pin_8; //GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOF,&GPIO_InitStructure); //初始化PF9,10,7,8;
TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//初始化定时器2
//初始化TIM14 Channel1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse=200;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 4OC1
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM2,ENABLE);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM2在CCR1上的预装载寄存器
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2,ENABLE);//ARPE使能
TIM_Cmd(TIM2, ENABLE); //使能TIM2
}
这是主函数
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "pwms.h"
#include"key.h"
int main(void)
{
u16 led0pwmval=20;
u16 led0pwmva2=20;
u16 led0pwmva3=20;
u16 led0pwmva4=20;
u8 key; //保存键值
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200);//初始化串口波特率为115200
TIM2_PWM_Init(500-1,84-1);//84M/84=1Mhz的计数频率,重装载值500,所以PWM频率为 1M/500=2Khz. //
KEY_Init(); //初始化与按键连接的硬件接口
while(1)
{
key=KEY_Scan(0); //得到键值
if(key)
{
switch(key)
{
case WKUP_PRES:
led0pwmval+=20;//PF9占空比增加;
break;
case KEY0_PRES:
led0pwmva2+=20;//PF10占空比增加;
break;
case KEY1_PRES:
led0pwmva3+=20; //PF8占空比增加;
break;
case KEY2_PRES:
led0pwmva4+=20; //PF7占空比增加;
break;
}
if(led0pwmval>300)led0pwmval=20;
if(led0pwmva2>300)led0pwmva2=20;
if(led0pwmva3>300)led0pwmva3=20;
if(led0pwmva4>300)led0pwmva4=20;
TIM_SetCompare1(TIM2,led0pwmval); //修改比较值,修改占空比
TIM_SetCompare2(TIM2,led0pwmva2); //修改比较值,修改占空比
TIM_SetCompare3(TIM2,led0pwmva3); //修改比较值,修改占空比
TIM_SetCompare4(TIM2,led0pwmva4); //修改比较值,修改占空比
}
}
}
我只做过,用一路定时器控制4路步进电机,不过我的占空比都是50%
一周热门 更多>