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

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

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

图形化界面配置好,串口,和I2C
STM32-I2C通讯-AHT20温湿度检测_第1张图片

STM32-I2C通讯-AHT20温湿度检测_第2张图片

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

STM32-I2C通讯-AHT20温湿度检测_第3张图片
在相应文件夹中,建立.h和.c文件

STM32-I2C通讯-AHT20温湿度检测_第4张图片

STM32-I2C通讯-AHT20温湿度检测_第5张图片

STM32-I2C通讯-AHT20温湿度检测_第6张图片
STM32-I2C通讯-AHT20温湿度检测_第7张图片
建立ATH20.C文件



#include "AHT20.h"

#define AHT20_ADDRESS 0x70
//这里定义从机地址,由于AHT20从机为0x38。
//因为这个AHT20 ,从机地址为7位,所以需要左移一位,变成8位。
//所以,0x38,左移一位后,变成了0x70


void AHT20_Int()
{
	uint8_t readBuffer;//用来存放,AHT20工作状态是否正常的数据。是否为0x38
	HAL_Delay(50);//由于模块启动,需要等待40ms
	HAL_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 
#include 
/* 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 */

你可能感兴趣的:(嵌入式硬件,单片机,stm32)