-
Notifications
You must be signed in to change notification settings - Fork 0
/
hr_monitor.c
361 lines (290 loc) · 7.63 KB
/
hr_monitor.c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*HR_monitor.c
Carson Berry and Abdo Elhosary
Code to interpret the schmidt trigger-filtered signal of someone's HR through their finger.
Should be coupled with a finger-sensor that utilizes IR radiation and it's differential absorbtion with a phototransistor
*/
// The next line clears the "C51 command line options:" field when compiling with CrossIDE
// ~C51~
#include <EFM8LB1.h>
#include <stdio.h>
#define SYSCLK 72000000L // SYSCLK frequency in Hz
#define BAUDRATE 115200L // Baud rate of UART in bps
#define LCD_RS P2_6
// #define LCD_RW Px_x // Not used in this code. Connect to GND
#define LCD_E P2_5
#define LCD_D4 P2_4
#define LCD_D5 P2_3
#define LCD_D6 P2_2
#define LCD_D7 P2_1
#define input P2_0
#define CHARS_PER_LINE 16
#define sample_size 6
unsigned char overflow_count;
char _c51_external_startup (void)
{
// Disable Watchdog with key sequence
SFRPAGE = 0x00;
WDTCN = 0xDE; //First key
WDTCN = 0xAD; //Second key
VDM0CN |= 0x80;
RSTSRC = 0x02;
#if (SYSCLK == 48000000L)
SFRPAGE = 0x10;
PFE0CN = 0x10; // SYSCLK < 50 MHz.
SFRPAGE = 0x00;
#elif (SYSCLK == 72000000L)
SFRPAGE = 0x10;
PFE0CN = 0x20; // SYSCLK < 75 MHz.
SFRPAGE = 0x00;
#endif
#if (SYSCLK == 12250000L)
CLKSEL = 0x10;
CLKSEL = 0x10;
while ((CLKSEL & 0x80) == 0);
#elif (SYSCLK == 24500000L)
CLKSEL = 0x00;
CLKSEL = 0x00;
while ((CLKSEL & 0x80) == 0);
#elif (SYSCLK == 48000000L)
// Before setting clock to 48 MHz, must transition to 24.5 MHz first
CLKSEL = 0x00;
CLKSEL = 0x00;
while ((CLKSEL & 0x80) == 0);
CLKSEL = 0x07;
CLKSEL = 0x07;
while ((CLKSEL & 0x80) == 0);
#elif (SYSCLK == 72000000L)
// Before setting clock to 72 MHz, must transition to 24.5 MHz first
CLKSEL = 0x00;
CLKSEL = 0x00;
while ((CLKSEL & 0x80) == 0);
CLKSEL = 0x03;
CLKSEL = 0x03;
while ((CLKSEL & 0x80) == 0);
#else
#error SYSCLK must be either 12250000L, 24500000L, 48000000L, or 72000000L
#endif
P0MDOUT |= 0x10; // Enable UART0 TX as push-pull output
XBR0 = 0x01; // Enable UART0 on P0.4(TX) and P0.5(RX)
XBR1 = 0X00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
#if (((SYSCLK/BAUDRATE)/(2L*12L))>0xFFL)
#error Timer 0 reload value is incorrect because (SYSCLK/BAUDRATE)/(2L*12L) > 0xFF
#endif
// Configure Uart 0
SCON0 = 0x10;
CKCON0 |= 0b_0000_0000 ; // Timer 1 uses the system clock divided by 12.
TH1 = 0x100-((SYSCLK/BAUDRATE)/(2L*12L));
TL1 = TH1; // Init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit auto-reload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI = 1; // Indicate TX0 ready
return 0;
}
// Uses Timer3 to delay <us> micro-seconds.
void Timer3us(unsigned char us)
{
unsigned char i; // usec counter
// The input for Timer 3 is selected as SYSCLK by setting T3ML (bit 6) of CKCON0:
CKCON0|=0b_0100_0000;
TMR3RL = (-(SYSCLK)/1000000L); // Set Timer3 to overflow in 1us.
TMR3 = TMR3RL; // Initialize Timer3 for first overflow
TMR3CN0 = 0x04; // Sart Timer3 and clear overflow flag
for (i = 0; i < us; i++) // Count <us> overflows
{
while (!(TMR3CN0 & 0x80)); // Wait for overflow
TMR3CN0 &= ~(0x80); // Clear overflow indicator
}
TMR3CN0 = 0 ; // Stop Timer3 and clear overflow flag
}
void waitms (unsigned int ms)
{
unsigned int j;
unsigned char k;
for(j=0; j<ms; j++)
for (k=0; k<4; k++) Timer3us(250);
}
void LCD_pulse (void)
{
LCD_E=1;
Timer3us(40);
LCD_E=0;
}
void LCD_byte (unsigned char x)
{
// The accumulator in the C8051Fxxx is bit addressable!
ACC=x; //Send high nible
LCD_D7=ACC_7;
LCD_D6=ACC_6;
LCD_D5=ACC_5;
LCD_D4=ACC_4;
LCD_pulse();
Timer3us(40);
ACC=x; //Send low nible
LCD_D7=ACC_3;
LCD_D6=ACC_2;
LCD_D5=ACC_1;
LCD_D4=ACC_0;
LCD_pulse();
}
void WriteData (unsigned char x)
{
LCD_RS=1;
LCD_byte(x);
waitms(2);
}
void WriteCommand (unsigned char x)
{
LCD_RS=0;
LCD_byte(x);
waitms(5);
}
void LCD_4BIT (void)
{
LCD_E=0; // Resting state of LCD's enable is zero
// LCD_RW=0; // We are only writing to the LCD in this program
waitms(20);
// First make sure the LCD is in 8-bit mode and then change to 4-bit mode
WriteCommand(0x33);
WriteCommand(0x33);
WriteCommand(0x32); // Change to 4-bit mode
// Configure the LCD
WriteCommand(0x28);
WriteCommand(0x0c);
WriteCommand(0x01); // Clear screen command (takes some time)
waitms(20); // Wait for clear screen command to finsih.
}
void LCDprint(char * string, unsigned char line, bit clear)
{
int j;
WriteCommand(line==2?0xc0:0x80);
waitms(5);
for(j=0; string[j]!=0; j++) WriteData(string[j]);// Write the message
if(clear) for(; j<CHARS_PER_LINE; j++) WriteData(' '); // Clear the rest of the line
}
void LCDprint_inv(char * string, unsigned char line, bit clear)
{
int j;
int length=0;
WriteCommand(line==2?0xc0:0x80);
waitms(5);
for(j=0; string[j]!=0; j++) length++;
for(j=length-1; j>=0; j--) WriteData(string[j]);// Write the message
if(clear) for(; j<CHARS_PER_LINE; j++) WriteData(' '); // Clear the rest of the line
}
int getsn (char * buff, int len)
{
int j;
char c;
for(j=0; j<(len-1); j++)
{
c=getchar();
if ( (c=='\n') || (c=='\r') )
{
buff[j]=0;
return j;
}
else
{
buff[j]=c;
}
}
buff[j]=0;
return len;
}
void TIMER0_Init(void)
{
TMOD&=0b_1111_0000; // Set the bits of Timer/Counter 0 to zero
TMOD|=0b_0000_0001; // Timer/Counter 0 used as a 16-bit timer
TR0=0; // Stop Timer/Counter 0
}
void main (void)
{
// char buff[17];
float period=0.0;
float bpm=0.0;
float mem=0.0;
int unstable=0;
int bpm_int=0;
int count=0;
float sum=0.0;
char * bpm_string=NULL;
//Initialize Timer0 to be used to count the period
TIMER0_Init();
// Configure the LCD
LCD_4BIT();
// Display something in the LCD
// 1234567890123456
LCDprint("Place clip on ", 1, 1);
LCDprint("Ring fingernail", 2, 1);
// Wait for user to comply. Give putty a chance to start
waitms(1000);
printf("\x1b[2J"); // Clear screen using ANSI escape sequence.
/* printf ("EFM8 BPM measurement at pin P2.0 using Timer 0.\n"
"File: %s\n"
"Compiled: %s, %s\n\n",
__FILE__, __DATE__, __TIME__);
*/
while (1)
{
// Reset the counter
TL0=0;
TH0=0;
TF0=0;
overflow_count=0;
while(input!=0); // Wait for the signal to be zero
while(input!=1); // Wait for the signal to be one
TR0=1; // Start the timer
while(input!=0) // Wait for the signal to be zero
{
if(TF0==1) // Did the 16-bit timer overflow?
{
TF0=0;
overflow_count++;
}
}
while(input!=1) // Wait for the signal to be one
{
if(TF0==1) // Did the 16-bit timer overflow?
{
TF0=0;
overflow_count++;
}
}
TR0=0; // Stop timer 0, the 24-bit number [overflow_count-TH0-TL0] has the period!
period=(overflow_count*65536.0+TH0*256.0+TL0)*(12.0/SYSCLK);
// Send the period to the serial port
// printf( "\rT=%f ms ", period*1000.0);
bpm = 60.0/period;
if(!unstable)
{
if(bpm >= 40 && bpm <= 180)
{mem = bpm; unstable=1;}
else {printf("\rNot stable yet");
LCDprint("HR Not Stable", 2, 1); continue;}
}
//filters out errant HR measurements to smooth out signal.
if(bpm >= 50 && bpm <= 180 && ((bpm-mem) < 40 || (mem-bpm) < 40))
{
mem = bpm;
}
else bpm = mem;
// Send the period to the serial port
//Averages current and last bpm measurement
sum=bpm+mem;
bpm=sum/2;
bpm_int = (int) bpm;
printf( "%d\r\n" , bpm_int);
count = 0;
while(bpm_int)
{
bpm_string[count] = 48+bpm_int%10;
bpm_int /= 10;
count++;
}
bpm_string[count]='\0';
LCDprint("BPM:", 1, 1);
LCDprint_inv(bpm_string, 2, 1);
}
}