找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4049|回复: 0
收起左侧

MAX30102空气质量监测模块STM32源程序与资料

[复制链接]
ID:802483 发表于 2020-7-13 11:58 | 显示全部楼层 |阅读模式
空气质量监测模块芯片数据手册、电路图及测试程序

电路原理图如下:
51hei.png

功能实现:采用STM32F103C8T6小板,驱动血氧心率传感器max30102,实现PPG信号采集,并将计算的心率和血氧值显示在0.96寸OLED和串口上。
软件实现:ST标准库3.5
硬件连接:
MAX30102:
    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 && n_heart_rate<120)//**/ 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_hr == 0 && dis_spo2 == 0
  129.                 {
  130.                         sprintf((char *)str,"HR:---        ");//**HR:--- SpO2:---
  131.                 }
  132.                 else{
  133.                         sprintf((char *)str,"HR:%3d        ",dis_hr);//**HR:%3d SpO2:%3d
  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. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
51hei.png
所有资料51hei提供下载:
MAX30102测试资料完整版.7z (1.07 MB, 下载次数: 97)

评分

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

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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