Post Go back to editing

Read the MAX30102 data to -999,Request help

Category: Software

The program code is as follows:

int main(void)
{
/* USER CODE BEGIN 1 */

char ach_i2c_data1[6];

uint32_t un_min, un_max, un_prev_data;
int i;
int32_t n_brightness;
float f_temp;
uint8_t temp_num=0;
uint8_t temp[6];
uint8_t str[100];

/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */

IIC_Init();

if(maxim_max30102_reset())//复位 MAX30102
{}
// printf("max30102_reset failed!\r\n");
if(maxim_max30102_read_reg(REG_INTR_STATUS_1,&uch_dummy))//read and clear status register
{}
// printf("read_reg REG_INTR_STATUS_1 failed!\r\n");
if(maxim_max30102_init())//初始化MAX30102
{}
// printf("max30102_init failed!\r\n");

id = max30102_Bus_Read(REG_PART_ID); //Read ID is normal  OK



/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
un_min=0x3FFFF;
un_max=0;
unsigned char uch_temp;

//read the first 100 samples, and determine the signal range
for (i = 0; i < BUFFER_SIZE; i++)
{
while (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_15)== GPIO_PIN_SET) ;
// ff = max30102_Bus_Read(REG_INTR_STATUS_1);
// while(ff & 0x40 != 0X40);

maxim_max30102_read_fifo((aun_red_buffer + i), ( aun_ir_buffer + i)); //read from MAX30102 FIFO

if(un_min>aun_red_buffer[i])
un_min=aun_red_buffer[i]; //update signal min
if(un_max<aun_red_buffer[i])
un_max=aun_red_buffer[i]; //update signal max
}

//calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
rf_heart_rate_and_oxygen_saturation(aun_ir_buffer, BUFFER_SIZE, aun_red_buffer, &n_spo2, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid, &ratio, &correl);

while(1)
{
i=0;
un_min=0x3FFFF;
un_max=0;

//dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
for (i = 25; i < 100; i++)
{
aun_red_buffer[i - 25] = aun_red_buffer[i];
aun_ir_buffer[i - 25] = aun_ir_buffer[i];

//update the signal min and max
if(un_min>aun_red_buffer[i])
un_min=aun_red_buffer[i];
if(un_max<aun_red_buffer[i])
un_max=aun_red_buffer[i];
}

//take 25 sets of samples before calculating the heart rate.
for (i = 75; i < 100; i++)
{

while (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_15)== GPIO_PIN_SET ) ;
// ff = max30102_Bus_Read(REG_INTR_STATUS_1);
// while(ff & 0x40 != 0X40);
maxim_max30102_read_fifo((aun_red_buffer + i), ( aun_ir_buffer + i));//read from MAX30102 FIFO
}

rf_heart_rate_and_oxygen_saturation(aun_ir_buffer, BUFFER_SIZE, aun_red_buffer, &n_spo2, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid, &ratio, &correl);



}


}
/* USER CODE END 3 */
}

int8_t maxim_max30102_init(void)
{


max30102_Bus_Write(REG_MODE_CONFIG,0x40);
HAL_Delay(20);

max30102_Bus_Write(REG_INTR_ENABLE_1,0xc0); // INTR setting 不使用接近功能,SPO2与HR理解开始
max30102_Bus_Write(REG_INTR_ENABLE_2,0x00);

max30102_Bus_Write(REG_FIFO_WR_PTR,0x00); //FIFO_WR_PTR[4:0]
max30102_Bus_Write(REG_OVF_COUNTER,0x00); //OVF_COUNTER[4:0]
max30102_Bus_Write(REG_FIFO_RD_PTR,0x00); //FIFO_RD_PTR[4:0]

max30102_Bus_Write(REG_FIFO_CONFIG,0x0f); //sample avg = 1, fifo rollover=false, fifo almost full = 17
max30102_Bus_Write(REG_MODE_CONFIG,0x03); //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
max30102_Bus_Write(REG_SPO2_CONFIG,0x27); // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS)
max30102_Bus_Write(REG_LED1_PA,0x24); //Choose value for ~ 7mA for LED1
max30102_Bus_Write(REG_LED2_PA,0x24); // Choose value for ~ 7mA for LED2
max30102_Bus_Write(REG_PILOT_PA,0x7f); // Choose value for ~ 25mA for Pilot LED

max30102_Bus_Write(REG_TEMP_CONFIG,0X01);

max30102_Bus_Read(REG_INTR_STATUS_1);
max30102_Bus_Read(REG_INTR_STATUS_2);

}

The following is a logic analyzer waveform diagram:

0 ----INT

1 ---SDA

2 ---SCL

THANKS!

  • int8_t maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led)
    {
    uint32_t un_temp;
    unsigned char uch_temp;
    char ach_i2c_data[6];
    uint8_t Ack1,Ack2,Ack3;
    *pun_red_led=0;
    *pun_ir_led=0;


    //read and clear status register
    maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_temp);
    maxim_max30102_read_reg(REG_INTR_STATUS_2, &uch_temp);


    ach_i2c_data[0]=REG_FIFO_DATA;

    IIC_Start();
    IIC_Send_Byte(I2C_WRITE_ADDR); //发送设备写地址
    Ack1 = IIC_Wait_Ack();
    IIC_Send_Byte(ach_i2c_data[0]); //发送寄存器地址
    Ack2 = IIC_Wait_Ack();

    IIC_Start();
    IIC_Send_Byte(I2C_READ_ADDR); //发送设备读地址
    Ack3 = IIC_Wait_Ack();


    //un_temp=(unsigned char) ach_i2c_data[0];
    un_temp = IIC_Read_Byte(0);//读取
    un_temp<<=16;
    *pun_red_led+=un_temp;
    //un_temp=(unsigned char) ach_i2c_data[1];
    un_temp = IIC_Read_Byte(0);//读取
    un_temp<<=8;
    *pun_red_led+=un_temp;
    //un_temp=(unsigned char) ach_i2c_data[2];
    un_temp = IIC_Read_Byte(0);//读取
    *pun_red_led+=un_temp;

    // un_temp=(unsigned char) ach_i2c_data[3];
    un_temp = IIC_Read_Byte(0);//读取
    un_temp<<=16;
    *pun_ir_led+=un_temp;
    //un_temp=(unsigned char) ach_i2c_data[4];
    un_temp = IIC_Read_Byte(0);//读取
    un_temp<<=8;
    *pun_ir_led+=un_temp;
    //un_temp=(unsigned char) ach_i2c_data[5];
    un_temp = IIC_Read_Byte(0);//读取
    IIC_Stop();//产生停止

    *pun_ir_led+=un_temp;

    *pun_red_led&=0x03FFFF; //Mask MSB [23:18]
    *pun_ir_led&=0x03FFFF; //Mask MSB [23:18]

    if(Ack1 || Ack2 || Ack3)//如果
    return -1; //发送失败
    else
    return 0; //发送成功
    }

  • New progress, the data is read, but the blood oxygen and heart rate do not show 0