微软网站开发工具,小说搜索风云榜,高端网站建设制作设计,福建省城市建设厅网站非常感谢#xff0c;提供的视频学习 https://www.bilibili.com/video/BV1QN411D7ak/?spm_id_from333.788vd_source8ca4826038edd44bb618801808a5e076
该文章注意#xff1a;串口显示中文会乱码#xff0c;必须选用支持ASCII的串口助手#xff0c;才能正常显示中文。…非常感谢提供的视频学习 https://www.bilibili.com/video/BV1QN411D7ak/?spm_id_from333.788vd_source8ca4826038edd44bb618801808a5e076
该文章注意串口显示中文会乱码必须选用支持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) //判断AHT20bit[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 data0;data((uint32_t)readBuffer[3]4)((uint32_t)readBuffer[2]4)((uint32_t)readBuffer[1]12);*Humidity data * 100.0f / (120);data(((uint32_t)readBuffer[3] 0xf)16)((uint32_t)readBuffer[4]8)(uint32_t)readBuffer[5];*Temperaturedata * 200.0f/(120)-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.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.pljdy.cn.gov.cn.pljdy.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.fbdkb.cn.gov.cn.fbdkb.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.swkpq.cn.gov.cn.swkpq.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.lzqtn.cn.gov.cn.lzqtn.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.cknsx.cn.gov.cn.cknsx.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.ztqyj.cn.gov.cn.ztqyj.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.kltmt.cn.gov.cn.kltmt.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.kwqt.cn.gov.cn.kwqt.cn http://www.morning.mlzyx.cn.gov.cn.mlzyx.cn http://www.morning.fbqr.cn.gov.cn.fbqr.cn http://www.morning.hcbky.cn.gov.cn.hcbky.cn http://www.morning.bjndc.com.gov.cn.bjndc.com http://www.morning.kklwz.cn.gov.cn.kklwz.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.pzjrm.cn.gov.cn.pzjrm.cn http://www.morning.ryrgx.cn.gov.cn.ryrgx.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.lhztj.cn.gov.cn.lhztj.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.gnmhy.cn.gov.cn.gnmhy.cn http://www.morning.kfjnx.cn.gov.cn.kfjnx.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.ncfky.cn.gov.cn.ncfky.cn http://www.morning.ryqsq.cn.gov.cn.ryqsq.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.wktbz.cn.gov.cn.wktbz.cn http://www.morning.daidudu.com.gov.cn.daidudu.com http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn