IICハードウェアに基づくAHT20温度および湿度センサーのデータ収集

IICハードウェアに基づくAHT20温度および湿度センサーのデータ収集

ソフトウェアIICとハードウェアIIC

IICプロトコル:I2C通信プロトコル、数ピン、シンプルなハードウェア実装、強力なスケーラビリティ、USART、CAN、その他の通信プロトコルなどの外部トランシーバデバイスは不要。現在、システム通信の複数の集積回路(IC)で広く使用されています。
1.ソフトウェアIIC:ソフトウェアI2Cは通常GPIOピンを使用し、ソフトウェアはピンのステータスを制御してI2C通信波形をシミュレートします。
2.ハードウェアIIC:いわゆるハードウェアI2Cは、チップ上のI2C周辺機器に対応し、対応するI2C駆動回路があり、使用されるI2Cピンも専用です。
違いは、ハードウェアI2Cの効率はソフトウェアの効率よりもはるかに高いのに対し、ソフトウェアI2Cはピンによって制限されないため、より柔軟なインターフェイスを備えていることです。ハードウェアIICの使用法はより複雑であり、IICをシミュレートするプロセスはより明確です。

実験手順

目的

設計プログラムは、温度と湿度のデータが2秒ごとに収集され、シリアルポートを介してホストコンピュータに送信されることを認識しています。

コード

#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);
}

コード書き込み

ここに画像の説明を挿入

シリアル通信

シリアルポートを開いて、温度と湿度のテスト結果を表示しますここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/rude_dragon/article/details/111508703