请写一串开发板代码,使之有以下功能:1 开发板上选三个LED,分别设置为 红灯、绿灯、黄灯(板子上都是红色,可以标记一下)其中,红灯10秒,绿灯15秒,黄闪5秒,往返循环 2 数码管显示当前信号灯的倒计时 3 USB转串口,接串口调试助手,可以实时输出:当前灯色+剩余时间
时间: 2024-01-01 07:06:32 浏览: 139
好的,以下是一个基于STM32的开发板代码,实现了你所要求的功能:
```c
#include "stm32f10x.h"
#include <stdio.h>
// 定义LED端口和引脚
#define RED_LED_PIN GPIO_Pin_0
#define GREEN_LED_PIN GPIO_Pin_1
#define YELLOW_LED_PIN GPIO_Pin_2
#define LED_PORT GPIOC
// 定义数码管引脚
#define DIGIT1_PIN GPIO_Pin_0
#define DIGIT2_PIN GPIO_Pin_1
#define DIGIT3_PIN GPIO_Pin_2
#define DIGIT4_PIN GPIO_Pin_3
#define DIGIT_PORT GPIOB
// 定义红绿黄灯的时间
#define RED_TIME 10
#define GREEN_TIME 15
#define YELLOW_TIME 5
// 定义状态枚举类型,表示当前灯的状态
typedef enum {
RED,
GREEN,
YELLOW
} LightState;
// 定义全局变量,表示当前灯的状态和剩余时间
LightState lightState = RED;
int remainingTime = RED_TIME;
// 定义函数,用于控制灯的状态
void setLightState(LightState state) {
switch (state) {
case RED:
GPIO_SetBits(LED_PORT, RED_LED_PIN);
GPIO_ResetBits(LED_PORT, GREEN_LED_PIN);
GPIO_ResetBits(LED_PORT, YELLOW_LED_PIN);
break;
case GREEN:
GPIO_ResetBits(LED_PORT, RED_LED_PIN);
GPIO_SetBits(LED_PORT, GREEN_LED_PIN);
GPIO_ResetBits(LED_PORT, YELLOW_LED_PIN);
break;
case YELLOW:
GPIO_ResetBits(LED_PORT, RED_LED_PIN);
GPIO_ResetBits(LED_PORT, GREEN_LED_PIN);
GPIO_SetBits(LED_PORT, YELLOW_LED_PIN);
break;
}
}
// 定义函数,用于控制数码管显示剩余时间
void setDigit(int digit) {
GPIO_ResetBits(DIGIT_PORT, DIGIT1_PIN | DIGIT2_PIN | DIGIT3_PIN | DIGIT4_PIN);
switch (digit) {
case 0:
GPIO_SetBits(DIGIT_PORT, DIGIT1_PIN | DIGIT2_PIN | DIGIT3_PIN);
break;
case 1:
GPIO_SetBits(DIGIT_PORT, DIGIT2_PIN | DIGIT3_PIN);
break;
case 2:
GPIO_SetBits(DIGIT_PORT, DIGIT1_PIN | DIGIT2_PIN | DIGIT4_PIN);
break;
case 3:
GPIO_SetBits(DIGIT_PORT, DIGIT2_PIN | DIGIT4_PIN);
break;
case 4:
GPIO_SetBits(DIGIT_PORT, DIGIT1_PIN | DIGIT3_PIN | DIGIT4_PIN);
break;
case 5:
GPIO_SetBits(DIGIT_PORT, DIGIT1_PIN | DIGIT4_PIN);
break;
case 6:
GPIO_SetBits(DIGIT_PORT, DIGIT1_PIN | DIGIT2_PIN | DIGIT3_PIN | DIGIT4_PIN);
break;
case 7:
GPIO_SetBits(DIGIT_PORT, DIGIT2_PIN | DIGIT3_PIN | DIGIT4_PIN);
break;
case 8:
GPIO_SetBits(DIGIT_PORT, DIGIT1_PIN | DIGIT2_PIN | DIGIT3_PIN | DIGIT4_PIN);
break;
case 9:
GPIO_SetBits(DIGIT_PORT, DIGIT1_PIN | DIGIT3_PIN | DIGIT4_PIN);
break;
}
}
// 定义函数,用于初始化GPIO口
void initGPIO() {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = RED_LED_PIN | GREEN_LED_PIN | YELLOW_LED_PIN;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DIGIT1_PIN | DIGIT2_PIN | DIGIT3_PIN | DIGIT4_PIN;
GPIO_Init(DIGIT_PORT, &GPIO_InitStructure);
GPIO_SetBits(LED_PORT, RED_LED_PIN);
GPIO_ResetBits(LED_PORT, GREEN_LED_PIN);
GPIO_ResetBits(LED_PORT, YELLOW_LED_PIN);
}
int main(void) {
initGPIO();
// 初始化串口
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
while (1) {
// 更新剩余时间
remainingTime--;
// 判断是否需要切换灯的状态
if (remainingTime == 0) {
switch (lightState) {
case RED:
lightState = GREEN;
remainingTime = GREEN_TIME;
break;
case GREEN:
lightState = YELLOW;
remainingTime = YELLOW_TIME;
break;
case YELLOW:
lightState = RED;
remainingTime = RED_TIME;
break;
}
setLightState(lightState);
}
// 计算数码管显示的数字
int digit = remainingTime % 10;
// 控制数码管显示数字和灯的状态
setDigit(digit);
char message[256];
sprintf(message, "%s %d\n", lightState == RED ? "RED" : lightState == GREEN ? "GREEN" : "YELLOW", remainingTime);
USART_SendData(USART1, message);
for (int i = 0; i < 1000000; i++); // 延时一段时间
}
}
```
需要注意的是,这段代码只是一个简单的示例,实际开发中可能需要根据硬件平台和具体需求进行修改。
阅读全文