2019-07-15 10:24发布
人中狼 发表于 2017-6-20 22:44 最好能把觉得有问题的代码贴上来,下载附件的方式有时并不太方便,对于stm32引脚设置是否正确,也L298N的连接是否正确
人中狼 发表于 2017-6-21 08:13 编译没报错是代表没有语法错误,运行不正常,代表逻辑错误或时序错误,大部分bug都属于后者。把L298N部分的程序贴上来吧,L298N驱动直流电机不复杂,应该只是控制问题而已
最多设置5个标签!
编译的时候并没有报错,所以我就不是很清楚了
- #include "stm32f4xx.h"
- #include "led.h"
- #include "led1.h"
- #define MOTOR_L_IN1_LOW (GPIO_ResetBits(GPIOB, GPIO_Pin_6))
- #define MOTOR_L_IN1_HIGH (GPIO_SetBits(GPIOB, GPIO_Pin_6))
- #define MOTOR_L_IN2_LOW (GPIO_ResetBits(GPIOB, GPIO_Pin_5))
- #define MOTOR_L_IN2_HIGH (GPIO_SetBits(GPIOB, GPIO_Pin_5))
- #define MOTOR_R_IN1_LOW (GPIO_ResetBits(GPIOC, GPIO_Pin_11))
- #define MOTOR_R_IN1_HIGH (GPIO_SetBits(GPIOC, GPIO_Pin_11))
- /*C10引脚找不到
- #define MOTOR_R_IN2_LOW (GPIO_ResetBits(GPIOC, GPIO_Pin_10))
- #define MOTOR_R_IN2_HIGH (GPIO_SetBits(GPIOC, GPIO_Pin_10))
- 改用C7引脚*/
- #define MOTOR_R_IN2_LOW (GPIO_ResetBits(GPIOC, GPIO_Pin_7))
- #define MOTOR_R_IN2_HIGH (GPIO_SetBits(GPIOC, GPIO_Pin_7))
- //左轮初始化
-
- void MotorDriver_L_Config(void){
-
- GPIO_InitTypeDef GPIO_InitStructure ;
- TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- /*使能GPIOB、TIM1外设时钟*/
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
-
- /*初始化PB6端口为Out_PP模式*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- /*初始化PB8端口为Out_PP模式*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- /*初始化PA7端口(TIM4_CH2)为AF_PP模式推挽输出*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /*定时器基本配置time3 的 ch2通道*/
- TIM_BaseInitStructure.TIM_Prescaler = 3-1; //时钟预分频数3,TIM8的计数时钟频率为24MHz
- TIM_BaseInitStructure.TIM_Period = 1000-1; //自动重装载寄存器数值,PWM2频率为24MHz/1000=24KHz
- TIM_BaseInitStructure.TIM_ClockDivision = 0; //采样分频
- TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
- TIM_BaseInitStructure.TIM_RepetitionCounter = 0; //重复寄存器,是重复计数,就是重复溢出多少次才给你来一个溢出中断,
- TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
- TIM_OCStructInit(&TIM_OCInitStructure);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; /*PWM2 ch2输出模式PWM模式2- 在向上计数时,
- 一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为
- 有效电平;在向下计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为有效电平
- ,否则为无效电平。*/
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能该通道输出
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性
- TIM_OC4Init(TIM1, &TIM_OCInitStructure); //按指定参数初始化
- TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR上的预装载寄存器
- TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIM8在ARR上的预装载寄存器
-
- TIM_Cmd(TIM1, ENABLE); //使能TIM8
- TIM_CtrlPWMOutputs(TIM1, ENABLE); //使能TIM8,PWM //PWM输出使能
- }
- //右轮初始化
- void MotorDriver_R_Config(void){
- GPIO_InitTypeDef GPIO_InitStructure ;
- TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- /*使能GPIOc、TIM8外设时钟*/
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
- /*初始化PC10端口为Out_PP模式*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /*初始化PC11端口为Out_PP模式*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /*初始化PC9端口(TIM8_CH4)为AF_PP模式*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /*定时器基本配置time8 的 ch4通道*/
- TIM_BaseInitStructure.TIM_Prescaler = 3-1; //时钟预分频数psc=3,TIM8的计数时钟频率为24MHz
- TIM_BaseInitStructure.TIM_Period = 1000-1; //自动重装载寄存器数值arr=1000,PWM2频率为24MHz/1000=24KHz
- TIM_BaseInitStructure.TIM_ClockDivision = 0; //采样分频
- TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
- TIM_BaseInitStructure.TIM_RepetitionCounter = 0; //重复寄存器
- TIM_TimeBaseInit(TIM8, &TIM_BaseInitStructure);
- //TIM_OCStructInit(&TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM2 ch2输出模式
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能该通道输出
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性
- TIM_OC4Init(TIM8, &TIM_OCInitStructure); //按指定参数初始化
- TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR上的预装载寄存器
- TIM_ARRPreloadConfig(TIM8, ENABLE); //使能TIM8在ARR上的预装载寄存器
-
- TIM_Cmd(TIM8, ENABLE); //打开TIM8
- TIM_CtrlPWMOutputs(TIM8, ENABLE); //PWM输出使能
- }
- //LED对应IO初始化
- void LED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOG, ENABLE);//使能GPIOG时钟
- //PG13、PG14和PG15初始化设置
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4; //LED0、LED1和LED2对应IO口
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //普通输出模式
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- GPIO_Init(GPIOE, &GPIO_InitStructure); //初始化GPIO
-
- GPIO_SetBits(GPIOE, GPIO_Pin_3 | GPIO_Pin_4);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_Init(GPIOG, &GPIO_InitStructure);
- GPIO_SetBits(GPIOG, GPIO_Pin_9);
- }
- void MotorDriver_L_Turn_Forward(void){ //左轮电机正转
-
- MOTOR_L_IN1_LOW;
- MOTOR_L_IN2_HIGH;
- }
- void MotorDriver_L_Turn_Reverse(void){ //左轮电机反转
-
- MOTOR_L_IN1_HIGH;
- MOTOR_L_IN2_LOW;
- }
- void MotorDriver_R_Turn_Forward(void){ //右轮电机正转
-
- MOTOR_R_IN1_HIGH;
- MOTOR_R_IN2_LOW;
- }
- void MotorDriver_R_Turn_Reverse(void){ //右轮电机反转
-
- MOTOR_R_IN1_LOW;
- MOTOR_R_IN2_HIGH;
- }
- void MotorDriver_L_Turn_Stop(void) //左轮电机制动
- {
- MOTOR_L_IN1_HIGH;
- MOTOR_L_IN2_HIGH;
- }
- void MotorDriver_R_Turn_Stop(void) //右轮电机制动
- {
- MOTOR_R_IN1_HIGH;
- MOTOR_R_IN2_HIGH;
- }
- void MotorDriver_Init(void){
-
- MotorDriver_R_Config();
- MotorDriver_L_Config();
-
- MotorDriver_L_Turn_Stop(); //左轮电机制动
- MotorDriver_R_Turn_Stop(); //右轮电机制动
- }
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置系统中断优先级分组2
- delay_init(); //初始化延时函数
- LED_Init(); //初始化LED端口
- MotorDriver_L_Config();
- MotorDriver_R_Config();
-
- while(1)
- {
- LED0=0; //LED0亮
- LED1=1; //LED1灭
- LED2=1; //LED2灭
-
- delay_ms(500);
- LED0=1; //LED0灭
- LED1=0; //LED1亮
- LED2=1; //LED2灭
-
- delay_ms(500);
- LED0=1; //LED0灭
- LED1=1; //LED1灭
- LED2=0; //LED2亮
- delay_ms(500);
-
- MotorDriver_L_Turn_Reverse();
- delay_ms(1000);
- }
- }
复制代码刚放上去
一周热门 更多>