当前位置: 首页 > article >正文

【蓝桥杯嵌入式】2_LED

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~PC15
	HAL_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.kler.cn/a/534220.html

相关文章:

  • DeepSeek大模型介绍、本地化部署与使用!【AI大模型】
  • 【NLP 20、Encoding编码 和 Embedding嵌入】
  • 简单本地部署deepseek(软件版)
  • 从 0 到 1 构建数仓之DWD层
  • 【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】2.22 多项式运算:从求根到拟合的数值方法
  • RabbitMQ深度探索:简单实现 MQ
  • 汽车加气站操作工试题及答案​
  • 前端组件标准化专家Prompt指令的最佳实践
  • VulnHub | Prime - 1
  • Ollama AI 开发助手完全指南:从入门到实践
  • C++常用拷贝和替换算法
  • FastAPI与Selenium:打造高效的Web数据抓取服务
  • 【Rancher】简化Kubernetes容器管理与部署的开源平台
  • AlwaysOn 可用性组副本所在服务器以及该副本上数据库的各项状态信息
  • kamailio-osp模块
  • 洛谷P2789 直线交点数
  • 除了 Python,还有哪些语言可以调用淘宝 API?
  • 深度学习系列--02.损失函数
  • k8m 是一款轻量级、跨平台的 Kubernetes 仪表板
  • RabbitMQ:python基础调用
  • DS图(中)(19)
  • 【分布式架构理论2】分布式架构要处理的问题及解决方案
  • 【自然语言处理(NLP)】Bahdanau 注意力(Bahdanau Attention)原理及代码实现
  • Day36-【13003】短文,数组的行主序方式,矩阵的压缩存储,对称、三角、稀疏矩阵和三元组线性表,广义表求长度、深度、表头、表尾等
  • 02、NodeJS学习笔记,第二节:express与中间件
  • Redis常见数据类型与编码方式