#include "stm32f10x.h"\n\n// \u5B9A\u4E49\u5F15\u811A\u53F7\n#define SENSOR_PIN1 GPIO_Pin_0\n#define SENSOR_PIN2 GPIO_Pin_1\n#define SENSOR_PIN3 GPIO_Pin_2\n#define SENSOR_PIN4 GPIO_Pin_3\n#define SENSOR_PIN5 GPIO_Pin_4\n\n// \u5B9A\u4E49\u7535\u673A\u5F15\u811A\u53F7\n#define MOTOR_PIN1 GPIO_Pin_0\n#define MOTOR_PIN2 GPIO_Pin_1\n#define MOTOR_PIN3 GPIO_Pin_2\n#define MOTOR_PIN4 GPIO_Pin_3\n\n// \u5B9A\u4E49\u5FAA\u8FF9\u4F20\u611F\u5668\u7AEF\u53E3\n#define SENSOR_PORT GPIOA\n\n// \u5B9A\u4E49\u7535\u673A\u63A7\u5236\u7AEF\u53E3\n#define MOTOR_PORT GPIOB\n\n// \u521D\u59CB\u5316GPIO\nvoid GPIO_Configuration(void) {\n GPIO_InitTypeDef GPIO_InitStructure;\n\n // \u542F\u7528GPIOA\u548CGPIOB\u65F6\u949F\n RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);\n\n // \u914D\u7F6E\u5FAA\u8FF9\u4F20\u611F\u5668\u5F15\u811A\u4E3A\u8F93\u5165\n GPIO_InitStructure.GPIO_Pin = SENSOR_PIN1 | SENSOR_PIN2 | SENSOR_PIN3 | SENSOR_PIN4 | SENSOR_PIN5;\n GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;\n GPIO_Init(SENSOR_PORT, &GPIO_InitStructure);\n\n // \u914D\u7F6E\u7535\u673A\u63A7\u5236\u5F15\u811A\u4E3A\u8F93\u51FA\n GPIO_InitStructure.GPIO_Pin = MOTOR_PIN1 | MOTOR_PIN2 | MOTOR_PIN3 | MOTOR_PIN4;\n GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;\n GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;\n GPIO_Init(MOTOR_PORT, &GPIO_InitStructure);\n}\n\n// \u63A7\u5236\u7535\u673A\u8FD0\u52A8\nvoid Motor_Control(uint8_t state1, uint8_t state2, uint8_t state3, uint8_t state4) {\n GPIO_WriteBit(MOTOR_PORT, MOTOR_PIN1, (BitAction)state1);\n GPIO_WriteBit(MOTOR_PORT, MOTOR_PIN2, (BitAction)state2);\n GPIO_WriteBit(MOTOR_PORT, MOTOR_PIN3, (BitAction)state3);\n GPIO_WriteBit(MOTOR_PORT, MOTOR_PIN4, (BitAction)state4);\n}\n\n// \u4E3B\u51FD\u6570\nint main(void) {\n // \u521D\u59CB\u5316GPIO\n GPIO_Configuration();\n\n while (1) {\n // \u8BFB\u53D6\u5FAA\u8FF9\u4F20\u611F\u5668\u72B6\u6001\n uint8_t sensor1 = GPIO_ReadInputDataBit(SENSOR_PORT, SENSOR_PIN1);\n uint8_t sensor2 = GPIO_ReadInputDataBit(SENSOR_PORT, SENSOR_PIN2);\n uint8_t sensor3 = GPIO_ReadInputDataBit(SENSOR_PORT, SENSOR_PIN3);\n uint8_t sensor4 = GPIO_ReadInputDataBit(SENSOR_PORT, SENSOR_PIN4);\n uint8_t sensor5 = GPIO_ReadInputDataBit(SENSOR_PORT, SENSOR_PIN5);\n\n // \u6839\u636E\u4F20\u611F\u5668\u72B6\u6001\u63A7\u5236\u7535\u673A\u8FD0\u52A8\n if (sensor3 == 0) {\n // \u5FAA\u8FF9\u4F20\u611F\u56683\u5728\u9ED1\u7EBF\u4E0A\n Motor_Control(1, 0, 1, 0); // \u524D\u8FDB\n } else if (sensor2 == 0) {\n // \u5FAA\u8FF9\u4F20\u611F\u56682\u5728\u9ED1\u7EBF\u4E0A\n Motor_Control(1, 0, 0, 0); // \u5DE6\u8F6C\n } else if (sensor4 == 0) {\n // \u5FAA\u8FF9\u4F20\u611F\u56684\u5728\u9ED1\u7EBF\u4E0A\n Motor_Control(0, 0, 1, 0); // \u53F3\u8F6C\n } else if (sensor1 == 0) {\n // \u5FAA\u8FF9\u4F20\u611F\u56681\u5728\u9ED1\u7EBF\u4E0A\n Motor_Control(1, 1, 0, 0); // \u505C\u6B62\n } else if (sensor5 == 0) {\n // \u5FAA\u8FF9\u4F20\u611F\u56685\u5728\u9ED1\u7EBF\u4E0A\n Motor_Control(0, 0, 0, 0); // \u505C\u6B62\n }\n }\n}\n\n\u8FD9\u6BB5\u4EE3\u7801\u5047\u8BBE\u5FAA\u8FF9\u4F20\u611F\u5668\u7684\u5F15\u811A\u8FDE\u63A5\u5230\u4E86STM32\u7684GPIOA\u76840-4\u53F7\u5F15\u811A\uFF0C\u5E76\u4E14\u7535\u673A\u63A7\u5236\u5F15\u811A\u8FDE\u63A5\u5230\u4E86STM32\u7684GPIOB\u76840-3\u53F7\u5F15\u811A\uFF0C\u4F60\u53EF\u4EE5\u6839\u636E\u5B9E\u9645\u60C5\u51B5\u8FDB\u884C\u4FEE\u6539\u3002\u5FAA\u8FF9\u4F20\u611F\u5668\u68C0\u6D4B\u5230\u9ED1\u7EBF\u65F6\uFF0C\u5BF9\u5E94\u5F15\u811A\u7684\u503C\u4E3A0\uFF0C\u5426\u5219\u4E3A1\u3002\u6839\u636E\u4F20\u611F\u5668\u7684\u72B6\u6001\uFF0C\u63A7\u5236\u7535\u673A\u8FDB\u884C\u524D\u8FDB\u3001\u5DE6\u8F6C\u3001\u53F3\u8F6C\u6216\u505C\u6B62\u3002


原文地址: https://www.cveoy.top/t/topic/p4m6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录