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>

#포함 <math.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 ms)

{

    unsigned int 나, 제이;

    ~을 위한 (나= 0; 나< ms; 나++) {

        ~을 위한 (제이= 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_중지(무효의)

{

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

        만약 (바이트& 0x80) {

            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) {

            바이트|= 0x01;

        }

        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_중지();

        반품 1;

    }

    I2C_SendByte(reg_addr);

    만약 (I2C_WaitAck()) {

        I2C_중지();

        반품 1;

    }

    I2C_시작();

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

    만약 (I2C_WaitAck()) {

        I2C_중지();

        반품 1;

    }