当前位置:网站首页>Hal library operation STM32 serial port
Hal library operation STM32 serial port
2022-07-01 03:07:00 【two thousand and twenty-one point zero nine】
This blog knowledge comes from Mr. Wei Dongshan's 7 Internet of things course .
One 、cubeMX Generate engineering framework
First select the serial port from the left 1, Then select asynchronous communication .

Two 、 analysis program
Here's the picture ,cubeMX The serial port initialization function is automatically generated .

3、 ... and 、 Programming
After the above initialization , You can use HAL Library provides the “ HAL_UART_Transmit()” Send data from serial port , Use “ HAL_UART_Receive()” receive data , But it is inconvenient to use this way , You need to handle the data types yourself . I'm learning C Language , Usually use printf Format and print the data , It's more convenient . therefore , Here you need to redirect the print function , In the use of printf Called when the “ HAL_UART_Transmit()” Print .
printf and scanf Will call... Separately “ fputc()” and “ fgetc()”, So here we override these two functions , Use HAL The function provided realizes sending and receiving data .
Put the following code into usart.c in , Remember to add header file #include <stdio.h>.
printf and scanf Will call... Separately “ fputc()” and “ fgetc()”, So here we override these two functions , Use HAL The function provided realizes sending and receiving data .
usart.c
#pragma import(__use_no_semihosting)
struct __FILE
{
int a;
};
FILE __stdout;
FILE __stdin;
void _sys_exit(int x)
{
}
/*****************************************************
*function: Write character file function
*param1: Output characters
*param2: The file pointer
*return: The output character of ASCII code
******************************************************/
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 10);
return ch;
}
/*****************************************************
*function: Read character file function
*param1: The file pointer
*return: Read characters ASCII code
******************************************************/
int fgetc(FILE *f)
{
uint8_t ch = 0;
HAL_UART_Receive(&huart1, (uint8_t*)&ch, 1, 10);
return (int)ch;
}Write the following code in the main function
#include <stdio.h>
printf(" Baiwen Technology www.100ask.net\n\r");
printf("UART experiment \n\r");
printf("test char = %c,%c\n\r", 'H', 'c');
printf("test string1 = %s\n\r", "www.100ask.net");
printf("test string2 = %s\n\r", " Shenzhen Baiwen Technology Co., Ltd ");
printf("test decimal1 number = %d\n\r", 123456);
printf("test decimal2 number = %d\n\r", -123456);
printf("test hex1 number = 0x%x\n\r", 0x123456);
printf("test hex2 number = 0x%08x\n\r", 0x123456);
printf("test float = %.5f\n\r", 3.1415);
printf("test double = %.10lf\n\r", 3.141592653);Four 、 Effect validation
open MobaXterm Follow these steps


The final result

边栏推荐
- Lavaweb [first understanding the solution of subsequent problems]
- 【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)
- 第03章_用戶與權限管理
- 【Qt】添加第三方库的知识补充
- PTA 1016
- Catch 222222
- Magnetic manometer and measurement of foreign coins
- How to open a stock account? Also, is it safe to open an account online?
- The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey
- UE4 rendering pipeline learning notes
猜你喜欢
![[applet project development -- Jingdong Mall] classified navigation area of uni app](/img/cb/b0b79444dc90980cd2220ff9e68549.png)
[applet project development -- Jingdong Mall] classified navigation area of uni app

Redis分布式锁的8大坑

POI exports excel and displays hierarchically according to parent-child nodes

别再说不会解决 “跨域“ 问题啦

彻底解决Lost connection to MySQL server at ‘reading initial communication packet

So easy deploy program to server

Latest interface automation interview questions

Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend

实战 ELK 优雅管理服务器日志

Design practice of current limiting components
随机推荐
Chapter 03_ User and authority management
UE4 rendering pipeline learning notes
CX5120控制汇川IS620N伺服报错E15解决方案
Share Creators萌芽人才培养计划来了!
Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
[machine learning] vectorized computing -- a must on the way of machine learning
Redis分布式锁的8大坑
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
Mouse over effect 8
最好用的信任关系自动化脚本(shell)
Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
限流组件设计实战
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Complete training and verification of a neural network based on pytorch
几行事务代码,让我赔了16万
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
Introduction to kubernetes resource objects and common commands (II)
Big orange crazy blog move notice
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip