找回密码
 立即注册

QQ登录

只需一步,快速开始

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

stm32c8t6+MAX30102心率检测源程序

[复制链接]
跳转到指定楼层
楼主
ID:385235 发表于 2019-7-28 11:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  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.     maxim_max30102_reset(); //resets the MAX30102
  25.     // initialize serial communication at 115200 bits per second:
  26.     pc.baud(115200);
  27.     pc.format(8,SerialBase::None,1);
  28.     wait(1);

  29.     //read and clear status register
  30.     maxim_max30102_read_reg(0,&uch_dummy);

  31.     //wait until the user presses a key
  32. //    while(pc.readable()==0)
  33. //    {
  34. //        pc.printf("\x1B[2J");  //clear terminal program screen
  35. //        pc.printf("Press any key to start conversion\n\r");
  36. //        wait(1);
  37. //    }
  38. //    uch_dummy=getchar();

  39.     maxim_max30102_init();  //initializes the MAX30102


  40.     n_brightness=0;
  41.     un_min=0x3FFFF;
  42.     un_max=0;

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

  44.     //read the first 500 samples, and determine the signal range
  45.     for(i=0;i<n_ir_buffer_length;i++)
  46.     {
  47.         while(INT.read()==1);   //wait until the interrupt pin asserts

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

  49.         if(un_min>aun_red_buffer[i])
  50.             un_min=aun_red_buffer[i];    //update signal min
  51.         if(un_max<aun_red_buffer[i])
  52.             un_max=aun_red_buffer[i];    //update signal max
  53.         pc.printf("red=");
  54.         pc.printf("%i", aun_red_buffer[i]);
  55.         pc.printf(", ir=");
  56.         pc.printf("%i\n\r", aun_ir_buffer[i]);
  57.     }
  58.     un_prev_data=aun_red_buffer[i];


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

  61.     //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
  62.     while(1)
  63.     {
  64.         i=0;
  65.         un_min=0x3FFFF;
  66.         un_max=0;

  67.         //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
  68.         for(i=100;i<500;i++)
  69.         {
  70.             aun_red_buffer[i-100]=aun_red_buffer[i];
  71.             aun_ir_buffer[i-100]=aun_ir_buffer[i];

  72.             //update the signal min and max
  73.             if(un_min>aun_red_buffer[i])
  74.             un_min=aun_red_buffer[i];
  75.             if(un_max<aun_red_buffer[i])
  76.             un_max=aun_red_buffer[i];
  77.         }

  78.         //take 100 sets of samples before calculating the heart rate.
  79.         for(i=400;i<500;i++)
  80.         {
  81.             un_prev_data=aun_red_buffer[i-1];
  82.             while(INT.read()==1);
  83.             maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));

  84.             if(aun_red_buffer[i]>un_prev_data)//just to determine the brightness of LED according to the deviation of adjacent two AD data
  85.             {
  86.                 f_temp=aun_red_buffer[i]-un_prev_data;
  87.                 f_temp/=(un_max-un_min);
  88.                 f_temp*=MAX_BRIGHTNESS;
  89.                 n_brightness-=(int)f_temp;
  90.                 if(n_brightness<0)
  91.                     n_brightness=0;
  92.             }
  93.             else
  94.             {
  95.                 f_temp=un_prev_data-aun_red_buffer[i];
  96.                 f_temp/=(un_max-un_min);
  97.                 f_temp*=MAX_BRIGHTNESS;
  98.                 n_brightness+=(int)f_temp;
  99.                 if(n_brightness>MAX_BRIGHTNESS)
  100.                     n_brightness=MAX_BRIGHTNESS;
  101.             }

  102.             pwmled.write(1-(float)n_brightness/256);//pwm control led brightness
  103.                                                 if(n_brightness<120)
  104.                                                         led=1;
  105.                                                 else
  106.                                                         led=0;

  107.             //send samples and calculation result to terminal program through UART
  108.             pc.printf("red=");
  109.             pc.printf("%i", aun_red_buffer[i]);
  110.             pc.printf(", ir=");
  111.             pc.printf("%i", aun_ir_buffer[i]);
  112.             pc.printf(", HR=%i, ", n_heart_rate);
  113.             pc.printf("HRvalid=%i, ", ch_hr_valid);
  114.             pc.printf("SpO2=%i, ", n_sp02);
  115.             pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid);
  116.         }
  117.         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);
  118.     }
  119. }
复制代码

MAX30102_uvision5_stm32f103c8.7z

557.14 KB, 下载次数: 185, 下载积分: 黑币 -5

评分

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

查看全部评分

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

使用道具 举报

沙发
ID:924572 发表于 2022-1-20 20:50 | 只看该作者
请问 有max30102 proteus 仿真吗?
回复

使用道具 举报

板凳
ID:192215 发表于 2022-5-12 20:11 | 只看该作者
Mr、Zhou 发表于 2022-1-20 20:50
请问 有max30102 proteus 仿真吗?

MAX30102应该不能仿真吧
回复

使用道具 举报

地板
ID:1071867 发表于 2023-4-15 12:09 | 只看该作者
kkscan0821 发表于 2022-5-12 20:11
MAX30102应该不能仿真吧

不可以吗
回复

使用道具 举报

5#
ID:1072160 发表于 2023-4-18 11:11 | 只看该作者
我想问下楼主,Max30100和这个有什么区别?我不会写Max30100的驱动
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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