电子脉搏器怎么用如何使用PIC微控制器和脉搏传感器进行心跳监测

新闻资讯2026-04-21 19:04:34

心跳率是监测任何人健康状况的最重要参数。在可穿戴设备的现代时代,有很多设备可以测量心跳、血压、脚步声、燃烧的卡路里和许多其他东西。这些设备内部有脉冲传感器来感应脉搏率。今天,我们还将使用带有PIC微控制器的脉冲传感器来计算每分钟的心跳次数和心跳间隔,这些值将进一步显示在16x2字符LCD上。我们将在本项目中使用PIC16F877A PIC微控制器。我们已经将脉冲传感器与Arduino连接起来,用于患者监测系统。

必需组件

PIC16F877A 微控制器

20 兆赫晶体

33pF电容 2个

4.7k 电阻器 1 个

16x2 字符液晶显示器

10K 电位器,用于 LCD 的对比度控制

SEN-11574 脉冲传感器

魔术贴带

5V电源适配器

面包板和连接线

脉冲传感器 SEN-11574

为了测量心跳,我们需要一个脉搏传感器。在这里,我们选择了SEN-11574脉冲传感器,它可以在网上或线下商店轻松买到。我们使用此传感器是因为制造商提供了示例代码,但那是Arduino代码。我们为PIC微控制器转换了该代码。

电子脉搏器怎么用如何使用PIC微控制器和脉搏传感器进行心跳监测_https://www.jmylbn.com_新闻资讯_第1张

该传感器非常小,非常适合通过耳垂或指尖读取心跳。它的直径为 0.625“,圆形 PCB 侧的厚度为 0.125”。

该传感器提供模拟信号,传感器可由3V或5V驱动,传感器的电流消耗为4 mA,非常适合移动应用。传感器配有三根电线,末端带有 24 英寸长的连接电缆和 berg 公接头。此外,传感器还配有魔术贴指带,可将其佩戴在指尖上。

脉冲传感器原理图也由制造商提供,也可在 sparkfun.com 上购买。

电子脉搏器怎么用如何使用PIC微控制器和脉搏传感器进行心跳监测_https://www.jmylbn.com_新闻资讯_第2张

传感器原理图由光学心率传感器、降噪RC电路或滤波器组成,如原理图所示。R2、C2、C1、C3 和运算放大器 MCP6001 用于可靠的放大模拟输出。

用于心跳监测的其他传感器很少,但SEN-11574脉冲传感器广泛用于电子项目。

脉冲传感器与PIC微控制器接口的电路图

电子脉搏器怎么用如何使用PIC微控制器和脉搏传感器进行心跳监测_https://www.jmylbn.com_新闻资讯_第3张

在这里,我们已经连接了脉冲传感器跨 2德·微控制器的引脚单位。由于传感器提供模拟数据,我们需要通过进行必要的计算将模拟数据转换为数字信号

20Mhz的晶体振荡器通过两个陶瓷33pF电容器连接在微控制器单元的两个OSC引脚上。液晶屏通过微控制器的RB端口连接。

电子脉搏器怎么用如何使用PIC微控制器和脉搏传感器进行心跳监测_https://www.jmylbn.com_新闻资讯_第4张

PIC16F877A 心跳监护仪代码说明

对于初学者来说,代码有点复杂。制造商提供了SEN-11574传感器的示例代码,但它是为Arduino平台编写的。我们需要转换微芯片 PIC16F877A 的计算结果。本项目结束时将提供完整的代码,并附有演示视频。并且可以从此处下载支持的C文件。

我们的代码流相对简单,我们使用开关大小写来执行步骤。根据制造商的说法,我们需要每 2 毫秒从传感器获取数据。因此,我们使用了计时器中断服务例程,它将每 2 毫秒触发一个函数。

switch语句中的代码流将如下所示:

案例1:读取ADC

案例2:计算心跳和IBI

情况 3:在液晶屏上显示心跳和 IBI

案例 4:空闲(不执行任何操作)

定时器中断功能中,我们将程序的状态更改为情况 1:每 2 毫秒读取一次 ADC。

因此,在 main 函数中,我们定义了程序状态和所有开关情况。

void main() {

system_init();

main_state = READ_ADC;

while (1) {

switch (main_state) {

case READ_ADC:

{

adc_value = ADC_Read(0); // 0 is the channel number

main_state = CALCULATE_HEART_BEAT;

break;

}

case CALCULATE_HEART_BEAT:

{

calculate_heart_beat(adc_value);

main_state = SHOW_HEART_BEAT;

break;

}

case SHOW_HEART_BEAT:

}

main_state = IDLE;

break;

case IDLE:

{

break;

}

default:

{

}

}

}

}

我们使用 PIC16F877A 的两个硬件外设:定时器0 和 ADC。

在 timer0.c 文件中,

TMR0 = (uint8_t)(tmr0_mask & (256-(((2 *_XTAL_FREQ)/(256*4))/1000)));

此计算提供 2 毫秒计时器中断。计算公式为

// TimerCountMax - (((delay(ms) * Focs(hz)) / (PreScale_Val * 4)) / 1000)

如果我们看到timer_isr函数,它是-

void timer_isr() {

main_state = READ_ADC;

}

在此函数中,程序状态每 2ms 更改为 READ_ADC。

然后,CALCULATE_HEART_BEAT函数取自 Arduino 示例代码。

void calculate_heart_beat(int adc_value)

}

………….

………………………..

此外,下面给出了完整的代码,并通过注释进行了很好的解释。这种心跳传感器数据可以进一步上传到云端,并从任何地方通过互联网进行监控,从而使其成为基于物联网的心跳监测系统。

/*

* File: main.c

* Author: Sourav Gupta

* By:- circuitdigest.com

* Created on September 30, 2018, 2:26 PM

*/

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)

#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)

#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)

#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)

#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)

#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)

#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)

#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

#include

#include

#include

#include

#include

#include "supporing_cfilelcd.h"

#include "supporing_cfileeusart1.h"

#include "supporing_cfileadc.h"

#include "supporing_cfiletmr0.h"

/*

Hardware related definition

*/

#define _XTAL_FREQ 200000000 //Crystal Frequency, used in delay

/*

Program Flow related definition

*/

#define READ_ADC 1

#define CALCULATE_HEART_BEAT 2

#define SHOW_HEART_BEAT 3

#define IDLE 0

#define DEFAULT -1

volatile int rate[10]; // array to hold last ten IBI values

volatile unsigned long sampleCounter = 0; // used to determine pulse timing

volatile unsigned long lastBeatTime = 0; // used to find IBI

volatile int P = 512; // used to find peak in pulse wave, seeded

volatile int T = 512; // used to find trough in pulse wave, seeded

volatile int thresh = 530; // used to find instant moment of heart beat, seeded

volatile int amp = 0; // used to hold amplitude of pulse waveform, seeded

volatile bool firstBeat = true; // used to seed rate array so we startup with reasonable BPM

volatile bool secondBeat = false; // used to seed rate array so we startup with reasonable BPM

volatile int BPM; // int that holds raw Analog in 0. updated every 2mS

volatile int Signal; // holds the incoming raw data

volatile int IBI = 600; // int that holds the time interval between beats! Must be seeded!

volatile bool Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".

volatile bool QS = false; // becomes true when finds a beat.

int main_state = -1;

int adc_value = 0;

int tune = 0;

/*

Other Specific definition

*/

void system_init(void);

void calculate_heart_beat(int adc_value)

}

if (Signal > thresh && Signal > P) { // thresh condition helps avoid noise

P = Signal; // P is the peak

} // keep track of highest point in pulse wave

// NOW IT'S TIME TO LOOK FOR THE HEART BEAT

// signal surges up in value every time there is a pulse

if (N > 250)

}

if (firstBeat)

// keep a running total of the last 10 IBI values

uint16_t runningTotal = 0; // clear the runningTotal variable

int i;

for (i = 0; i <= 8; i++)
rate[9] = IBI; // add the latest IBI to the rate array

runningTotal += rate[9]; // add the latest IBI to runningTotal

runningTotal /= 10; // average the last 10 IBI values

BPM = 60000 / runningTotal; // how many beats can fit into a minute? that's BPM!

QS = true; // set Quantified Self flag

// QS FLAG IS NOT CLEARED INSIDE THIS ISR

}

}

if (Signal < thresh && Pulse == true)
if (N > 2500)

}

void main() {

system_init();

main_state = READ_ADC;

while (1) {

switch (main_state) {

case READ_ADC:

{

adc_value = ADC_Read(0);

main_state = CALCULATE_HEART_BEAT;

break;

}

case CALCULATE_HEART_BEAT:

{

calculate_heart_beat(adc_value);

main_state = SHOW_HEART_BEAT;

break;

}

case SHOW_HEART_BEAT:

}

main_state = IDLE;

break;

case IDLE:

{

break;

}

default:

{

}

}

}

}

/*

This Function is for system initializations.

*/

void system_init(void){

TRISB = 0x00;

lcd_init(); // This will initialize the lcd

TMR0_Initialize();

TMR0_StartTimer();

INTERRUPT_GlobalInterruptEnable();

INTERRUPT_PeripheralInterruptEnable();

ADC_Init();

}

/*

* Custom timer callback function

*/

void timer_isr() {

main_state = READ_ADC;

}

void interrupt INTERRUPT_InterruptManager (void)

}