|
如題,我利用pwm波產(chǎn)生兩路的Pwm波去產(chǎn)生點(diǎn)擊的驅(qū)動(dòng)信號(hào),并且進(jìn)行調(diào)速,但是呢你發(fā)現(xiàn)有很多的問(wèn)題,希望高手解答,謝謝!
首先說(shuō)明下我是用的正點(diǎn)原子的開(kāi)發(fā)板調(diào)試的,程序也是用的他們的程序改的,但是我感覺(jué)這個(gè)pwm 并沒(méi)有按照我設(shè)定的調(diào)速,左右兩個(gè)輪子并不能實(shí)現(xiàn)獨(dú)立的改變占空比,如都是50%,或者一個(gè)50%一個(gè)40%然后一個(gè)遞減,實(shí)現(xiàn)轉(zhuǎn)彎之類的。代碼如下所示:- #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); //
- }
-
- }
復(fù)制代碼 timer.c
- #include "timer.h"
- #include "led.h"
- void Timer3_Init(u16 arr, u16 psc,u16 CCR1,u16 CCR2)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; //定時(shí)器初始化結(jié)構(gòu)體
- GPIO_InitTypeDef GPIO_InitStruct; //IO初始化結(jié)構(gòu)體
- TIM_OCInitTypeDef TIM_OCInitStruct; //通道設(shè)置結(jié)構(gòu)體
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //開(kāi)啟定時(shí)器2時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE); //開(kāi)啟IO口時(shí)鐘和復(fù)用
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); //開(kāi)啟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); //定時(shí)器初始化
-
-
-
- //TIM_Cmd(TIM3,ENABLE); //使能定時(shí)器2時(shí)鐘
-
-
- /********************************************************************************8
- 一下為IO口的配置
-
- *********************************************************************************/
-
-
- //GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE); //設(shè)置定時(shí)器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初始化,對(duì)應(yīng)于定時(shí)器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);
- /************************************************************************************
- 以下為通道相關(guān)設(shè)置
-
- ************************************************************************************/
- 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); //使能定時(shí)器3時(shí)鐘
- }
復(fù)制代碼
|
|