Heart rate measurement from fingertip. Part 2. Software

Part 1. Schematic


The firmware does all the control and computation operation. In order to save the power, the sensor module is not activated continuously. Instead, it is turned on for 15 sec only once the start button is pressed. The pulses arriving at T0CKI are counted through Timer0 module operated in counter mode without prescaler. The complete program written for MikroC compiler (mikroElektronika) is provided below. An assembled HEX file is also available to download.

Project: Measuring heart rate through fingertip
Copyright @ Rajendra Bhatt
January 18, 2011
PIC16F628A at 4.0 MHz external clock, MCLR enabled

sbit IR_Tx at RA3_bit;
sbit DD0_Set at RA2_bit;
sbit DD1_Set at RA1_bit;
sbit DD2_Set at RA0_bit;
sbit start at RB7_bit;
unsigned short j, DD0, DD1, DD2, DD3;
unsigned short pulserate, pulsecount;
unsigned int i;
//-------------- Function to Return mask for common anode 7-seg. display
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
} //case end

void delay_debounce(){

void delay_refresh(){

void countpulse(){
IR_Tx = 1;
Delay_ms(15000); // Delay 15 Sec
IR_Tx = 0;
pulsecount = TMR0;
pulserate = pulsecount*4;

void display(){
DD0 = pulserate%10;
DD0 = mask(DD0);
DD1 = (pulserate/10)%10;
DD1 = mask(DD1);
DD2 = pulserate/100;
DD2 = mask(DD2);
for (i = 0; i<=180*j; i++) {
DD0_Set = 0;
DD1_Set = 1;
DD2_Set = 1;
DD0_Set = 1;
DD1_Set = 0;
DD2_Set = 1;
DD0_Set = 1;
DD1_Set = 1;
DD2_Set = 0;
DD2_Set = 1;

void main() {
CMCON = 0x07; // Disable Comparators
TRISA = 0b00110000; // RA4/T0CKI input, RA5 is I/P only
TRISB = 0b10000000; // RB7 input, rest output
OPTION_REG = 0b00101000; // Prescaler (1:1), TOCS =1 for counter mode
pulserate = 0;
j = 1;
do {
j= 3;
} while(1); // Infinite loop


Source Code and hex - download

Part 3. References, notes, using the device.


You may have to register before you can post comments and get full access to forum.
User Name
Fragments of discussion:Full version of discussion »
  • in the heart sensor cct pin no 5 of 16f628 is meant for vss(-). it is again shown +5v through10k resistance to pin no 5. please clarify. thanking you
  • "Clear" button should be connected to pin 4 of PIC18F628 (MCLR). I think, The schematic will be fixed soon...
  • Hey i am doing the same project using AT89C51. Can u please send me the code for that? in C language . Mi id is : [email]niklimaye@yahoo.co.in[/email]
  • dear nikl plz make open everything and here is software unsigned int counter1=0;//counter2=0; unsigned long freq=0,counter2=0,tx=0; unsigned char ei=0,test=0; unsigned char op[13],lcd[13],j=0,i=0,k=0; unsigned float efreq=0; void lcd_out1() { k++; freq=(counter2*256+counter1+5)*2; //if(freq!=0) efreq=1000000/freq; tx=efreq*60; usart_write(tx); //lcd_chr(3,1,test+48); } void interrupt(void) { if(INTCON.INTF)/////EXTERNAL INTERRUPT { ei++; INTCON.INTF=0; test++; if(ei==1) { // lcd_out(2,1," ); tmr0=0; counter2=0; intcon.f5=1;////////start timer } else if(ei==2) { ei=0; intcon.f5=0;////stop timer counter1=tmr0; intcon.gie=0; lcd_out1(); intcon.gie=1; } } if (INTCON.TMR0IF)//////timer interrupt { counter2++; TMR0 = 0; INTCON.TMR0IF = 0; } } void main(void) { //trisb=0xff; trisb.f0=1; //intcon=0x10;/////global interrupt enabled option_reg=0x80; option_reg.f6=1;///interrupt on rising edge //trisc=0; //lcd_init(&portc); //lcd_config(&portc,0,2,1,7,6,5,4); usart_init(9600); INTCON = 0xb4; intcon.f5=0;////////start timer for(;;) { } }
  • Hey thanks a lot... Could you please send me the assembly program as well .. thanks
  • We are using a power supply which converts 230V AC to 5V DC .. CAn you please tell the current rating of the secondary of the transformer ASAP!
  • hello, i'm doing the same project but using PIC16f877a and in place of 7-segment i'm going to use lcd .can anyone plz explain me how the PIC microcontroller counts the pulses?
  • Use external interrupt int. The number of Ismaylov for 1 second and will be the frequency...
  • hii can u please send me the code for pic16f877a at my email id [email]ansari_shaherbano@yahoo.com[/email] in place of seven segment display i'm using lcd.... please help me.... thanks:)
  • Hi sherry Try to do it yourself. There is nothing complicated. Ask questions..
  • Hii can anyone pls explain how it calculates heart rate using pulses in program?
Full version of discussion »