Supports

I2C Code with 8051(Sensor ID:0x58)

/**

  ******************************************************************************

  * @file    Sencoch_Sensor_0x58_Driver.c

  * @brief   Generic I2C driver for Sencoch sensors with device address 0x58

  * @version V2.0

  * @date    2025-07-19

  * @note    Compatible with: 8051 series (AT89S52, STC89C52, и т.п.)

  *          Supports all Sencoch sensors with I2C address 0x58

  ******************************************************************************

  */

 

#включать <reg52.h>

#включать <intrins.h>

#включать <математика.ч>

#включать <stdio.h>

 

/*============================================================================*/

/*                              Pin Definitions */

/*============================================================================*/

sbit SCL_PIN"=" P1^0;

sbit SDA_PIN"=" P1^1;

 

/*============================================================================*/

/*                       Sencoch Sensor I2C Configuration */

/*============================================================================*/

#define SENCOCH_I2C_ADDR0x58

#define SENCOCH_REG_DATA0x04

#define SENCOCH_REG_EOF0x20

#define SENCOCH_REG_SHIFT0x21

 

/*============================================================================*/

/*                        Sencoch Sensor Parameters */

/*============================================================================*/

/*

 * ПРИМЕЧАНИЕ: Modify PMIN and PMAX according to your specific sensor model

 * 

 * Common Ranges for I2C ADD 0x58 Sensors (unit: Pa):

 *   0 ~ 0.5kPa:     PMIN = 0.0f,       PMAX = 500.0f

 *   0 ~ 5kPa:       PMIN = 0.0f,       PMAX = 5000.0f

 *   0 ~ 1000kPa:    PMIN = 0.0f,       PMAX = 1000000.0f

 *   -1 ~ 1kPa:      PMIN = -1000.0f,   PMAX = 1000.0f

 *   -5 ~ 5kPa:      PMIN = -5000.0f,   PMAX = 5000.0f

 *   -100 ~ 100kPa:  PMIN = -100000.0f, PMAX = 100000.0f

 */

#define PMIN-100000.0f

#define PMAX100000.0f

 

/*============================================================================*/

/*                      Data Buffer Definitions */

/*============================================================================*/

#define SENCOCH_READ_LEN5

 

/*============================================================================*/

/*                      Public Data Structure */

/*============================================================================*/

typedef struct {

    signed long pressure_AD;

    signed int temperature_AD;

    unsigned char temp_calib_offset_code;

    unsigned char temp_calib_gain_exp;

    signed int eof_out;

    плавать shift_n;

    плавать давление;

    плавать температура;

} Sencoch_Sensor_Data_t;

 

/*============================================================================*/

/*                           Delay Functions */

/*============================================================================*/

 

static пустота delay_us(unsigned int us)

{

    unsigned int я, Дж;

    для (я"=" 0; я< us; я++) {

        для (Дж"=" 0; Дж< 5; Дж++) {

            _nop_();

        }

    }

}

 

static пустота delay_ms(unsigned int РС)

{

    unsigned int я, Дж;

    для (я"=" 0; я< РС; я++) {

        для (Дж"=" 0; Дж< 125; Дж++) {

            delay_us(8);

        }

    }

}

 

/*============================================================================*/

/*                         I2C Timing Generation */

/*============================================================================*/

 

static пустота I2C_Старт(пустота)

{

    SDA_PIN"=" 1;

    delay_us(5);

    SCL_PIN"=" 1;

    delay_us(5);

    SDA_PIN"=" 0;

    delay_us(5);

    SCL_PIN"=" 0;

    delay_us(5);

}

 

static пустота I2C_Stop(пустота)

{

    SDA_PIN"=" 0;

    delay_us(5);

    SCL_PIN"=" 1;

    delay_us(5);

    SDA_PIN"=" 1;

    delay_us(5);

}

 

static unsigned char I2C_WaitAck(пустота)

{

    unsigned char ack"=" 0;

    SDA_PIN"=" 1;

    delay_us(5);

    SCL_PIN"=" 1;

    delay_us(5);

    если (SDA_PIN) {

        ack"=" 1;

    }

    SCL_PIN"=" 0;

    delay_us(5);

    SDA_PIN"=" 0;

    возвращаться ack;

}

 

static пустота I2C_SendByte(unsigned char байт)

{

    unsigned char я;

    для (я"=" 0; я< 8; я++) {

        SCL_PIN"=" 0;

        delay_us(5);

        если (байт& 0х80) {

            SDA_PIN"=" 1;

        } еще {

            SDA_PIN"=" 0;

        }

        байт<<"=" 1;

        delay_us(5);

        SCL_PIN"=" 1;

        delay_us(5);

    }

    SCL_PIN"=" 0;

    delay_us(5);

}

 

static unsigned char I2C_ReadByte(unsigned char ack)

{

    unsigned char я, байт"=" 0;

    SDA_PIN"=" 1;

    для (я"=" 0; я< 8; я++) {

        байт<<"=" 1;

        SCL_PIN"=" 1;

        delay_us(5);

        если (SDA_PIN) {

            байт|"=" 0х01;

        }

        SCL_PIN"=" 0;

        delay_us(5);

    }

    SDA_PIN"=" 0;

    если (ack) {

        SDA_PIN"=" 0;

    } еще {

        SDA_PIN"=" 1;

    }

    delay_us(5);

    SCL_PIN"=" 1;

    delay_us(5);

    SCL_PIN"=" 0;

    delay_us(5);

    возвращаться байт;

}

 

/*============================================================================*/

/*                       High-Level I2C Operations */

/*============================================================================*/

 

unsigned char Sencoch_ReadBytes(unsigned char reg_addrunsigned char *dataunsigned char len)

{

    I2C_Старт();

    I2C_SendByte(SENCOCH_I2C_ADDR<< 1);

    если (I2C_WaitAck()) {

        I2C_Stop();

        возвращаться 1;

    }

    I2C_SendByte(reg_addr);

    если (I2C_WaitAck()) {

        I2C_Stop();

        возвращаться 1;

    }

    I2C_Старт();

    I2C_SendByte((SENCOCH_I2C_ADDR<< 1| 0х01);

    если (I2C_WaitAck()) {

        I2C_Stop();

        возвращаться 1;

    }