2023/6/5 09:29:26 写一个基于STM32F103的4层电梯控制程序,要求用一位数码管显示楼层,两个led灯显示上下楼,绿灯表示上,红灯表示下,两个led都不亮表示电梯停止,代码加上注释
时间: 2024-03-05 09:47:49 浏览: 23
好的,以下是基于STM32F103的4层电梯控制程序,同时满足一位数码管显示楼层,两个LED灯显示上下楼的要求,代码中也加上了注释以方便理解:
```c
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
// 定义常量,分别表示4层楼的楼层号
#define FLOOR1 1
#define FLOOR2 2
#define FLOOR3 3
#define FLOOR4 4
// 定义常量,分别表示上下楼的状态
#define GO_UP 1
#define GO_DOWN 2
#define STOP 0
// 定义常量,分别表示数码管显示的数字
const uint8_t Num[10] = {
0x3f, // 0
0x06, // 1
0x5b, // 2
0x4f, // 3
0x66, // 4
0x6d, // 5
0x7d, // 6
0x07, // 7
0x7f, // 8
0x6f // 9
};
// 定义全局变量,表示当前楼层
uint8_t current_floor = FLOOR1;
// 定义全局变量,表示电梯的上下状态
uint8_t elevator_status = STOP;
// 函数声明
void LED_Init(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void SysTick_Init(void);
void delay_ms(uint16_t ms);
void display_floor(uint8_t floor);
int main(void)
{
GPIO_Configuration(); // GPIO初始化
LED_Init(); // LED初始化
NVIC_Configuration(); // NVIC初始化
SysTick_Init(); // SysTick初始化
while(1)
{
switch(current_floor)
{
case FLOOR1: // 当前在1楼
if(elevator_status == GO_UP) // 如果电梯要上楼
{
// 停留一段时间
delay_ms(1000);
// 到达2楼
current_floor = FLOOR2;
}
else if(elevator_status == GO_DOWN) // 如果电梯要下楼
{
// 亮红灯,表示向下
GPIO_SetBits(GPIOB, GPIO_Pin_0);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
// 停留一段时间
delay_ms(1000);
// 到达B1楼
current_floor = FLOOR4;
}
else // 如果电梯停止
{
// 两个灯都不亮
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
}
break;
case FLOOR2: // 当前在2楼
if(elevator_status == GO_UP) // 如果电梯要上楼
{
// 停留一段时间
delay_ms(1000);
// 到达3楼
current_floor = FLOOR3;
}
else if(elevator_status == GO_DOWN) // 如果电梯要下楼
{
// 亮红灯,表示向下
GPIO_SetBits(GPIOB, GPIO_Pin_0);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
// 停留一段时间
delay_ms(1000);
// 到达1楼
current_floor = FLOOR1;
}
else // 如果电梯停止
{
// 两个灯都不亮
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
}
break;
case FLOOR3: // 当前在3楼
if(elevator_status == GO_UP) // 如果电梯要上楼
{
// 停留一段时间
delay_ms(1000);
// 到达4楼
current_floor = FLOOR4;
}
else if(elevator_status == GO_DOWN) // 如果电梯要下楼
{
// 亮红灯,表示向下
GPIO_SetBits(GPIOB, GPIO_Pin_0);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
// 停留一段时间
delay_ms(1000);
// 到达2楼
current_floor = FLOOR2;
}
else // 如果电梯停止
{
// 两个灯都不亮
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
}
break;
case FLOOR4: // 当前在4楼
if(elevator_status == GO_UP) // 如果电梯要上楼
{
// 亮绿灯,表示向上
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
GPIO_SetBits(GPIOB, GPIO_Pin_1);
// 停留一段时间
delay_ms(1000);
// 到达B1楼
current_floor = FLOOR1;
}
else if(elevator_status == GO_DOWN) // 如果电梯要下楼
{
// 停留一段时间
delay_ms(1000);
// 到达3楼
current_floor = FLOOR3;
}
else // 如果电梯停止
{
// 两个灯都不亮
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
}
break;
}
// 数码管显示当前楼层
display_floor(current_floor);
}
}
// 初始化LED灯
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
// 初始化GPIO
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
// 初始化NVIC
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = SysTick_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
// 初始化SysTick
void SysTick_Init(void)
{
if(SysTick_Config(SystemCoreClock / 1000))
{
while(1);
}
}
// 延时函数,单位为毫秒
void delay_ms(uint16_t ms)
{
uint32_t i, j;
for(i = 0; i < ms; i++)
{
for(j = 0; j < 2000; j++);
}
}
// 数码管显示函数
void display_floor(uint8_t floor)
{
uint8_t i;
for(i = 0; i < 8; i++)
{
GPIO_ResetBits(GPIOA, 0x00ff); // 清除数据线
GPIO_SetBits(GPIOA, Num[floor % 10] << i); // 设置数据线
delay_ms(2);
}
}
// SysTick中断处理函数
void SysTick_Handler(void)
{
static uint8_t count = 0;
count++;
if(count == 500)
{
// 每500ms切换一次上下状态
if(elevator_status == GO_UP) // 如果当前是向上状态
{
elevator_status = GO_DOWN; // 切换为向下状态
}
else if(elevator_status == GO_DOWN) // 如果当前是向下状态
{
elevator_status = GO_UP; // 切换为向上状态
}
else // 如果电梯停止
{
elevator_status = GO_UP; // 默认切换为向上状态
}
count = 0;
}
}
```
希望这份代码对您有所帮助!
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)