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;
unsigned int ADC, heart_rate, ADC_Prev;
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. */
ADC_Init(); // Initialising ADC converter.
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{
ADC = ADC_Read(2); // sample from AN2.
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.
{
ADC_Prev = ADC; // Make ADC the new sample.
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);
} |