在STM32F103上使用MAX30102,使用模拟IIC进行通讯,GPIO口已成功初始化,30102的寄存器也已成功配置之后,在调用读取FIFO寄存器的函数max30102_FIFO_ReadBytes(REG_FIFO_DATA,temp);时,读到的数组temp每个元素一直都是255。
代码如下:
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "stdio.h"
#include "usart.h"
#include "mlx90614.h"
#include "YW03.h"
#include "SD01.h"
#include "max30102.h"
#include "algorithm.h"
#include "YW01.h"
#include "usart2.h"
#include "common.h"
#include "wifista.h"
#include "malloc.h"
#include "stdlib.h"
//ALIENTEK miniSTM32?????1
//????:www.openedv.com
//?????????????
extern u16 TIM3CH1_CAPTURE_STA; //输入捕获状态
extern u16 TIM2CH1_CAPTURE_VAL;//输入捕获值
uint32_t aun_ir_buffer[500]; //IR LED sensor data
int32_t n_ir_buffer_length; //data length
uint32_t aun_red_buffer[500]; //Red LED sensor data
int32_t n_sp02; //SPO2 value
int8_t ch_spo2_valid; //指示器,用于显示 SP02 计算是否 有效
int32_t n_heart_rate; //heart rate value
int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid
uint8_t uch_dummy;
#define MAX_BRIGHTNESS 255
void dis_DrawCurve(u32* data,u8 x);
int main(void)
{
/*******************************************************************************
*
* 辅助变量
*
*******************************************************************************/
// u8* msg;
/*******************************************************************************
*
* MAX30102采样变量
*
*******************************************************************************/
//variables to calculate the on-board LED brightness that reflects the heartbeats
uint32_t un_min, un_max, un_prev_data;
int i;
int32_t n_brightness;
float f_temp;
// u8 temp_num=0;
u8 temp[6];
u8 statue1;
u8 m;
// u8 str[100];
// u8 dis_hr=0,dis_spo2=0;
/*******************************************************************************
*
* 采样数据
*
*******************************************************************************/
// u8 blood_oxygen = 50; //血氧
// u8 heart_rate = 87; //心率
float temperature = 38.5; //体温
// u8 drip_rate =40; //滴速
// u8 liquid_level = 78; //液位
/*******************************************************************************
*
* 初始化函数
*
*******************************************************************************/
delay_init(); //延时函数
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断分组
uart_init(9600); //电脑串口通信
//传感器初始化
// SD01_Init(); //滴速检测模块
mlx90614_Init(); //体温检测模块
// YW01_Init(); //液位检测模块
// USART2_Init(115200); //wifi模块通信串口初始化
// atk_8266_test(); //检查wifi模块
// atk_8266_at_response(1);//检查ATK-ESP8266模块发送过来的数据,及时上传给电脑
// atk_8266_wifista_test();//连接阿里云物联网
// msg = malloc(180);
statue1 = max30102_init();
printf("\nsuccess?%u\n",statue1);
/*******************************************************************************
*
* max30102准备
*
*******************************************************************************/
un_min=0x3FFFF;
un_max=0;
n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps
//read the first 500 samples, and determine the signal range
for(i=0;i<n_ir_buffer_length;i++)
{
while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) ==1); //wait until the interrupt pin asserts
max30102_FIFO_ReadBytes(REG_FIFO_DATA,temp);
aun_red_buffer[i] = (long)((long)((long)temp[0]&0x03)<<16) | (long)temp[1]<<8 | (long)temp[2]; // Combine values to get the actual number
aun_ir_buffer[i] = (long)((long)((long)temp[3] & 0x03)<<16) |(long)temp[4]<<8 | (long)temp[5]; // Combine values to get the actual number
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
}
un_prev_data=aun_red_buffer[i];
//calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);
while(1)
{
i=0;
un_min=0x3FFFF;
un_max=0;
//将前 100 组样本转储到内存中,并将最后 400 组样本移到顶部
for(i=100;i<500;i++)
{
aun_red_buffer[i-100]=aun_red_buffer[i];
aun_ir_buffer[i-100]=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];
}
//在计算心率之前取100组样本。
for(i=400;i<500;i++)
{
un_prev_data=aun_red_buffer[i-1];
while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) ==1);
max30102_FIFO_ReadBytes(REG_FIFO_DATA,temp);
printf("\n");
for(m=0;m<6;m++)
{
printf("temp[%u]:%u,",m,temp[m]);
}
aun_red_buffer[i] = (long)((long)((long)temp[0]&0x03)<<16) | (long)temp[1]<<8 | (long)temp[2]; // 组合值以获得 这实际数量
aun_ir_buffer[i] = (long)((long)((long)temp[3] & 0x03)<<16) |(long)temp[4]<<8 | (long)temp[5]; // Combine values to get the actual number
printf("\naun_red_buffer[%u]=%u,aun_ir_buffer[%u]=%u",i,aun_red_buffer[i],i,aun_ir_buffer[i]);
if(aun_red_buffer[i]>un_prev_data)
{
f_temp=aun_red_buffer[i]-un_prev_data;
f_temp/=(un_max-un_min);
f_temp*=MAX_BRIGHTNESS;
n_brightness-=(int)f_temp;
if(n_brightness<0)
n_brightness=0;
}
else
{
f_temp=un_prev_data-aun_red_buffer[i];
f_temp/=(un_max-un_min);
f_temp*=MAX_BRIGHTNESS;
n_brightness+=(int)f_temp;
if(n_brightness>MAX_BRIGHTNESS)
n_brightness=MAX_BRIGHTNESS;
}
//通过UART将样品和计算结果发送到终端程序
// if(ch_hr_valid == 1 && n_heart_rate<120)//**/ ch_hr_valid == 1 && ch_spo2_valid ==1 && n_heart_rate<120 && n_sp02<101
// {
// dis_hr = n_heart_rate;
// dis_spo2 = n_sp02;
// }
// else
// {
// dis_hr = 0;
// dis_spo2 = 0;
// }
}
printf("HR=%i, ", n_heart_rate);
printf("HRvalid=%i, ", ch_hr_valid);
printf("SpO2=%i, ", n_sp02);
printf("SPO2Valid=%i\r\n", ch_spo2_valid);
temperature = ReadTemp();
printf("temperature=%f\r\n", temperature);
maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);
}
// while(1)
// {
//
//// heart_rate = read_heart_rate();
// temperature = ReadTemp();
//// drip_rate = get_drip_rate();
//// liquid_level = Read_Level_Liquid();
//
// printf("speed:%u\n",drip_rate);
// printf("heart rate:%u\n",heart_rate);
// printf("temperature:%f\n",temperature);
// printf("drip_rate:%u\n",drip_rate);
// printf("level:%u\n",liquid_level);
//
// //发送数据并接收信息
//// sprintf((char*)msg, "AT+MQTTPUB=0,\"/k1bup6fRrSA/ESP8266/user/update\",\"{\\\"bloodOxygen\\\":%u\\,\\\"liquidResidue\\\":%u\\,\\\"heartRate\\\":%u\\,\\\"temperature\\\":%f\\,\\\"dripRate\\\":%u}\",1,0",blood_oxygen,liquid_level,heart_rate,temperature,drip_rate);
//// atk_wifista_communication(msg);//接收
// printf("\n\n\n\n");
// delay_ms(5000);
//
//
// }
}