找出错误#include "motor.h" #include "interface.h" #include "stm32f10x.h" void MotorGPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = FRONT_LEFT_F_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(FRONT_LEFT_F_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = FRONT_LEFT_B_PIN; GPIO_Init(FRONT_LEFT_B_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = FRONT_RIGHT_F_PIN; GPIO_Init(FRONT_RIGHT_F_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = FRONT_RIGHT_B_PIN; GPIO_Init(FRONT_RIGHT_B_GPIO, &GPIO_InitStructure); } void CarMove(void) { BEHIND_RIGHT_EN; if(front_right_speed_duty > 0) { if(speed_count < front_right_speed_duty) { FRONT_RIGHT_GO; }else { FRONT_RIGHT_STOP; } } else if(front_right_speed_duty < 0) { if(speed_count < (-1)*front_right_speed_duty) { FRONT_RIGHT_BACK; }else { FRONT_RIGHT_STOP; } } else { FRONT_RIGHT_STOP; } if(behind_left_speed_duty > 0) { if(speed_count < behind_left_speed_duty) { BEHIND_LEFT_GO; } else { BEHIND_LEFT_STOP; } } else if(behind_left_speed_duty < 0) { if(speed_count < (-1)*behind_left_speed_duty) { BEHIND_LEFT_BACK; } else { BEHIND_LEFT_STOP; } } else { BEHIND_LEFT_STOP; } void CarGo(void) { front_left_speed_duty=SPEED_DUTY; front_right_speed_duty=SPEED_DUTY; behind_left_speed_duty=SPEED_DUTY; behind_right_speed_duty=SPEED_DUTY; } void CarBack(void) { front_left_speed_duty=-SPEED_DUTY; front_right_speed_duty=-SPEED_DUTY; behind_left_speed_duty=-SPEED_DUTY; behind_right_speed_duty=-SPEED_DUTY; } void CarLeft(void) { front_left_speed_duty=-20; front_right_speed_duty=SPEED_DUTY; behind_left_speed_duty=-20; behind_right_speed_duty=SPEED_DUTY+10; } void CarRight(void) { front_left_speed_duty=SPEED_DUTY; front_right_speed_duty=-20; behind_left_speed_duty=SPEED_DUTY+10; behind_right_speed_duty=-20; } void CarStop(void) { front_left_speed_duty=0; front_right_speed_duty=0; behind_left_speed_duty=0; behind_right_speed_duty=0; } void MotorInit(void) { MotorGPIO_Configuration(); CarStop(); }
时间: 2023-08-02 17:05:50 浏览: 101
这段代码存在两个问题:
1. 函数 CarMove 中定义的函数体没有闭合大括号,应该在最后添加一个大括号。
2. 函数 CarLeft 和 CarRight 中设置电机速度的值有问题,应该修改为:
```
void CarLeft(void) {
front_left_speed_duty = -20;
front_right_speed_duty = SPEED_DUTY;
behind_left_speed_duty = -20;
behind_right_speed_duty = SPEED_DUTY + 10;
}
void CarRight(void) {
front_left_speed_duty = SPEED_DUTY;
front_right_speed_duty = -20;
behind_left_speed_duty = SPEED_DUTY + 10;
behind_right_speed_duty = -20;
}
```
这样才能正确控制电机的速度和方向。
相关问题
找出错误#include "motor.h" #include "interface.h" #include "stm32f10x.h" //GPIO配置函数 void MotorGPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = FRONT_LEFT_F_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz
这段代码的错误在于缺少了对 GPIO 端口的配置。应该在函数 MotorGPIO_Configuration 的开头添加如下代码:
```
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx, ENABLE);
```
其中,x 表示使用的 GPIO 端口,比如使用 GPIOA 端口,则应该写成:
```
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
```
这样才能正确地配置 GPIO 端口。
#include "motor.h" #include "infrared.h" int main(void) { Motor_Configuration(); Infrared_Configuration(); while (1) { if (Infrared_IsOnTrack(LEFT_SENSOR)) { Motor_LeftTurn(); } else if (Infrared_IsOnTrack(RIGHT_SENSOR)) { Motor_RightTurn(); } else { Motor_Straight(); } } }中motor.h和infrared.h具体代码
以下是 motor.h 和 infrared.h 中的代码示例:
motor.h:
```c
#ifndef __MOTOR_H
#define __MOTOR_H
#include "stm32f10x.h"
// 定义 GPIO 引脚
#define LEFT_MOTOR_FORWARD GPIO_SetBits(GPIOA, GPIO_Pin_1)
#define LEFT_MOTOR_BACKWARD GPIO_ResetBits(GPIOA, GPIO_Pin_1)
#define LEFT_MOTOR_STOP GPIO_ResetBits(GPIOA, GPIO_Pin_1)
#define RIGHT_MOTOR_FORWARD GPIO_SetBits(GPIOA, GPIO_Pin_2)
#define RIGHT_MOTOR_BACKWARD GPIO_ResetBits(GPIOA, GPIO_Pin_2)
#define RIGHT_MOTOR_STOP GPIO_ResetBits(GPIOA, GPIO_Pin_2)
// GPIO 配置函数
void Motor_Configuration(void);
void Motor_LeftTurn(void);
void Motor_RightTurn(void);
void Motor_Straight(void);
void Motor_Stop(void);
#endif /* __MOTOR_H */
```
infrared.h:
```c
#ifndef __INFRARED_H
#define __INFRARED_H
#include "stm32f10x.h"
// 定义 GPIO 引脚
#define LEFT_SENSOR GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0)
#define RIGHT_SENSOR GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)
// GPIO 配置函数
void Infrared_Configuration(void);
// 判断是否在黑线上
uint8_t Infrared_IsOnTrack(uint8_t sensor);
#endif /* __INFRARED_H */
```
其中,motor.h 中定义了一些 GPIO 引脚的宏定义,以及电机控制相关的函数。
infrared.h 中定义了一些 GPIO 引脚的宏定义,以及红外传感器控制相关的函数,包括 Infrared_Configuration() 函数和 Infrared_IsOnTrack() 函数。其中 Infrared_IsOnTrack() 函数用于判断红外传感器是否在黑线上。
阅读全文