如题,我利用pwm波产生两路的Pwm波去产生点击的驱动信号,并且进行调速,但是呢你发现有很多的问题,希望高手解答,谢谢!
首先说明下我是用的正点原子的开发板调试的,程序也是用的他们的程序改的,但是我感觉这个pwm 并没有按照我设定的调速,左右两个轮子并不能实现独立的改变占空比,如都是50%,或者一个50%一个40%然后一个递减,实现转弯之类的。代码如下所示:- #include "stm32f10x.h"
- #include "led.h"
- #include "delay.h"
- #include "timer.h"
- u16 pwmval=0;
- u16 pwmval1=0;
- u8 change=1;
- u8 change1=1;
- int main(void)
- {
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- LED_Init();
- delay_init();
- Timer3_Init(899,0,100,400);
- while(1)
- {
- if(change) pwmval++;
- else pwmval--;
- delay_ms(10);
- if(pwmval>200) change=0;
- if(pwmval==0) change=1; //pwmval×÷ÎaÕ¼¿Õ±è
-
- if(change1) pwmval1++;
- else pwmval1--;
- delay_ms(10);
- if(pwmval1>200) change1=0;
- if(pwmval1==0) change1=1;
- TIM_SetCompare1(TIM3,pwmval1);
- TIM_SetCompare2(TIM3,pwmval); //
- }
-
- }
复制代码 timer.c
- #include "timer.h"
- #include "led.h"
- void Timer3_Init(u16 arr, u16 psc,u16 CCR1,u16 CCR2)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; //定时器初始化结构体
- GPIO_InitTypeDef GPIO_InitStruct; //IO初始化结构体
- TIM_OCInitTypeDef TIM_OCInitStruct; //通道设置结构体
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //开启定时器2时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE); //开启IO口时钟和复用
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); //开启IO口
-
-
- TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseInitStruct.TIM_Period=arr;
- TIM_TimeBaseInitStruct.TIM_Prescaler=psc;
- TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
-
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct); //定时器初始化
-
-
-
- //TIM_Cmd(TIM3,ENABLE); //使能定时器2时钟
-
-
- /********************************************************************************8
- 一下为IO口的配置
-
- *********************************************************************************/
-
-
- //GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE); //设置定时器3重映射
-
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStruct); //PA.6初始化,对应于定时器3通道1
-
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5;
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStruct);
- //
- // GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
- // GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5;
- // GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- // GPIO_Init(GPIOE,&GPIO_InitStruct);
- /************************************************************************************
- 以下为通道相关设置
-
- ************************************************************************************/
- TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;
- TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High; //高电平
- TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStruct.TIM_Pulse=CCR2;
- TIM_OC2Init(TIM3,&TIM_OCInitStruct); //通道2初始化、
-
- TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
-
- TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;
- TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High; //高电平
- TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStruct.TIM_Pulse=CCR1;
- TIM_OC1Init(TIM3,&TIM_OCInitStruct); //通道1初始化、
-
- TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
- // TIM_CtrlPWMOutputs(TIM3,ENABLE);
- // TIM_ARRPreloadConfig(TIM3,ENABLE);
- TIM_Cmd(TIM3,ENABLE); //使能定时器3时钟
- }
复制代码
|