STM32项目分享:基于单片机的心率血氧监测设计

“我们不做一锤子买卖,只做技术成长的长期伙伴!”

目录

一、视频展示

二、项目简介

三、原理图设计

四、PCB硬件设计

五、程序设计

六、资料分享


一、视频展示

基于单片机便携式心率监测系统 -视频分享

二、项目简介

该设计由STM32F103C8T6作为主控芯片和蜂鸣器电路以及供电电路,0.96oled显示屏,3个独立按键,MAX30102心率血氧模块,DS18B20温度传感器模块。
本设计的功能:
1、 屏幕显示
        用来显示当前心率血氧温度和阈值。
 2、心率血氧检测
        通过MAX30102心率血氧模块检测当前心率血氧。
3、阈值设置
        在主页面按下设置键会进入阈值设置界面,有三个阈值设置界面,分别是是血氧设置下限,心率阈值设置上限,和温度阈值上限,在任意一个阈值设置界面按下+键会对对应的阈值进行数值增加操作,按下-键会对阈值数值进行减操作。当在第二个阈值修改界面时按下设置键会重新回到初始的数据显示页面。
4、警报系统
        当任何一个测的数据不在阈值内,会触发警报,开启蜂鸣器报警,直到恢复正常阈值范围内。

三、原理图设计

四、PCB硬件设计

五、程序设计

#include "stm32f10x.h"                  // Device header
#include "delay.h"
#include "lcd.h"
#include "timer.h"
#include "IOput.h"
#include "max30102.h" 
#include "myiic.h"
#include "algorithm.h"
#include "ds18b20.h"
#include "usart.h"

short zj;
float wd;

u8 show_flag;

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;   //indicator to show if the SP02 calculation is valid
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;
u8 dis_hr=0,dis_spo2=0;

u8 max_hr = 140,min_spo2 = 90,max_wd = 38;
#define MAX_BRIGHTNESS 255
//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 str[100];

void dis_DrawCurve(u32* data,u8 x);

void MAX30102_Read_Data(void)
{
	
	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=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];
			}
	//take 100 sets of samples before calculating the heart rate.
			for(i=400;i<500;i++)
			{
				un_prev_data=aun_red_buffer[i-1];
				while(MAX30102_INT==1);
				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(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;
				}
				//send samples and calculation result to terminal program through 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;
				}		
			}
					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);
			
			//显示刷新
			if(dis_hr == 0 && dis_spo2 == 0)  //**dis_hr == 0 && dis_spo2 == 0
			{
	//			sprintf((char *)str,"HR:--- SpO2:--- ");//**HR:--- SpO2:--- 
				OLED_ShowString(48,2,"---",16);
				OLED_ShowString(48,4,"---",16);
			}
			else{
	//			sprintf((char *)str,"HR:%3d SpO2:%3d ",dis_hr,dis_spo2);//**HR:%3d SpO2:%3d 
				OLED_ShowNum(48,2,dis_hr,3,16);
				OLED_ShowNum(48,4,dis_spo2,3,16);
			}
	
}

void Show_State(void)
{
	OLED_ShowCHinese(0,0,0);
	OLED_ShowCHinese(21,0,1);
	OLED_ShowCHinese(42,0,2);
	OLED_ShowCHinese(63,0,3);
	OLED_ShowCHinese(84,0,4);
	OLED_ShowCHinese(105,0,5);  //夜间监控系统
	
	OLED_ShowCHinese(0,2,0);
	OLED_ShowCHinese(16,2,1);
	OLED_ShowChar(40,2,':',16);
	
	OLED_ShowCHinese(0,4,2);
	OLED_ShowCHinese(16,4,3);
	OLED_ShowChar(40,4,':',16);
	
	OLED_ShowCHinese(0,6,6);
	OLED_ShowCHinese(16,6,7);
	OLED_ShowChar(40,6,':',16);
	
	
}

int main (void)
{

	
	delay_init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

	
	OLED_Init();
	delay_ms(1);
	OLED_Clear();
	input_init();
	output_init();
	DS18B20_Init();

	TIM2_Int_Init(71,9999);

	max30102_init();
	
	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(MAX30102_INT==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)
	{
		zj=DS18B20_Get_Temp();
		wd=zj/10+(zj%10)*0.1;
		key_scan(1,0,0);
		if(set_flag == 1)
		{
			set_flag = 0;
			show_flag ++;
			if(show_flag >= 4)
				show_flag = 0;
		}
		if(show_flag == 1)
		{
			OLED_ShowCH(0,0,"  阈 值 修 改 ");
			OLED_ShowCH(0,4,"心率:");
			OLED_ShowNum(40,4,max_hr,3,16);
			if(dec_flag == 1)
			{
				dec_flag = 0;
				max_hr --;
			}
			if(add_flag == 1)
			{
				add_flag = 0;
				max_hr ++;
			}
		}
		if(show_flag == 2)
		{
			OLED_ShowCH(0,0,"  阈 值 修 改 ");
			OLED_ShowCH(0,4,"血氧:");
			OLED_ShowNum(40,4,min_spo2,3,16);
			if(dec_flag == 1)
			{
				dec_flag = 0;
				min_spo2 --;
			}
			if(add_flag == 1)
			{
				add_flag = 0;
				min_spo2 ++;
			}
		}
		if(show_flag == 3)
		{
			OLED_ShowCH(0,0,"  阈 值 修 改 ");
			OLED_ShowCH(0,4,"温度:");
			OLED_ShowNum(40,4,max_wd,3,16);
			if(dec_flag == 1)
			{
				dec_flag = 0;
				max_wd --;
			}
			if(add_flag == 1)
			{
				add_flag = 0;
				max_wd ++;
			}
		}
		if(show_flag == 0)
		{
			if((dis_hr > max_hr) || ((dis_spo2 > 0) && (dis_spo2 < min_spo2)) || (wd > max_wd))  //**dis_hr == 0 && dis_spo2 == 0
				BEEP = 0;
			else
				BEEP = 1;
			Show_State();
			
			MAX30102_Read_Data();
			
			
			OLED_Showdecimal(48,6,wd,2,2,16);

		}
	}
}


六、资料分享

点击即可查看当前资料分享。

### STM32 心率血氧手环项目 示例代码与资源 #### 1. 系统初始化 为了使STM32单片机能够正常工作并与其他外围设备交互,系统初始化至关重要。这一步骤通常包括配置时钟树、启用必要的外设时钟以及初始化GPIO引脚。 ```c void System_Init(void) { // 配置HSE作为PLL输入源, 设置PLL倍频系数等参数 RCC_OscInitTypeDef osc_init; RCC_ClkInitTypeDef clk_init; HAL_RCC_DeInit(); osc_init.OscillatorType = RCC_OSCILLATORTYPE_HSE; osc_init.HSEState = RCC_HSE_ON; osc_init.PLL.PLLState = RCC_PLL_ON; osc_init.PLL.PLLSource = RCC_PLLSOURCE_HSE; osc_init.PLL.PLLMUL = RCC_PLL_MUL9; // PLL频率= HSE/PLL_M * PLL_N / PLL_P if (HAL_RCC_OscConfig(&osc_init) != HAL_OK) { Error_Handler(); } clk_init.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; clk_init.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; clk_init.AHBCLKDivider = RCC_SYSCLK_DIV1; clk_init.APB1CLKDivider = RCC_HCLK_DIV2; clk_init.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&clk_init, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } ``` #### 2. MAX30102 初始化 MAX30102 是一款集成化的光电容积描记(PPG)信号处理芯片,用于实现心率血氧饱和度(SpO2)的测量。其初始化过程涉及设置采样速率、LED电流强度以及其他操作模式的选择。 ```c void MX_MAX30102_Init(void){ uint8_t reg_data; /* Reset the sensor */ write_byte(MAX30102_REG_MODE_CONFIG, MODE_RESET); delay_ms(1); /* Set LED pulse width to 411us and sample rate to 100 SPS */ write_byte(MAX30102_REG_SPO2_CONFIG, ADC_RANGE_4096 | SAMPLE_AVG_4 | LED_PWM_411); /* Enable SpO2 mode with both red & IR LEDs enabled */ write_byte(MAX30102_REG_MODE_CONFIG, SPO2_EN | HR_EN | SHDN_BIT_OFF); /* Configure FIFO settings */ write_byte(MAX30102_REG_FIFO_CONFIG, FIFO_ROLLOVER_EN | FIFO_A_FULL_THD_32); } ``` #### 3. 数据采集与处理 由于MAX30102返回的是原始ADC数值而非直接的心率或SpO2百分比值,因此需要进一步计算来获取这些生理指标。这部分逻辑可以放在定时中断服务函数(ISR)内执行周期性的数据读取与分析。 ```c uint32_t heart_rate = 0; float spo2_level = 0.0f; void Timer_ISR(void){ static int buffer_index = 0; static float ir_buffer[BUFFER_SIZE]; static float red_buffer[BUFFER_SIZE]; while(FIFO_Readable()){ fifo_data raw_data; read_fifo(&raw_data); ir_buffer[buffer_index] = raw_data.ir; red_buffer[buffer_index++] = raw_data.red; if(buffer_index >= BUFFER_SIZE){ calculate_hr_and_spo2(ir_buffer, red_buffer, &heart_rate, &spo2_level); buffer_index = 0; } } } // 计算HR 和 SpO2 的算法省略... ``` #### 4. OLED 显示屏驱动 对于采用SSD1306控制器的小尺寸OLED屏幕而言,可以通过SPI/IIC接口轻松完成图形化界面的设计与刷新。这里给出一段简单的文字打印例子: ```cpp #include "ssd1306.h" void Display_Info(uint32_t hr, float spO2){ SSD1306_Clear(); char str[20]; sprintf(str,"Heart Rate:%lu",hr); SSD1306_DrawString(0, 0, str); sprintf(str,"SPO2 Level:%.1f%%",spO2); SSD1306_DrawString(0, 16, str); SSD1306_UpdateScreen(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值