stm32四层电梯控制系统仿真电梯上行led1亮,下行led2亮,两灯都熄灭时电梯停止电梯外每层一个按钮呼叫电梯,电梯内四个按钮对应四层控制电梯上下代码
时间: 2024-03-09 12:48:36 浏览: 24
以下是一个简单的四层电梯控制系统的代码:
```c
#include "stm32f10x.h"
#include "delay.h"
// 定义LED和按钮的GPIO口
#define LED1_GPIO GPIO_Pin_0
#define LED2_GPIO GPIO_Pin_1
#define BUTTON1_GPIO GPIO_Pin_2
#define BUTTON2_GPIO GPIO_Pin_3
#define BUTTON3_GPIO GPIO_Pin_4
#define BUTTON4_GPIO GPIO_Pin_5
// 定义电梯运行状态的枚举类型
typedef enum {
STOPPED,
GOING_UP,
GOING_DOWN
} ElevatorState;
ElevatorState elevatorState = STOPPED; // 初始化电梯状态为停止状态
int currentFloor = 1; // 初始化当前楼层为1层
// 初始化GPIO口
void initGPIO() {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能GPIOA时钟
GPIO_InitStructure.GPIO_Pin = LED1_GPIO | LED2_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = BUTTON1_GPIO | BUTTON2_GPIO | BUTTON3_GPIO | BUTTON4_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
// 控制LED灯的亮灭
void controlLED(int led1, int led2) {
if (led1) {
GPIO_SetBits(GPIOA, LED1_GPIO);
} else {
GPIO_ResetBits(GPIOA, LED1_GPIO);
}
if (led2) {
GPIO_SetBits(GPIOA, LED2_GPIO);
} else {
GPIO_ResetBits(GPIOA, LED2_GPIO);
}
}
// 检测按钮是否按下
int isButtonPressed(int button) {
if (GPIO_ReadInputDataBit(GPIOA, button) == 0) {
delay_ms(10); // 延迟10ms去抖动
if (GPIO_ReadInputDataBit(GPIOA, button) == 0) {
return 1;
}
}
return 0;
}
// 控制电梯上行
void goUp() {
elevatorState = GOING_UP;
controlLED(1, 0);
// 模拟电梯上行的运动
while (currentFloor < 4) {
delay_ms(1000);
currentFloor++;
}
elevatorState = STOPPED;
controlLED(0, 0);
}
// 控制电梯下行
void goDown() {
elevatorState = GOING_DOWN;
controlLED(0, 1);
// 模拟电梯下行的运动
while (currentFloor > 1) {
delay_ms(1000);
currentFloor--;
}
elevatorState = STOPPED;
controlLED(0, 0);
}
int main(void) {
initGPIO();
while (1) {
// 检测电梯内的按钮
if (isButtonPressed(BUTTON1_GPIO)) {
if (currentFloor < 2) {
goUp();
} else if (currentFloor > 2) {
goDown();
}
} else if (isButtonPressed(BUTTON2_GPIO)) {
if (currentFloor < 3) {
goUp();
} else if (currentFloor > 3) {
goDown();
}
} else if (isButtonPressed(BUTTON3_GPIO)) {
if (currentFloor < 4) {
goUp();
} else if (currentFloor > 4) {
goDown();
}
} else if (isButtonPressed(BUTTON4_GPIO)) {
if (currentFloor > 1) {
goDown();
}
}
// 检测电梯外的按钮
if (elevatorState == STOPPED) {
if (isButtonPressed(BUTTON1_GPIO)) {
goUp();
} else if (isButtonPressed(BUTTON4_GPIO)) {
goDown();
}
}
}
}
```
以上代码只是一个简单的示例,还有很多细节需要注意,比如怎么处理错误状态、怎么防止多次按键等等,仅供参考。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)