当前位置:   article > 正文

STM32传感器外设集--心率模块(MAX30102)

max30102

目录​​​​​​​

一、模块介绍

二、资料获取连接 

欢迎关注微信公众号--星之援工作室 发送关键字(MAX30102)

三、接线方式

四、代码编写

main.c

max30102.c

max30102.h

myiic.c

myiic.h

algorithm.c

algorithm.h

五 、参考


一、模块介绍

MAX30102模块是一种集成了光学传感器和信号处理器的模块,广泛应用于心率监测、脉搏氧饱和度监测以及其他生物参数检测等医疗领域。它具有高度集成、低功耗、高精度等特点,能够实时检测心率和血氧饱和度。 MAX30102模块内部集成了红外LED、红色光LED和光电检测器,通过红外LED和红色光LED照射皮肤,然后光电检测器采集反射光信号,进而进行血氧饱和度和心率的计算。模块还具有自动增益控制、环境光抑制和运动抑制等功能,能够有效去除干扰信号,提高监测精度。 MAX30102模块通常通过I2C总线与主控板进行通信,提供了许多可配置的参数,如采样速率、工作模式和各种滤波器等。模块还支持多种操作模式,包括连续测量模式、单次测量模式和间断测量模式,以满足不同应用需求。

二、资料获取连接 

欢迎关注微信公众号--星之援工作室 发送关键字(MAX30102)

欢迎关注微信公众号星之援工作室,公众号不定时开源设计项目

支持单片机,Android系统设计成品定制,项目代做

请联系微信:13648103287

三、接线方式

四、代码编写

main.c

  1. uint32_t aun_ir_buffer[500]; //IR LED sensor data
  2. int32_t n_ir_buffer_length; //data length
  3. uint32_t aun_red_buffer[500]; //Red LED sensor data
  4. int32_t n_sp02; //SPO2 value
  5. int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid
  6. int32_t n_heart_rate; //heart rate value
  7. int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid
  8. uint8_t uch_dummy;
  9. #define MAX_BRIGHTNESS 255
  10. void dis_DrawCurve(u32* data,u8 x);
  11. int main(void)
  12. {
  13. //variables to calculate the on-board LED brightness that reflects the heartbeats
  14. uint32_t un_min, un_max, un_prev_data;
  15. int i;
  16. int32_t n_brightness;
  17. float f_temp;
  18. u8 temp_num=0;
  19. u8 temp[6];
  20. u8 str[100];
  21. u8 dis_hr=0,dis_spo2=0;
  22. char string[100];
  23. NVIC_Configuration();
  24. delay_init(); //延时函数初始化
  25. uart_init(115200); //串口初始化为115200
  26. LED_Init();
  27. max30102_init();
  28. printf("\r\n MAX30102 init \r\n");
  29. un_min=0x3FFFF;
  30. un_max=0;
  31. n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps
  32. //read the first 500 samples, and determine the signal range
  33. for(i=0;i<n_ir_buffer_length;i++)
  34. {
  35. while(MAX30102_INT==1); //wait until the interrupt pin asserts
  36. max30102_FIFO_ReadBytes(REG_FIFO_DATA,temp);
  37. 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
  38. 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
  39. if(un_min>aun_red_buffer[i])
  40. un_min=aun_red_buffer[i]; //update signal min
  41. if(un_max<aun_red_buffer[i])
  42. un_max=aun_red_buffer[i]; //update signal max
  43. }
  44. un_prev_data=aun_red_buffer[i];
  45. //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
  46. 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);
  47. while(1)
  48. {
  49. i=0;
  50. un_min=0x3FFFF;
  51. un_max=0;
  52. //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
  53. for(i=100;i<500;i++)
  54. {
  55. aun_red_buffer[i-100]=aun_red_buffer[i];
  56. aun_ir_buffer[i-100]=aun_ir_buffer[i];
  57. //update the signal min and max
  58. if(un_min>aun_red_buffer[i])
  59. un_min=aun_red_buffer[i];
  60. if(un_max<aun_red_buffer[i])
  61. un_max=aun_red_buffer[i];
  62. }
  63. //take 100 sets of samples before calculating the heart rate.
  64. for(i=400;i<500;i++)
  65. {
  66. un_prev_data=aun_red_buffer[i-1];
  67. while(MAX30102_INT==1);
  68. max30102_FIFO_ReadBytes(REG_FIFO_DATA,temp);
  69. 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
  70. 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
  71. if(aun_red_buffer[i]>un_prev_data)
  72. {
  73. f_temp=aun_red_buffer[i]-un_prev_data;
  74. f_temp/=(un_max-un_min);
  75. f_temp*=MAX_BRIGHTNESS;
  76. n_brightness-=(int)f_temp;
  77. if(n_brightness<0)
  78. n_brightness=0;
  79. }
  80. else
  81. {
  82. f_temp=un_prev_data-aun_red_buffer[i];
  83. f_temp/=(un_max-un_min);
  84. f_temp*=MAX_BRIGHTNESS;
  85. n_brightness+=(int)f_temp;
  86. if(n_brightness>MAX_BRIGHTNESS)
  87. n_brightness=MAX_BRIGHTNESS;
  88. }
  89. //send samples and calculation result to terminal program through UART
  90. if(ch_hr_valid == 1 && n_heart_rate<120)//**/ ch_hr_valid == 1 && ch_spo2_valid ==1 && n_heart_rate<120 && n_sp02<101
  91. {
  92. dis_hr = n_heart_rate;
  93. dis_spo2 = n_sp02;
  94. }
  95. else
  96. {
  97. dis_hr = 0;
  98. dis_spo2 = 0;
  99. }
  100. printf("HR=%i, ", n_heart_rate);
  101. printf("HRvalid=%i, ", ch_hr_valid);
  102. printf("SpO2=%i, ", n_sp02);
  103. printf("SPO2Valid=%i\r\n", ch_spo2_valid);
  104. }
  105. 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);
  106. }
  107. }

max30102.c

  1. #include "max30102.h"
  2. #include "myiic.h"
  3. #include "delay.h"
  4. u8 max30102_Bus_Write(u8 Register_Address, u8 Word_Data)
  5. {
  6. /* 采用串行EEPROM随即读取指令序列,连续读取若干字节 */
  7. /*1步:发起I2C总线启动信号 */
  8. IIC_Start();
  9. /*2步:发起控制字节,高7bit是地址,bit0是读写控制位,0表示写,1表示读 */
  10. IIC_Send_Byte(max30102_WR_address | I2C_WR); /* 此处是写指令 */
  11. /*3步:发送ACK */
  12. if (IIC_Wait_Ack() != 0)
  13. {
  14. goto cmd_fail; /* EEPROM器件无应答 */
  15. }
  16. /*4步:发送字节地址 */
  17. IIC_Send_Byte(Register_Address);
  18. if (IIC_Wait_Ack() != 0)
  19. {
  20. goto cmd_fail; /* EEPROM器件无应答 */
  21. }
  22. /*5步:开始写入数据 */
  23. IIC_Send_Byte(Word_Data);
  24. /*6步:发送ACK */
  25. if (IIC_Wait_Ack() != 0)
  26. {
  27. goto cmd_fail; /* EEPROM器件无应答 */
  28. }
  29. /* 发送I2C总线停止信号 */
  30. IIC_Stop();
  31. return 1; /* 执行成功 */
  32. cmd_fail: /* 命令执行失败后,切记发送停止信号,避免影响I2C总线上其他设备 */
  33. /* 发送I2C总线停止信号 */
  34. IIC_Stop();
  35. return 0;
  36. }
  37. u8 max30102_Bus_Read(u8 Register_Address)
  38. {
  39. u8 data;
  40. /*1步:发起I2C总线启动信号 */
  41. IIC_Start();
  42. /*2步:发起控制字节,高7bit是地址,bit0是读写控制位,0表示写,1表示读 */
  43. IIC_Send_Byte(max30102_WR_address | I2C_WR); /* 此处是写指令 */
  44. /*3步:发送ACK */
  45. if (IIC_Wait_Ack() != 0)
  46. {
  47. goto cmd_fail; /* EEPROM器件无应答 */
  48. }
  49. /*4步:发送字节地址, */
  50. IIC_Send_Byte((uint8_t)Register_Address);
  51. if (IIC_Wait_Ack() != 0)
  52. {
  53. goto cmd_fail; /* EEPROM器件无应答 */
  54. }
  55. /*6步:重新启动I2C总线。下面开始读取数据 */
  56. IIC_Start();
  57. /*7步:发起控制字节,高7bit是地址,bit0是读写控制位,0表示写,1表示读 */
  58. IIC_Send_Byte(max30102_WR_address | I2C_RD); /* 此处是读指令 */
  59. /*8步:发送ACK */
  60. if (IIC_Wait_Ack() != 0)
  61. {
  62. goto cmd_fail; /* EEPROM器件无应答 */
  63. }
  64. /*9步:读取数据 */
  65. {
  66. data = IIC_Read_Byte(0); /*1个字节 */
  67. IIC_NAck(); /* 最后1个字节读完后,CPU产生NACK信号(驱动SDA = 1) */
  68. }
  69. /* 发送I2C总线停止信号 */
  70. IIC_Stop();
  71. return data; /* 执行成功 返回data*/
  72. cmd_fail: /* 命令执行失败后,切记发送停止信号,避免影响I2C总线上其他设备 */
  73. /* 发送I2C总线停止信号 */
  74. IIC_Stop();
  75. return 0;
  76. }
  77. void max30102_FIFO_ReadWords(u8 Register_Address,u16 Word_Data[][2],u8 count)
  78. {
  79. u8 i=0;
  80. u8 no = count;
  81. u8 data1, data2;
  82. /*1步:发起I2C总线启动信号 */
  83. IIC_Start();
  84. /*2步:发起控制字节,高7bit是地址,bit0是读写控制位,0表示写,1表示读 */
  85. IIC_Send_Byte(max30102_WR_address | I2C_WR); /* 此处是写指令 */
  86. /*3步:发送ACK */
  87. if (IIC_Wait_Ack() != 0)
  88. {
  89. goto cmd_fail; /* EEPROM器件无应答 */
  90. }
  91. /*4步:发送字节地址, */
  92. IIC_Send_Byte((uint8_t)Register_Address);
  93. if (IIC_Wait_Ack() != 0)
  94. {
  95. goto cmd_fail; /* EEPROM器件无应答 */
  96. }
  97. /*6步:重新启动I2C总线。下面开始读取数据 */
  98. IIC_Start();
  99. /*7步:发起控制字节,高7bit是地址,bit0是读写控制位,0表示写,1表示读 */
  100. IIC_Send_Byte(max30102_WR_address | I2C_RD); /* 此处是读指令 */
  101. /*8步:发送ACK */
  102. if (IIC_Wait_Ack() != 0)
  103. {
  104. goto cmd_fail; /* EEPROM器件无应答 */
  105. }
  106. /*9步:读取数据 */
  107. while (no)
  108. {
  109. data1 = IIC_Read_Byte(0);
  110. IIC_Ack();
  111. data2 = IIC_Read_Byte(0);
  112. IIC_Ack();
  113. Word_Data[i][0] = (((u16)data1 << 8) | data2); //
  114. data1 = IIC_Read_Byte(0);
  115. IIC_Ack();
  116. data2 = IIC_Read_Byte(0);
  117. if(1==no)
  118. IIC_NAck(); /* 最后1个字节读完后,CPU产生NACK信号(驱动SDA = 1) */
  119. else
  120. IIC_Ack();
  121. Word_Data[i][1] = (((u16)data1 << 8) | data2);
  122. no--;
  123. i++;
  124. }
  125. /* 发送I2C总线停止信号 */
  126. IIC_Stop();
  127. cmd_fail: /* 命令执行失败后,切记发送停止信号,避免影响I2C总线上其他设备 */
  128. /* 发送I2C总线停止信号 */
  129. IIC_Stop();
  130. }
  131. void max30102_FIFO_ReadBytes(u8 Register_Address,u8* Data)
  132. {
  133. max30102_Bus_Read(REG_INTR_STATUS_1);
  134. max30102_Bus_Read(REG_INTR_STATUS_2);
  135. /*1步:发起I2C总线启动信号 */
  136. IIC_Start();
  137. /*2步:发起控制字节,高7bit是地址,bit0是读写控制位,0表示写,1表示读 */
  138. IIC_Send_Byte(max30102_WR_address | I2C_WR); /* 此处是写指令 */
  139. /*3步:发送ACK */
  140. if (IIC_Wait_Ack() != 0)
  141. {
  142. goto cmd_fail; /* EEPROM器件无应答 */
  143. }
  144. /*4步:发送字节地址, */
  145. IIC_Send_Byte((uint8_t)Register_Address);
  146. if (IIC_Wait_Ack() != 0)
  147. {
  148. goto cmd_fail; /* EEPROM器件无应答 */
  149. }
  150. /*6步:重新启动I2C总线。下面开始读取数据 */
  151. IIC_Start();
  152. /*7步:发起控制字节,高7bit是地址,bit0是读写控制位,0表示写,1表示读 */
  153. IIC_Send_Byte(max30102_WR_address | I2C_RD); /* 此处是读指令 */
  154. /*8步:发送ACK */
  155. if (IIC_Wait_Ack() != 0)
  156. {
  157. goto cmd_fail; /* EEPROM器件无应答 */
  158. }
  159. /*9步:读取数据 */
  160. Data[0] = IIC_Read_Byte(1);
  161. Data[1] = IIC_Read_Byte(1);
  162. Data[2] = IIC_Read_Byte(1);
  163. Data[3] = IIC_Read_Byte(1);
  164. Data[4] = IIC_Read_Byte(1);
  165. Data[5] = IIC_Read_Byte(0);
  166. /* 最后1个字节读完后,CPU产生NACK信号(驱动SDA = 1) */
  167. /* 发送I2C总线停止信号 */
  168. IIC_Stop();
  169. cmd_fail: /* 命令执行失败后,切记发送停止信号,避免影响I2C总线上其他设备 */
  170. /* 发送I2C总线停止信号 */
  171. IIC_Stop();
  172. // u8 i;
  173. // u8 fifo_wr_ptr;
  174. // u8 firo_rd_ptr;
  175. // u8 number_tp_read;
  176. // //Get the FIFO_WR_PTR
  177. // fifo_wr_ptr = max30102_Bus_Read(REG_FIFO_WR_PTR);
  178. // //Get the FIFO_RD_PTR
  179. // firo_rd_ptr = max30102_Bus_Read(REG_FIFO_RD_PTR);
  180. //
  181. // number_tp_read = fifo_wr_ptr - firo_rd_ptr;
  182. //
  183. // //for(i=0;i<number_tp_read;i++){
  184. // if(number_tp_read>0){
  185. // IIC_ReadBytes(max30102_WR_address,REG_FIFO_DATA,Data,6);
  186. // }
  187. //max30102_Bus_Write(REG_FIFO_RD_PTR,fifo_wr_ptr);
  188. }
  189. void max30102_init(void)
  190. {
  191. GPIO_InitTypeDef GPIO_InitStructure;
  192. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  193. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  194. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  195. GPIO_Init(GPIOB, &GPIO_InitStructure);
  196. IIC_Init();
  197. max30102_reset();
  198. // max30102_Bus_Write(REG_MODE_CONFIG, 0x0b); //mode configuration : temp_en[3] MODE[2:0]=010 HR only enabled 011 SP02 enabled
  199. // max30102_Bus_Write(REG_INTR_STATUS_2, 0xF0); //open all of interrupt
  200. // max30102_Bus_Write(REG_INTR_STATUS_1, 0x00); //all interrupt clear
  201. // max30102_Bus_Write(REG_INTR_ENABLE_2, 0x02); //DIE_TEMP_RDY_EN
  202. // max30102_Bus_Write(REG_TEMP_CONFIG, 0x01); //SET TEMP_EN
  203. // max30102_Bus_Write(REG_SPO2_CONFIG, 0x47); //SPO2_SR[4:2]=001 100 per second LED_PW[1:0]=11 16BITS
  204. // max30102_Bus_Write(REG_LED1_PA, 0x47);
  205. // max30102_Bus_Write(REG_LED2_PA, 0x47);
  206. max30102_Bus_Write(REG_INTR_ENABLE_1,0xc0); // INTR setting
  207. max30102_Bus_Write(REG_INTR_ENABLE_2,0x00);
  208. max30102_Bus_Write(REG_FIFO_WR_PTR,0x00); //FIFO_WR_PTR[4:0]
  209. max30102_Bus_Write(REG_OVF_COUNTER,0x00); //OVF_COUNTER[4:0]
  210. max30102_Bus_Write(REG_FIFO_RD_PTR,0x00); //FIFO_RD_PTR[4:0]
  211. max30102_Bus_Write(REG_FIFO_CONFIG,0x0f); //sample avg = 1, fifo rollover=false, fifo almost full = 17
  212. max30102_Bus_Write(REG_MODE_CONFIG,0x03); //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
  213. max30102_Bus_Write(REG_SPO2_CONFIG,0x27); // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS)
  214. max30102_Bus_Write(REG_LED1_PA,0x24); //Choose value for ~ 7mA for LED1
  215. max30102_Bus_Write(REG_LED2_PA,0x24); // Choose value for ~ 7mA for LED2
  216. max30102_Bus_Write(REG_PILOT_PA,0x7f); // Choose value for ~ 25mA for Pilot LED
  217. // // Interrupt Enable 1 Register. Set PPG_RDY_EN (data available in FIFO)
  218. // max30102_Bus_Write(0x2, 1<<6);
  219. // // FIFO configuration register
  220. // // SMP_AVE: 16 samples averaged per FIFO sample
  221. // // FIFO_ROLLOVER_EN=1
  222. // //max30102_Bus_Write(0x8, 1<<4);
  223. // max30102_Bus_Write(0x8, (0<<5) | 1<<4);
  224. // // Mode Configuration Register
  225. // // SPO2 mode
  226. // max30102_Bus_Write(0x9, 3);
  227. // // SPO2 Configuration Register
  228. // max30102_Bus_Write(0xa,
  229. // (3<<5) // SPO2_ADC_RGE 2 = full scale 8192 nA (LSB size 31.25pA); 3 = 16384nA
  230. // | (1<<2) // sample rate: 0 = 50sps; 1 = 100sps; 2 = 200sps
  231. // | (3<<0) // LED_PW 3 = 411μs, ADC resolution 18 bits
  232. // );
  233. // // LED1 (red) power (0 = 0mA; 255 = 50mA)
  234. // max30102_Bus_Write(0xc, 0xb0);
  235. // // LED (IR) power
  236. // max30102_Bus_Write(0xd, 0xa0);
  237. }
  238. void max30102_reset(void)
  239. {
  240. max30102_Bus_Write(REG_MODE_CONFIG,0x40);
  241. max30102_Bus_Write(REG_MODE_CONFIG,0x40);
  242. }
  243. void maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data)
  244. {
  245. // char ach_i2c_data[2];
  246. // ach_i2c_data[0]=uch_addr;
  247. // ach_i2c_data[1]=uch_data;
  248. //
  249. // IIC_WriteBytes(I2C_WRITE_ADDR, ach_i2c_data, 2);
  250. IIC_Write_One_Byte(I2C_WRITE_ADDR,uch_addr,uch_data);
  251. }
  252. void maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data)
  253. {
  254. // char ch_i2c_data;
  255. // ch_i2c_data=uch_addr;
  256. // IIC_WriteBytes(I2C_WRITE_ADDR, &ch_i2c_data, 1);
  257. //
  258. // i2c.read(I2C_READ_ADDR, &ch_i2c_data, 1);
  259. //
  260. // *puch_data=(uint8_t) ch_i2c_data;
  261. IIC_Read_One_Byte(I2C_WRITE_ADDR,uch_addr,puch_data);
  262. }
  263. void maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led)
  264. {
  265. uint32_t un_temp;
  266. unsigned char uch_temp;
  267. char ach_i2c_data[6];
  268. *pun_red_led=0;
  269. *pun_ir_led=0;
  270. //read and clear status register
  271. maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_temp);
  272. maxim_max30102_read_reg(REG_INTR_STATUS_2, &uch_temp);
  273. IIC_ReadBytes(I2C_WRITE_ADDR,REG_FIFO_DATA,(u8 *)ach_i2c_data,6);
  274. un_temp=(unsigned char) ach_i2c_data[0];
  275. un_temp<<=16;
  276. *pun_red_led+=un_temp;
  277. un_temp=(unsigned char) ach_i2c_data[1];
  278. un_temp<<=8;
  279. *pun_red_led+=un_temp;
  280. un_temp=(unsigned char) ach_i2c_data[2];
  281. *pun_red_led+=un_temp;
  282. un_temp=(unsigned char) ach_i2c_data[3];
  283. un_temp<<=16;
  284. *pun_ir_led+=un_temp;
  285. un_temp=(unsigned char) ach_i2c_data[4];
  286. un_temp<<=8;
  287. *pun_ir_led+=un_temp;
  288. un_temp=(unsigned char) ach_i2c_data[5];
  289. *pun_ir_led+=un_temp;
  290. *pun_red_led&=0x03FFFF; //Mask MSB [23:18]
  291. *pun_ir_led&=0x03FFFF; //Mask MSB [23:18]
  292. }

max30102.h

  1. #ifndef __MYIIC_H
  2. #define __MYIIC_H
  3. #include "sys.h"
  4. //
  5. #define MAX30102_INT PBin(9)
  6. #define I2C_WR 0 /* 写控制bit */
  7. #define I2C_RD 1 /* 读控制bit */
  8. #define max30102_WR_address 0xAE
  9. #define I2C_WRITE_ADDR 0xAE
  10. #define I2C_READ_ADDR 0xAF
  11. //register addresses
  12. #define REG_INTR_STATUS_1 0x00
  13. #define REG_INTR_STATUS_2 0x01
  14. #define REG_INTR_ENABLE_1 0x02
  15. #define REG_INTR_ENABLE_2 0x03
  16. #define REG_FIFO_WR_PTR 0x04
  17. #define REG_OVF_COUNTER 0x05
  18. #define REG_FIFO_RD_PTR 0x06
  19. #define REG_FIFO_DATA 0x07
  20. #define REG_FIFO_CONFIG 0x08
  21. #define REG_MODE_CONFIG 0x09
  22. #define REG_SPO2_CONFIG 0x0A
  23. #define REG_LED1_PA 0x0C
  24. #define REG_LED2_PA 0x0D
  25. #define REG_PILOT_PA 0x10
  26. #define REG_MULTI_LED_CTRL1 0x11
  27. #define REG_MULTI_LED_CTRL2 0x12
  28. #define REG_TEMP_INTR 0x1F
  29. #define REG_TEMP_FRAC 0x20
  30. #define REG_TEMP_CONFIG 0x21
  31. #define REG_PROX_INT_THRESH 0x30
  32. #define REG_REV_ID 0xFE
  33. #define REG_PART_ID 0xFF
  34. void max30102_init(void);
  35. void max30102_reset(void);
  36. u8 max30102_Bus_Write(u8 Register_Address, u8 Word_Data);
  37. u8 max30102_Bus_Read(u8 Register_Address);
  38. void max30102_FIFO_ReadWords(u8 Register_Address,u16 Word_Data[][2],u8 count);
  39. void max30102_FIFO_ReadBytes(u8 Register_Address,u8* Data);
  40. void maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data);
  41. void maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data);
  42. void maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led);
  43. #endif

myiic.c

  1. #include "myiic.h"
  2. #include "delay.h"
  3. //初始化IIC
  4. void IIC_Init(void)
  5. {
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. //RCC->APB2ENR|=1<<4;//先使能外设IO PORTC时钟
  8. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8;
  10. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //推挽输出
  11. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12. GPIO_Init(GPIOB, &GPIO_InitStructure);
  13. IIC_SCL=1;
  14. IIC_SDA=1;
  15. }
  16. //产生IIC起始信号
  17. void IIC_Start(void)
  18. {
  19. SDA_OUT(); //sda线输出
  20. IIC_SDA=1;
  21. IIC_SCL=1;
  22. delay_us(4);
  23. IIC_SDA=0;//START:when CLK is high,DATA change form high to low
  24. delay_us(4);
  25. IIC_SCL=0;//钳住I2C总线,准备发送或接收数据
  26. }
  27. //产生IIC停止信号
  28. void IIC_Stop(void)
  29. {
  30. SDA_OUT();//sda线输出
  31. IIC_SCL=0;
  32. IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
  33. delay_us(4);
  34. IIC_SCL=1;
  35. IIC_SDA=1;//发送I2C总线结束信号
  36. delay_us(4);
  37. }
  38. //等待应答信号到来
  39. //返回值:1,接收应答失败
  40. // 0,接收应答成功
  41. u8 IIC_Wait_Ack(void)
  42. {
  43. u8 ucErrTime=0;
  44. SDA_IN(); //SDA设置为输入
  45. IIC_SDA=1;delay_us(1);
  46. IIC_SCL=1;delay_us(1);
  47. while(READ_SDA)
  48. {
  49. ucErrTime++;
  50. if(ucErrTime>250)
  51. {
  52. IIC_Stop();
  53. return 1;
  54. }
  55. }
  56. IIC_SCL=0;//时钟输出0
  57. return 0;
  58. }
  59. //产生ACK应答
  60. void IIC_Ack(void)
  61. {
  62. IIC_SCL=0;
  63. SDA_OUT();
  64. IIC_SDA=0;
  65. delay_us(2);
  66. IIC_SCL=1;
  67. delay_us(2);
  68. IIC_SCL=0;
  69. }
  70. //不产生ACK应答
  71. void IIC_NAck(void)
  72. {
  73. IIC_SCL=0;
  74. SDA_OUT();
  75. IIC_SDA=1;
  76. delay_us(2);
  77. IIC_SCL=1;
  78. delay_us(2);
  79. IIC_SCL=0;
  80. }
  81. //IIC发送一个字节
  82. //返回从机有无应答
  83. //1,有应答
  84. //0,无应答
  85. void IIC_Send_Byte(u8 txd)
  86. {
  87. u8 t;
  88. SDA_OUT();
  89. IIC_SCL=0;//拉低时钟开始数据传输
  90. for(t=0;t<8;t++)
  91. {
  92. IIC_SDA=(txd&0x80)>>7;
  93. txd<<=1;
  94. delay_us(2); //对TEA5767这三个延时都是必须的
  95. IIC_SCL=1;
  96. delay_us(2);
  97. IIC_SCL=0;
  98. delay_us(2);
  99. }
  100. }
  101. //1个字节,ack=1时,发送ACK,ack=0,发送nACK
  102. u8 IIC_Read_Byte(unsigned char ack)
  103. {
  104. unsigned char i,receive=0;
  105. SDA_IN();//SDA设置为输入
  106. for(i=0;i<8;i++ )
  107. {
  108. IIC_SCL=0;
  109. delay_us(2);
  110. IIC_SCL=1;
  111. receive<<=1;
  112. if(READ_SDA)receive++;
  113. delay_us(1);
  114. }
  115. if (!ack)
  116. IIC_NAck();//发送nACK
  117. else
  118. IIC_Ack(); //发送ACK
  119. return receive;
  120. }
  121. void IIC_WriteBytes(u8 WriteAddr,u8* data,u8 dataLength)
  122. {
  123. u8 i;
  124. IIC_Start();
  125. IIC_Send_Byte(WriteAddr); //发送写命令
  126. IIC_Wait_Ack();
  127. for(i=0;i<dataLength;i++)
  128. {
  129. IIC_Send_Byte(data[i]);
  130. IIC_Wait_Ack();
  131. }
  132. IIC_Stop();//产生一个停止条件
  133. delay_ms(10);
  134. }
  135. void IIC_ReadBytes(u8 deviceAddr, u8 writeAddr,u8* data,u8 dataLength)
  136. {
  137. u8 i;
  138. IIC_Start();
  139. IIC_Send_Byte(deviceAddr); //发送写命令
  140. IIC_Wait_Ack();
  141. IIC_Send_Byte(writeAddr);
  142. IIC_Wait_Ack();
  143. IIC_Send_Byte(deviceAddr|0X01);//进入接收模式
  144. IIC_Wait_Ack();
  145. for(i=0;i<dataLength-1;i++)
  146. {
  147. data[i] = IIC_Read_Byte(1);
  148. }
  149. data[dataLength-1] = IIC_Read_Byte(0);
  150. IIC_Stop();//产生一个停止条件
  151. delay_ms(10);
  152. }
  153. void IIC_Read_One_Byte(u8 daddr,u8 addr,u8* data)
  154. {
  155. IIC_Start();
  156. IIC_Send_Byte(daddr); //发送写命令
  157. IIC_Wait_Ack();
  158. IIC_Send_Byte(addr);//发送地址
  159. IIC_Wait_Ack();
  160. IIC_Start();
  161. IIC_Send_Byte(daddr|0X01);//进入接收模式
  162. IIC_Wait_Ack();
  163. *data = IIC_Read_Byte(0);
  164. IIC_Stop();//产生一个停止条件
  165. }
  166. void IIC_Write_One_Byte(u8 daddr,u8 addr,u8 data)
  167. {
  168. IIC_Start();
  169. IIC_Send_Byte(daddr); //发送写命令
  170. IIC_Wait_Ack();
  171. IIC_Send_Byte(addr);//发送地址
  172. IIC_Wait_Ack();
  173. IIC_Send_Byte(data); //发送字节
  174. IIC_Wait_Ack();
  175. IIC_Stop();//产生一个停止条件
  176. delay_ms(10);
  177. }

myiic.h

  1. #ifndef __MAX30102_H
  2. #define __MAX30102_H
  3. #include "sys.h"
  4. //
  5. //IO方向设置
  6. #define SDA_IN() {GPIOB->CRH&=0XFFFFFFF0;GPIOB->CRH|=4;}
  7. #define SDA_OUT() {GPIOB->CRH&=0XFFFFFFF0;GPIOB->CRH|=7;}
  8. //IO操作函数
  9. #define IIC_SCL PBout(7) //SCL
  10. #define IIC_SDA PBout(8) //SDA
  11. #define READ_SDA PBin(8) //输入SDA
  12. //IIC所有操作函数
  13. void IIC_Init(void); //初始化IIC的IO口
  14. void IIC_Start(void); //发送IIC开始信号
  15. void IIC_Stop(void); //发送IIC停止信号
  16. void IIC_Send_Byte(u8 txd); //IIC发送一个字节
  17. u8 IIC_Read_Byte(unsigned char ack);//IIC读取一个字节
  18. u8 IIC_Wait_Ack(void); //IIC等待ACK信号
  19. void IIC_Ack(void); //IIC发送ACK信号
  20. void IIC_NAck(void); //IIC不发送ACK信号
  21. void IIC_Write_One_Byte(u8 daddr,u8 addr,u8 data);
  22. void IIC_Read_One_Byte(u8 daddr,u8 addr,u8* data);
  23. void IIC_WriteBytes(u8 WriteAddr,u8* data,u8 dataLength);
  24. void IIC_ReadBytes(u8 deviceAddr, u8 writeAddr,u8* data,u8 dataLength);
  25. #endif

algorithm.c

  1. /** \file algorithm.c ******************************************************
  2. *
  3. * Project: MAXREFDES117#
  4. * Filename: algorithm.cpp
  5. * Description: This module calculates the heart rate/SpO2 level
  6. *
  7. *
  8. * --------------------------------------------------------------------
  9. *
  10. * This code follows the following naming conventions:
  11. *
  12. * char ch_pmod_value
  13. * char (array) s_pmod_s_string[16]
  14. * float f_pmod_value
  15. * int32_t n_pmod_value
  16. * int32_t (array) an_pmod_value[16]
  17. * int16_t w_pmod_value
  18. * int16_t (array) aw_pmod_value[16]
  19. * uint16_t uw_pmod_value
  20. * uint16_t (array) auw_pmod_value[16]
  21. * uint8_t uch_pmod_value
  22. * uint8_t (array) auch_pmod_buffer[16]
  23. * uint32_t un_pmod_value
  24. * int32_t * pn_pmod_value
  25. *
  26. * ------------------------------------------------------------------------- */
  27. /*******************************************************************************
  28. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  29. *
  30. * Permission is hereby granted, free of charge, to any person obtaining a
  31. * copy of this software and associated documentation files (the "Software"),
  32. * to deal in the Software without restriction, including without limitation
  33. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  34. * and/or sell copies of the Software, and to permit persons to whom the
  35. * Software is furnished to do so, subject to the following conditions:
  36. *
  37. * The above copyright notice and this permission notice shall be included
  38. * in all copies or substantial portions of the Software.
  39. *
  40. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  41. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  42. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  43. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  44. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  45. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  46. * OTHER DEALINGS IN THE SOFTWARE.
  47. *
  48. * Except as contained in this notice, the name of Maxim Integrated
  49. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  50. * Products, Inc. Branding Policy.
  51. *
  52. * The mere transfer of this software does not imply any licenses
  53. * of trade secrets, proprietary technology, copyrights, patents,
  54. * trademarks, maskwork rights, or any other form of intellectual
  55. * property whatsoever. Maxim Integrated Products, Inc. retains all
  56. * ownership rights.
  57. *******************************************************************************
  58. */
  59. #include "algorithm.h"
  60. const uint16_t auw_hamm[31]={ 41, 276, 512, 276, 41 }; //Hamm= long16(512* hamming(5)');
  61. //uch_spo2_table is computed as -45.060*ratioAverage* ratioAverage + 30.354 *ratioAverage + 94.845 ;
  62. const uint8_t uch_spo2_table[184]={ 95, 95, 95, 96, 96, 96, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 99, 99, 99, 99,
  63. 99, 99, 99, 99, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
  64. 100, 100, 100, 100, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 98, 98, 98, 97, 97,
  65. 97, 97, 96, 96, 96, 96, 95, 95, 95, 94, 94, 94, 93, 93, 93, 92, 92, 92, 91, 91,
  66. 90, 90, 89, 89, 89, 88, 88, 87, 87, 86, 86, 85, 85, 84, 84, 83, 82, 82, 81, 81,
  67. 80, 80, 79, 78, 78, 77, 76, 76, 75, 74, 74, 73, 72, 72, 71, 70, 69, 69, 68, 67,
  68. 66, 66, 65, 64, 63, 62, 62, 61, 60, 59, 58, 57, 56, 56, 55, 54, 53, 52, 51, 50,
  69. 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 31, 30, 29,
  70. 28, 27, 26, 25, 23, 22, 21, 20, 19, 17, 16, 15, 14, 12, 11, 10, 9, 7, 6, 5,
  71. 3, 2, 1 } ;
  72. static int32_t an_dx[ BUFFER_SIZE-MA4_SIZE]; // delta
  73. static int32_t an_x[ BUFFER_SIZE]; //ir
  74. static int32_t an_y[ BUFFER_SIZE]; //red
  75. void maxim_heart_rate_and_oxygen_saturation(uint32_t *pun_ir_buffer, int32_t n_ir_buffer_length, uint32_t *pun_red_buffer, int32_t *pn_spo2, int8_t *pch_spo2_valid,
  76. int32_t *pn_heart_rate, int8_t *pch_hr_valid)
  77. /**
  78. * \brief Calculate the heart rate and SpO2 level
  79. * \par Details
  80. * By detecting peaks of PPG cycle and corresponding AC/DC of red/infra-red signal, the ratio for the SPO2 is computed.
  81. * Since this algorithm is aiming for Arm M0/M3. formaula for SPO2 did not achieve the accuracy due to register overflow.
  82. * Thus, accurate SPO2 is precalculated and save longo uch_spo2_table[] per each ratio.
  83. *
  84. * \param[in] *pun_ir_buffer - IR sensor data buffer
  85. * \param[in] n_ir_buffer_length - IR sensor data buffer length
  86. * \param[in] *pun_red_buffer - Red sensor data buffer
  87. * \param[out] *pn_spo2 - Calculated SpO2 value
  88. * \param[out] *pch_spo2_valid - 1 if the calculated SpO2 value is valid
  89. * \param[out] *pn_heart_rate - Calculated heart rate value
  90. * \param[out] *pch_hr_valid - 1 if the calculated heart rate value is valid
  91. *
  92. * \retval None
  93. */
  94. {
  95. uint32_t un_ir_mean ,un_only_once ;
  96. int32_t k ,n_i_ratio_count;
  97. int32_t i, s, m, n_exact_ir_valley_locs_count ,n_middle_idx;
  98. int32_t n_th1, n_npks,n_c_min;
  99. int32_t an_ir_valley_locs[15] ;
  100. int32_t an_exact_ir_valley_locs[15] ;
  101. int32_t an_dx_peak_locs[15] ;
  102. int32_t n_peak_interval_sum;
  103. int32_t n_y_ac, n_x_ac;
  104. int32_t n_spo2_calc;
  105. int32_t n_y_dc_max, n_x_dc_max;
  106. int32_t n_y_dc_max_idx, n_x_dc_max_idx;
  107. int32_t an_ratio[5],n_ratio_average;
  108. int32_t n_nume, n_denom ;
  109. // remove DC of ir signal
  110. un_ir_mean =0;
  111. for (k=0 ; k<n_ir_buffer_length ; k++ ) un_ir_mean += pun_ir_buffer[k] ;
  112. un_ir_mean =un_ir_mean/n_ir_buffer_length ;
  113. for (k=0 ; k<n_ir_buffer_length ; k++ ) an_x[k] = pun_ir_buffer[k] - un_ir_mean ;
  114. // 4 pt Moving Average
  115. for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){
  116. n_denom= ( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3]);
  117. an_x[k]= n_denom/(int32_t)4;
  118. }
  119. // get difference of smoothed IR signal
  120. for( k=0; k<BUFFER_SIZE-MA4_SIZE-1; k++)
  121. an_dx[k]= (an_x[k+1]- an_x[k]);
  122. // 2-pt Moving Average to an_dx
  123. for(k=0; k< BUFFER_SIZE-MA4_SIZE-2; k++){
  124. an_dx[k] = ( an_dx[k]+an_dx[k+1])/2 ;
  125. }
  126. // hamming window
  127. // flip wave form so that we can detect valley with peak detector
  128. for ( i=0 ; i<BUFFER_SIZE-HAMMING_SIZE-MA4_SIZE-2 ;i++){
  129. s= 0;
  130. for( k=i; k<i+ HAMMING_SIZE ;k++){
  131. s -= an_dx[k] *auw_hamm[k-i] ;
  132. }
  133. an_dx[i]= s/ (int32_t)1146; // divide by sum of auw_hamm
  134. }
  135. n_th1=0; // threshold calculation
  136. for ( k=0 ; k<BUFFER_SIZE-HAMMING_SIZE ;k++){
  137. n_th1 += ((an_dx[k]>0)? an_dx[k] : ((int32_t)0-an_dx[k])) ;
  138. }
  139. n_th1= n_th1/ ( BUFFER_SIZE-HAMMING_SIZE);
  140. // peak location is acutally index for sharpest location of raw signal since we flipped the signal
  141. maxim_find_peaks( an_dx_peak_locs, &n_npks, an_dx, BUFFER_SIZE-HAMMING_SIZE, n_th1, 8, 5 );//peak_height, peak_distance, max_num_peaks
  142. n_peak_interval_sum =0;
  143. if (n_npks>=2){
  144. for (k=1; k<n_npks; k++)
  145. n_peak_interval_sum += (an_dx_peak_locs[k]-an_dx_peak_locs[k -1]);
  146. n_peak_interval_sum=n_peak_interval_sum/(n_npks-1);
  147. *pn_heart_rate=(int32_t)(6000/n_peak_interval_sum);// beats per minutes
  148. *pch_hr_valid = 1;
  149. }
  150. else {
  151. *pn_heart_rate = -999;
  152. *pch_hr_valid = 0;
  153. }
  154. for ( k=0 ; k<n_npks ;k++)
  155. an_ir_valley_locs[k]=an_dx_peak_locs[k]+HAMMING_SIZE/2;
  156. // raw value : RED(=y) and IR(=X)
  157. // we need to assess DC and AC value of ir and red PPG.
  158. for (k=0 ; k<n_ir_buffer_length ; k++ ) {
  159. an_x[k] = pun_ir_buffer[k] ;
  160. an_y[k] = pun_red_buffer[k] ;
  161. }
  162. // find precise min near an_ir_valley_locs
  163. n_exact_ir_valley_locs_count =0;
  164. for(k=0 ; k<n_npks ;k++){
  165. un_only_once =1;
  166. m=an_ir_valley_locs[k];
  167. n_c_min= 16777216;//2^24;
  168. if (m+5 < BUFFER_SIZE-HAMMING_SIZE && m-5 >0){
  169. for(i= m-5;i<m+5; i++)
  170. if (an_x[i]<n_c_min){
  171. if (un_only_once >0){
  172. un_only_once =0;
  173. }
  174. n_c_min= an_x[i] ;
  175. an_exact_ir_valley_locs[k]=i;
  176. }
  177. if (un_only_once ==0)
  178. n_exact_ir_valley_locs_count ++ ;
  179. }
  180. }
  181. if (n_exact_ir_valley_locs_count <2 ){
  182. *pn_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range
  183. *pch_spo2_valid = 0;
  184. return;
  185. }
  186. // 4 pt MA
  187. for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){
  188. an_x[k]=( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3])/(int32_t)4;
  189. an_y[k]=( an_y[k]+an_y[k+1]+ an_y[k+2]+ an_y[k+3])/(int32_t)4;
  190. }
  191. //using an_exact_ir_valley_locs , find ir-red DC andir-red AC for SPO2 calibration ratio
  192. //finding AC/DC maximum of raw ir * red between two valley locations
  193. n_ratio_average =0;
  194. n_i_ratio_count =0;
  195. for(k=0; k< 5; k++) an_ratio[k]=0;
  196. for (k=0; k< n_exact_ir_valley_locs_count; k++){
  197. if (an_exact_ir_valley_locs[k] > BUFFER_SIZE ){
  198. *pn_spo2 = -999 ; // do not use SPO2 since valley loc is out of range
  199. *pch_spo2_valid = 0;
  200. return;
  201. }
  202. }
  203. // find max between two valley locations
  204. // and use ratio betwen AC compoent of Ir & Red and DC compoent of Ir & Red for SPO2
  205. for (k=0; k< n_exact_ir_valley_locs_count-1; k++){
  206. n_y_dc_max= -16777216 ;
  207. n_x_dc_max= - 16777216;
  208. if (an_exact_ir_valley_locs[k+1]-an_exact_ir_valley_locs[k] >10){
  209. for (i=an_exact_ir_valley_locs[k]; i< an_exact_ir_valley_locs[k+1]; i++){
  210. if (an_x[i]> n_x_dc_max) {n_x_dc_max =an_x[i];n_x_dc_max_idx =i; }
  211. if (an_y[i]> n_y_dc_max) {n_y_dc_max =an_y[i];n_y_dc_max_idx=i;}
  212. }
  213. n_y_ac= (an_y[an_exact_ir_valley_locs[k+1]] - an_y[an_exact_ir_valley_locs[k] ] )*(n_y_dc_max_idx -an_exact_ir_valley_locs[k]); //red
  214. n_y_ac= an_y[an_exact_ir_valley_locs[k]] + n_y_ac/ (an_exact_ir_valley_locs[k+1] - an_exact_ir_valley_locs[k]) ;
  215. n_y_ac= an_y[n_y_dc_max_idx] - n_y_ac; // subracting linear DC compoenents from raw
  216. n_x_ac= (an_x[an_exact_ir_valley_locs[k+1]] - an_x[an_exact_ir_valley_locs[k] ] )*(n_x_dc_max_idx -an_exact_ir_valley_locs[k]); // ir
  217. n_x_ac= an_x[an_exact_ir_valley_locs[k]] + n_x_ac/ (an_exact_ir_valley_locs[k+1] - an_exact_ir_valley_locs[k]);
  218. n_x_ac= an_x[n_y_dc_max_idx] - n_x_ac; // subracting linear DC compoenents from raw
  219. n_nume=( n_y_ac *n_x_dc_max)>>7 ; //prepare X100 to preserve floating value
  220. n_denom= ( n_x_ac *n_y_dc_max)>>7;
  221. if (n_denom>0 && n_i_ratio_count <5 && n_nume != 0)
  222. {
  223. an_ratio[n_i_ratio_count]= (n_nume*20)/n_denom ; //formular is ( n_y_ac *n_x_dc_max) / ( n_x_ac *n_y_dc_max) ; ///*************************n_nume原来是*100************************//
  224. n_i_ratio_count++;
  225. }
  226. }
  227. }
  228. maxim_sort_ascend(an_ratio, n_i_ratio_count);
  229. n_middle_idx= n_i_ratio_count/2;
  230. if (n_middle_idx >1)
  231. n_ratio_average =( an_ratio[n_middle_idx-1] +an_ratio[n_middle_idx])/2; // use median
  232. else
  233. n_ratio_average = an_ratio[n_middle_idx ];
  234. if( n_ratio_average>2 && n_ratio_average <184){
  235. n_spo2_calc= uch_spo2_table[n_ratio_average] ;
  236. *pn_spo2 = n_spo2_calc ;
  237. *pch_spo2_valid = 1;// float_SPO2 = -45.060*n_ratio_average* n_ratio_average/10000 + 30.354 *n_ratio_average/100 + 94.845 ; // for comparison with table
  238. }
  239. else{
  240. *pn_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range
  241. *pch_spo2_valid = 0;
  242. }
  243. }
  244. void maxim_find_peaks(int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height, int32_t n_min_distance, int32_t n_max_num)
  245. /**
  246. * \brief Find peaks
  247. * \par Details
  248. * Find at most MAX_NUM peaks above MIN_HEIGHT separated by at least MIN_DISTANCE
  249. *
  250. * \retval None
  251. */
  252. {
  253. maxim_peaks_above_min_height( pn_locs, pn_npks, pn_x, n_size, n_min_height );
  254. maxim_remove_close_peaks( pn_locs, pn_npks, pn_x, n_min_distance );
  255. *pn_npks = min( *pn_npks, n_max_num );
  256. }
  257. void maxim_peaks_above_min_height(int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height)
  258. /**
  259. * \brief Find peaks above n_min_height
  260. * \par Details
  261. * Find all peaks above MIN_HEIGHT
  262. *
  263. * \retval None
  264. */
  265. {
  266. int32_t i = 1, n_width;
  267. *pn_npks = 0;
  268. while (i < n_size-1){
  269. if (pn_x[i] > n_min_height && pn_x[i] > pn_x[i-1]){ // find left edge of potential peaks
  270. n_width = 1;
  271. while (i+n_width < n_size && pn_x[i] == pn_x[i+n_width]) // find flat peaks
  272. n_width++;
  273. if (pn_x[i] > pn_x[i+n_width] && (*pn_npks) < 15 ){ // find right edge of peaks
  274. pn_locs[(*pn_npks)++] = i;
  275. // for flat peaks, peak location is left edge
  276. i += n_width+1;
  277. }
  278. else
  279. i += n_width;
  280. }
  281. else
  282. i++;
  283. }
  284. }
  285. void maxim_remove_close_peaks(int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_min_distance)
  286. /**
  287. * \brief Remove peaks
  288. * \par Details
  289. * Remove peaks separated by less than MIN_DISTANCE
  290. *
  291. * \retval None
  292. */
  293. {
  294. int32_t i, j, n_old_npks, n_dist;
  295. /* Order peaks from large to small */
  296. maxim_sort_indices_descend( pn_x, pn_locs, *pn_npks );
  297. for ( i = -1; i < *pn_npks; i++ ){
  298. n_old_npks = *pn_npks;
  299. *pn_npks = i+1;
  300. for ( j = i+1; j < n_old_npks; j++ ){
  301. n_dist = pn_locs[j] - ( i == -1 ? -1 : pn_locs[i] ); // lag-zero peak of autocorr is at index -1
  302. if ( n_dist > n_min_distance || n_dist < -n_min_distance )
  303. pn_locs[(*pn_npks)++] = pn_locs[j];
  304. }
  305. }
  306. // Resort indices longo ascending order
  307. maxim_sort_ascend( pn_locs, *pn_npks );
  308. }
  309. void maxim_sort_ascend(int32_t *pn_x,int32_t n_size)
  310. /**
  311. * \brief Sort array
  312. * \par Details
  313. * Sort array in ascending order (insertion sort algorithm)
  314. *
  315. * \retval None
  316. */
  317. {
  318. int32_t i, j, n_temp;
  319. for (i = 1; i < n_size; i++) {
  320. n_temp = pn_x[i];
  321. for (j = i; j > 0 && n_temp < pn_x[j-1]; j--)
  322. pn_x[j] = pn_x[j-1];
  323. pn_x[j] = n_temp;
  324. }
  325. }
  326. void maxim_sort_indices_descend(int32_t *pn_x, int32_t *pn_indx, int32_t n_size)
  327. /**
  328. * \brief Sort indices
  329. * \par Details
  330. * Sort indices according to descending order (insertion sort algorithm)
  331. *
  332. * \retval None
  333. */
  334. {
  335. int32_t i, j, n_temp;
  336. for (i = 1; i < n_size; i++) {
  337. n_temp = pn_indx[i];
  338. for (j = i; j > 0 && pn_x[n_temp] > pn_x[pn_indx[j-1]]; j--)
  339. pn_indx[j] = pn_indx[j-1];
  340. pn_indx[j] = n_temp;
  341. }
  342. }

algorithm.h

  1. /** \file algorithm.h ******************************************************
  2. *
  3. * Project: MAXREFDES117#
  4. * Filename: algorithm.h
  5. * Description: This module is the heart rate/SpO2 calculation algorithm header file
  6. *
  7. * Revision History:
  8. *\n 1-18-2016 Rev 01.00 SK Initial release.
  9. *\n
  10. *
  11. * --------------------------------------------------------------------
  12. *
  13. * This code follows the following naming conventions:
  14. *
  15. *\n char ch_pmod_value
  16. *\n char (array) s_pmod_s_string[16]
  17. *\n float f_pmod_value
  18. *\n int32_t n_pmod_value
  19. *\n int32_t (array) an_pmod_value[16]
  20. *\n int16_t w_pmod_value
  21. *\n int16_t (array) aw_pmod_value[16]
  22. *\n uint16_t uw_pmod_value
  23. *\n uint16_t (array) auw_pmod_value[16]
  24. *\n uint8_t uch_pmod_value
  25. *\n uint8_t (array) auch_pmod_buffer[16]
  26. *\n uint32_t un_pmod_value
  27. *\n int32_t * pn_pmod_value
  28. *
  29. * ------------------------------------------------------------------------- */
  30. /*******************************************************************************
  31. * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
  32. *
  33. * Permission is hereby granted, free of charge, to any person obtaining a
  34. * copy of this software and associated documentation files (the "Software"),
  35. * to deal in the Software without restriction, including without limitation
  36. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  37. * and/or sell copies of the Software, and to permit persons to whom the
  38. * Software is furnished to do so, subject to the following conditions:
  39. *
  40. * The above copyright notice and this permission notice shall be included
  41. * in all copies or substantial portions of the Software.
  42. *
  43. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  44. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  45. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  46. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  47. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  48. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  49. * OTHER DEALINGS IN THE SOFTWARE.
  50. *
  51. * Except as contained in this notice, the name of Maxim Integrated
  52. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  53. * Products, Inc. Branding Policy.
  54. *
  55. * The mere transfer of this software does not imply any licenses
  56. * of trade secrets, proprietary technology, copyrights, patents,
  57. * trademarks, maskwork rights, or any other form of intellectual
  58. * property whatsoever. Maxim Integrated Products, Inc. retains all
  59. * ownership rights.
  60. *******************************************************************************
  61. */
  62. #ifndef ALGORITHM_H_
  63. #define ALGORITHM_H_
  64. #include "sys.h"
  65. #define true 1
  66. #define false 0
  67. #define FS 100
  68. #define BUFFER_SIZE (FS* 5)
  69. #define HR_FIFO_SIZE 7
  70. #define MA4_SIZE 4 // DO NOT CHANGE
  71. #define HAMMING_SIZE 5// DO NOT CHANGE
  72. #define min(x,y) ((x) < (y) ? (x) : (y))
  73. //const uint16_t auw_hamm[31]={ 41, 276, 512, 276, 41 }; //Hamm= long16(512* hamming(5)');
  74. uch_spo2_table is computed as -45.060*ratioAverage* ratioAverage + 30.354 *ratioAverage + 94.845 ;
  75. //const uint8_t uch_spo2_table[184]={ 95, 95, 95, 96, 96, 96, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 99, 99, 99, 99,
  76. // 99, 99, 99, 99, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
  77. // 100, 100, 100, 100, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 98, 98, 98, 97, 97,
  78. // 97, 97, 96, 96, 96, 96, 95, 95, 95, 94, 94, 94, 93, 93, 93, 92, 92, 92, 91, 91,
  79. // 90, 90, 89, 89, 89, 88, 88, 87, 87, 86, 86, 85, 85, 84, 84, 83, 82, 82, 81, 81,
  80. // 80, 80, 79, 78, 78, 77, 76, 76, 75, 74, 74, 73, 72, 72, 71, 70, 69, 69, 68, 67,
  81. // 66, 66, 65, 64, 63, 62, 62, 61, 60, 59, 58, 57, 56, 56, 55, 54, 53, 52, 51, 50,
  82. // 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 31, 30, 29,
  83. // 28, 27, 26, 25, 23, 22, 21, 20, 19, 17, 16, 15, 14, 12, 11, 10, 9, 7, 6, 5,
  84. // 3, 2, 1 } ;
  85. //static int32_t an_dx[ BUFFER_SIZE-MA4_SIZE]; // delta
  86. //static int32_t an_x[ BUFFER_SIZE]; //ir
  87. //static int32_t an_y[ BUFFER_SIZE]; //red
  88. void maxim_heart_rate_and_oxygen_saturation(uint32_t *pun_ir_buffer , int32_t n_ir_buffer_length, uint32_t *pun_red_buffer , int32_t *pn_spo2, int8_t *pch_spo2_valid , int32_t *pn_heart_rate , int8_t *pch_hr_valid);
  89. void maxim_find_peaks( int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height, int32_t n_min_distance, int32_t n_max_num );
  90. void maxim_peaks_above_min_height( int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height );
  91. void maxim_remove_close_peaks( int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_min_distance );
  92. void maxim_sort_ascend( int32_t *pn_x, int32_t n_size );
  93. void maxim_sort_indices_descend( int32_t *pn_x, int32_t *pn_indx, int32_t n_size);
  94. #endif /* ALGORITHM_H_ */

五 、参考

 STM32+ MAX30102通过指尖测量心率+血氧饱和度icon-default.png?t=N7T8https://blog.csdn.net/qq_37603131/article/details/127943666?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170202716916800225583292%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170202716916800225583292&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-2-127943666-null-null.142%5Ev96%5Epc_search_result_base7&utm_term=MAX30102&spm=1018.2226.3001.4187


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/383043
推荐阅读
相关标签
  

闽ICP备14008679号