# Help with Programming (mikroC heart rate detect, count, calculate)

Status
Not open for further replies.

#### CWMC

##### Newbie level 5
Hi,

I am currently working on a heart rate monitor with the use of a micro controller. I would like to place the index finger over the sensor and the micro controller will calculate my heart rate by multiplying the beats by 4 after 15 seconds and displaying it on a 16 by 2 LCD display. I was interested to know if anyone could help me with the programming side using mikroC as i am not entirely sure what i should be doing.

I thought it would be an analogue input from the signal obtained by the sensor, therefore i believe i need to count using mikroC but I'm not sure the best way to do it.

Any help would be greatly appreciated.

Thanks

#### Easyrider83

##### Advanced Member level 5
Now we should guess the sensor that you wanna use?

#### CWMC

##### Newbie level 5
It is a TCRT1000 that i am using

#### andre_luis

##### Super Moderator
Staff member
Are you aware on how heart beat detection is done with optical devices ? The part you specified if for reflexive operation, whereas heart beat sensing is made with optical sensors whose emitter is placed behind the object of interest. Basically, variation in blood flow is detected through the variation of the light.

#### Easyrider83

##### Advanced Member level 5
Well, this is a bit difficult for beginner. Acquare some data, normalize it, then switch to frequency domain (FFT), that filter it, then return back to time domain, then analyze it.

#### CWMC

##### Newbie level 5
I understand how the pulse is created and how to collect a pulse. I'm just sure on how to count the analogue input in mikroC.

For example, the link below shows the code used but i don't quite understand what is going on in the code. If anyone can talk me through what is happening that would be great.
https://embedded-lab.com/blog/heart-rate-measurement-from-fingertip/2/

CWMC

### CWMC

Points: 2

#### CWMC

##### Newbie level 5
Heart Rate Monitor Code in mikroC

Hi,

I am using easy PIC 6 board with a pic16f887 chip. The code is shown below but I seem to be getting really large values. I am using RA2 as a analogue input (which will be for the amplification circuitry output from my fingertip sensor). I want to achieve each peak (positive) value within 15 seconds and multiply this value by 4 to get beats per minute. It is just a simple design.

If anyone can help me with my code for the heart rate monitor that would be greatly appreciated.

MikroC code:

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

int Peak_count = 0;
int sample = 0;
char bpm[7];

char txt1[]="Welcome";                         // Text 1 for LCD display.
char txt2[]="Normal bpm=60-80";                // Text 2 for LCD display.
char txt3[]="Finger on Sensor";                // Text 3 for LCD display.
char txt4[]="Press start PB";                  // Text 4 for LCD display.
char txt5[]="Please wait...";                  // Text 5 for LCD display.
char txt6[]="Your heart rate";                 // Text 6 for LCD display.
char txt7[]="is = ";                           // Text 7 for LCD display.
char txt8[]="Press clear to start over";       // Text 8 for LCD display.

int a;

void main() {

ANSEL  = 0x07;              // Setting AN2 pin as analog.
ANSELH = 0;                 // Setting other AN pins as digital I/O.
C1ON_bit = 0;               // Disable comparators.
C2ON_bit = 0;

TRISA = 0b00000100;        // Setting RA2/AN2 as input.

TRISB = 0;                 // LCD display set as output.

TRISC = 0;
TRISD = 0;
ADC_Prev = 0;              // Set first sample point as 0

a = 0;

/* This first section is simply to provide the user of what is about happen. */

Lcd_Init();                // Initialising LCD display.
Lcd_cmd(_LCD_CLEAR);       // Clearling display.
Lcd_Cmd(_LCD_CURSOR_OFF);  // Turing cursor off.
Lcd_Out(1,1,txt1);         // Displaying text 1 on line 1.
Lcd_Out(2,1,txt2);         // Displaying text 2 on line 2.

delay_ms(3000);

Lcd_cmd(_LCD_CLEAR);       // Clearling display.
Lcd_Out(1,1,txt3);         // Displaying text 3 on line 1.
Lcd_Out(2,1,txt4);         // Displaying text 4 on line 2.
/* In this next section, when the start push button is pressed, the pulse counting process will begin. */

do{
sample++;                      // add one to sample
delay_us(375);

if (ADC > ADC_Prev)    // No it isnt, add one to the count (PEAK VALUE).
{
ADC_Prev = ADC;       // Yes it is, make the new sample ADC.
delay_us(375);
a = 1;
}

if ((ADC < ADC_Prev)&&(a == 1))    // if value is greater than previous and a = 1.
{
Peak_count++;        // Adds one to the count. ie peak is found.
delay_us(375);

a = 0;
}
}
while(sample < 10000);

heart_rate = Peak_count * 4;
Lcd_Cmd(_LCD_CLEAR);
IntToStr(heart_rate, bpm);
Lcd_Out(1,1, txt6);
Lcd_Out(2,5, bpm);
Lcd_Out(2,1, txt7);
}

Last edited by a moderator:

#### andre_luis

##### Super Moderator
Staff member
If anyone can help me with my code for the heart rate monitor that would be greatly appreciated

You provided a code with no textual headers or any mention of what hardware was used to capture the pulses. Has it gone through your mind to detail a little more everything that involves the project? The only thing related to the hardware that I can see at the referred link is a smplified diagram and a picture of the board. I not patience to seek if it was hidden in some link there.

#### CWMC

##### Newbie level 5
Re: Heart Rate Monitor Code in mikroC

Thank you. If you could help me to solve this problem with mikroC and pic16f887 that would be a great help.
I have a few questions:
How does the INT pin or timer work to do this?
I have looked at that code on the embedded lab website but don't understand every line or how to implement it to my code on mikroC.
Any help on sections to write the code for this project would be greatly appreciated.
Hope to hear back soon

Status
Not open for further replies.