|
- #include "stm32f10x.h"
- #include "usart.h"
- #include "oled.h"
- #include "word.h"
- #include "delay.h"
- #include "max30102.h"
- #include "30102_algorithm.h"
- #include "IIC.h"
- #define MAX_BRIGHTNESS 255
- uint32_t aun_ir_buffer[500]; //IR LED sensor data
- int32_t n_ir_buffer_length; //data length
- uint32_t aun_red_buffer[500]; //Red LED sensor data
- int32_t n_sp02; //SPO2 value
- int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid
- int32_t n_heart_rate; //heart rate value
- int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid
- uint8_t uch_dummy;
- int32_t hr_buf[16];
- int32_t hrSum;
- int32_t hrAvg;
- int32_t spo2_buf[16];
- int32_t spo2Sum;
- int32_t spo2Avg;
- int32_t spo2BuffFilled;
- int32_t hrBuffFilled;
- int32_t hrValidCnt = 0;
- int32_t spo2ValidCnt = 0;
- int32_t hrThrowOutSamp = 0;
- int32_t spo2ThrowOutSamp = 0;
- int32_t spo2Timeout = 0;
- int32_t hrTimeout = 0;
- int main(void)
- {
- // char buf[] = {"Red = "};
- //char buf1[] = {"Ir = "};
- char buf2[] = {"HR = "};
- char buf3[] = {"SpO2 = "};
- //char buf[] = {"There is no luck"};
- //char buf1[] = {"There is only"};
- //char buf2[] = {"word."};
- //char author[] = {"Qing"};
- int i;
- float f_temp;
- uint32_t un_min, un_max, un_prev_data; //variables to calculate the on-board LED brightness that reflects the heartbeats
- int32_t n_brightness;
- delay_init();
- Oled_Init();
-
- //Oled_Display_String(, 0, buf); //顯示字符串
- //Oled_Display_String(2, 0, buf1); //顯示字符串
- Oled_Display_String(0, 0, buf2); //顯示字符串
- Oled_Display_String(4, 0, buf3);
- //Oled_Display_Pic(128,64,0,0,pic);
-
- //uint8_t IIC_Flag=1;//IIC通信狀態(tài),0為成功1為失敗
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 設(shè)置中斷優(yōu)先級分組2
- delay_init(); //延時函數(shù)初始化
- uart_init(115200); //串口初始化為9600
- IIC_Init();
- if(!maxim_max30102_reset())//復位 MAX30102
- printf("max30102_reset failed!\r\n");
- if(!maxim_max30102_read_reg(REG_INTR_STATUS_1,&uch_dummy))//讀取并清除狀態(tài)寄存器
- printf("read_reg REG_INTR_STATUS_1 failed!\r\n");
- if(!maxim_max30102_init())//初始化MAX30102
- printf("max30102_init failed!\r\n");
- //printf("指示燈亮了嗎?\r\n");
- n_brightness = 0;
- un_min = 0x3FFFF;
- un_max = 0;
- n_ir_buffer_length = 500; //緩沖區(qū)長度為100存儲以100sps運行的5秒樣本
- printf("采集500個樣本\r\n");
- //讀取前500個樣本,并確定信號范圍
- for(i = 0; i < n_ir_buffer_length; i++)
- {
- while(max30102_INTPin == 1); //等待MAX30102中斷引腳拉低
- maxim_max30102_read_fifo((aun_ir_buffer+i), (aun_red_buffer+i)); //從MAX30102 FIFO讀取
- if(un_min > aun_red_buffer[i])
- un_min = aun_red_buffer[i]; //更新信號最小值
- if(un_max < aun_red_buffer[i])
- un_max = aun_red_buffer[i]; //更新信號最大值
- printf("red=");
- printf("%i", aun_red_buffer[i]);
- printf(", ir=");
- printf("%i\r\n", aun_ir_buffer[i]);
- }
- un_prev_data = aun_red_buffer[i];
- //計算前500個樣本后的心率和血氧飽和度(樣本的前5秒)
- 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);
- //從MAX30102連續(xù)取樣。每1秒計算一次心率和血氧飽和度
- while(1)
- {
- i = 0;
- un_min = 0x3FFFF;
- un_max = 0;
- //轉(zhuǎn)儲內(nèi)存中的前100組樣本,并將最后400組樣本移到頂部
- for(i = 100; i < 500; i++)
- {
- aun_red_buffer[i-100] = aun_red_buffer[i];
- aun_ir_buffer[i-100] = aun_ir_buffer[i];
- //update the signal min and max
- if(un_min > aun_red_buffer[i])
- un_min = aun_red_buffer[i];
- if(un_max < aun_red_buffer[i])
- un_max = aun_red_buffer[i];
- }
- //在計算心率之前采集100組樣本。
- for(i = 400; i < 500; i++)
- {
- un_prev_data = aun_red_buffer[i-1];
- while(max30102_INTPin == 1); //等待MAX30102中斷引腳拉低
- maxim_max30102_read_fifo((aun_ir_buffer+i), (aun_red_buffer+i));
- if(aun_red_buffer[i] > un_prev_data)//根據(jù)相鄰兩個AD數(shù)據(jù)的偏差來確定LED的亮度
- {
- f_temp = aun_red_buffer[i] - un_prev_data;
- f_temp /= (un_max-un_min);
- f_temp *= MAX_BRIGHTNESS;
- n_brightness -= (int)f_temp;
- if(n_brightness < 0)
- n_brightness = 0;
- }
- else
- {
- f_temp = un_prev_data-aun_red_buffer[i];
- f_temp /= (un_max-un_min);
- f_temp *= MAX_BRIGHTNESS;
- n_brightness += (int)f_temp;
- if(n_brightness > MAX_BRIGHTNESS)
- n_brightness = MAX_BRIGHTNESS;
- }
- //通過UART向終端程序發(fā)送樣本和計算結(jié)果
- printf("red=");
- printf("%i", aun_red_buffer[i]);
- printf(", ir=");
- printf("%i", aun_ir_buffer[i]);
- printf(", HR=%i, ", n_heart_rate);
- printf("HRvalid=%i, ", ch_hr_valid);
- printf("SpO2=%i, ", n_sp02);
-
- printf("SPO2Valid=%i\r\n", ch_spo2_valid);
- }
- //OLED_ShowInt(0, 60, aun_red_buffer[i]); //顯示數(shù)據(jù)
- //OLED_ShowInt(2, 60, aun_ir_buffer[i]); //顯示數(shù)據(jù)
- OLED_ShowInt(0, 60, n_heart_rate); //顯示數(shù)據(jù)
- OLED_ShowInt(4, 60, n_sp02); //顯示數(shù)據(jù)
- 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);
- }
- }
- /*
- int main(void)
- {
- char buf[] = {"There is no luck"};
- char buf1[] = {"There is only"};
- char buf2[] = {"word."};
- char author[] = {"Qing"};
-
- delay_init();
- Oled_Init();
- while(1)
- {
- Oled_Display_Pic(128,64,0,0,pic);
- }
- }
- // Oled_Display_Char(0,0,'A');//顯示單個字符
- //There is no luck.There is only work.幸運是不存在的,努力才是硬道理。
- //Oled_Display_String(0,0,buf); //顯示字符串
- //Oled_Display_String(2,0,buf1); //顯示字符串
- //Oled_Display_String(4,0,buf2); //顯示字符串
- //Oled_Display_String(6,80,author);
- // Oled_Display_Pic(128,64,0,0,pic);//顯示圖片
- // Oled_Display_Pic(32,32,2,48,chain);//顯示漢字
- */
復制代碼 |
-
-
程序.7z
2023-7-30 13:49 上傳
點擊文件名下載附件
312.96 KB, 下載次數(shù): 3
|