找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 11400|回复: 18
收起左侧

stm32c8t6+max30102心率传感器+0.96 oled显示源码

  [复制链接]
ID:338948 发表于 2018-10-30 21:01 | 显示全部楼层 |阅读模式
max30102心率传感制作。
功能实现:采用STM32F103C8T6小板(bluepill板),驱动血氧心率传感器HXDZ-30102或HXDZ-30102-ACC(MAX30102),实现PPG信号采集,并将计算的心率和血氧值显示在0.96寸OLED和串口上。
软件实现:ST标准库3.5
硬件连接:
HXDZ-30102:
    VCC<->3.3V
    GND<->GND
    SCL<->PB7
    SDA<->PB8
    IM<->PB9
0.96inch OLED :
    VCC<->3.3V
    GND<->GND
    SCL<->PA5
    SDA<->PA6
    RST<->PA3
    DC<->PA4
    CS<->PA2
USB-TTL:
    5V<->5V
    GND<->GND
    RXD<->PA9
    TXD<->PA10

单片机源程序如下:
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "usart.h"
  5. #include "max30102.h"
  6. #include "myiic.h"
  7. #include "algorithm.h"
  8. #include "oled.h"

  9. uint32_t aun_ir_buffer[500]; //IR LED sensor data
  10. int32_t n_ir_buffer_length;    //data length
  11. uint32_t aun_red_buffer[500];    //Red LED sensor data
  12. int32_t n_sp02; //SPO2 value
  13. int8_t ch_spo2_valid;   //indicator to show if the SP02 calculation is valid
  14. int32_t n_heart_rate;   //heart rate value
  15. int8_t  ch_hr_valid;    //indicator to show if the heart rate calculation is valid
  16. uint8_t uch_dummy;

  17. #define MAX_BRIGHTNESS 255

  18. void dis_DrawCurve(u32* data,u8 x);

  19. int main(void)
  20. {
  21.         //variables to calculate the on-board LED brightness that reflects the heartbeats
  22.         uint32_t un_min, un_max, un_prev_data;  
  23.         int i;
  24.         int32_t n_brightness;
  25.         float f_temp;
  26.         u8 temp_num=0;
  27.         u8 temp[6];
  28.         u8 str[100];
  29.         u8 dis_hr=0,dis_spo2=0;

  30.         NVIC_Configuration();
  31.         delay_init();                     //延时函数初始化          
  32.         uart_init(115200);                 //串口初始化为115200
  33.         LED_Init();
  34.        
  35.         //OLED
  36.         OLED_Init();
  37.         OLED_ShowString(0,0,"  initializing  ",16);
  38.         OLED_Refresh_Gram();//更新显示到OLED         

  39.         max30102_init();

  40.         printf("\r\n MAX30102  init  \r\n");

  41.         un_min=0x3FFFF;
  42.         un_max=0;
  43.        
  44.         n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps
  45.         //read the first 500 samples, and determine the signal range
  46.     for(i=0;i<n_ir_buffer_length;i++)
  47.     {
  48.         while(MAX30102_INT==1);   //wait until the interrupt pin asserts
  49.         
  50.                 max30102_FIFO_ReadBytes(REG_FIFO_DATA,temp);
  51.                 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
  52.                 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
  53.             
  54.         if(un_min>aun_red_buffer[i])
  55.             un_min=aun_red_buffer[i];    //update signal min
  56.         if(un_max<aun_red_buffer[i])
  57.             un_max=aun_red_buffer[i];    //update signal max
  58.     }
  59.         un_prev_data=aun_red_buffer[i];
  60.         //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
  61.     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);
  62.        
  63.         while(1)
  64.         {
  65.                 i=0;
  66.         un_min=0x3FFFF;
  67.         un_max=0;
  68.                
  69.                 //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
  70.         for(i=100;i<500;i++)
  71.         {
  72.             aun_red_buffer[i-100]=aun_red_buffer[i];
  73.             aun_ir_buffer[i-100]=aun_ir_buffer[i];
  74.             
  75.             //update the signal min and max
  76.             if(un_min>aun_red_buffer[i])
  77.             un_min=aun_red_buffer[i];
  78.             if(un_max<aun_red_buffer[i])
  79.             un_max=aun_red_buffer[i];
  80.         }
  81.                 //take 100 sets of samples before calculating the heart rate.
  82.         for(i=400;i<500;i++)
  83.         {
  84.             un_prev_data=aun_red_buffer[i-1];
  85.             while(MAX30102_INT==1);
  86.             max30102_FIFO_ReadBytes(REG_FIFO_DATA,temp);
  87.                         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
  88.                         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
  89.         
  90.             if(aun_red_buffer[i]>un_prev_data)
  91.             {
  92.                 f_temp=aun_red_buffer[i]-un_prev_data;
  93.                 f_temp/=(un_max-un_min);
  94.                 f_temp*=MAX_BRIGHTNESS;
  95.                 n_brightness-=(int)f_temp;
  96.                 if(n_brightness<0)
  97.                     n_brightness=0;
  98.             }
  99.             else
  100.             {
  101.                 f_temp=un_prev_data-aun_red_buffer[i];
  102.                 f_temp/=(un_max-un_min);
  103.                 f_temp*=MAX_BRIGHTNESS;
  104.                 n_brightness+=(int)f_temp;
  105.                 if(n_brightness>MAX_BRIGHTNESS)
  106.                     n_brightness=MAX_BRIGHTNESS;
  107.             }
  108.                         //send samples and calculation result to terminal program through UART
  109.                         if(ch_hr_valid == 1 && ch_spo2_valid ==1 && n_heart_rate<120 && n_sp02<101)
  110.                         {
  111.                                 dis_hr = n_heart_rate;
  112.                                 dis_spo2 = n_sp02;
  113.                         }
  114.                         else
  115.                         {
  116.                                 dis_hr = 0;
  117.                                 dis_spo2 = 0;
  118.                         }
  119.                                 printf("HR=%i, ", n_heart_rate);
  120.                                 printf("HRvalid=%i, ", ch_hr_valid);
  121.                                 printf("SpO2=%i, ", n_sp02);
  122.                                 printf("SPO2Valid=%i\r\n", ch_spo2_valid);
  123.                 }
  124.         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);
  125.                
  126.                 //显示刷新
  127.                 LED0=0;
  128.                 if(dis_hr == 0 && dis_spo2 == 0)
  129.                 {
  130.                         sprintf((char *)str,"HR:--- SpO2:--- ");
  131.                 }
  132.                 else{
  133.                         sprintf((char *)str,"HR:%3d SpO2:%3d ",dis_hr,dis_spo2);
  134.                 }
  135.                 OLED_ShowString(0,0,str,16);
  136.                 OLED_Fill(0,23,127,63,0);
  137.                 //红光在上,红外在下
  138.                 dis_DrawCurve(aun_red_buffer,20);
  139.                 dis_DrawCurve(aun_ir_buffer,0);
  140.                 OLED_Refresh_Gram();//更新显示到OLED         
  141.         }
  142. }

  143. void dis_DrawCurve(u32* data,u8 x)
  144. {
  145.         u16 i;
  146.         u32 max=0,min=262144;
  147.         u32 temp;
  148.         u32 compress;
  149.        
  150.         for(i=0;i<128*2;i++)
  151.         {
  152.                 if(data[i]>max)
  153.                 {
  154.                         max = data[i];
  155.                 }
  156.                 if(data[i]<min)
  157.                 {
  158.                         min = data[i];
  159.                 }
  160.         }
  161.        
  162.         compress = (max-min)/20;
  163.        
  164.         for(i=0;i<128;i++)
  165.         {
  166.                 temp = data[i*2] + data[i*2+1];
  167.                 temp/=2;
  168.                 temp -= min;
  169.                 temp/=compress;
  170.                 if(temp>20)temp=20;
  171.                 OLED_DrawPoint(i,63-x-temp,1);
  172.         }
  173. }
复制代码

所有资料51hei提供下载:
MAX30102.rar (320.96 KB, 下载次数: 629)

评分

参与人数 2黑币 +80 收起 理由
ruyuer + 30 很给力!
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:392947 发表于 2018-10-31 09:17 | 显示全部楼层
学习一下
回复

使用道具 举报

ID:263750 发表于 2018-11-1 14:51 | 显示全部楼层
有实物图吗?
回复

使用道具 举报

ID:349706 发表于 2018-11-2 17:51 | 显示全部楼层
找到的30102官方测试程序,共享一下

max30102测试程序.rar

5.48 MB, 下载次数: 190, 下载积分: 黑币 -5

回复

使用道具 举报

ID:247041 发表于 2018-12-20 20:53 | 显示全部楼层
这个的采样率是多少
回复

使用道具 举报

ID:491006 发表于 2019-3-15 10:44 | 显示全部楼层
学习一下
回复

使用道具 举报

ID:139621 发表于 2019-5-27 22:12 | 显示全部楼层
谢谢分享,学习一下!
回复

使用道具 举报

ID:543151 发表于 2019-6-10 19:27 | 显示全部楼层
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);
69行的这个函数在哪里
回复

使用道具 举报

ID:399735 发表于 2020-2-21 14:24 | 显示全部楼层
有图片就好了。。。
回复

使用道具 举报

ID:703842 发表于 2020-3-15 18:53 | 显示全部楼层
可以改用52吗
回复

使用道具 举报

ID:723793 发表于 2020-4-10 08:08 | 显示全部楼层
请教大神,max30100和stm32f103c8t6怎么接线
回复

使用道具 举报

ID:432991 发表于 2020-5-13 15:31 | 显示全部楼层
这个n_brightness变量一通算,最后还是没用上啊,这个应该是要用于反馈调整led亮度的。
回复

使用道具 举报

ID:390410 发表于 2021-4-28 22:28 | 显示全部楼层
有实物图吗?
回复

使用道具 举报

ID:736539 发表于 2021-8-6 09:52 | 显示全部楼层
楼主用的是什么MAX30102芯片?我下载了只有心率值,血氧为-999是怎么回事
回复

使用道具 举报

ID:584195 发表于 2021-8-6 19:18 | 显示全部楼层
楼主这个做成成品了吗?低功耗做得怎么样?
回复

使用道具 举报

ID:44626 发表于 2023-10-18 13:39 | 显示全部楼层
感谢提供思路  非常感谢
回复

使用道具 举报

ID:1097879 发表于 2023-11-16 21:54 | 显示全部楼层
能做出实物吗
回复

使用道具 举报

ID:1103971 发表于 2023-12-12 19:56 | 显示全部楼层
我觉得挺不错的,有注释,不会太费劲
回复

使用道具 举报

ID:1110053 发表于 2024-1-24 18:11 | 显示全部楼层
Ivy_Lee 发表于 2018-11-2 17:51
找到的30102官方测试程序,共享一下

谢谢分享 参与一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表