当前位置:网站首页>Teach you how to use Hal library to get started -- become a lighting master
Teach you how to use Hal library to get started -- become a lighting master
2022-07-01 00:12:00 【Please trust me】
List of articles
Preface
Thank you very much for your teaching and answers , It is highly recommended that students go to baiwen.com to learn embedded knowledge
Official website of baiwen.com
Basic knowledge of
The clock

- HSI: Internal high speed clock
- LSI: Internal low speed clock
- HSE: External high-speed clock
- LSE: External low speed clock
Because the internal clock is easily affected by the chip temperature , So the accuracy is not very good . Therefore, the external clock is generally used .
GPIO I / O mode of

Push pull output :
The push-pull structure consists of two MOS The tubes are connected in a complementary and symmetrical manner , At any time, one of the triodes is always on , The other triode is off . Such as ① Shown , The interior consists of a P-MOS Tube and a N-MOS Tube composition , Two MOS The grid of the tube ( Gate, G) Receive
Left “ Output control ” , Drain electrode ( Drain, D) Connected to external output , P-MOS The source of the tube ( Source, S) Receive VDD( 3.3V),N-MOS The source of the tube is connected to Vss( 0V).
MOS The tube is used as a switch ,“ Output control ” To two MOS Apply a certain voltage to the tube grid , P-MOS Conduction between the source and drain of the tube ,VDD after P-MOS Tubular S->G->D Output , N-MOS The tube is in a high resistance state ( The resistance is very high , Approximate open circuit ), The overall external is high level ;“ Output control ” Cancel to two MOS Apply voltage to the tube grid , P-MOS Tube source and grid cut-off ,P-MOS The tube is in a high resistance state , N-MOS The source and drain of the tube are connected , The overall external is low level .
Push pull mode , Give Way “ Output control ” Change into VDD/Vss Output , Increase the output current , The driving ability of the output pin is improved , Improve
The load capacity of the circuit and the action speed of the switch .Open drain output
In open drain mode ,“ Output control ” Will not control P-MOS tube , “ Output control ” Only to N-MOS Apply a certain voltage to the tube grid , Two
MOS All tubes are in the cut-off state , The two drains are suspended , It's called open drain .“ Output control ” Cancel the applied voltage of the grid , PMOS The tube is still in the high resistance state , N-MOS The pipe is open , The overall external is low level .
Multiplexing push pull output / Reuse open drain output
GPIO In addition to being a general-purpose input / The output pin uses an external pin , It can also be used as an on-chip peripheral ( USART、 I2C、 SPI etc. ) Dedicated pin , That is, one pin can be used for many purposes , But one pin can only use one of the multiplexing functions at the same time .
When the pin is set to the multiplexing function , The multiplexed push-pull output mode or multiplexed open drain output mode can be selected , Set to multiplex open drain output mode
when , It needs to be connected with pull resistance .Pull up input
Such as ② Shown , VDD Through the switch 、 Pull up resistance , Connect external I/O Pin . When the switch is closed , external I/O When there is no input signal , The default input is high .
The typical application of this mode is the external key , When no key is pressed , MCU The pin of is the determined high level , When the key is pressed , The pin level is pulled to low level .Drop down input
Such as ② Shown , Vss Through the switch 、 Pull down resistance , Connect external I/O Pin . When the switch is closed , external I/O When there is no input signal , The default input is low .Floating input
Such as ② Shown , Two up / The pull-down resistance switches are disconnected , Neither pull-up nor pull-down , I/O The pins are connected directly TTL Schottky trigger , here I/O The pin is floating , The reading level is uncertain , What level is the external signal , MCU The pin inputs what level . MCU
Reset after power on , The default is floating input mode .Analog input
Such as ② Shown , Two up / The pull-down resistance switches are disconnected , meanwhile TTL The Schottky trigger switch is also off , The pin signal is directly connected to the analog input , Realize the acquisition of external signals .
CubeMX To configure


First step , Set the clock 

The second step , To configure GPIO Pin
First, check the schematic diagram , find LED And the buttons 




Keil Programming


The main program 
main.c
/* USER CODE BEGIN Header */
/** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2022 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"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* 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);
static void MX_GPIO_Init(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();// System clock configuration
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init(); //GPIO initialization
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
while(1){
if(GPIO_PIN_RESET==HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)){
HAL_Delay(8);// Delay , Eliminate key jitter
if(GPIO_PIN_RESET==HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)){
// still 0, It means pressing
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);// Light on
}else{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);// The light goes out
}
}
}
}
/* USER CODE END 3 */
}
/** * @brief System Clock Configuration * @retval None */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {
0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {
0};
/** 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.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/** * @brief GPIO Initialization Function * @param None * @retval None */
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {
0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PA1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // Push pull output
GPIO_InitStruct.Pull = GPIO_PULLUP; // Pull up
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* 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 */
Experimental phenomena
No problem 
边栏推荐
- 5G智慧建筑解决方案2021
- Implementation of OSD on Hisilicon platform (1)
- Thoughts on the future of data analysis in "miscellaneous talk"
- 什么是SRM系统,如何规范公司内部采购流程
- 2022-2028 global carbon fiber room scraper system industry research and trend analysis report
- The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
- Understand target detection in one article: r-cnn, fast r-cnn, fast r-cnn, Yolo, SSD "suggestions collection"
- C /platform:anycpu32bitpererrored can only be used with /t:exe, /t:winexe and /t:appcontainerexe
- Which is better, server rental or hosting services in the United States?
- [PHP] self developed framework qphp, used by qphp framework
猜你喜欢
![[NLP] [textcnn] text classification](/img/bb/c8fd9f1ed458a88a17b8d5c70d9ce2.png)
[NLP] [textcnn] text classification

Error 2059 when Navicat connects to MySQL

The programmer's girlfriend gave me a fatigue driving test

Quick start of wechat applet -- project introduction

2022-2028 global rampant travel industry research and trend analysis report

Random ball size, random motion collision

5g smart building solution 2021

VR panorama adds contrast function to make the display of differentiation effect more intuitive!

SSM integration process (integration configuration, function module development, interface test)

Redis - cache penetration, cache breakdown, cache avalanche
随机推荐
When we look at the industrial Internet, we always look at it from the opposite of the consumer Internet
Design e-commerce seckill system
The programmer's girlfriend gave me a fatigue driving test
Never use redis expired monitoring to implement scheduled tasks!
女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
Error 2059 when Navicat connects to MySQL
CentOS 6.3 x64 PHP 5.2.6 扩展安装OpenSSL出错的解决方法
2022-2028 global rotary transmission system industry research and trend analysis report
composer
C language array interception, C string by array interception method (c/s)
五分钟搞懂探索式测试
The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?
Is it safe to choose mobile phone for stock trading account opening in Hangzhou?
Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
leetcode 474. Ones and zeroes (medium)
Fund sales code of conduct and information management
1. crawler's beautifulsoup parsing library & online parsing image verification code
What is the fastest way to import data from HDFS to Clickhouse? Spark is imported through JDBC or HDFS
Techo youth 2022 academic year college open class: behind the live broadcast of Lianmai, explore how to apply audio and video technology
Software engineering best practices - project requirements analysis