找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4220|回复: 8
收起左侧

MAX30102基于STM32F103C8T6的程序源码

  [复制链接]
ID:527176 发表于 2019-5-9 15:40 | 显示全部楼层 |阅读模式
单片机源程序如下:
  1. /** \file main.cpp ******************************************************
  2. *
  3. * Project: STM32F103C8T6+MAX30102
  4. * Edited by Anning
  5. * ------------------------------------------------------------------------- */
  6. /*******************************************************************************
  7. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  8. *
  9. /*!\mainpage Main Page
  10. *
  11. * \section intro_sec Introduction
  12. *
  13. * This is the code documentation for the MAXREFDES117# subsystem reference design.
  14. *
  15. *  The Files page contains the File List page and the Globals page.
  16. *
  17. *  The Globals page contains the Functions, Variables, and Macros sub-pages.
  18. *
  19. * \image html MAXREFDES117_Block_Diagram.png "MAXREFDES117# System Block Diagram"
  20. *
  21. * \image html MAXREFDES117_firmware_Flowchart.png "MAXREFDES117# Firmware Flowchart"
  22. *
  23. */
  24. #include "stm32f103c8t6.h"
  25. #include "mbed.h"
  26. #include "algorithm.h"
  27. #include "MAX30102.h"

  28. #define MAX_BRIGHTNESS 255

  29. uint32_t aun_ir_buffer[500]; //IR LED sensor data
  30. int32_t n_ir_buffer_length;    //data length
  31. uint32_t aun_red_buffer[500];    //Red LED sensor data
  32. int32_t n_sp02; //SPO2 value
  33. int8_t ch_spo2_valid;   //indicator to show if the SP02 calculation is valid
  34. int32_t n_heart_rate;   //heart rate value
  35. int8_t  ch_hr_valid;    //indicator to show if the heart rate calculation is valid
  36. uint8_t uch_dummy;

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

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


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

  142.             pwmled.write(1-(float)n_brightness/256);//pwm control led brightness
  143.                                                 if(n_brightness<120)
  144.                                                         led=1;
  145.                                                 else
  146.                                                         led=0;

  147.             //send samples and calculation result to terminal program through UART
  148.             pc.printf("red=");
  149.             pc.printf("%i", aun_red_buffer[i]);
  150.             pc.printf(", ir=");
  151.             pc.printf("%i", aun_ir_buffer[i]);
  152.             pc.printf(", HR=%i, ", n_heart_rate);
  153.             pc.printf("HRvalid=%i, ", ch_hr_valid);
  154.             pc.printf("SpO2=%i, ", n_sp02);
  155.             pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid);
  156.         }
  157.         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);
  158.     }
  159. }
复制代码

所有程序下载,如果有问题,请大家多多指导:
Nucleo_MAX30102.7z (557.01 KB, 下载次数: 133)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:546524 发表于 2019-5-24 18:24 | 显示全部楼层
十分有帮助
回复

使用道具 举报

ID:404056 发表于 2019-7-22 16:57 | 显示全部楼层
很好
回复

使用道具 举报

ID:138247 发表于 2020-6-25 12:52 | 显示全部楼层

谢谢分享。。。
回复

使用道具 举报

ID:287575 发表于 2020-7-3 10:35 | 显示全部楼层
HR=-999, HRvalid=0, SpO2=-999, SPO2Valid=0
HR=-999, HRvalid=0, SpO2=-999, SPO2Valid=0
HR=-999, HRvalid=0, SpO2=-999, SPO2Valid=0
HR=-999, HRvalid=0, SpO2=-999, SPO2Valid=0

为什么我的数据是这样的,你的数据对吗?

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:695384 发表于 2020-7-6 16:56 | 显示全部楼层
  1. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a
  4. * copy of this software and associated documentation files (the "Software"),
  5. * to deal in the Software without restriction, including without limitation
  6. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. * and/or sell copies of the Software, and to permit persons to whom the
  8. * Software is furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included
  11. * in all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  17. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  18. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  19. * OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * Except as contained in this notice, the name of Maxim Integrated
  22. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  23. * Products, Inc. Branding Policy.
  24. *
  25. * The mere transfer of this software does not imply any licenses
  26. * of trade secrets, proprietary technology, copyrights, patents,
  27. * trademarks, maskwork rights, or any other form of intellectual
  28. * property whatsoever. Maxim Integrated Products, Inc. retains all
  29. * ownership rights.
  30. *******************************************************************************
  31. */
  32. /*!\mainpage Main Page
  33. *
  34. * \section intro_sec Introduction
  35. *
  36. * This is the code documentation for the MAXREFDES117# subsystem reference design.
  37. *
  38. *  The Files page contains the File List page and the Globals page.
  39. *
  40. *  The Globals page contains the Functions, Variables, and Macros sub-pages.
  41. *
  42. * \image html MAXREFDES117_Block_Diagram.png "MAXREFDES117# System Block Diagram"
  43. *
  44. * \image html MAXREFDES117_firmware_Flowchart.png "MAXREFDES117# Firmware Flowchart"
  45. *
  46. */
  47. #include "stm32f103c8t6.h"
  48. #include "mbed.h"
  49. #include "algorithm.h"
  50. #include "MAX30102.h"

  51. #define MAX_BRIGHTNESS 255

  52. uint32_t aun_ir_buffer[500]; //IR LED sensor data
  53. int32_t n_ir_buffer_length;    //data length
  54. uint32_t aun_red_buffer[500];    //Red LED sensor data
  55. int32_t n_sp02; //SPO2 value
  56. int8_t ch_spo2_valid;   //indicator to show if the SP02 calculation is valid
  57. int32_t n_heart_rate;   //heart rate value
  58. int8_t  ch_hr_valid;    //indicator to show if the heart rate calculation is valid
  59. uint8_t uch_dummy;

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

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


  64. // the setup routine runs once when you press reset:
  65. int main() {
  66.     uint32_t un_min, un_max, un_prev_data;  //variables to calculate the on-board LED brightness that reflects the heartbeats
  67.     int i;
  68.     int32_t n_brightness;
  69.     float f_temp;

  70.     maxim_max30102_reset(); //resets the MAX30102
  71.     // initialize serial communication at 115200 bits per second:
  72.     pc.baud(115200);
  73.     pc.format(8,SerialBase::None,1);
  74.     wait(1);

  75.     //read and clear status register
  76.     maxim_max30102_read_reg(0,&uch_dummy);

  77.     //wait until the user presses a key
  78. //    while(pc.readable()==0)
  79. //    {
  80. //        pc.printf("\x1B[2J");  //clear terminal program screen
  81. //        pc.printf("Press any key to start conversion\n\r");
  82. //        wait(1);
  83. //    }
  84. //    uch_dummy=getchar();

  85.     maxim_max30102_init();  //initializes the MAX30102


  86.     n_brightness=0;
  87.     un_min=0x3FFFF;
  88.     un_max=0;

  89.     n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps

  90.     //read the first 500 samples, and determine the signal range
  91.     for(i=0;i<n_ir_buffer_length;i++)
  92.     {
  93.         while(INT.read()==1);   //wait until the interrupt pin asserts

  94.         maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));  //read from MAX30102 FIFO

  95.         if(un_min>aun_red_buffer[i])
  96.             un_min=aun_red_buffer[i];    //update signal min
  97.         if(un_max<aun_red_buffer[i])
  98.             un_max=aun_red_buffer[i];    //update signal max
  99.         pc.printf("red=");
  100.         pc.printf("%i", aun_red_buffer[i]);
  101.         pc.printf(", ir=");
  102.         pc.printf("%i\n\r", aun_ir_buffer[i]);
  103.     }
  104.     un_prev_data=aun_red_buffer[i];


  105.     //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
  106.     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);

  107.     //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
  108.     while(1)
  109.     {
  110.         i=0;
  111.         un_min=0x3FFFF;
  112.         un_max=0;

  113.         //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
  114.         for(i=100;i<500;i++)
  115.         {
  116.             aun_red_buffer[i-100]=aun_red_buffer[i];
  117.             aun_ir_buffer[i-100]=aun_ir_buffer[i];

  118.             //update the signal min and max
  119.             if(un_min>aun_red_buffer[i])
  120.             un_min=aun_red_buffer[i];
  121.             if(un_max<aun_red_buffer[i])
  122.             un_max=aun_red_buffer[i];
  123.         }

  124.         //take 100 sets of samples before calculating the heart rate.
  125.         for(i=400;i<500;i++)
  126.         {
  127.             un_prev_data=aun_red_buffer[i-1];
  128.             while(INT.read()==1);
  129.             maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));

  130.             if(aun_red_buffer[i]>un_prev_data)//just to determine the brightness of LED according to the deviation of adjacent two AD data
  131.             {
  132.                 f_temp=aun_red_buffer[i]-un_prev_data;
  133.                 f_temp/=(un_max-un_min);
  134.                 f_temp*=MAX_BRIGHTNESS;
  135.                 n_brightness-=(int)f_temp;
  136.                 if(n_brightness<0)
  137.                     n_brightness=0;
  138.             }
  139.             else
  140.             {
  141.                 f_temp=un_prev_data-aun_red_buffer[i];
  142.                 f_temp/=(un_max-un_min);
  143.                 f_temp*=MAX_BRIGHTNESS;
  144.                 n_brightness+=(int)f_temp;
  145.                 if(n_brightness>MAX_BRIGHTNESS)
  146.                     n_brightness=MAX_BRIGHTNESS;
  147.             }

  148.             pwmled.write(1-(float)n_brightness/256);//pwm control led brightness
  149.                                                 if(n_brightness<120)
  150.                                                         led=1;
  151.                                                 else
  152.                                                         led=0;

  153.             //send samples and calculation result to terminal program through UART
  154.             pc.printf("red=");
  155.             pc.printf("%i", aun_red_buffer[i]);
  156.             pc.printf(", ir=");
  157.             pc.printf("%i", aun_ir_buffer[i]);
  158.             pc.printf(", HR=%i, ", n_heart_rate);
  159.             pc.printf("HRvalid=%i, ", ch_hr_valid);
  160.             pc.printf("SpO2=%i, ", n_sp02);
  161.             pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid);
  162.         }
  163.         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);
  164.     }
  165. }
复制代码
回复

使用道具 举报

ID:750420 发表于 2021-2-26 17:09 | 显示全部楼层
zhangxiaozi 发表于 2020-7-3 10:35
HR=-999, HRvalid=0, SpO2=-999, SPO2Valid=0
HR=-999, HRvalid=0, SpO2=-999, SPO2Valid=0
HR=-999, HRv ...

我的也有点问题
回复

使用道具 举报

ID:378659 发表于 2021-3-8 16:26 | 显示全部楼层
这的算法滤波不稳定,心率乱跳
回复

使用道具 举报

ID:885188 发表于 2021-3-11 17:38 | 显示全部楼层
没看懂呢?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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