初级会员
- 积分
- 56
- 金钱
- 56
- 注册时间
- 2019-10-15
- 在线时间
- 14 小时
|
1金钱
A4988的引脚,EN,MS1,MS2,MS3,RESET,SLEEP 全部没有接,是悬空。 VMOT 12V,VDD 5V
- void GPIO_init()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIOA口
- //步进电机方向信号 PA4
- GPIO_InitStructure .GPIO_Pin =GPIO_Pin_4;
- GPIO_InitStructure .GPIO_Mode =GPIO_Mode_Out_PP ;//推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- void PWM_init()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE); //使能GPIOA口和AFIO复用功能模块时钟
-
- //设置该引脚为复用输出功能,输出TIM3 CH1的PWM脉冲波形 PA6
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //TIM_CH1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
-
- //定时器初始化
- TIM_TimeBaseStructure.TIM_Period = 2500; //定时周期 2.5ms PWM频率 400HZ
- TIM_TimeBaseStructure.TIM_Prescaler =72-1;// 频率为1MHZ
- TIM_TimeBaseStructure.TIM_ClockDivision =0;//设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up ;//向上计数
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
- //定时器PWM初始化
- TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;//选择PWM模式1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//输出极性高
- TIM_OC1Init(TIM3 ,&TIM_OCInitStructure);
- TIM_OC1PolarityConfig (TIM3,TIM_OCPreload_Enable );//使能预装载寄存器
- TIM_Cmd (TIM3,ENABLE ); //tim使能
- }
复制代码- int main()
- {
- delay_init();
- uart_init(115200);
- GPIO_init();
- PWM_init();
- TIM_SetCompare1(TIM3,2500-1250);
- while(1)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4);
- delay_ms(5000);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4) ;
- delay_ms(5000);
- }
- }
复制代码
运行后,电机一点反应也没有,求大佬帮忙看看程序有没有问题
A4988芯片的两个GND要共地吗?
|
|