In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to send and receive float type data in STM32". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In practical applications, we may need two devices to transmit floating-point data through serial ports:
In order to facilitate the demonstration, this note uses a serial port assistant to simulate one of the devices.
Let's create a common body for managing float type data:
Union float_data
{
Float f_data
Uint8_t byte [4]
}
The flow of data is as follows:
This time, the serial port assistant is used to simulate the sending equipment, omitting the first step, mainly looking at steps ② and ③.
Create two common volume variables for sending and receiving:
Union float_data rx_float_data, tx_float_data
How does STM32 configure the SD card and how does SDIO use it?
Send and receive related codes:
Swipe left and right to see all the code >
/ / official account: embedded hodgepodge
If (& huart3, rx_float_data.byte, 4, 1000) = = HAL_OK)
{
Printf ("rx_float_data.byte [0] =% .2x (addr:% # x)\ n", rx_float_data.byte [0], & rx_float_data.byte [0])
Printf ("rx_float_data.byte [1] =% .2x (addr:% # x)\ n", rx_float_data.byte [1], & rx_float_data.byte [1])
Printf ("rx_float_data.byte [2] =% .2x (addr:% # x)\ n", rx_float_data.byte [2], & rx_float_data.byte [2])
Printf ("rx_float_data.byte [3] =% .2x (addr:% # x)\ n", rx_float_data.byte [3], & rx_float_data.byte [3])
Printf ("rx_float_data.f_data =% f (addr:% # x)\ n", (float) rx_float_data.f_data, & rx_float_data.f_data)
Tx_float_data.f_data = rx_float_data.f_data + 1.0f
Printf ("tx_float_data.f_data =% f\ n", tx_float_data.f_data)
HAL_UART_Transmit & huart3, tx_float_data.byte, 4, 1000)
}
The code is very simple. UART3 receives the hexadecimal data corresponding to the float type sent by the serial port assistant and stores the hexadecimal data in rx_float_data.byte, and prints out each byte of rx_float_data.byte. At this time, rx_float_data.f_data is the floating-point data we receive. After adding 1.0, the received data is given to tx_float_data.f_data and then sent to tx_float_data.byte through UART3.
Running result:
Here, take transmission data 1.28 as an example, what is the hexadecimal data corresponding to 1.28?
We can look at it through some online websites, such as
Http://www.styb.cn/cms/ieee_754.php
STM32 configure SPI communication function
The corresponding hexadecimal number is: 3FA3D70A. Because STM32 is in small-end mode, it is necessary to send 0AD7A33F in reverse order when using serial helper to send. From the experimental results, we can see that:
After STM32 receives 1.28, it is sent back to the host computer through UART3 after adding 1.0, and 2.28 corresponds to 4011EB85:
Here we use the serial port assistant to simulate a device, so we need to check the hexadecimal data corresponding to the float data we want to send. In fact, there is no need to do this for transmission between two devices. You can send whatever you want:
This is the end of the content of "how to send and receive float type data in STM32". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.