找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2752|回复: 2
收起左侧

stm32导航车max30102上传整数型数据的源码

[复制链接]
ID:321668 发表于 2018-5-3 23:30 | 显示全部楼层 |阅读模式
单片机源程序如下:
  1. #include "struct_all.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "usart1_dma.h"

  5. #include "algorithm.h"
  6. #include "max30102.h"

  7. #include "myiic.h"
  8. #include "key.h"

  9. #define MAX_BRIGHTNESS 255

  10. uint32_t aun_ir_buffer[150]; //infrared LED sensor data
  11. uint32_t aun_red_buffer[150];  //red LED sensor data
  12. int32_t n_ir_buffer_length; //data length
  13. int32_t n_spo2;  //SPO2 value
  14. int8_t ch_spo2_valid;  //indicator to show if the SPO2 calculation is valid
  15. int32_t n_heart_rate; //heart rate value
  16. int8_t  ch_hr_valid;  //indicator to show if the heart rate calculation is valid
  17. uint8_t uch_dummy;

  18. int32_t hr_buf[16];
  19. int32_t hrSum;
  20. int32_t hrAvg;
  21. int32_t spo2_buf[16];
  22. int32_t spo2Sum;
  23. int32_t spo2Avg;
  24. int32_t spo2BuffFilled;
  25. int32_t hrBuffFilled;
  26. int32_t hrValidCnt = 0;
  27. int32_t spo2ValidCnt = 0;
  28. int32_t hrThrowOutSamp = 0;
  29. int32_t spo2ThrowOutSamp = 0;
  30. int32_t spo2Timeout = 0;
  31. int32_t hrTimeout = 0;



  32. void Send_To_PC( int rate, int spo2 );
  33. void Send_To_PC2( unsigned int red, unsigned int ir);
  34. void loop(void);
  35. int main(void)
  36. {
  37.     uart1_tx.buf[0] = 0x55;
  38.     uart1_tx.buf[1] = 0xAA;
  39.     uart1_tx.buf[10] = 0x0D;
  40.     delay_init();                            //延时函数初始化
  41.     NVIC_Configuration();
  42.     USART1_DMA_Init(115200);    //PA9:TX  PA10:RX        接WIFI模块         与手机相连发送状态数据

  43.     KEY_Init();                                  //实际是  max30102的中断线

  44.     bsp_InitI2C();
  45.     maxim_max30102_reset(); //resets the MAX30102

  46.     //pinMode(2, INPUT);  //pin D2 connects to the interrupt output pin of the MAX30102

  47.     maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_dummy); //Reads/clears the interrupt status register
  48.     maxim_max30102_init();  //initialize the MAX30102
  49.     while(1)
  50.     {
  51.          loop();
  52.     }
  53. }


  54. // the loop routine runs over and over again forever:
  55. void loop(void)
  56. {
  57.     uint32_t un_min, un_max, un_prev_data, un_brightness;  //variables to calculate the on-board LED brightness that reflects the heartbeats
  58.     int32_t i;
  59.     float f_temp;

  60.     un_brightness = 0;
  61.     un_min = 0x3FFFF;
  62.     un_max = 0;

  63.     n_ir_buffer_length = 150; //buffer length of 150 stores 3 seconds of samples running at 50sps

  64.     //read the first 150 samples, and determine the signal range
  65.     for(i = 0; i < n_ir_buffer_length; i++)
  66.     {
  67.         while(KEY0 == 1); //wait until the interrupt pin asserts
  68.         maxim_max30102_read_fifo((aun_red_buffer + i), (aun_ir_buffer + i)); //read from MAX30102 FIFO

  69.         if(un_min > aun_red_buffer[i])
  70.             un_min = aun_red_buffer[i]; //update signal min
  71.         if(un_max < aun_red_buffer[i])
  72.             un_max = aun_red_buffer[i]; //update signal max
  73.         /*SerialUSB.print(F("red="));
  74.         SerialUSB.print(aun_red_buffer[i], DEC);
  75.         SerialUSB.print(F(", ir="));
  76.         SerialUSB.println(aun_ir_buffer[i], DEC);*/
  77.                 //Send_To_PC2(aun_red_buffer[i],aun_ir_buffer[i]);
  78.     }
  79.     un_prev_data = aun_red_buffer[i];
  80.     //calculate heart rate and SpO2 after first 150 samples (first 3 seconds of samples)
  81.     maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_spo2, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);

  82.     //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
  83.     while(1)
  84.     {
  85.         i = 0;
  86.         un_min = 0x3FFFF;
  87.         un_max = 0;

  88.         //dumping the first 50 sets of samples in the memory and shift the last 100 sets of samples to the top
  89.         for(i = 50; i < 150; i++)
  90.         {
  91.             aun_red_buffer[i - 50] = aun_red_buffer[i];
  92.             aun_ir_buffer[i - 50] = aun_ir_buffer[i];

  93.             //update the signal min and max
  94.             if(un_min > aun_red_buffer[i])
  95.                 un_min = aun_red_buffer[i];
  96.             if(un_max < aun_red_buffer[i])
  97.                 un_max = aun_red_buffer[i];
  98.         }

  99.         //take 50 sets of samples before calculating the heart rate.
  100.         for(i = 100; i < 150; i++)
  101.         {
  102.             un_prev_data = aun_red_buffer[i - 1];
  103.             while(KEY0 == 1);
  104.             maxim_max30102_read_fifo((aun_red_buffer + i), (aun_ir_buffer + i));

  105.             //calculate the brightness of the LED
  106.             if(aun_red_buffer[i] > un_prev_data)
  107.             {
  108.                 f_temp = aun_red_buffer[i] - un_prev_data;
  109.                 f_temp /= (un_max - un_min);
  110.                 f_temp *= MAX_BRIGHTNESS;
  111.                 f_temp = un_brightness - f_temp;
  112.                 if(f_temp < 0)
  113.                     un_brightness = 0;
  114.                 else
  115.                     un_brightness = (int)f_temp;
  116.             }
  117.             else
  118.             {
  119.                 f_temp = un_prev_data - aun_red_buffer[i];
  120.                 f_temp /= (un_max - un_min);
  121.                 f_temp *= MAX_BRIGHTNESS;
  122.                 un_brightness += (int)f_temp;
  123.                 if(un_brightness > MAX_BRIGHTNESS)
  124.                     un_brightness = MAX_BRIGHTNESS;
  125.             }
  126.                         //Send_To_PC2( aun_red_buffer[i], aun_ir_buffer[i] );
  127.             //send samples and calculation result to terminal program through UART
  128.             /*SerialUSB.print(F("red="));
  129.             SerialUSB.print(aun_red_buffer[i], DEC);
  130.             SerialUSB.print(F(", ir="));
  131.             SerialUSB.print(aun_ir_buffer[i], DEC);

  132.             SerialUSB.print(F(", HR="));
  133.             SerialUSB.print(n_heart_rate, DEC);

  134.             SerialUSB.print(F(", HRvalid="));
  135.             SerialUSB.print(ch_hr_valid, DEC);

  136.             SerialUSB.print(F(", SPO2="));
  137.             SerialUSB.print(n_spo2, DEC);

  138.             SerialUSB.print(F(", SPO2Valid="));
  139.             SerialUSB.println(ch_spo2_valid, DEC);*/

  140.             //      SerialUSB.println(aun_ir_buffer[i], DEC);
  141.         }
  142.         maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_spo2, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);

  143.         if ((ch_hr_valid == 1) && (n_heart_rate < 190) && (n_heart_rate > 40))
  144.         {
  145.             hrTimeout = 0;

  146.             // Throw out up to 1 out of every 5 valid samples if wacky
  147.             if (hrValidCnt == 4)
  148.             {
  149.                 hrThrowOutSamp = 1;
  150.                 hrValidCnt = 0;
  151.                 for (i = 12; i < 16; i++)
  152.                 {
  153.                     if (n_heart_rate < hr_buf[i] + 10)
  154.                     {
  155.                         hrThrowOutSamp = 0;
  156.                         hrValidCnt   = 4;
  157.                     }
  158.                 }
  159.             }
  160.             else
  161.             {
  162.                 hrValidCnt = hrValidCnt + 1;
  163.             }

  164.             if (hrThrowOutSamp == 0)
  165.             {

  166.                 // Shift New Sample into buffer
  167.                 for(i = 0; i < 15; i++)
  168.                 {
  169.                     hr_buf[i] = hr_buf[i + 1];
  170.                 }
  171.                 hr_buf[15] = n_heart_rate;

  172.                 // Update buffer fill value
  173.                 if (hrBuffFilled < 16)
  174.                 {
  175.                     hrBuffFilled = hrBuffFilled + 1;
  176.                 }

  177.                 // Take moving average
  178.                 hrSum = 0;
  179.                 if (hrBuffFilled < 2)
  180.                 {
  181.                     hrAvg = 0;
  182.                 }
  183.                 else if (hrBuffFilled < 4)
  184.                 {
  185.                     for(i = 14; i < 16; i++)
  186.                     {
  187.                         hrSum = hrSum + hr_buf[i];
  188.                     }
  189.                     hrAvg = hrSum >> 1;
  190.                 }
  191.                 else if (hrBuffFilled < 8)
  192.                 {
  193.                     for(i = 12; i < 16; i++)
  194.                     {
  195.                         hrSum = hrSum + hr_buf[i];
  196.                     }
  197.                     hrAvg = hrSum >> 2;
  198.                 }
  199.                 else if (hrBuffFilled < 16)
  200.                 {
  201.                     for(i = 8; i < 16; i++)
  202.                     {
  203.                         hrSum = hrSum + hr_buf[i];
  204.                     }
  205.                     hrAvg = hrSum >> 3;
  206.                 }
  207.                 else
  208.                 {
  209.                     for(i = 0; i < 16; i++)
  210.                     {
  211.                         hrSum = hrSum + hr_buf[i];
  212.                     }
  213.                     hrAvg = hrSum >> 4;
  214.                 }
  215.             }
  216.             hrThrowOutSamp = 0;
  217.         }
  218.         else
  219.         {
  220.             hrValidCnt = 0;
  221.             if (hrTimeout == 4)
  222.             {
  223.                 hrAvg = 0;
  224.                 hrBuffFilled = 0;
  225.             }
  226.             else
  227.             {
  228.                 hrTimeout++;
  229.             }
  230.         }

  231.         if ((ch_spo2_valid == 1) && (n_spo2 > 59))
  232.         {
  233.             spo2Timeout = 0;

  234.             // Throw out up to 1 out of every 5 valid samples if wacky
  235.             if (spo2ValidCnt == 4)
  236.             {
  237.                 spo2ThrowOutSamp = 1;
  238.                 spo2ValidCnt = 0;
  239.                 for (i = 12; i < 16; i++)
  240.                 {
  241.                     if (n_spo2 > spo2_buf[i] - 10)
  242.                     {
  243.                         spo2ThrowOutSamp = 0;
  244.                         spo2ValidCnt   = 4;
  245.                     }
  246.                 }
  247.             }
  248.             else
  249.             {
  250.                 spo2ValidCnt = spo2ValidCnt + 1;
  251.             }

  252.             if (spo2ThrowOutSamp == 0)
  253.             {

  254.                 // Shift New Sample into buffer
  255.                 for(i = 0; i < 15; i++)
  256.                 {
  257.                     spo2_buf[i] = spo2_buf[i + 1];
  258.                 }
  259.                 spo2_buf[15] = n_spo2;

  260.                 // Update buffer fill value
  261.                 if (spo2BuffFilled < 16)
  262.                 {
  263.                     spo2BuffFilled = spo2BuffFilled + 1;
  264.                 }

  265.                 // Take moving average
  266.                 spo2Sum = 0;
  267.                 if (spo2BuffFilled < 2)
  268.                 {
  269.                     spo2Avg = 0;
  270.                 }
  271.                 else if (spo2BuffFilled < 4)
  272.                 {
  273.                     for(i = 14; i < 16; i++)
  274.                     {
  275.                         spo2Sum = spo2Sum + spo2_buf[i];
  276.                     }
  277.                     spo2Avg = spo2Sum >> 1;
  278.                 }
  279.                 else if (spo2BuffFilled < 8)
  280.                 {
  281.                     for(i = 12; i < 16; i++)
  282.                     {
  283.                         spo2Sum = spo2Sum + spo2_buf[i];
  284.                     }
  285.                     spo2Avg = spo2Sum >> 2;
  286.                 }
  287.                 else if (spo2BuffFilled < 16)
  288.                 {
  289.                     for(i = 8; i < 16; i++)
  290.                     {
  291.                         spo2Sum = spo2Sum + spo2_buf[i];
  292.                     }
  293.                     spo2Avg = spo2Sum >> 3;
  294.                 }
  295.                 else
  296.                 {
  297.                     for(i = 0; i < 16; i++)
  298.                     {
  299.                         spo2Sum = spo2Sum + spo2_buf[i];
  300.                     }
  301.                     spo2Avg = spo2Sum >> 4;
  302.                 }
  303.             }
  304.             spo2ThrowOutSamp = 0;
  305.         }
  306.         else
  307.         {
  308.             spo2ValidCnt = 0;
  309.             if (spo2Timeout == 4)
  310.             {
  311.                 spo2Avg = 0;
  312.                 spo2BuffFilled = 0;
  313.             }
  314.             else
  315.             {
  316.                 spo2Timeout++;
  317.             }
  318.         }

  319.         Send_To_PC(hrAvg, spo2Avg);
  320.     }
  321. }







  322. /**
  323. * @Description: wifi发送到手机APP的数据,连接在uart1上
  324. * @param  encoder_left - 左轮编码器值
  325. * @param  encoder_right- 右轮编码器值
  326. * @note1  :数据协议   0xFFD8 左轮速度double型(8位)   右轮速度double型(8)位 数据个数 0xFFD9
  327. */
  328. union INT_CHAR
  329. {
  330.     char                     char_buf[4];
  331.     signed int       int_buf;                  //车轮的实际速度   单位:脉冲/s
  332. }int_char;
  333. void Send_To_PC( int rate, int spo2 )
  334. {
  335.     u8 i;
  336.     uart1_tx.buf[0] = 0xFF;
  337.     uart1_tx.buf[1] = 0xD8;
  338.         int_char.int_buf = rate;
  339.         for (i=0;i<4;i++)
  340.         {
  341.                 uart1_tx.buf[i+2] = int_char.char_buf[3-i];        //注意需要倒着发送
  342.         }
  343.         int_char.int_buf = spo2;
  344.         for (i=0;i<4;i++)
  345.         {
  346.                 uart1_tx.buf[i+6] = int_char.char_buf[3-i];        //注意需要倒着发送
  347.         }
  348.     uart1_tx.buf[10] = 0xFF;
  349.     uart1_tx.buf[11] = 0xD9;
  350.     USART1_DMA_Send_Once_Data( uart1_tx.buf, 12 );
  351. }

  352. union UNINT_CHAR
  353. {
  354.     char                     char_buf[4];
  355.     unsigned int     unint_buf;                  //车轮的实际速度   单位:脉冲/s
  356. }unint_char;
  357. void Send_To_PC2( unsigned int red, unsigned int ir)
  358. {
  359.     u8 i;
  360.     uart1_tx.buf[0] = 0xFF;
  361.     uart1_tx.buf[1] = 0xD8;
  362.         unint_char.unint_buf = red;
  363.         for (i=0;i<4;i++)
  364.         {
  365.                 uart1_tx.buf[i+2] = unint_char.char_buf[3-i];        //注意需要倒着发送
  366.         }
  367.         unint_char.unint_buf = ir;
  368.         for (i=0;i<4;i++)
  369.         {
  370.                 uart1_tx.buf[i+6] = unint_char.char_buf[3-i];        //注意需要倒着发送
  371.         }
  372.     uart1_tx.buf[10] = 0xFF;
  373.     uart1_tx.buf[11] = 0xD9;
  374.     USART1_DMA_Send_Once_Data( uart1_tx.buf, 12 );
  375. }
  376. ……………………

  377. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
stm32 max30102上传整数型数据.rar (337.99 KB, 下载次数: 22)
回复

使用道具 举报

ID:302512 发表于 2018-5-8 11:19 | 显示全部楼层
功能是实现什么的
回复

使用道具 举报

ID:893440 发表于 2021-3-18 12:50 | 显示全部楼层
int32_t hrValidCnt = 0;
int32_t spo2ValidCnt = 0;
int32_t hrThrowOutSamp = 0;
int32_t spo2ThrowOutSamp = 0;
int32_t spo2Timeout = 0;
int32_t hrTimeout = 0;  楼主,这些变量不是很懂,可以解释一下吗
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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