【蓝桥杯嵌入式】2_LED

embedded/2025/2/12 2:56:26/

全部代码网盘自取

链接:https://pan.baidu.com/s/1PX2NCQxnADxYBQx5CsOgPA?pwd=3ii2 
提取码:3ii2

1、电路图

74HC573是八位锁存器,当控制端LE脚为高电平时,芯片“导通”,LE为低电平时芯片“截止”即将输出状态“锁存”,led此时不会改变状态,所以可通过led对应的八个引脚的电平来控制led的状态,原理图分析可知,PC脚低电平时led导通。

将PC8-PC15和PD2设置成output,并选择Output Push Pull(推挽输出)

将PC8-PC15初始电平设置为高电平,PD2设置为低电平(高电平使能)

2、代码

2.1 led.c

#include "led.h"void LED_Disp(uchar dsLED)
{HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET);//将GPIO的所有引脚置为高电平HAL_GPIO_WritePin(GPIOC, dsLED<<8, GPIO_PIN_RESET);//控制PC8~PC15HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2,GPIO_PIN_SET);//将锁存器打开(高电平有效),高电平使能,PD2高电平的一瞬间数据就已经过去了HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2,GPIO_PIN_RESET);//将锁存器关闭,防止引脚冲突导致的误写
}

2.2 led.h

#ifndef _LED_H_
#define _LED_H_#include "main.h"
void LED_Disp(uchar dsLED);#endif

2.3 main.c

/* USER CODE BEGIN Header */
/********************************************************************************* @file           : main.c* @brief          : Main program body******************************************************************************* @attention** Copyright (c) 2024 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "led.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*** @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();/* USER CODE BEGIN 2 */uchar i = 0;LED_Disp(0x00);//LED ij ʼ  /* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){for(i = 0; i < 8; i++){LED_Disp(0x01<<i);HAL_Delay(100);}/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* 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_PWREx_ControlVoltageScaling(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 = RCC_PLLM_DIV3;RCC_OscInitStruct.PLL.PLLN = 20;RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != 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_DIV1;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* 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 *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* 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 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

http://www.ppmy.cn/embedded/161500.html

相关文章

0 CAD开源内核 Truck

Truck是一个基于Rust编写的开源CAD内核&#xff0c;专注于高性能、安全性和模块化设计&#xff0c;适用于寻求高效、可靠CAD解决方案的开发者和企业。开源地址&#xff1a;https://github.com/ricosjp/truck 一、Truck CAD 内核概述 项目背景&#xff1a; Truck是一个创新的C…

springboot 事务管理

在Spring Boot中&#xff0c;事务管理是通过Spring框架的事务管理模块来实现的。Spring提供了声明式事务管理和编程式事务管理两种方式。通常&#xff0c;我们使用声明式事务管理&#xff0c;因为它更简洁且易于维护。 1. 声明式事务管理 声明式事务管理是通过注解来实现的。…

软件工程-软件需求分析基础

基本任务 准确地回答“系统必须做什么&#xff1f;”&#xff0c;也就是对目标系统提出完整、准确、清晰、具体的要求 目标是&#xff0c;在分析阶段结束之前&#xff0c;系统分析员应该写出软件需求规格说明书&#xff0c;以书面形式准确地描述软件需求。 准则 1&#xff…

c++初始

目录 一数据类型 1. 2.sizeof 3.布尔 4.字符串类型 二.数据输入与输出 1.输出 2.输入 三.运算 1.加减乘除取模&#xff0c;&#xff0c;--都一样 2.逻辑非与或&#xff0c;与C语言一样 3.比较运算符&#xff0c;与C语言一样 4.三目运算符&#xff08;与C语言一样&a…

Thymeleaf在循环时设置递增序号

场景 在使用Thymeleaf进行遍历循环时&#xff0c;数据前面的序号如果使用实体类的id属性&#xff0c;id要是不从1开始递增则不能实现计数效果。 所以需要在循环时实现一个递增的序号效果。 如果没有显示设置状态变量&#xff0c;thymeleaf会默 认给个“变量名Stat"的状态…

从MySQL优化到脑力健康:技术人与效率的双重提升

文章目录 零&#xff1a;前言一&#xff1a;MySQL性能优化的核心知识点1. 索引优化的最佳实践实战案例&#xff1a; 2. 高并发事务的处理机制实战案例&#xff1a; 3. 查询性能调优实战案例&#xff1a; 4. 缓存与连接池的优化实战案例&#xff1a; 二&#xff1a;技术工作者的…

ASAP Utilities:Excel 插件中的高效助手

Excel 是职场必备工具&#xff0c;但你真的用好了吗&#xff1f; 几乎每个职场人都离不开Excel&#xff0c;但很多人可能只是简单地打开文件、输入数据&#xff0c;对于批量处理数据等高级操作却无从下手。今天为大家介绍一款强大的 Excel 插件——ASAP Utilities&#xff0c;它…

rebase和merge

rebase 和merge区别&#xff1a; rebase变基&#xff0c;改变基底&#xff1a;rebase会抹去提交记录。 git pull 默认merge&#xff0c;git pull --rebase 变基 rebase C、D提交属于feature分支&#xff0c;是基于master分支&#xff0c;在B提交额外拉出来的&#xff0c;当…