数值还是稳定的,但太贵了,stm32单片机的话,直接用这篇博客(https://qq742971636.blog.csdn.net/article/details/137359381)的代码,一样稳定。
解析这个模块的数据
char rev_max30102_bufferdata[30];
char rev_max30102_data_cnt = 0;
char rev_max30102_flag = 0;
char rev_max30102_timercnt = 0;
char rev_max30102_sucess = 0;
uint32_t max30102_value_heart = 0;
uint32_t max30102_value_spo2 = 0;
char trig_max30102 = 0;
char trig_max30102_suc_cnt = 0;
char trig_max30102_timer_cnt = 0;
void USART3_IRQHandler(void) {
u8 res;
if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) /* 接收到数据 */
{
res = USART_ReceiveData(USART3);
if (rev_max30102_flag == 0) {
rev_max30102_data_cnt = 0;
rev_max30102_bufferdata[rev_max30102_data_cnt++] = res;
rev_max30102_flag = 1;
} else if (rev_max30102_flag == 1) {
rev_max30102_bufferdata[rev_max30102_data_cnt++] = res;
if (rev_max30102_data_cnt >= 30) {
rev_max30102_data_cnt = 0;
}
}
}
}
// 10ms
void TIM4_IRQHandler(void) /* TIM4中断 */
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) /* 检查TIM4更新中断发生与否 */
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update); /* 清除TIM4更新中断标志 */
//10ms一帧
if (rev_max30102_flag == 1) {
rev_max30102_timercnt++;
}
if (rev_max30102_timercnt >= 4) {
rev_max30102_timercnt = 0;
rev_max30102_flag = 0;
//数据处理标志
rev_max30102_sucess = 1;
}
}
}
if (trig_max30102) {
if (trig_max30102_suc_cnt == 0) {//心率测量
trig_max30102_timer_cnt++;
if (trig_max30102_timer_cnt == 2) {
USART3_SendString("AT+HEART\r\n");
}
if (trig_max30102_timer_cnt > 20) { //200ms发一次
trig_max30102_timer_cnt = 0;
}
if (rev_max30102_sucess == 1) {
rev_max30102_sucess = 0;
//收到的是"+HEART=75\r\nOK"
if ((rev_max30102_bufferdata[0] == '+') && (rev_max30102_bufferdata[1] == 'H') &&
(rev_max30102_bufferdata[2] == 'E') && (rev_max30102_bufferdata[3] == 'A') &&
(rev_max30102_bufferdata[4] == 'R') && (rev_max30102_bufferdata[5] == 'T')) {
if (rev_max30102_bufferdata[9] == '\r') {
rev_max30102_bufferdata[7] = rev_max30102_bufferdata[7] - '0';
rev_max30102_bufferdata[8] = rev_max30102_bufferdata[8] - '0';
max30102_value_heart = rev_max30102_bufferdata[7] * 10 + rev_max30102_bufferdata[8];
trig_max30102_suc_cnt = 1;//心率测完了
} else if (rev_max30102_bufferdata[10] == '\r') {
rev_max30102_bufferdata[7] = rev_max30102_bufferdata[7] - '0';
rev_max30102_bufferdata[8] = rev_max30102_bufferdata[8] - '0';
rev_max30102_bufferdata[9] = rev_max30102_bufferdata[9] - '0';
max30102_value_heart = rev_max30102_bufferdata[7] * 100 + rev_max30102_bufferdata[8] * 10 +
rev_max30102_bufferdata[9];
trig_max30102_suc_cnt = 1;//心率测完了
}
}
}
} else if (trig_max30102_suc_cnt == 1) {//测血氧
trig_max30102_timer_cnt++;
if (trig_max30102_timer_cnt == 2) {
USART3_SendString("AT+SPO2\r\n");
}
if (trig_max30102_timer_cnt > 20) { //200ms发一次
trig_max30102_timer_cnt = 0;
}
if (rev_max30102_sucess == 1) {
rev_max30102_sucess = 0;
//收到的是"+SPO2=75\r\nOK"
if ((rev_max30102_bufferdata[0] == '+') && (rev_max30102_bufferdata[1] == 'S') &&
(rev_max30102_bufferdata[2] == 'P') && (rev_max30102_bufferdata[3] == 'O') &&
(rev_max30102_bufferdata[4] == '2')) {
if (rev_max30102_bufferdata[8] == '\r') {
rev_max30102_bufferdata[6] = rev_max30102_bufferdata[6] - '0';
rev_max30102_bufferdata[7] = rev_max30102_bufferdata[7] - '0';
max30102_value_spo2 = rev_max30102_bufferdata[6] * 10 + rev_max30102_bufferdata[7];
trig_max30102_suc_cnt = 2;//血氧测完了
trig_max30102 = 0;
} else if (rev_max30102_bufferdata[9] == '\r') {
rev_max30102_bufferdata[6] = rev_max30102_bufferdata[6] - '0';
rev_max30102_bufferdata[7] = rev_max30102_bufferdata[7] - '0';
rev_max30102_bufferdata[8] = rev_max30102_bufferdata[8] - '0';
max30102_value_spo2 = rev_max30102_bufferdata[6] * 100 + rev_max30102_bufferdata[7] * 10 +
rev_max30102_bufferdata[8];
trig_max30102_suc_cnt = 2;//血氧测完了
trig_max30102 = 0;
}
}
}
}
}
//显示心率和血氧
memset(disp, 0, 16);
sprintf(disp, "HR%d SPO2%d", max30102_value_heart, max30102_value_spo2);
OLED_P8x16Str(0, 4, disp, 0);
帮助、问询
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2