找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5892|回复: 5
打印 上一主题 下一主题
收起左侧

基于max30102的心率检测stm32源码

[复制链接]
跳转到指定楼层
楼主
ID:337585 发表于 2018-5-24 21:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于max30102的心率监测

单片机源程序如下:
  1. /*!\mainpage Main Page
  2. *
  3. * \section intro_sec Introduction
  4. *
  5. * This is the code documentation for the MAXREFDES117# subsystem reference design.
  6. *
  7. *  The Files page contains the File List page and the Globals page.
  8. *
  9. *  The Globals page contains the Functions, Variables, and Macros sub-pages.
  10. *
  11. * \image html MAXREFDES117_Block_Diagram.png "MAXREFDES117# System Block Diagram"
  12. *
  13. * \image html MAXREFDES117_firmware_Flowchart.png "MAXREFDES117# Firmware Flowchart"
  14. *
  15. */
  16. #include "stm32f103c8t6.h"
  17. #include "mbed.h"
  18. #include "algorithm.h"
  19. #include "MAX30102.h"

  20. #define MAX_BRIGHTNESS 255

  21. uint32_t aun_ir_buffer[500]; //IR LED sensor data
  22. int32_t n_ir_buffer_length;    //data length
  23. uint32_t aun_red_buffer[500];    //Red LED sensor data
  24. int32_t n_sp02; //SPO2 value
  25. int8_t ch_spo2_valid;   //indicator to show if the SP02 calculation is valid
  26. int32_t n_heart_rate;   //heart rate value
  27. int8_t  ch_hr_valid;    //indicator to show if the heart rate calculation is valid
  28. uint8_t uch_dummy;

  29. Serial pc(SERIAL_TX, SERIAL_RX);    //initializes the serial port, TX-PA2, RX-PA3

  30. PwmOut pwmled(PB_3);  //initializes the pwm output PB3 that connects to the LED
  31. DigitalIn INT(PB_7);  //pin PB7 connects to the interrupt output pin of the MAX30102
  32. DigitalOut led(PC_13); //PC13 connects to the on board user LED


  33. // the setup routine runs once when you press reset:
  34. int main() {
  35.     uint32_t un_min, un_max, un_prev_data;  //variables to calculate the on-board LED brightness that reflects the heartbeats
  36.     int i;
  37.     int32_t n_brightness;
  38.     float f_temp;
  39.    
  40.     maxim_max30102_reset(); //resets the MAX30102
  41.     // initialize serial communication at 115200 bits per second:
  42.     pc.baud(115200);
  43.     pc.format(8,SerialBase::None,1);
  44.     wait(1);
  45.    
  46.     //read and clear status register
  47.     maxim_max30102_read_reg(0,&uch_dummy);
  48.    
  49.     //wait until the user presses a key
  50.     while(pc.readable()==0)
  51.     {
  52.         pc.printf("\x1B[2J");  //clear terminal program screen
  53.         pc.printf("Press any key to start conversion\n\r");
  54.         wait(1);
  55.     }
  56.     uch_dummy=getchar();
  57.    
  58.     maxim_max30102_init();  //initializes the MAX30102
  59.         
  60.         
  61.     n_brightness=0;
  62.     un_min=0x3FFFF;
  63.     un_max=0;
  64.   
  65.     n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps
  66.    
  67.     //read the first 500 samples, and determine the signal range
  68.     for(i=0;i<n_ir_buffer_length;i++)
  69.     {
  70.         while(INT.read()==1);   //wait until the interrupt pin asserts
  71.         
  72.         maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));  //read from MAX30102 FIFO
  73.             
  74.         if(un_min>aun_red_buffer[i])
  75.             un_min=aun_red_buffer[i];    //update signal min
  76.         if(un_max<aun_red_buffer[i])
  77.             un_max=aun_red_buffer[i];    //update signal max
  78.         pc.printf("red=");
  79.         pc.printf("%i", aun_red_buffer[i]);
  80.         pc.printf(", ir=");
  81.         pc.printf("%i\n\r", aun_ir_buffer[i]);
  82.     }
  83.     un_prev_data=aun_red_buffer[i];
  84.    
  85.    
  86.     //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
  87.     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);
  88.    
  89.     //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
  90.     while(1)
  91.     {
  92.         i=0;
  93.         un_min=0x3FFFF;
  94.         un_max=0;
  95.         
  96.         //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
  97.         for(i=100;i<500;i++)
  98.         {
  99.             aun_red_buffer[i-100]=aun_red_buffer[i];
  100.             aun_ir_buffer[i-100]=aun_ir_buffer[i];
  101.             
  102.             //update the signal min and max
  103.             if(un_min>aun_red_buffer[i])
  104.             un_min=aun_red_buffer[i];
  105.             if(un_max<aun_red_buffer[i])
  106.             un_max=aun_red_buffer[i];
  107.         }
  108.         
  109.         //take 100 sets of samples before calculating the heart rate.
  110.         for(i=400;i<500;i++)
  111.         {
  112.             un_prev_data=aun_red_buffer[i-1];
  113.             while(INT.read()==1);
  114.             maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));
  115.         
  116.             if(aun_red_buffer[i]>un_prev_data)//just to determine the brightness of LED according to the deviation of adjacent two AD data
  117.             {
  118.                 f_temp=aun_red_buffer[i]-un_prev_data;
  119.                 f_temp/=(un_max-un_min);
  120.                 f_temp*=MAX_BRIGHTNESS;
  121.                 n_brightness-=(int)f_temp;
  122.                 if(n_brightness<0)
  123.                     n_brightness=0;
  124.             }
  125.             else
  126.             {
  127.                 f_temp=un_prev_data-aun_red_buffer[i];
  128.                 f_temp/=(un_max-un_min);
  129.                 f_temp*=MAX_BRIGHTNESS;
  130.                 n_brightness+=(int)f_temp;
  131.                 if(n_brightness>MAX_BRIGHTNESS)
  132.                     n_brightness=MAX_BRIGHTNESS;
  133.             }

  134.             pwmled.write(1-(float)n_brightness/256);//pwm control led brightness
  135.                                                 if(n_brightness<120)
  136.                                                         led=1;
  137.                                                 else
  138.                                                         led=0;

  139.             //send samples and calculation result to terminal program through UART
  140.             pc.printf("red=");
  141.             pc.printf("%i", aun_red_buffer[i]);
  142.             pc.printf(", ir=");
  143.             pc.printf("%i", aun_ir_buffer[i]);
  144.             pc.printf(", HR=%i, ", n_heart_rate);
  145.             pc.printf("HRvalid=%i, ", ch_hr_valid);
  146.             pc.printf("SpO2=%i, ", n_sp02);
  147.             pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid);
  148.         }
  149.         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);
  150.     }
  151. }
复制代码

所有资料51hei提供下载:
程序.rar (993.22 KB, 下载次数: 107)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:391204 发表于 2018-8-29 14:50 | 只看该作者
谢谢谢谢
回复

使用道具 举报

板凳
ID:366320 发表于 2018-8-30 00:05 | 只看该作者
这个编译都通过不了
回复

使用道具 举报

地板
ID:349706 发表于 2018-11-2 17:39 | 只看该作者
我有这个代码但是没看懂串口是怎么配置的,是不是跟妹妹配置啊?
回复

使用道具 举报

5#
ID:543151 发表于 2019-6-10 19:22 | 只看该作者
93行的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); 函数在哪里?
回复

使用道具 举报

6#
ID:543151 发表于 2019-6-10 19:23 | 只看该作者
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); 93行的这个函数在哪里调用
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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