Data collection of AHT20 temperature and humidity sensor based on IIC hardware
Software IIC and hardware IIC
IIC protocol: I2C communication protocol, few pins, simple hardware implementation, strong scalability, no need for external transceiver devices such as USART, CAN and other communication protocols. It is now widely used in multiple integrated circuits (ICs) in the system communication.
1. Software IIC: Software I2C generally uses GPIO pins, and software controls the pin status to simulate I2C communication waveforms.
2. Hardware IIC: The so-called hardware I2C corresponds to the I2C peripherals on the chip, there is a corresponding I2C drive circuit, and the I2C pins used are also dedicated.
The difference is that the efficiency of hardware I2C is much higher than that of software, while software I2C is not limited by pins, so the interface is more flexible. Hardware IIC usage is more complicated, and the process of simulating IIC is clearer.
experiment procedure
Purpose
The design program realizes that the temperature and humidity data is collected every 2 seconds and sent to the host computer through the serial port
Code
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#include "stdio.h"
#include "delay.h"
#include "bsp_i2c.h"
#include "ATH20.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
GPIO_InitTypeDef GPIO_InitStructure;
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
_sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,(uint8_t)ch);
return ch;
}
void uart_init(u32 bound)
{
//GPIO¶Ë¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ
USART_DeInit(USART1); //¸´Î»´®¿Ú1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA10
//USART ³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate = bound;//Ò»°ãÉèÖÃΪ9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæżУÑéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½
USART_Init(USART1, &USART_InitStructure); //³õʼ»¯´®¿Ú
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú
}
int main(void)
{
uint8_t ret = 0;
float P,T,ALT;
uint32_t CT_data[2];
int c1,t1;
uint8_t LED_Stat = 0;
RCC_Configuration(); //ÉèÖÃϵͳʱÖÓ
GPIO_Configuration(); //IO¿ÚÉè
I2C_Bus_Init();
uart_init(115200);
ret = ATH20_Init();
if(ret == 0)
{
printf("ATH20´«¸ÐÆ÷³õʼ»¯´íÎó\n");
while(1);
}
while(1)
{
/* ¶ÁÈ¡ ATH20 ´«¸ÐÆ÷Êý¾Ý*/
while(ATH20_Read_Cal_Enable() == 0)
{
ATH20_Init();//Èç¹ûΪ0ÔÙʹÄÜÒ»´Î
SoftDelay_ms(30);
}
ATH20_Read_CTdata(CT_data); //¶ÁȡζȺÍʪ¶È
c1 = CT_data[0] * 1000 / 1024 / 1024; //¼ÆËãµÃµ½Êª¶ÈÖµ£¨·Å´óÁË10±¶,Èç¹ûc1=523£¬±íʾÏÖÔÚʪ¶ÈΪ52.3%£©
t1 = CT_data[1] * 200 *10 / 1024 / 1024 - 500;//¼ÆËãµÃµ½Î¶ÈÖµ£¨·Å´óÁË10±¶£¬Èç¹ût1=245£¬±íʾÏÖÔÚζÈΪ24.5¡æ£©
printf("AHT20ÎÂʪ¶È´«¸ÐÆ÷²âÊÔÊý¾Ý:\n");
printf("ζÈ: %d.%d ¡æ\n",(t1/10),(t1%10));
printf("ʪ¶È: %d.%d %%\n",(c1/10),(c1%10));
printf("\n");
SoftDelay_ms(1000);//ÿ¸ôÁ½Ãë¶ÁÒ»´ÎÊý
if(LED_Stat == 0)
{
LED_Stat = 1;
GPIO_ResetBits(GPIOC, GPIO_Pin_2);
}
else
{
LED_Stat = 0;
GPIO_SetBits(GPIOC, GPIO_Pin_2);
}
}
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE , ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7; //״̬LED1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ͨÓÃÍÆÍìÊä³öģʽ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Êä³öģʽ×î´óËÙ¶È50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
Code burning
Serial communication
Open the serial port to view the temperature and humidity test results