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, etc.)

  *          Supports all Sencoch sensors with I2C address 0x58

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

  */

 

#include <reg52.h>

#include <intrins.h>

#include <math.h>

#include <stdio.h>

 

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

/*                              Pin Definitions                               */

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

sbit SCL_PIN = P1^0;

sbit SDA_PIN = P1^1;

 

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

/*                       Sencoch Sensor I2C Configuration                     */

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

#define SENCOCH_I2C_ADDR    0x58

#define SENCOCH_REG_DATA    0x04

#define SENCOCH_REG_EOF     0x20

#define SENCOCH_REG_SHIFT   0x21

 

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

/*                        Sencoch Sensor Parameters                           */

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

/*

 * NOTE: 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 PMAX            100000.0f

 

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

/*                      Data Buffer Definitions                               */

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

#define SENCOCH_READ_LEN    5

 

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

/*                      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;

    float shift_n;

    float pressure;

    float temperature;

} Sencoch_Sensor_Data_t;

 

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

/*                           Delay Functions                                  */

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

 

static void delay_us(unsigned int us)

{

    unsigned int i, j;

    for (i = 0; i < us; i++) {

        for (j = 0; j < 5; j++) {

            _nop_();

        }

    }

}

 

static void delay_ms(unsigned int ms)

{

    unsigned int i, j;

    for (i = 0; i < ms; i++) {

        for (j = 0; j < 125; j++) {

            delay_us(8);

        }

    }

}

 

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

/*                         I2C Timing Generation                              */

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

 

static void I2C_Start(void)

{

    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 void I2C_Stop(void)

{

    SDA_PIN = 0;

    delay_us(5);

    SCL_PIN = 1;

    delay_us(5);

    SDA_PIN = 1;

    delay_us(5);

}

 

static unsigned char I2C_WaitAck(void)

{

    unsigned char ack = 0;

    SDA_PIN = 1;

    delay_us(5);

    SCL_PIN = 1;

    delay_us(5);

    if (SDA_PIN) {

        ack = 1;

    }

    SCL_PIN = 0;

    delay_us(5);

    SDA_PIN = 0;

    return ack;

}

 

static void I2C_SendByte(unsigned char byte)

{

    unsigned char i;

    for (i = 0; i < 8; i++) {

        SCL_PIN = 0;

        delay_us(5);

        if (byte & 0x80) {

            SDA_PIN = 1;

        } else {

            SDA_PIN = 0;

        }

        byte <<= 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 i, byte = 0;

    SDA_PIN = 1;

    for (i = 0; i < 8; i++) {

        byte <<= 1;

        SCL_PIN = 1;

        delay_us(5);

        if (SDA_PIN) {

            byte |= 0x01;

        }

        SCL_PIN = 0;

        delay_us(5);

    }

    SDA_PIN = 0;

    if (ack) {

        SDA_PIN = 0;

    } else {

        SDA_PIN = 1;

    }

    delay_us(5);

    SCL_PIN = 1;

    delay_us(5);

    SCL_PIN = 0;

    delay_us(5);

    return byte;

}

 

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

/*                       High-Level I2C Operations                            */

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

 

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

{

    I2C_Start();

    I2C_SendByte(SENCOCH_I2C_ADDR << 1);

    if (I2C_WaitAck()) {

        I2C_Stop();

        return 1;

    }

    I2C_SendByte(reg_addr);

    if (I2C_WaitAck()) {

        I2C_Stop();

        return 1;

    }

    I2C_Start();

    I2C_SendByte((SENCOCH_I2C_ADDR << 1| 0x01);

    if (I2C_WaitAck()) {

        I2C_Stop();

        return 1;

    }