一、项目介绍
当前文章介绍基于STM32设计的人体健康检测仪。设备采用STM32系列MCU作为主控芯片,配备血氧浓度传感器(使用MAX30102血氧浓度检测传感器)、OLED屏幕和电池供电等外设模块。设备可以广泛应用于医疗、健康等领域。可以帮助医生和病人更好地了解病情变化,提高治疗效果和生活质量。设备也可以用于健康管理、运动监测等场景,帮助用户了解自己的身体状况,保持健康的生活方式。
在项目中,使用了KEIL作为开发平台和工具,通过血氧模块采集人体的心跳和血氧浓度参数,并通过OLED屏幕显示现在的心跳和血氧浓度。同时,通过指标分析,提供采集到的数据与正常指标比对,分析被检测人员的健康状态。采集的数据可通过蓝牙或者WIFI传递给手机APP进行处理,方便用户随时了解自己的身体状况。
本设计采用STM32为主控芯片,搭配血氧浓度传感器和OLED屏幕,实现了人体健康数据的采集和展示,并对采集到的数据进行分析,判断被检测人员的健康状态。同时,设计使用蓝牙或WiFi将采集到的数据传递给手机APP进行处理。
二、项目设计思路
2.1 硬件设计
(1)主控芯片:STM32系列MCU,负责驱动其他外设模块;
(2)血氧浓度传感器:使用MAX30102血氧浓度检测传感器,用于采集人体的心跳和血氧浓度参数;
(3)OLED屏:用于显示现在的心跳和血氧浓度;
2.2 软件设计
(1) 通过血氧模块采集人体的心跳和血氧浓度参数;
(2) 通过OLED屏显示现在的心跳和血氧浓度;
(3) 对采集到的数据进行指标分析,将采集到的数据与正常指标比对,分析被检测人员的健康状态;
(4) 采集的数据可通过蓝牙或WiFi传递给手机APP进行处理。
2.3 技术实现
(1)设计采用AD8232心电图(ECG)模块和MAX30102血氧模块采集心跳和血氧浓度参数,并通过I2C接口连接主控芯片STM32。
(2)OLED屏使用I2C接口与主控芯片STM32连接。
(3)采集到的数据通过算法进行指标分析,将采集到的数据与正常指标比对,判断被检测人员的健康状态。
(4)设备通过蓝牙或WiFi将采集到的数据传递给手机APP进行处理。
三、代码设计
3.1 MAX30102血氧模块代码
I2C协议代码:
#define MAX30102_I2C_ADDR 0xAE
void MAX30102_I2C_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
/* Enable GPIOB clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Enable I2C1 and I2C2 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 | RCC_APB1Periph_I2C2, ENABLE);
// Configure I2C SCL and SDA pins
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; // Open-drain output
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure I2C parameters
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000; // 100KHz
I2C_Init(I2C1, &I2C_InitStructure);
// Enable I2C
I2C_Cmd(I2C1, ENABLE);
}
void MAX30102_I2C_WriteReg(uint8_t reg, uint8_t value)
{
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, MAX30102_I2C_ADDR, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, reg);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, value);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
}
uint8_t MAX30102_I2C_ReadReg(uint8_t reg)
{
uint8_t value;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, MAX30102_I2C_ADDR, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, reg);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, MAX30102_I2C_ADDR, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_AcknowledgeConfig(I2C1, DISABLE);
value = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
return value;
}
void MAX30102_I2C_ReadArray(uint8_t reg, uint8_t* data, uint8_t len)
{
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, MAX30102_I2C_ADDR, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, reg);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, MAX30102_I2C_ADDR, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while(len > 1)
{
I2C_AcknowledgeConfig(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
*data++ = I2C_ReceiveData(I2C1);
len--;
}
I2C_AcknowledgeConfig(I2C1, DISABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
*data++ = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
}
MAX30102的初始化函数和数据获取函数:
void MAX30102_Init(void)
{
MAX30102_I2C_Init();
// Reset the device
MAX30102_I2C_WriteReg(0x09, 0x40);
HAL_Delay(100);
MAX30102_I2C_WriteReg(0x09, 0x00);
// Set FIFO average to 4 samples
MAX30102_I2C_WriteReg(0x08, 0x03);
// Set LED pulse amplitude
MAX30102_I2C_WriteReg(0x0C, 0x1F);
MAX30102_I2C_WriteReg(0x0D, 0x1F);
// Set sample rate to 100Hz
MAX30102_I2C_WriteReg(0x0F, 0x04);
// Enable the red LED only
MAX30102_I2C_WriteReg(0x11, 0x02);
// Read the temperature value to start a reading
MAX30102_I2C_ReadReg(0x1F);
}
uint32_t MAX30102_GetHeartRate(void)
{
uint8_t buffer[MAX30102_FIFO_DEPTH*4];
MAX30102_Data sensor_data = {0};
uint16_t ir_value;
uint16_t red_value;
uint8_t byte_count, fifo_overflow;
// Check if any data is available in FIFO
byte_count = MAX30102_I2C_ReadReg(0x06) - MAX30102_I2C_ReadReg(0x04);
if(byte_count > 0)
{
fifo_overflow = MAX30102_I2C_ReadReg(0x09) & 0x80;
// Read the data from FIFO
MAX30102_I2C_ReadArray(0x07, buffer, byte_count);
// Parse the data
for(int i=0; i< byte_count; i+=4)
{
ir_value = ((uint16_t)buffer[i] < < 8) | buffer[i+1];
red_value = ((uint16_t)buffer[i+2] < < 8) | buffer[i+3];
// Update the sensor data
MAX30102_UpdateData(&sensor_data, ir_value, red_value);
}
if(!fifo_overflow && MAX30102_CheckForBeat(sensor_data.IR_AC_Signal_Current))
{
return MAX30102_HeartRate(sensor_data.IR_AC_Signal_Previous, 16);
}
}
return 0;
}
数据处理函数:
void MAX30102_UpdateData(MAX30102_Data* data, uint16_t ir_value, uint16_t red_value)
{
int32_t ir_val_diff = ir_value - data- >IR_AC_Signal_Current;
int32_t red_val_diff = red_value - data- >Red_AC_Signal_Current;
// Update IR AC and DC signals
data- >IR_AC_Signal_Current = (ir_val_diff + (7 * data- >IR_AC_Signal_Previous)) / 8;
data- >IR_DC_Signal_Current = (ir_value + data- >IR_AC_Signal_Current + (2 * data- >IR_DC_Signal_Current)) / 4;
data- >IR_AC_Signal_Previous = data- >IR_AC_Signal_Current;
// Update Red AC and DC signals
data- >Red_AC_Signal_Current = (red_val_diff + (7 * data- >Red_AC_Signal_Previous)) / 8;
data- >Red_DC_Signal_Current = (red_value + data- >Red_AC_Signal_Current + (2 * data- >Red_DC_Signal_Current)) / 4;
data- >Red_AC_Signal_Previous = data- >Red_AC_Signal_Current;
// Update IR and Red AC signal peak-to-peak values
if(data- >IR_AC_Signal_Current > data- >IR_AC_Max)
data- >IR_AC_Max = data- >IR_AC_Signal_Current;
else if(data- >IR_AC_Signal_Current < data- >IR_AC_Min)
data- >IR_AC_Min = data- >IR_AC_Signal_Current;
if(data- >Red_AC_Signal_Current > data- >Red_AC_Max)