[其他ST产品] 使用STM32F103完成基于I2C协议的AHT20温湿度传感器的数据采集

[复制链接]
1378|20
 楼主| 个百zz分点个 发表于 2023-1-31 20:37 | 显示全部楼层 |阅读模式
一、I2C协议
1、应用
I2C 通讯协议(Inter-Integrated Circuit)是由 Phiilps 公司开发的,由于它引脚少,硬件实现简单,可扩展性强,不需要 USART、CAN 等通讯协议的外部收发设备,现在被广泛地
使用在系统内多个集成电路(IC)间的通讯。

2、组成
最基本的是把它分为物理层和协议层。
物理层规定通讯系统中具有机械、电子功能部分的特性,确保原始数据在物理媒体的传输。
协议层主要规定通讯逻辑,统一收发双方的数据打包、解包标准。简单来说物理层规定我们用嘴巴还是用肢体来交流,协议层则规定我们用中文还是英文来交流。
- 物理层
I2C是一个支持设备的总线。可连接多个 I2C 通讯设备,支持多个通讯主机及多个通讯从机。对于I2C 总线,只使用两条总线线路,一条双向串行数据线(SDA) ,一条串行时钟线(SCL)。
- 协议层
I2C 的协议定义了通讯的起始和停止信号、数据有效性、响应、仲裁、时钟同步和地址广播等环节。
3、软件I2C和硬件I2C
3.1软件I2C
通过CPU 控制每个时刻的引脚状态来控制 GPIO 引脚电平产生通讯时序的方式称为软件I2C。

3.2硬件I2C
STM32 的 I2C 片上外设专门负责实现 I2C 通讯协议,只要配置好该外设,它就会自动根据协议要求产生通讯信号,收发数据并缓存起来,CPU只要检测该外设的状态和访问数据寄存器,就能完成数据收发。这种通过外设进行数据收发的方式称为硬件I2C。
————————————————
版权声明:本文为CSDN博主「m0_54384176」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_54384176/article/details/127923921

 楼主| 个百zz分点个 发表于 2023-1-31 20:37 | 显示全部楼层
差别
硬件 I2C 直接使用外设来控制引脚,可以减轻 CPU 的负担。不过使用硬件I2C 时必须使用某些固定的引脚作为 SCL 和 SDA,软件模拟 I2C 则可以使用任意 GPIO 引脚,相对比较灵活。对于硬件I2C用法比较复杂,软件I2C的流程更清楚一些。如果要详细了解I2C的协议,使用软件I2C可能更好的理解这个过程。在使用I2C过程,硬件I2C可能通信更加快,更加稳定。
 楼主| 个百zz分点个 发表于 2023-1-31 20:38 | 显示全部楼层
二、通过硬件I2C协议采集AHT20的数据
1、配置项目
默认你已经创建了一个新项目

时钟RCC配置,将HSE选为外部晶振模式
131463d90bcea1213.png
 楼主| 个百zz分点个 发表于 2023-1-31 20:38 | 显示全部楼层
SYS设置,选择Serial Wire模式。
d7f0cd4d28dd44708ce87ce697803890.png
 楼主| 个百zz分点个 发表于 2023-1-31 20:39 | 显示全部楼层
设置USART
07cdf8b32535402b89e5d86989db6281.png
 楼主| 个百zz分点个 发表于 2023-1-31 20:39 | 显示全部楼层
设置允许中断
9efc93e8222d4d0bbd06645d1185ef3d.png
 楼主| 个百zz分点个 发表于 2023-1-31 20:39 | 显示全部楼层
I2C设置 814263d90c1035c9a.png
 楼主| 个百zz分点个 发表于 2023-1-31 20:42 | 显示全部楼层
设置DMA模式
953bbc0f47624ac18bb5cd175b71601f.png
 楼主| 个百zz分点个 发表于 2023-1-31 20:42 | 显示全部楼层
RCC时钟
1a67a55b3bb240c58ea4320b40f30ff1.png
设置工程路径、工程名,最后导出文件。使用keil打开并进行编写。
 楼主| 个百zz分点个 发表于 2023-1-31 20:42 | 显示全部楼层

配置代码
新建一个文件夹,里面放置两个文件:
AHT20-21_DEMO_V1_3.c和AHT20-21_DEMO_V1_3.h

然后通过小方块创建一个工程文件夹,将刚刚的两个文件添加进来,后面需要进行修改.
0ad05798612841cfb6730fd1b646e3f3.png


 楼主| 个百zz分点个 发表于 2023-1-31 20:43 | 显示全部楼层
1843663d90ccec55e2.png 导入文件路径
复制前面新建的文件夹路径
 楼主| 个百zz分点个 发表于 2023-1-31 20:43 | 显示全部楼层
设置文件路径
c110d9c597f645d8bb284fb39a4a58ab.png
2efb745075f14cd9947d1d1aa6bd29ac.png
将刚刚文件路径放在最后步骤2那里,然后一致点击ok返回。
 楼主| 个百zz分点个 发表于 2023-1-31 20:43 | 显示全部楼层
因为后面重写了prntf函数,需要勾选允许微库。
197c628cbb504abe9ce288fa07b58e52.png
 楼主| 个百zz分点个 发表于 2023-1-31 20:44 | 显示全部楼层
修改AHT20-21_DEMO_V1_3.h文件
 楼主| 个百zz分点个 发表于 2023-1-31 20:44 | 显示全部楼层
  1. #ifndef _AHT20_DEMO_
  2. #define _AHT20_DEMO_

  3. #include "main.h"  

  4. void Delay_N10us(uint32_t t);//延时函数
  5. void SensorDelay_us(uint32_t t);//延时函数
  6. void Delay_4us(void);                //延时函数
  7. void Delay_5us(void);                //延时函数
  8. void Delay_1ms(uint32_t t);       
  9. void AHT20_Clock_Init(void);                //延时函数
  10. void SDA_Pin_Output_High(void)  ; //将PB15配置为输出 , 并设置为高电平, PB15作为I2C的SDA
  11. void SDA_Pin_Output_Low(void);  //将P15配置为输出  并设置为低电平
  12. void SDA_Pin_IN_FLOATING(void);  //SDA配置为浮空输入
  13. void SCL_Pin_Output_High(void); //SCL输出高电平,P14作为I2C的SCL
  14. void SCL_Pin_Output_Low(void); //SCL输出低电平
  15. void Init_I2C_Sensor_Port(void); //初始化I2C接口,输出为高电平
  16. void I2C_Start(void);                 //I2C主机发送START信号
  17. void AHT20_WR_Byte(uint8_t Byte); //往AHT20写一个字节
  18. uint8_t AHT20_RD_Byte(void);//从AHT20读取一个字节
  19. uint8_t Receive_ACK(void);   //看AHT20是否有回复ACK
  20. void Send_ACK(void)        ;          //主机回复ACK信号
  21. void Send_NOT_ACK(void);        //主机不回复ACK
  22. void Stop_I2C(void);          //一条协议结束
  23. uint8_t AHT20_Read_Status(void);//读取AHT20的状态寄存器
  24. uint8_t AHT20_Read_Cal_Enable(void);  //查询cal enable位有没有使能
  25. void AHT20_SendAC(void); //向AHT20发送AC命令
  26. uint8_t Calc_CRC8(uint8_t *message,uint8_t Num);
  27. void AHT20_Read_CTdata(uint32_t *ct); //没有CRC校验,直接读取AHT20的温度和湿度数据
  28. void AHT20_Read_CTdata_crc(uint32_t *ct); //CRC校验后,读取AHT20的温度和湿度数据
  29. void AHT20_Init(void);   //初始化AHT20
  30. void JH_Reset_REG(uint8_t addr);///重置寄存器
  31. void AHT20_Start_Init(void);///上电初始化进入正常测量状态
  32. #endif

 楼主| 个百zz分点个 发表于 2023-1-31 20:44 | 显示全部楼层
修改AHT20-21_DEMO_V1_3.c文件
  1. /*******************************************/
  2. /*@版权所有:广州奥松电子有限公司          */
  3. /*@作者:温湿度传感器事业部                */
  4. /*@版本:V1.2                              */
  5. /*******************************************/
  6. //#include "main.h"
  7. #include "AHT20-21_DEMO_V1_3.h"
  8. #include "gpio.h"
  9. #include "i2c.h"


  10. void Delay_N10us(uint32_t t)//延时函数
  11. {
  12.   uint32_t k;

  13.    while(t--)
  14.   {
  15.     for (k = 0; k < 2; k++);//110
  16.   }
  17. }

  18. void SensorDelay_us(uint32_t t)//延时函数
  19. {
  20.                
  21.         for(t = t-2; t>0; t--)
  22.         {
  23.                 Delay_N10us(1);
  24.         }
  25. }

  26. void Delay_4us(void)                //延时函数
  27. {       
  28.         Delay_N10us(1);
  29.         Delay_N10us(1);
  30.         Delay_N10us(1);
  31.         Delay_N10us(1);
  32. }
  33. void Delay_5us(void)                //延时函数
  34. {       
  35.         Delay_N10us(1);
  36.         Delay_N10us(1);
  37.         Delay_N10us(1);
  38.         Delay_N10us(1);
  39.         Delay_N10us(1);

  40. }

  41. void Delay_1ms(uint32_t t)                //延时函数
  42. {
  43.    while(t--)
  44.   {
  45.     SensorDelay_us(1000);//延时1ms
  46.   }
  47. }


  48. //void AHT20_Clock_Init(void)                //延时函数
  49. //{
  50. //        RCC_APB2PeriphClockCmd(CC_APB2Periph_GPIOB,ENABLE);
  51. //}

  52. void SDA_Pin_Output_High(void)   //将PB7配置为输出 , 并设置为高电平, PB7作为I2C的SDA
  53. {
  54.         GPIO_InitTypeDef  GPIO_InitStruct;
  55.         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//推挽输出
  56.         GPIO_InitStruct.Pin = GPIO_PIN_7;
  57.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  58.         HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
  59.         HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET);
  60. }

  61. void SDA_Pin_Output_Low(void)  //将P7配置为输出  并设置为低电平
  62. {
  63.         GPIO_InitTypeDef  GPIO_InitStruct;
  64.         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//推挽输出
  65.         GPIO_InitStruct.Pin = GPIO_PIN_7;
  66.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  67.         HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
  68.         HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_RESET);
  69. }

  70. void SDA_Pin_IN_FLOATING(void)  //SDA配置为浮空输入
  71. {
  72.         GPIO_InitTypeDef  GPIO_InitStruct;
  73.         GPIO_InitStruct.Mode = GPIO_MODE_INPUT;//浮空
  74.         GPIO_InitStruct.Pin = GPIO_PIN_7;
  75.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  76.         HAL_GPIO_Init( GPIOB,&GPIO_InitStruct);
  77. }


  78. void SCL_Pin_Output_High(void) //SCL输出高电平,P14作为I2C的SCL
  79. {
  80.         HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_SET);
  81. }

  82. void SCL_Pin_Output_Low(void) //SCL输出低电平
  83. {
  84.         HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
  85. }

  86. void Init_I2C_Sensor_Port(void) //初始化I2C接口,输出为高电平
  87. {       
  88.         GPIO_InitTypeDef  GPIO_InitStruct;
  89.         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//推挽输出
  90.         GPIO_InitStruct.Pin = GPIO_PIN_7;
  91.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  92.         HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
  93.         HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET);

  94.        
  95.         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//推挽输出
  96.         GPIO_InitStruct.Pin = GPIO_PIN_6;
  97.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  98.         HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
  99.         HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET);
  100.        
  101. }
  102. void I2C_Start(void)                 //I2C主机发送START信号
  103. {
  104.         SDA_Pin_Output_High();
  105.         SensorDelay_us(8);
  106.         SCL_Pin_Output_High();
  107.         SensorDelay_us(8);
  108.         SDA_Pin_Output_Low();
  109.         SensorDelay_us(8);
  110.         SCL_Pin_Output_Low();
  111.         SensorDelay_us(8);   
  112. }


  113. void AHT20_WR_Byte(uint8_t Byte) //往AHT20写一个字节
  114. {
  115.         uint8_t Data,N,i;       
  116.         Data=Byte;
  117.         i = 0x80;
  118.         for(N=0;N<8;N++)
  119.         {
  120.                 SCL_Pin_Output_Low();
  121.                 Delay_4us();       
  122.                 if(i&Data)
  123.                 {
  124.                         SDA_Pin_Output_High();
  125.                 }
  126.                 else
  127.                 {
  128.                         SDA_Pin_Output_Low();
  129.                 }       
  130.                        
  131.     SCL_Pin_Output_High();
  132.                 Delay_4us();
  133.                 Data <<= 1;
  134.                  
  135.         }
  136.         SCL_Pin_Output_Low();
  137.         SensorDelay_us(8);   
  138.         SDA_Pin_IN_FLOATING();
  139.         SensorDelay_us(8);       
  140. }       


  141. uint8_t AHT20_RD_Byte(void)//从AHT20读取一个字节
  142. {
  143.                 uint8_t Byte,i,a;
  144.         Byte = 0;
  145.         SCL_Pin_Output_Low();
  146.        
  147.         SDA_Pin_IN_FLOATING();
  148.         SensorDelay_us(8);       
  149.        
  150.         for(i=0;i<8;i++)
  151.         {
  152.     SCL_Pin_Output_High();
  153.                
  154.                 Delay_5us();
  155.                 a=0;
  156.                
  157.                 //if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)) a=1;
  158.                 if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7)) a=1;
  159.                 Byte = (Byte<<1)|a;
  160.                
  161.                 //SCL_Pin_Output_Low();
  162.                 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
  163.                 Delay_5us();
  164.         }
  165.   SDA_Pin_IN_FLOATING();
  166.         SensorDelay_us(8);       
  167.         return Byte;
  168. }


  169. uint8_t Receive_ACK(void)   //看AHT20是否有回复ACK
  170. {
  171.         uint16_t CNT;
  172.         CNT = 0;
  173.         SCL_Pin_Output_Low();       
  174.         SDA_Pin_IN_FLOATING();
  175.         SensorDelay_us(8);       
  176.         SCL_Pin_Output_High();       
  177.         SensorDelay_us(8);       
  178.         while((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7))  && CNT < 100)
  179.         CNT++;
  180.         if(CNT == 100)
  181.         {
  182.                 return 0;
  183.         }
  184.         SCL_Pin_Output_Low();       
  185.         SensorDelay_us(8);       
  186.         return 1;
  187. }

  188. void Send_ACK(void)                  //主机回复ACK信号
  189. {
  190.         SCL_Pin_Output_Low();       
  191.         SensorDelay_us(8);       
  192.         SDA_Pin_Output_Low();
  193.         SensorDelay_us(8);       
  194.         SCL_Pin_Output_High();       
  195.         SensorDelay_us(8);
  196.         SCL_Pin_Output_Low();       
  197.         SensorDelay_us(8);
  198.         SDA_Pin_IN_FLOATING();
  199.         SensorDelay_us(8);
  200. }

  201. void Send_NOT_ACK(void)        //主机不回复ACK
  202. {
  203.         SCL_Pin_Output_Low();       
  204.         SensorDelay_us(8);
  205.         SDA_Pin_Output_High();
  206.         SensorDelay_us(8);
  207.         SCL_Pin_Output_High();       
  208.         SensorDelay_us(8);               
  209.         SCL_Pin_Output_Low();       
  210.         SensorDelay_us(8);
  211.     SDA_Pin_Output_Low();
  212.         SensorDelay_us(8);
  213. }

  214. void Stop_I2C(void)          //一条协议结束
  215. {
  216.         SDA_Pin_Output_Low();
  217.         SensorDelay_us(8);
  218.         SCL_Pin_Output_High();       
  219.         SensorDelay_us(8);
  220.         SDA_Pin_Output_High();
  221.         SensorDelay_us(8);
  222. }

  223. uint8_t AHT20_Read_Status(void)//读取AHT20的状态寄存器
  224. {

  225.         uint8_t Byte_first;       
  226.         I2C_Start();
  227.         AHT20_WR_Byte(0x71);
  228.         Receive_ACK();
  229.         Byte_first = AHT20_RD_Byte();
  230.         Send_NOT_ACK();
  231.         Stop_I2C();
  232.         return Byte_first;
  233. }

  234. uint8_t AHT20_Read_Cal_Enable(void)  //查询cal enable位有没有使能
  235. {
  236.         uint8_t val = 0;//ret = 0,
  237.   val = AHT20_Read_Status();
  238.          if((val & 0x68)==0x08)
  239.                  return 1;
  240.    else  return 0;
  241. }

  242. void AHT20_SendAC(void) //向AHT20发送AC命令
  243. {

  244.         I2C_Start();
  245.         AHT20_WR_Byte(0x70);
  246.         Receive_ACK();
  247.         AHT20_WR_Byte(0xac);//0xAC采集命令
  248.         Receive_ACK();
  249.         AHT20_WR_Byte(0x33);
  250.         Receive_ACK();
  251.         AHT20_WR_Byte(0x00);
  252.         Receive_ACK();
  253.         Stop_I2C();

  254. }

  255. //CRC校验类型:CRC8/MAXIM
  256. //多项式:X8+X5+X4+1
  257. //Poly:0011 0001  0x31
  258. //高位放到后面就变成 1000 1100 0x8c
  259. //C现实代码:
  260. uint8_t Calc_CRC8(uint8_t *message,uint8_t Num)
  261. {
  262.         uint8_t i;
  263.         uint8_t byte;
  264.         uint8_t crc=0xFF;
  265.   for(byte=0; byte<Num; byte++)
  266.   {
  267.     crc^=(message[byte]);
  268.     for(i=8;i>0;--i)
  269.     {
  270.       if(crc&0x80) crc=(crc<<1)^0x31;
  271.       else crc=(crc<<1);
  272.     }
  273.   }
  274.         return crc;
  275. }

  276. void AHT20_Read_CTdata(uint32_t *ct) //没有CRC校验,直接读取AHT20的温度和湿度数据
  277. {
  278.                 volatile uint8_t  Byte_1th=0;
  279.         volatile uint8_t  Byte_2th=0;
  280.         volatile uint8_t  Byte_3th=0;
  281.         volatile uint8_t  Byte_4th=0;
  282.         volatile uint8_t  Byte_5th=0;
  283.         volatile uint8_t  Byte_6th=0;
  284.          uint32_t RetuData = 0;
  285.         uint16_t cnt = 0;
  286.         AHT20_SendAC();//向AHT10发送AC命令
  287.         Delay_1ms(80);//延时80ms左右       
  288.     cnt = 0;
  289.         while(((AHT20_Read_Status()&0x80)==0x80))//直到状态bit[7]为0,表示为空闲状态,若为1,表示忙状态
  290.         {
  291.                 SensorDelay_us(1508);
  292.                 if(cnt++>=100)
  293.                 {
  294.                  break;
  295.                  }
  296.         }
  297.         I2C_Start();
  298.         AHT20_WR_Byte(0x71);
  299.         Receive_ACK();
  300.         Byte_1th = AHT20_RD_Byte();//状态字,查询到状态为0x98,表示为忙状态,bit[7]为1;状态为0x1C,或者0x0C,或者0x08表示为空闲状态,bit[7]为0
  301.         Send_ACK();
  302.         Byte_2th = AHT20_RD_Byte();//湿度
  303.         Send_ACK();
  304.         Byte_3th = AHT20_RD_Byte();//湿度
  305.         Send_ACK();
  306.         Byte_4th = AHT20_RD_Byte();//湿度/温度
  307.         Send_ACK();
  308.         Byte_5th = AHT20_RD_Byte();//温度
  309.         Send_ACK();
  310.         Byte_6th = AHT20_RD_Byte();//温度
  311.         Send_NOT_ACK();
  312.         Stop_I2C();

  313.         RetuData = (RetuData|Byte_2th)<<8;
  314.         RetuData = (RetuData|Byte_3th)<<8;
  315.         RetuData = (RetuData|Byte_4th);
  316.         RetuData =RetuData >>4;
  317.         ct[0] = RetuData;//湿度
  318.         RetuData = 0;
  319.         RetuData = (RetuData|Byte_4th)<<8;
  320.         RetuData = (RetuData|Byte_5th)<<8;
  321.         RetuData = (RetuData|Byte_6th);
  322.         RetuData = RetuData&0xfffff;
  323.         ct[1] =RetuData; //温度

  324. }


  325. void AHT20_Read_CTdata_crc(uint32_t *ct) //CRC校验后,读取AHT20的温度和湿度数据
  326. {
  327.                 volatile uint8_t  Byte_1th=0;
  328.         volatile uint8_t  Byte_2th=0;
  329.         volatile uint8_t  Byte_3th=0;
  330.         volatile uint8_t  Byte_4th=0;
  331.         volatile uint8_t  Byte_5th=0;
  332.         volatile uint8_t  Byte_6th=0;
  333.         volatile uint8_t  Byte_7th=0;
  334.          uint32_t RetuData = 0;
  335.          uint16_t cnt = 0;
  336.         // uint8_t  CRCDATA=0;
  337.          uint8_t  CTDATA[6]={0};//用于CRC传递数组
  338.        
  339.         AHT20_SendAC();//向AHT10发送AC命令
  340.         Delay_1ms(80);//延时80ms左右       
  341.     cnt = 0;
  342.         while(((AHT20_Read_Status()&0x80)==0x80))//直到状态bit[7]为0,表示为空闲状态,若为1,表示忙状态
  343.         {
  344.                 SensorDelay_us(1508);
  345.                 if(cnt++>=100)
  346.                 {
  347.                  break;
  348.                 }
  349.         }
  350.        
  351.         I2C_Start();

  352.         AHT20_WR_Byte(0x71);
  353.         Receive_ACK();
  354.         CTDATA[0]=Byte_1th = AHT20_RD_Byte();//状态字,查询到状态为0x98,表示为忙状态,bit[7]为1;状态为0x1C,或者0x0C,或者0x08表示为空闲状态,bit[7]为0
  355.         Send_ACK();
  356.         CTDATA[1]=Byte_2th = AHT20_RD_Byte();//湿度
  357.         Send_ACK();
  358.         CTDATA[2]=Byte_3th = AHT20_RD_Byte();//湿度
  359.         Send_ACK();
  360.         CTDATA[3]=Byte_4th = AHT20_RD_Byte();//湿度/温度
  361.         Send_ACK();
  362.         CTDATA[4]=Byte_5th = AHT20_RD_Byte();//温度
  363.         Send_ACK();
  364.         CTDATA[5]=Byte_6th = AHT20_RD_Byte();//温度
  365.         Send_ACK();
  366.         Byte_7th = AHT20_RD_Byte();//CRC数据
  367.         Send_NOT_ACK();                           //注意: 最后是发送NAK
  368.         Stop_I2C();
  369.        
  370.         if(Calc_CRC8(CTDATA,6)==Byte_7th)
  371.         {
  372.         RetuData = (RetuData|Byte_2th)<<8;
  373.         RetuData = (RetuData|Byte_3th)<<8;
  374.         RetuData = (RetuData|Byte_4th);
  375.         RetuData =RetuData >>4;
  376.         ct[0] = RetuData;//湿度
  377.         RetuData = 0;
  378.         RetuData = (RetuData|Byte_4th)<<8;
  379.         RetuData = (RetuData|Byte_5th)<<8;
  380.         RetuData = (RetuData|Byte_6th);
  381.         RetuData = RetuData&0xfffff;
  382.         ct[1] =RetuData; //温度
  383.                
  384.         }
  385.         else
  386.         {
  387.                 ct[0]=0x00;
  388.                 ct[1]=0x00;//校验错误返回值,客户可以根据自己需要更改
  389.         }//CRC数据
  390. }


  391. void AHT20_Init(void)   //初始化AHT20
  392. {       
  393.         Init_I2C_Sensor_Port();
  394.         I2C_Start();
  395.         AHT20_WR_Byte(0x70);
  396.         Receive_ACK();
  397.         AHT20_WR_Byte(0xa8);//0xA8进入NOR工作模式
  398.         Receive_ACK();
  399.         AHT20_WR_Byte(0x00);
  400.         Receive_ACK();
  401.         AHT20_WR_Byte(0x00);
  402.         Receive_ACK();
  403.         Stop_I2C();

  404.         Delay_1ms(10);//延时10ms左右

  405.         I2C_Start();
  406.         AHT20_WR_Byte(0x70);
  407.         Receive_ACK();
  408.         AHT20_WR_Byte(0xbe);//0xBE初始化命令,AHT20的初始化命令是0xBE,   AHT10的初始化命令是0xE1
  409.         Receive_ACK();
  410.         AHT20_WR_Byte(0x08);//相关寄存器bit[3]置1,为校准输出
  411.         Receive_ACK();
  412.         AHT20_WR_Byte(0x00);
  413.         Receive_ACK();
  414.         Stop_I2C();
  415.         Delay_1ms(10);//延时10ms左右
  416. }
  417. void JH_Reset_REG(uint8_t addr)
  418. {
  419.        
  420.         uint8_t Byte_first,Byte_second,Byte_third;
  421.         I2C_Start();
  422.         AHT20_WR_Byte(0x70);//原来是0x70
  423.         Receive_ACK();
  424.         AHT20_WR_Byte(addr);
  425.         Receive_ACK();
  426.         AHT20_WR_Byte(0x00);
  427.         Receive_ACK();
  428.         AHT20_WR_Byte(0x00);
  429.         Receive_ACK();
  430.         Stop_I2C();

  431.         Delay_1ms(5);//延时5ms左右
  432.         I2C_Start();
  433.         AHT20_WR_Byte(0x71);//
  434.         Receive_ACK();
  435.         Byte_first = AHT20_RD_Byte();
  436.         Send_ACK();
  437.         Byte_second = AHT20_RD_Byte();
  438.         Send_ACK();
  439.         Byte_third = AHT20_RD_Byte();
  440.         Send_NOT_ACK();
  441.         Stop_I2C();
  442.        
  443.   Delay_1ms(10);//延时10ms左右
  444.         I2C_Start();
  445.         AHT20_WR_Byte(0x70);///
  446.         Receive_ACK();
  447.         AHT20_WR_Byte(0xB0|addr);寄存器命令
  448.         Receive_ACK();
  449.         AHT20_WR_Byte(Byte_second);
  450.         Receive_ACK();
  451.         AHT20_WR_Byte(Byte_third);
  452.         Receive_ACK();
  453.         Stop_I2C();
  454.        
  455.         Byte_second=0x00;
  456.         Byte_third =0x00;
  457. }

  458. void AHT20_Start_Init(void)
  459. {
  460.         JH_Reset_REG(0x1b);
  461.         JH_Reset_REG(0x1c);
  462.         JH_Reset_REG(0x1e);
  463. }

评论

请问下,JH_Reset_REG()寄存器初始化函数中AHT20_WR_Byte(0xB0|addr);寄存器命令,这样子初始化是在哪里规定了吗?不太懂AHT21B说明书中就寄存器初始化该怎么做  发表于 2024-7-23 11:33
 楼主| 个百zz分点个 发表于 2023-1-31 20:45 | 显示全部楼层
将main.c文件里main函数包括上面的头文件等信息删除,换成下面代码插入代码
  1. #include "main.h"
  2. #include "dma.h"
  3. #include "i2c.h"
  4. #include "usart.h"
  5. #include "gpio.h"
  6. #include "AHT20-21_DEMO_V1_3.h"
  7. #include <stdio.h>
  8. #include <string.h>
  9. int fputc(int ch,FILE *f)//重新printf
  10. {
  11.     HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFFFF);   
  12.                 while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=SET){
  13.                 }               
  14.     return ch;
  15. }
  16. void SystemClock_Config(void);
  17. volatile int  c1,t1;
  18. uint32_t CT_data[2]={0,0};
  19. int main(void)
  20. {
  21.   HAL_Init();
  22.   SystemClock_Config();
  23.   MX_USART1_UART_Init();
  24.   MX_DMA_Init();
  25.   MX_I2C1_Init();       
  26.         AHT20_Init();//放在其它之后
  27.   if((AHT20_Read_Status()&0x18)!=0x18)
  28.         {
  29.                 AHT20_Start_Init(); //重新初始化寄存器
  30.                 Delay_1ms(10);
  31.         }


  32.   while (1)
  33.   {
  34.    
  35.                 AHT20_Read_CTdata(CT_data);       //不经过CRC校验,直接读取AHT20的温度和湿度数据    推荐每隔大于1S读一次
  36.     //AHT20_Read_CTdata_crc(CT_data);  //crc校验后,读取AHT20的温度和湿度数据
  37.           c1 = CT_data[0]*100*10/1024/1024;  //计算得到湿度值c1(放大了10倍)
  38.           t1 = CT_data[1]*200*10/1024/1024-500;//计算得到温度值t1(放大了10倍)       
  39.                 printf("湿度:%d%s",c1/10,"%");
  40.           printf("温度:%d%s",t1/10,"℃");
  41.           printf("\r\n");
  42.                 HAL_Delay(3000);

  43.   }

  44. }


 楼主| 个百zz分点个 发表于 2023-1-31 20:46 | 显示全部楼层
最后编译效果 8429363d90d9e349b7.png
 楼主| 个百zz分点个 发表于 2023-1-31 20:46 | 显示全部楼层
吹起或用手捏住温度会明显变化。
四、总结
参考博客:https://blog.csdn.net/qq_52215423/article/details/127887672?app_version=5.8.1&code=app_1562916241&csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22127887672%22%2C%22source%22%3A%22qq_52215423%22%7D&uLinkId=usr1mkqgl919blen&utm_source=app
用厂商的代码实现了对温度的检测,第一次做此类实验,收获很多,对AHT20温湿度传感器的原理与应用有了更深的理解。
kqh11a 发表于 2024-7-29 14:27 | 显示全部楼层
软件I2C通过CPU控制每个时刻的引脚状态来控制GPIO引脚电平产生通讯时序的方式
您需要登录后才可以回帖 登录 | 注册

本版积分规则

53

主题

679

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部