找回密码
 立即注册

QQ登录

只需一步,快速开始

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

MAX30102芯片+心率血氧传感器模块+传感器模块 Arduino源程序

  [复制链接]
跳转到指定楼层
楼主
ID:253493 发表于 2019-5-21 22:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本程序使用STM32F103C8T6最小系统开发板验证。
MAX30102模块接口:PB9-SDA,PB8-SCL,PB7-INT
PA2/PA3为串口传输口TX和RX,波特率设置为115200
PC13为显示LED

软件版本:
MDK FOR ARM 5.24

单片机源程序如下:
  1. #include "stm32f103c8t6.h"
  2. #include "mbed.h"
  3. #include "algorithm.h"
  4. #include "MAX30102.h"

  5. #define MAX_BRIGHTNESS 255

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

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

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


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

  119.             pwmled.write(1-(float)n_brightness/256);//pwm control led brightness
  120.                                                 if(n_brightness<120)
  121.                                                         led=1;
  122.                                                 else
  123.                                                         led=0;

  124.             //send samples and calculation result to terminal program through UART
  125.             pc.printf("red=");
  126.             pc.printf("%i", aun_red_buffer[i]);
  127.             pc.printf(", ir=");
  128.             pc.printf("%i", aun_ir_buffer[i]);
  129.             pc.printf(", HR=%i, ", n_heart_rate);
  130.             pc.printf("HRvalid=%i, ", ch_hr_valid);
  131.             pc.printf("SpO2=%i, ", n_sp02);
  132.             pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid);
  133.         }
  134.         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);
  135.     }
  136. }
复制代码

所有资料51hei提供下载:
测试程序.7z (1.17 MB, 下载次数: 123)


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

使用道具 举报

沙发
ID:121029 发表于 2019-7-26 19:46 | 只看该作者
正在调试,需要这个
回复

使用道具 举报

板凳
ID:344943 发表于 2020-4-15 09:29 | 只看该作者
谢谢分享
回复

使用道具 举报

地板
ID:720776 发表于 2020-4-15 16:19 | 只看该作者
请问有Arduino的源程序吗
回复

使用道具 举报

5#
ID:686387 发表于 2020-4-15 23:00 | 只看该作者
正在研究这个希望有帮助
回复

使用道具 举报

6#
ID:591719 发表于 2020-4-28 21:41 | 只看该作者
感谢分享
回复

使用道具 举报

7#
ID:230374 发表于 2020-5-16 11:17 | 只看该作者
谢谢分享
回复

使用道具 举报

8#
ID:25178 发表于 2022-8-13 13:01 | 只看该作者
max30105的库max30102能用吗
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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