当前位置:网站首页>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
边栏推荐
- 限流组件设计实战
- Scale SVG to container without mask / crop
- Latest interface automation interview questions
- Redis高效点赞与取消功能
- POI exports excel and displays hierarchically according to parent-child nodes
- 调试定位导航遇到的问题总结
- STM32 - DS18B20 temperature sampling of first-line protocol
- If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?
- An article explaining the publisher subscriber model and the observer model
- Huawei operator level router configuration example | configuration static VPLS example
猜你喜欢
MySQL knowledge points
STM32——一线协议之DS18B20温度采样
Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
Introduction and basic knowledge of machine learning
MySQL index --01--- design principle of index
The best learning method in the world: Feynman learning method
Mysql知识点
[linear DP] longest common subsequence
随机推荐
The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey
So easy deploy program to server
How to determine the progress bar loaded in the loading interface when opening the game
Servlet [first introduction]
Network address translation (NAT) technology
Mouse over effect 9
通信协议——分类及其特征介绍
Mybati SQL statement printing
Lavaweb [first understanding the solution of subsequent problems]
How to open a stock account? Also, is it safe to open an account online?
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
Dart training and sphygmomanometer inflation pump power control DPC
Scale SVG to container without mask / crop
Introduction and installation of Solr
Design practice of current limiting components
HTB-Lame
xxl-job使用指南
【小程序项目开发-- 京东商城】uni-app之分类导航区域
Mouse over effect 10
EtherCAT简介