这个项目是正点原子精英版驱动max30102传感器,画出心电图。
使用i2c驱动
引脚接线:
PA5--->INT
PB6--->SDA
PB7--->CLS
VIN---->3.3V
GND--->GND
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "myiic.h"
#include "max30102.h"
#include "algorithm.h"
/************************************************
ALIENTEK精英STM32开发板实验22
IIC 实验
技术支持:www.openedv.com
淘宝店铺:http://eboard.taobao.com
关注微信公众平台微信号:"正点原子",免费获取STM32资料。
广州市星翼电子科技有限公司
作者:正点原子 @ALIENTEK
************************************************/
#define MAX_BRIGHTNESS 255
#define START 100
#define DATA_LENGTH 500
uint32_t aun_ir_buffer[DATA_LENGTH]; //IR LED sensor data
int32_t n_ir_buffer_length; //data length
uint32_t aun_red_buffer[DATA_LENGTH]; //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;
int main(void)
{
uint32_t un_min, un_max, un_prev_data; //变量来计算反映心跳的板载LED亮度 variables to calculate the on-board LED brightness that reflects the heartbeats
uint32_t x_d[2];
int out_number=0;
int i,x=0;
int32_t n_brightness;
float f_temp;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //初始化与LED连接的硬件接口
LCD_Init(); //初始化LCD
IIC_Init();
maxim_max30102_reset();
maxim_max30102_read_reg(0,&uch_dummy);
maxim_max30102_init();
LCD_Clear(BLACK);
BACK_COLOR=BLACK;
POINT_COLOR=BLUE;//设置字体为蓝色
n_brightness=0;
un_min=0x3FFFF;
un_max=0;
n_ir_buffer_length=DATA_LENGTH;
//读取前500个样本,并确定信号范围
for(i=0;i<n_ir_buffer_length;i++)
{
while(PAin(5)==1);
maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));
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]; //更新最大值
printf("red=%i,", aun_red_buffer[i]);
printf("ir=%i\r\n", aun_ir_buffer[i]);
}
un_prev_data=aun_red_buffer[i];
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;
for(i=START;i<DATA_LENGTH;i++)
{
aun_red_buffer[i-START]=aun_red_buffer[i];
aun_ir_buffer[i-START]=aun_ir_buffer[i];
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];
}
for(i=400;i<DATA_LENGTH;i++)
{
un_prev_data=aun_red_buffer[i-1];
while(PAin(5)==1);
maxim_max30102_read_fifo((aun_red_buffer+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;
}
// printf("red=%i,", aun_red_buffer[i]);
// printf(" ir=%i,", aun_ir_buffer[i]);
}
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);
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);
if(ch_hr_valid)
{
x_d[0]=x_d[1];
x_d[1]=n_heart_rate*2;
LCD_ShowxNum(100, 100, n_heart_rate, 3, 16, 0);
}
else
{
x_d[0]=x_d[1];
x_d[1]=50;
}
LCD_DrawLine(x_d[0],x,x_d[1],x+3);
x+=3;
if(x>320)
{LCD_Clear(BLACK);
x=0;
delay_ms(250);
}
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);
}
}
点点关注,代码放下面了
链接:https://pan.baidu.com/s/1g_RXgeFwhAYrnfCex9b8ow?pwd=krvd
提取码:krvd