我在用PWM控制电调的时候,下面的程序编译没问题,但是下进芯片后还是无法驱动电调,不知为何。
main 函数
#include "bsp-led.h"
static void DMP_Delay ( __IO uint32_t nCount )
{
for ( ; nCount != 0; nCount -- );
}
unsigned int time=90;
int main(void)
{
TIM2_PWM_Init(1199,1199);
Motor_Control(60);
DMP_Delay(5000000);
DMP_Delay(5000000);
while(1)
{
time=+10;
if(time>=119)
{time=119;}
}
Motor_Control(time);
DMP_Delay (5000000);//
DMP_Delay (5000000);//
DMP_Delay (5000000);//
DMP_Delay (5000000);//
}
#include"bsp-led.h"
void Motor_Control(float one)
{
TIM2->CCR1=one;
}
.c函数
void TIM2_PWM_Init(u32 per,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period=per;
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse =1199;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //???????¨??
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_Cmd(TIM2,ENABLE);
}
.h函数
#ifndef _LED_H
#define _LED_H
#include"stm32f10x.h"
void TIM2_PWM_Init(u32 per,u32 psc);
void Motor_Control(float one);
#endif
转载
|