/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define DEBUG_LED_PIN GPIO_PIN_13
#define DEBUG_LED_PORT GPIOC
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t debugEnabled = 1; // 设置为1启用调试输出
uint8_t systemRunning = 0; // 系统运行状态标志
/* USER CODE END PV */
int checkStopRequest(void);
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void Traffic_Control(uint8_t);
void Seg_Control(uint8_t t);
void Relay_Control(void);
void North_South_Passage(void);
void NS_to_EW(void);
void East_West_Passage(void);
void EW_to_NS(void);
void Stepper_Close(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/**
* @brief The application entry point.
* @retval int
*/
int main(void) {
/* USER CODE BEGIN 1 */
// 用户代码区域
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
// 初始化代码
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
// 系统初始化
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
// 添加系统启动指示LED闪烁
for(int i = 0; i < 3; i++) {
HAL_GPIO_WritePin(DEBUG_LED_PORT, DEBUG_LED_PIN, GPIO_PIN_SET);
HAL_Delay(200);
HAL_GPIO_WritePin(DEBUG_LED_PORT, DEBUG_LED_PIN, GPIO_PIN_RESET);
HAL_Delay(200);
}
// 启动消息
if(debugEnabled) {
printf("\r\n===== Traffic Light System Started =====\r\n");
printf("Clock Frequency: %lu Hz\r\n", HAL_RCC_GetSysClockFreq());
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(1) {
// 检查K1按键状态 (PE3引脚)
if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3) == GPIO_PIN_RESET) {
// 防止按键抖动
HAL_Delay(50);
if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3) == GPIO_PIN_RESET) {
// 等待按键释放
while(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3) == GPIO_PIN_RESET);
// 设置循环标志
uint8_t running = 1;
uint8_t cycle_count = 0;
if(debugEnabled) {
printf("Traffic light system started...\r\n");
}
// 循环执行交通灯控制
while(running) {
cycle_count++;
// 执行交通灯循环
North_South_Passage();
NS_to_EW();
East_West_Passage();
EW_to_NS();
// 循环完成指示
if(debugEnabled) {
printf("Cycle %d completed.\r\n", cycle_count);
}
// 每次循环切换LED状态
HAL_GPIO_TogglePin(DEBUG_LED_PORT, DEBUG_LED_PIN);
// 检查是否要停止(再次按下K1)
if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3) == GPIO_PIN_RESET) {
// 防止按键抖动
HAL_Delay(50);
if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3) == GPIO_PIN_RESET) {
running = 0;
if(debugEnabled) {
printf("Traffic light system stopped after %d cycles.\r\n", cycle_count);
}
// 等待按键释放
while(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3) == GPIO_PIN_RESET);
}
}
}
}
}
else {
// 按键未按下时禁用步进电机
Stepper_Close();
}
}
/* USER CODE END WHILE */
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage */
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Activate the Over-Drive mode */
if(HAL_PWREx_EnableOverDrive() != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Error_Handler();
}
}
//交通灯配置控制
void Traffic_Control(uint8_t i) {
uint8_t j = 0;
uint32_t val = 0;
// 定义交通灯的配置组
uint8_t traffic_table[6][12] = {
//全亮
{1,1,1, //R,Y,G
1,1,1, //R,Y,G
1,1,1, //R,Y,G
1,1,1}, //R,Y,G
//南北通行
{0,0,1, //R,Y,G 上
1,0,0, //R,Y,G 左
0,0,1, //R,Y,G 下
1,0,0}, //R,Y,G 右
//南北转东西黄灯
{0,1,0, //R,Y,G
1,0,0, //R,Y,G
0,1,0, //R,Y,G
1,0,0}, //R,Y,G
//东西通行
{1,0,0, //R,Y,G
0,0,1, //R,Y,G
1,0,0, //R,Y,G
0,0,1}, //R,Y,G
//东西转南北黄灯
{1,0,0, //R,Y,G
0,1,0, //R,Y,G
1,0,0, //R,Y,G
0,1,0}, //R,Y,G
//全灭
{0,0,0, //R,Y,G
0,0,0, //R,Y,G
0,0,0, //R,Y,G
0,0,0} //R,Y,G
};
if(debugEnabled) {
printf("Setting traffic state: %d\r\n", i);
}
//控制线1
val &= ~0xFF;
for(j = 0; j < 8; j++) {
if(traffic_table[i][j]) {
val |= (1 << j);
} else {
val &= ~(1 << j);
}
}
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_14, GPIO_PIN_RESET);
GPIOG->ODR = val; //先控制前8个LED状态
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_14, GPIO_PIN_SET);
//控制线2
val &= ~0xF0;
for(j = 4; j < 8; j++) {
if(traffic_table[i][4 + j]) {
val |= (1 << j);
} else {
val &= ~(1 << j);
}
}
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_RESET);
GPIOG->ODR = val; //再控制后4个LED状态
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_SET);
}
//数码管共阳级码表
uint8_t seg7table[] = {
0xC0, // 0
0xF9, // 1
0xA4, // 2
0xB0, // 3
0x99, // 4
0x92, // 5
0x82, // 6
0xF8, // 7
0x80, // 8
0x90, // 9
0x88, // A
0x83, // B
0xC6, // C
0xA1, // D
0x86, // E
0x8E // F
};
//数码管控制
void Seg_Control(uint8_t t) {
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_13, GPIO_PIN_RESET);
GPIOG->ODR &= 0x00;
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_13, GPIO_PIN_SET);
HAL_Delay(5); // 减少延迟
if(t < 0x10) {
if(debugEnabled) {
printf("7-Segment Display: %d\r\n", t);
}
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_12, GPIO_PIN_RESET);
GPIOG->ODR = seg7table[t]; // 通过寄存器控制GPIOG 0-7输出高低电平
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_12, GPIO_PIN_SET);
HAL_Delay(5); // 减少延迟
}
}
//继电器的控制 - 优化版本
void Relay_Control(void)
{
//交替控制 PF14 引脚为高电平或低电平进行开关控制,中间加上一个短暂延时
HAL_GPIO_WritePin(GPIOF,GPIO_PIN_14,GPIO_PIN_SET);
HAL_Delay(50);
HAL_GPIO_WritePin(GPIOF,GPIO_PIN_14,GPIO_PIN_RESET);
HAL_Delay(50);
}
//指定交通路口亮灭控制
//南北通行
void North_South_Passage(void) {
int i;
Traffic_Control(1);
for(i = 9; i >= 0; i--) {
Seg_Control(i);
Relay_Control();
HAL_Delay(900);
}
}
//南北转东西
void NS_to_EW(void) {
int i;
Traffic_Control(2);
for(i = 3; i >= 0; i--) {
Seg_Control(i);
HAL_Delay(1000);
}
}
//东西通行
void East_West_Passage(void) {
int i;
Traffic_Control(3);
for(i = 9; i >= 0; i--) {
Seg_Control(i);
Relay_Control();
HAL_Delay(900);
}
}
//东西转南北
void EW_to_NS(void) {
int i;
Traffic_Control(4);
for(i = 3; i >= 0; i--) {
Seg_Control(i);
HAL_Delay(1000);
}
}
void Stepper_Close(void) { //控制进步电机ENA引脚,防止发烫及损坏
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_RESET);
GPIOG->ODR &= ~(GPIO_PIN_0); // 步进电机 ENA
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_SET);
printf("Stepper motor disabled\r\n");
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void) {
/* USER CODE BEGIN Error_Handler_Debug */
while(1) {
// 错误处理 - 快速闪烁LED
HAL_GPIO_WritePin(DEBUG_LED_PORT, DEBUG_LED_PIN, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(DEBUG_LED_PORT, DEBUG_LED_PIN, GPIO_PIN_RESET);
HAL_Delay(100);
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line) {
/* USER CODE BEGIN 6 */
printf("Assert failed: %s, line %lu\r\n", file, line);
while(1) {}
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
给以上代码写上注释便于理解
最新发布