一、解释什么是“软件I2C”和“硬件I2C”?
I2C(Inter-Integrated Circuit)是一种用于在电子设备之间进行短距离通信的串行通信协议。软件I2C和硬件I2C是两种实现I2C通信的不同方式。
1.软件I2C
软件I2C是通过软件来模拟I2C通信协议的一种实现方式。在这种情况下,I2C通信的时序和数据传输由软件处理,而不是依赖硬件电路。这通常发生在一些微控制器或单片机没有专门的硬件支持I2C的情况下。
优点:
a.不依赖特定硬件支持,因此可以在不同平台上实现。
b.适用于资源有限的嵌入式系统。
缺点:
a.由于是软件模拟,通常速度较慢。
b.对于高速或实时性要求较高的应用可能不够理想。
2.硬件I2C
硬件I2C是通过专门的硬件电路来支持I2C通信协议的方式。现代的微控制器和一些集成电路通常都具有硬件I2C支持。
优点:
1.由硬件电路实现,因此通常速度较快。
2.更适合对实时性和性能有要求的应用。
缺点:
a.依赖特定硬件支持,可能不适用于所有平台。
b.对于一些资源有限的系统可能过于复杂。
二、阅读AHT20数据手册,编程实现:每隔2秒钟采集一次温湿度数据,并通过串口发送到上位机(win10)。
1、题目要求
每隔2秒钟采集一次温湿度数据,并通过串口发送到上位机。
2、前期准备
温湿度传感器AHT20
串口调试助手
3.配置
通过CubeMX配置好对应引脚以及串口,本次实验使用PB6模拟SCL,PB7模拟SDA.
4.代码
在奥松官网下载AHT20芯片代码,并进行修改
a.AHT20芯片的使用过程 read_AHT20_once函数
void read_AHT20_once(void)
{
delay_ms(10);
reset_AHT20();//重置AHT20芯片
delay_ms(10);
init_AHT20();//初始化AHT20芯片
delay_ms(10);
startMeasure_AHT20();//开始测试AHT20芯片
delay_ms(80);
read_AHT20();//读取AHT20采集的到的数据
delay_ms(5);
}
b.main函数修改
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include<stdio.h>
#include "AHT20-21_DEMO_V1_3.h"
void SystemClock_Config(void);
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFFFF);
//等待发送结束
while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=SET){
}
return ch;
}
int main(void)
{
/* USER CODE BEGIN 1 */
uint32_t CT_data[2]={0,0};
volatile int c1,t1;
Delay_1ms(500);
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
//初始化AHT20
AHT20_Init();
Delay_1ms(500);
while (1)
{
/* USER CODE END WHILE */
AHT20_Read_CTdata(CT_data); //不经过CRC校验,直接读取AHT20的温度和湿度数据 推荐每隔大于1S读一次
//AHT20_Read_CTdata_crc(CT_data); //crc校验后,读取AHT20的温度和湿度数据
c1 = CT_data[0]*1000/1024/1024; //计算得到湿度值c1(放大了10倍)
t1 = CT_data[1]*2000/1024/1024-500;//计算得到温度值t1(放大了10倍)
printf("正在检测");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
printf("\r\n");
HAL_Delay(1000);
printf("温度:%d%d.%d",t1/100,(t1/10)%10,t1%10);
printf("湿度:%d%d.%d",c1/100,(c1/10)%10,c1%10);
printf("\r\n");
printf("等待");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
HAL_Delay(100);
printf(".");
printf("\r\n");
HAL_Delay(1000);
/* 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 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
5.电路连接
本程序采用的软件I2C实现,GPIO引脚PB6,PB7。
SCL连接PB6,SDA连接PB7
6.编译无误后烧录运行
IIC协议采集温湿度
三、四、总结
I2C总线上允许连接多个微处理器以及各种外围设备,如存储器、LED及LCD驱动、A/D及D/A转换器等。为了保证数据可靠地传送,任一时刻总线只能由某一台主机控制,各微处理器应该在总线空闲时发送数据。
通过本次实验,详细了解到了I2C总线协议,以及AHT20温湿度传感器的使用,受益匪浅!