ESP32 温湿度和气体传感器驱动

驱动原理iic

iic.h文件

#ifndef __APP_IIC_H__

#define __APP_IIC_H__

#include "esp_types.h"

#include "driver/uart.h"

#include "driver/gpio.h"


 

esp_err_t i2c_master_init(void);

uint8_t AHT20_Read_Status(void);

uint8_t AHT20_Read_Cal_Enable(void);

void AC_CMD(void);

uint8_t Calc_CRC8(uint8_t *message,uint8_t Num);

void AHT20_Read_CTdata(uint32_t *ct);

void AHT20_Read_CTdata_crc(uint16_t *ct);

void AHT20_Init(void);

void JH_Reset_REG(uint8_t addr);

void AHT20_Start_Init(void);


 

//#define SENSOR_PORT_TYPE  unsigned char

typedef struct

{

//  SENSOR_PORT_TYPE  *pPORT;

//  unsigned int  Speed;

    unsigned char   Adrr;       //通讯设备地址

    unsigned char   Status; //AGS02MA模块状态字节

    unsigned short int  timcnt; //时间计数器

    unsigned char   ErrFlag;    //通讯错误次数

    unsigned short int  SetRTim;    //设置读取间隔时间

    unsigned char  RegAddr; //寄存器地址

    unsigned char  ReadByte[10];  //存放读取的数据

    unsigned int RSVal;  //传感器阻值

    unsigned int PPBVal;    //TVOC  ppb数值

    unsigned char  year;    //年

    unsigned char  month;//月

    unsigned char  day;     //日

    unsigned char  SoftVer; //版本号

}MYI2C_Struct;

extern  MYI2C_Struct SENx;

void  MYI2C_Init(MYI2C_Struct *pSENSOR,unsigned int ReadTim,unsigned char xAddr);

void  MYI2C_Handle(MYI2C_Struct *pSENSOR);

#endif

dht20温湿度传感器

#include "app_iic.h"

#include "app_fan_light.h"

#include

#include "string.h"

#include "freertos/FreeRTOS.h"

#include "freertos/task.h"

#include "freertos/event_groups.h"

#include "esp_system.h"

#include "esp_log.h"

#include "sdkconfig.h"

#include "driver/i2c.h"

#include "driver/uart.h"

#include "driver/gpio.h"

#include "app_param.h"

#include

#include

#include "app_wifi.h"

#include

#include

#include "esp_log.h"

#include "esp_wifi.h"

#include "nvs_flash.h"

#include "esp_flash.h"

#include "app_public.h"

#include "app_miio.h"

#include "app_sntp.h"

#include "app_time_task.h"

#include "app_miio.h"

#include "app_client.h"

#include "app_pwm_light.h"

#include

#include "app_function.h"

#include "app_mqtt.h"

#include "app_raw.h"

#include "blu_gattc.h"

#include "app_com.h"

static const char *TAG = "app_iic";

#define _I2C_NUMBER(num) I2C_NUM_##num

#define I2C_NUMBER(num) _I2C_NUMBER(num)

#define I2C_MASTER_SCL_IO 6               /*!< gpio number for I2C master clock */

#define I2C_MASTER_SDA_IO 7               /*!< gpio number for I2C master data  */

#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */

#define I2C_MASTER_FREQ_HZ 20000        /*!< I2C master clock frequency */

#define I2C_MASTER_TX_BUF_DISABLE 0                           /*!< I2C master doesn't need buffer */

#define I2C_MASTER_RX_BUF_DISABLE 0                           /*!< I2C master doesn't need buffer */

#define WRITE_BIT I2C_MASTER_WRITE              /*!< I2C master write */

#define READ_BIT I2C_MASTER_READ                /*!< I2C master read */

#define ACK_CHECK_EN    ACK_VAL                     /*!< I2C master will check ack from slave*/

#define ACK_CHECK_DIS   NACK_VAL                     /*!< I2C master will not check ack from slave */

#define ACK_VAL         0x0                     /*!< I2C ack value */

#define NACK_VAL        0x1                     /*!< I2C nack value */

/**

 * @brief IIC初始化

 *

 * @return esp_err_t

 */

esp_err_t i2c_master_init(void)

{

    int i2c_master_port = I2C_MASTER_NUM;

    i2c_config_t conf = {

        .mode = I2C_MODE_MASTER,

        .sda_io_num = I2C_MASTER_SDA_IO,

        .sda_pullup_en = GPIO_PULLUP_ENABLE,

        .scl_io_num = I2C_MASTER_SCL_IO,

        .scl_pullup_en = GPIO_PULLUP_ENABLE,

        .master.clk_speed = I2C_MASTER_FREQ_HZ,

        // .clk_flags = 0,          /*!< Optional, you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here. */

    };

    esp_err_t err = i2c_param_config(i2c_master_port, &conf);

    if (err != ESP_OK) {

        return err;

    }

    return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);

}

uint8_t AHT20_Read_Status(void)//读取AHT20的状态寄存器

{

    uint8_t Byte_first[1]={0};  

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x71, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_first[0],ACK_CHECK_DIS);

    vTaskDelay(2 / 1000);

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

    ESP_LOGE(TAG, "AHT20_Read_Status:%d ",Byte_first[0]);

    return Byte_first[0];

}

uint8_t AHT20_Read_Cal_Enable(void)  //查询cal enable位有没有使能

{

    uint8_t val = 0;//ret = 0,

    val = AHT20_Read_Status();

    ESP_LOGW(TAG, "AHT20_Read_Cal_Enable():%d",val);

    if((val & 0x68)==0x08)

        return 1;

    else  

        return 0;

}

void abc(void)

{

    int i=0;

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    i2c_master_write_byte(cmd, 0x70, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0xAC, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x33, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

}

//CRC校验类型:CRC8/MAXIM

//多项式:X8+X5+X4+1

//Poly:0011 0001  0x31

//高位放到后面就变成 1000 1100 0x8c

//C现实代码:

uint8_t Calc_CRC8(uint8_t *message,uint8_t Num)

{

    uint8_t i;

    uint8_t byte;

    uint8_t crc=0xFF;

    for(byte=0; byte

    {

        crc^=(message[byte]);

        for(i=8;i>0;--i)

        {

        if(crc&0x80) crc=(crc<<1)^0x31;

        else crc=(crc<<1);

        }

    }

    return crc;

}

void AHT20_Read_CTdata(uint32_t *ct) //没有CRC校验,直接读取AHT20的温度和湿度数据

{

     uint8_t  Byte_1th[1]={0};

     uint8_t  Byte_2th[1]={0};

     uint8_t  Byte_3th[1]={0};

     uint8_t  Byte_4th[1]={0};

     uint8_t  Byte_5th[1]={0};

     uint8_t  Byte_6th[1]={0};

     uint32_t RetuData = 0 ,c1=0,c2=0;

     uint8_t  CTDATA[7]={0};//用于CRC传递数组

    uint16_t cnt = 0;

    abc();//向AHT10发送AC命令

    vTaskDelay(80 / portTICK_RATE_MS);//延时80ms左右    

    cnt = 0;

    while(((AHT20_Read_Status()&0x80)==0x80))//直到状态bit[7]为0,表示为空闲状态,若为1,表示忙状态

    {

        vTaskDelay(2 / portTICK_RATE_MS);

        if(cnt++>=100)

        {

            break;

        }

    }

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x71, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_1th[0],ACK_CHECK_EN);//状态字,查询到状态为0x98,表示为忙状态,bit[7]为1;状态为0x1C,或者0x0C,或者0x08表示为空闲状态,bit[7]为0

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_2th[0],ACK_CHECK_EN);//湿度

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_3th[0],ACK_CHECK_EN);//湿度

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_4th[0],ACK_CHECK_EN);//湿度/温度

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_5th[0],ACK_CHECK_EN);//温度

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_6th[0],ACK_CHECK_DIS);//温度

    vTaskDelay(2 / 1000);

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

    RetuData = (RetuData|Byte_2th[0])<<8;

    RetuData = (RetuData|Byte_3th[0])<<8;

    RetuData = (RetuData|Byte_4th[0]);

    RetuData =RetuData >>4;

    c1 = RetuData;//湿度

    RetuData = 0;

    RetuData = (RetuData|Byte_4th[0])<<8;

    RetuData = (RetuData|Byte_5th[0])<<8;

    RetuData = (RetuData|Byte_6th[0]);

    RetuData = RetuData&0xfffff;

    c2 =RetuData; //温度

    printf("%d-----%d\r\n",c1*100*10/1024/1024,c2*200*10/1024/1024-500);

}

void AHT20_Read_CTdata_crc(uint16_t *ct) //CRC校验后,读取AHT20的温度和湿度数据

{

     uint8_t  Byte_1th[1]={0};

     uint8_t  Byte_2th[1]={0};

     uint8_t  Byte_3th[1]={0};

     uint8_t  Byte_4th[1]={0};

     uint8_t  Byte_5th[1]={0};

     uint8_t  Byte_6th[1]={0};

     uint8_t  Byte_7th[1]={0};

    uint16_t RetuData = 0;

    uint16_t cnt = 0;

    // uint8_t  CRCDATA=0;

    uint8_t  CTDATA[7]={0};//用于CRC传递数组

    abc();

    vTaskDelay(80 / portTICK_RATE_MS);//延时80ms左右    

    cnt = 0;

    while(((AHT20_Read_Status()&0x80)==0x80))//直到状态bit[7]为0,表示为空闲状态,若为1,表示忙状态

    {

        vTaskDelay(1 / portTICK_RATE_MS);

        if(cnt++>=100)

        {

            break;

        }

    }

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x71, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_1th[0],ACK_CHECK_EN);//状态字,查询到状态为0x98,表示为忙状态,bit[7]为1;状态为0x1C,或者0x0C,或者0x08表示为空闲状态,bit[7]为0

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_2th[0],ACK_CHECK_EN);//湿度

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_3th[0],ACK_CHECK_EN);//湿度

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_4th[0],ACK_CHECK_EN);//湿度/温度

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_5th[0],ACK_CHECK_EN);//温度

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_6th[0],ACK_CHECK_EN);//温度

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&Byte_7th[0],ACK_CHECK_DIS);//CRC数据

    vTaskDelay(2 / 1000);

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

    CTDATA[0]=Byte_1th[0];

    CTDATA[1]=Byte_2th[0];

    CTDATA[2]=Byte_3th[0];

    CTDATA[3]=Byte_4th[0];

    CTDATA[4]=Byte_5th[0];

    CTDATA[5]=Byte_6th[0];

    if(Calc_CRC8(CTDATA,6)==Byte_7th[6])

    {

        RetuData = (RetuData|Byte_2th[0])<<8;

        RetuData = (RetuData|Byte_3th[0])<<8;

        RetuData = (RetuData|Byte_4th[0]);

        RetuData =RetuData >>4;

        ct[0] = RetuData;//湿度

        RetuData = 0;

        RetuData = (RetuData|Byte_4th[0])<<8;

        RetuData = (RetuData|Byte_5th[0])<<8;

        RetuData = (RetuData|Byte_6th[0]);

        RetuData = RetuData&0xfffff;

        ct[1] =RetuData; //温度

    }

    else

    {

        ct[0]=0x00;

        ct[1]=0x00;//校验错误返回值,客户可以根据自己需要更改

    }//CRC数据

}


 

void AHT20_Init(void)   //初始化AHT20

{  

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x70, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0xa8, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

    vTaskDelay(10 / portTICK_RATE_MS);//延时10ms左右

    cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x70, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0xbe, ACK_CHECK_EN);//0xBE初始化命令,AHT20的初始化命令是0xBE,   AHT10的初始化命令是0xE1

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x08, ACK_CHECK_EN);//相关寄存器bit[3]置1,为校准输出

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

    vTaskDelay(10 / portTICK_RATE_MS);//延时10ms左右

}

void JH_Reset_REG(uint8_t addr)

{

    uint8_t  CTDATA1[1]={0};

    uint8_t  CTDATA2[1]={0};

    uint8_t  CTDATA3[1]={0};

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x70, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, addr, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

    vTaskDelay(10 / portTICK_RATE_MS); //延时5ms左右

    cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x71, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&CTDATA1[0],ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&CTDATA2[0],ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_read_byte(cmd,&CTDATA3[0],ACK_CHECK_DIS);

    vTaskDelay(2 / 1000);

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

   

    vTaskDelay(10 / portTICK_RATE_MS);//延时10ms左右

    cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0x70, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, 0xB0|addr, ACK_CHECK_EN);

    vTaskDelay(2 / 1000);//寄存器命令

    i2c_master_write_byte(cmd, CTDATA2[0], ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_write_byte(cmd, CTDATA3[0], ACK_CHECK_EN);

    vTaskDelay(2 / 1000);

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

}

void AHT20_Start_Init(void)

{

    JH_Reset_REG(0x1b);

    JH_Reset_REG(0x1c);

    JH_Reset_REG(0x1e);

}

ags02ma气体传感器

MYI2C_Struct SENx;

 /*******************************************************************************

* Function Name  :  MYI2C_Init

* Description    :  初始化I2C引脚与ASG02MA结构体变量

* Input          :  ReadTim 每隔ms时间读取AGS02MA模块

                    xAddr   AGS02MA模块通讯地址

* Output         :  None

* Return         :  None

*******************************************************************************/

//MYI2C_Init(&SENx,100,0x1A);

void  MYI2C_Init(MYI2C_Struct *pSENSOR,unsigned int ReadTim,unsigned char xAddr)

{  

      pSENSOR->Adrr=xAddr;

      pSENSOR->RegAddr=0x11;上电读版本

      if(ReadTim<100)ReadTim=200;

        pSENSOR->SetRTim=ReadTim;

}

/*******************************************************************************

* Function Name  :  MYI2C_READ_FUNC

* Description    :  IIC主机读取从机数据

* Input          :  pSENSOR存放读出数据

                                        device_addr从机地址

                                        register_addr寄存器地址

                                        len读出数据的长度  单位:byte

* Output         :  None

* Return         :  0:读取数据成功

                                        1:读取数据失败

*******************************************************************************/

unsigned char MYI2C_READ_FUNC(MYI2C_Struct *pSENSOR,unsigned char device_addr,signed char register_addr, unsigned char len)

{

    unsigned int TimeOut=9999;

    unsigned char i=0;

    if(register_addr>=0)

    {

        /* Send STRAT condition a second time */  

        i2c_cmd_handle_t cmd = i2c_cmd_link_create();

        i2c_master_start(cmd);

        vTaskDelay(2 / 1000);

        /* Send slave address for Write Regsiter */

        //MYI2C_Write_IIC_Byte( (device_addr<<1) + 0);

        i2c_master_write_byte(cmd, (device_addr<<1) + 0, ACK_CHECK_EN);

        /* ack */

        //if(MYI2C_IIC_Wait_Ack(255)==0)TimeOut=0;

        vTaskDelay(2 / 1000);

         /* Send register_addr   */

        //MYI2C_Write_IIC_Byte( (register_addr));

        i2c_master_write_byte(cmd, register_addr, ACK_CHECK_EN);

        /* Test on EV6 and clear it */

        //if(MYI2C_IIC_Wait_Ack(255)==0)TimeOut=0;

        vTaskDelay(2 / 1000);

        i2c_master_stop(cmd);

        i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

        i2c_cmd_link_delete(cmd);  

        vTaskDelay(10 / portTICK_RATE_MS);

    }  

    /* Send STRAT condition a second time */  

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    /* Send slave address for Read */

    //MYI2C_Write_IIC_Byte( (device_addr<<1)+1 );

    i2c_master_write_byte(cmd, (device_addr<<1)+1, ACK_CHECK_EN);

    /* ack */

    //if(MYI2C_IIC_Wait_Ack(255)==0)TimeOut=0;

    vTaskDelay(2 / 1000);

    /* While there is data to be read */

    while(i

    {

            //TimeOut--;

            //pSENSOR->ReadByte[i]=MYI2C_Read_IIC_Byte();

            if(i!=(len-1))i2c_master_read_byte(cmd,&pSENSOR->ReadByte[i],ACK_CHECK_EN);

            else i2c_master_read_byte(cmd,&pSENSOR->ReadByte[i],ACK_CHECK_DIS);

            /*  Acknowledgement */

            //MYI2C_IIC_Ack(len-1-i);//len = 1 NoAck  

            vTaskDelay(1 / 1000);    

            i++;          

    }

        /* Send STOP Condition */

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);  

    //  I2C_ClearFlag(pSENSOR->pPORT,I2C_FLAG_TIMEOUT);

    //if(TimeOut){pSENSOR->ErrFlag=0;return 0;}

    //else {pSENSOR->ErrFlag++;return 1;}

    return 1;

}

/*******************************************************************************

* Function Name  :  MYI2C_WRITE_FUNC

* Description    :  IIC主机向从机写入数据

* Input          :  pSENSOR主机需要发送给从机的数据

                                        device_addr从机地址

                                        register_addr寄存器地址

                                        len读出数据的长度  单位:byte

* Return         :  0:读取数据成功

                                        1:读取数据失败

*******************************************************************************/

unsigned char MYI2C_WRITE_FUNC(MYI2C_Struct *pSENSOR,unsigned char device_addr,unsigned char register_addr, unsigned char *pDATA, unsigned char len)

{

    unsigned int TimeOut=9999;  

    /* Send STRAT condition */

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();

    i2c_master_start(cmd);

    vTaskDelay(2 / 1000);

    /* Send EEPROM address for write */

    //MYI2C_Write_IIC_Byte( (device_addr<<1) + 0);

    i2c_master_write_byte(cmd, (device_addr<<1) + 0, ACK_CHECK_EN);

    /* Test on EV6 and clear it */

    vTaskDelay(2 / 1000);

    /* Send register_addr for read */

    //MYI2C_Write_IIC_Byte( (register_addr));

    i2c_master_write_byte(cmd, register_addr, ACK_CHECK_EN);

    /* Test on EV6 and clear it */

    vTaskDelay(2 / 1000);

    while(/*TimeOut &&*/len)//

    {

        /* Send the byte to be written */

        //MYI2C_Write_IIC_Byte( *pDATA);

        i2c_master_write_byte(cmd, *pDATA, ACK_CHECK_EN);

        /*  Acknowledgement */

        //MYI2C_IIC_Wait_Ack(9);    

        vTaskDelay(2 / 1000);      

        pDATA++;

        len--;

        //TimeOut--;        

    }

    /* Send STOP condition */

    i2c_master_stop(cmd);

    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);

    i2c_cmd_link_delete(cmd);

   

    //if(TimeOut){pSENSOR->ErrFlag=0;return 0;}

    //else {pSENSOR->ErrFlag++;return 1;}

    return 1;

}

/*******************************************************************************

* Function Name  :  CheckCrc

* Description    :  CRC校验

* Input          :  *pDAT需要进行CRC校验的数据

                                        Lenth进行CRC校验的数据长度  单位:byte

* Output         :  None

* Return         :  CRC校验码

*******************************************************************************/

unsigned char  CheckCrc8(unsigned char *pDAT,unsigned char Lenth)

{

    unsigned char crc = 0xff, i, j;

    for (i = 0; i < Lenth ; i++)

    {

        crc = crc ^ *pDAT;

        for (j = 0; j < 8; j++)

        {

            if (crc & 0x80) crc = (crc << 1) ^ 0x31;

            else crc <<= 1;

        }

                pDAT++;

    }

    return crc;

}


 

/*******************************************************************************

* Function Name  :  MYI2C_Handle

* Description    :  读取AGS02MA的版本号与输出的TVOC数据

* Input          :  *pSENSOR存放与AGS02MA通讯的相关数据

* Output         :  None

* Return         :  None

*******************************************************************************/

void  MYI2C_Handle(MYI2C_Struct *pSENSOR)

{

     pSENSOR->timcnt+=1;

    if(pSENSOR->timcnt > pSENSOR->SetRTim)

    {

        pSENSOR->timcnt=0;

        MYI2C_READ_FUNC(pSENSOR,pSENSOR->Adrr,pSENSOR->RegAddr,5);  

       

        pSENSOR->ReadByte[5]=CheckCrc8(&pSENSOR->ReadByte[0],4);

        ESP_LOGW(TAG, "check:%0.2X--%0.2X--%0.2X--%0.2X--%0.2X--%0.2X",

        pSENSOR->ReadByte[0],pSENSOR->ReadByte[1], pSENSOR->ReadByte[2],pSENSOR->ReadByte[3], pSENSOR->ReadByte[4],pSENSOR->ReadByte[5]);

       

        if(pSENSOR->ReadByte[5]==pSENSOR->ReadByte[4])

        {

            //ESP_LOGW(TAG, "pSENSOR->RegAddr:%d", pSENSOR->RegAddr);

            if(pSENSOR->RegAddr==0x00)

            {

                pSENSOR->RegAddr=0x00;

                pSENSOR->Status=pSENSOR->ReadByte[0];

                pSENSOR->PPBVal=pSENSOR->ReadByte[1];//TVOC  ppb数值

                pSENSOR->PPBVal=pSENSOR->PPBVal<<8;

                pSENSOR->PPBVal+=pSENSOR->ReadByte[2];

                pSENSOR->PPBVal=pSENSOR->PPBVal<<8;

                pSENSOR->PPBVal+=pSENSOR->ReadByte[3];

                ESP_LOGW(TAG, "TVOC:%d--%d", pSENSOR->PPBVal, pSENSOR->Status);

            }

            else if(pSENSOR->RegAddr==0x11)

            {

                pSENSOR->RegAddr=0x00;

                pSENSOR->year=pSENSOR->ReadByte[0];

                pSENSOR->month=pSENSOR->ReadByte[1];

                pSENSOR->day=pSENSOR->ReadByte[2];

                pSENSOR->SoftVer=pSENSOR->ReadByte[3];  //版本号

                pSENSOR->ReadByte[3]=0;

            }              

        }

       

    }

}

你可能感兴趣的:(ESP32,IIC驱动,温湿度传感器,气体传感器,mcu)