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

STM32-I2C通讯-AHT20温湿度检测

非常感谢,提供的视频学习
https://www.bilibili.com/video/BV1QN411D7ak/?spm_id_from=333.788&vd_source=8ca4826038edd44bb618801808a5e076

该文章注意:串口显示中文会乱码,必须选用支持ASCII的串口助手,才能正常显示中文。

图形化界面配置好,串口,和I2C
在这里插入图片描述

在这里插入图片描述

首先,给每个外设,都生成.h和.c文件

在这里插入图片描述
在相应文件夹中,建立.h和.c文件

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
建立ATH20.C文件

#include "AHT20.h"#define AHT20_ADDRESS 0x70
//这里定义从机地址,由于AHT20从机为0x38。
//因为这个AHT20 ,从机地址为7位,所以需要左移一位,变成8位。
//所以,0x38,左移一位后,变成了0x70void AHT20_Int()
{uint8_t readBuffer;//用来存放,AHT20工作状态是否正常的数据。是否为0x38HAL_Delay(50);//由于模块启动,需要等待40msHAL_I2C_Master_Receive(&hi2c1, AHT20_ADDRESS, &readBuffer, 1, HAL_MAX_DELAY);//&&hi2c1,表示需要操作的i2c的端口。//AHT20_ADDRESS,表示需要操作的从机地址。因为是读取数据,这个函数,会将AHT20——ADDRESS变成0x71//&readBuffer,读取到的数据,需要放的地方。//1,表示,需要读取1位数据// HAL_MAX_DELAY,一直等待下去,直到接收完成。if((readBuffer & 0x08)==0x00)	//判断,AHT20,bit[3]是否为1。不为1,就进入程序进行初始化。{uint8_t sendBuffer[3]={0xBE,0x08,0x00};//需要定义这个数组,存放,初始化的数据。HAL_I2C_Master_Transmit(&hi2c1, AHT20_ADDRESS, sendBuffer,3, HAL_MAX_DELAY);//&&hi2c1,表示需要操作的i2c的端口。//AHT20_ADDRESS,表示需要操作的从机地址。//sendBuffe,需要发送的数据,读取到的数据,需要放的地方。//3,表示,需要发送3位数据// HAL_MAX_DELAY,一直等待下去,直到发送完成。}}void AHT20_Red(float *Temperature , float *Humidity)
{uint8_t sendBuffer[3]={0xAC,0x33,0x00};uint8_t readBuffer[6];HAL_I2C_Master_Transmit(&hi2c1, AHT20_ADDRESS, sendBuffer, 3, HAL_MAX_DELAY);//&hi2c1,需要操作I2c的设备// AHT20_ADDRESS.温度传感器的I2C地址//sendBuffer,需要发送的指令(发送后就可以获得数据)// 3,需要发送的数据长度//HAL_MAX_DELAY,一直等待发送完成HAL_Delay(100);//--------AHT20 接收到信息后,需要等待100ms,让其完成测量HAL_I2C_Master_Receive(&hi2c1, AHT20_ADDRESS, readBuffer, 6, HAL_MAX_DELAY);//&hi2c1.获取这个接口的数据。//AHT20_ADDRESS,需要获取的从机地址。//readBuffer,需要保存到的地址。//6,需要保存的数据长度。//HAL_MAX_DELAY,一直等待6个数据,保存完成。if((readBuffer[0] & 0x80)==0x00){uint32_t data=0;data=((uint32_t)readBuffer[3]>>4)+((uint32_t)readBuffer[2]<<4)+((uint32_t)readBuffer[1]<<12);*Humidity = data * 100.0f / (1<<20);data=(((uint32_t)readBuffer[3]& 0xf)<<16)+((uint32_t)readBuffer[4]<<8)+(uint32_t)readBuffer[5];*Temperature=data * 200.0f/(1<<20)-50;}
}

再建立,AHT20.h这个头文件。

#ifndef INC_AHT20_H_
#define INC_AHT20_H_#include "i2c.h"	//这个库文件,是这个软件自带的,所以直接引用就好。因为我们的程序种,会用到这个库函数里的内容。
void AHT20_Red(float *Temperature , float *Humidity);
void AHT20_Int();#endif /* INC_AHT20_H_ */

主程序

/* USER CODE BEGIN Header */
/********************************************************************************* @file           : main.c* @brief          : Main program body******************************************************************************* @attention** Copyright (c) 2023 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 "i2c.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "AHT20.h"
#include <stdio.h>
#include <string.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 */
//AHT20_Int();
/* 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();MX_I2C1_Init();MX_USART2_UART_Init();/* USER CODE BEGIN 2 */AHT20_Int();float temperature,humidity;char txt[50];/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){AHT20_Red(&temperature, &humidity);sprintf(txt,"温度:%.1f,湿度:%.1f %%",temperature,humidity);HAL_UART_Transmit(&huart2, (uint8_t*)txt, strlen(txt), HAL_MAX_DELAY);HAL_Delay(1000);//  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_4);//  HAL_Delay(500);/* 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};/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;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_HSI;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_0) != 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.lryc.cn/news/263584.html

相关文章:

  • 【机器学习】043_准确率、精确率、召回率
  • 【Qt开发流程】之文件目录、文件、输入和输出
  • CSS的基本选择器及高级选择器(附详细示例以及效果图)
  • 股票价格预测 | Python实现基于Stacked-LSTM的股票预测模型,可预测未来(keras)
  • 数据可视化---离群值展示
  • LeetCode Hot100 51.N皇后
  • 机器学习 | 贝叶斯方法
  • 缓存的定义及重要知识点
  • TrustZone之顶层软件架构
  • SpringBoot Whitelabel Error Page 报错--【已解决】
  • 02.Git常用基本操作
  • 黑盒测试中关键截图如何打点
  • 画图之C4架构图idea和vscode环境搭建篇
  • 安卓小练习-校园闲置交易APP(SQLite+SimpleCursorAdapter适配器)
  • Pycharm 如何更改成中文版| Python循环语句| for 和 else 的搭配使用
  • 智合同是怎么审合同的?
  • 使用Httpclient来替代客户端的jsonp跨域解决方案
  • 测试工具Jmeter:设置中文界面
  • K8s攻击案例:RBAC配置不当导致集群接管
  • 运行hive的beelin2时候going to print operations logs printed operations logs
  • 从 MySQL 到 DolphinDB,Debezium + Kafka 数据同步实战
  • 六.聚合函数
  • Eclipse_03_如何加快index速度
  • scrapy的入门和使用
  • yolov5单目测距+速度测量+目标跟踪(算法介绍和代码)
  • flink 读取 apache paimon表,查看source的延迟时间 消费堆积情况
  • 无人机在融合通信系统中的应用
  • MySQL库的操作
  • 服务器解析漏洞有哪些?IIS\APACHE\NGINX解析漏洞利用
  • Https图片链接下载问题