Bluetooth heart rate monitor

Thread Starter

johnjameson2011

Joined Feb 17, 2011
12
Heys Guys,
Been flicking through the net looking at various heart rate circuits,for the detecting side this program doesn't seem too bad

Rich (BB code):
sbit SENSOR = P1^0; //sensor connected to  pin
unsigned int beatms;
float bpm;
char buf[20];
 
void main()
{
// -=-=- Intialize variables -=-=-=
lcdInit();
// -=-=- Welcome LCD Message -=-=-=
lcdClear();
lcdGotoXY(0,0); // 1st Line of LCD
// "xxxxxxxxxxxxxxxx"
lcdPrint("DIGITAL HEART");
lcdGotoXY(0,1); // 2nd Line of LCD
// "xxxxxxxxxxxxxxxx"
lcdPrint("BEAT MONITOR");
beatms=0; // will store duration between two pulses
// -=-=- Program Loop -=-=-=
while(1)
{
while(SENSOR==0);// wait for high pulse from sensor
DelayNmS(10); // 10ms delay so that it does not listen to any noise
beatms = 10; // start counting beatms from 10ms since we have delay after pulse
while(SENSOR==1)// wait until signal is high
{
DelayNmS(1); //wait 1msec
beatms++; //keep incrementing counter each 1ms
}
while(SENSOR==0) //keep looping till signal goes back high, wait for next
{
DelayNmS(1); //wait 1msec
beatms++; //keep incrementing counter each 1ms
}
// beatms variable will now have time in ms between two high edge pulse
lcdClear();
lcdGotoXY(0,0);
lcdPrint("HEART RATE : ");
bpm = (float)60000/beatms; 
if(bpm > 200)
{
lcdGotoXY(0,1);
sprintf (buf, "Processing......"); // Invalid, Wait for next cycle
lcdPrint(buf);
} else {
lcdGotoXY(0,1);
sprintf (buf, "%0.0f BPM", bpm); // Display reading in BPM
lcdPrint(buf);
}
}
}
Its been years since I did any sort of programming so when I went and had a go at building it in Keil I got this error, error C202: 'P1': undefined identifier.
I know its probably something painfully obvious
 

Thread Starter

johnjameson2011

Joined Feb 17, 2011
12
Actually just to add to this I thought it was the header file I was leaving out but when i added #include<reg51.h> I got this
sensor.c(15): warning C206: 'lcdInit': missing function-prototype
sensor.c(17): warning C206: 'lcdClear': missing function-prototype
sensor.c(18): warning C206: 'lcdGotoXY': missing function-prototype
sensor.c(18): error C267: 'lcdGotoXY': requires ANSI-style prototype

There's probably a couple of things I'm doing wrong?
Like should I specifing the folder in which the header file is contained? i.e #include<atmel/reg51.h>
Or should I be adding the header file to the source group?

Although I did do a copy of the reg51 header file and added to the folder where the sensor code was saved and tried building it using
#include "reg51.h" but got the same error
 

tshuck

Joined Oct 18, 2012
3,534
As far as the new errors go, you need a function prototype - something that tells the compiler what the function looks like.

If you use #include to include the definition for the functions, then you should verify your function spellings.

Are you using the same compiler as the source of your code? If not, you'll have to rectify the differences between the two.
 
Last edited:

Thread Starter

johnjameson2011

Joined Feb 17, 2011
12
Decided to try a different than the above,something thats a lot easier to understand
Rich (BB code):
#include reg51.h
//#include
void _nop_(void);
#define delay_us _nop_(); //generates 1 microsecond
#define LCD P3
sbit RS=P2^0; //connect p0.0 to rs pin of lcd
sbit EN=P2^2; //connect p0.1 to en pin of lcd
sbit RW=P2^1;
sbit PULSE=P0^0; //pulse input from heart beat sensor
//sbit LED=P1^0; //led indicator for pulse indication
void integer_lcd(int);
void init_lcd(void);
void cmd_lcd(unsigned char);
void write_lcd(unsigned char);
void delay_ms(unsigned int);
unsigned int num=0;
unsigned int num1=0;
unsigned int dig_1,dig_2,dig_3,dig_4,test=0;
unsigned int dig_11,dig_21,dig_31,dig_41,test1=0,test2=0;
unsigned char k=0;
void main(void)
{
init_lcd();
//LED=0;
write_lcd('H');
write_lcd('E');
write_lcd('A');
write_lcd('R');
write_lcd('T');
write_lcd(' ');
write_lcd('B');
write_lcd('E');
write_lcd('A');
write_lcd('T');
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
cmd_lcd(0xc0);
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
write_lcd(' ');
write_lcd('C');
write_lcd('O');
write_lcd('U');
write_lcd('N');
write_lcd('T');
write_lcd('E');
write_lcd('R');
delay_ms(300);
cmd_lcd(0x01);
write_lcd('B');
write_lcd('e');
write_lcd('a');
write_lcd('t');
write_lcd('s');
write_lcd(':');
write_lcd(48);
write_lcd(48);
write_lcd(48);
write_lcd(48);
	while(1)
	{
	if(PULSE==1) //check for input pulse from sensor
 {
cmd_lcd(0x01);
test++;
num=test;
dig_4=num%10;
num=num/10;
dig_3=num%10;
num=num/10;
dig_2=num%10;
dig_1=num/10;
	 if(test==9999)
test=0;
write_lcd('B');
write_lcd('e');
write_lcd('a');
write_lcd('t');
write_lcd('s');
write_lcd(':');
write_lcd(dig_1+48);
write_lcd(dig_2+48);
write_lcd(dig_3+48);
write_lcd(dig_4+48);
//LED=1;
delay_ms(50);
//LED=0;
 }
	}
	}
////////////////////////////////////////////////////////////////
void init_lcd(void)
{
delay_ms(10); //delay 10 milliseconds
cmd_lcd(0x38); //8 bit initialize, 5x7 character font, 16x2 display
cmd_lcd(0x0e); //lcd on, cursor on
cmd_lcd(0x06); //right shift cursor automatically after each character is displayed
cmd_lcd(0x01); //clear lcd
}
//transmit command or instruction to lcd
void cmd_lcd(unsigned char c)
{
EN=1;
RW=0;//set enable pin
RS=0; //clear register select pin
LCD=c; //load 8 bit data
EN=0; //clear enable pin
delay_ms(2); //delay 2 milliseconds
}
//transmit a character to be displayed on lcd
void write_lcd(unsigned char c)
{
EN=1; //set enable pin
RW=0;
RS=1; //set register select pin
LCD=c; //load 8 bit data
EN=0; //clear enable pin
delay_ms(2); //delay 2 milliseconds
}
//generates delay in milli seconds
void delay_ms(unsigned int i)
{
unsigned int j;
	while(i-->0)
	{
	 for(j=0;j<500;j++)
	 {
	 ;
	 }
	}
}
I think this only works as a pulse counter so to calculate the beats per minute a time reference is needed I think.
When I load it onto my chip,the welcome message comes up on the lcd and then everytime I put my finger on the lrd the no of beats increment by 1 on the lcd screen.
 

Thread Starter

johnjameson2011

Joined Feb 17, 2011
12
So went back to square one,bought a heartbeat sensor,its one for an arduino but I hope its compatible with the 8051 too.

Using a different code taken from this datasheet
http://www.sunrom.com/media/files/p/202/1181-datasheet.pdf

Rich (BB code):
// Compiler: Keil, Target Chip AT89S52 or similar 
sbit SENSOR = P3^7; //sensor is connected to this pin, can be any other pin also 
unsigned int beatms; //Calculate time between two high going pulses in ms 
float bpm; // Beats per minute calculated from beatms variable above 
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 
Delay x Milisecond 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/ 
void delay_ms(unsigned int x) // delays x msec (at fosc=11.0592MHz) 
{ 
 unsigned char j=0; 
while(x-- > 0) 
 { 
 for (j=0; j<125; j++){;} 
 } 
} 
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 
// -=-=-=-=- Main Program -=-=-=-=-=-=-= 
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 
void main() 
{ 
// -=-=- Intialize variables -=-=-= 
beatms=0; // will store duration between two pulses 
 
// -=-=- Program Loop -=-=-= 
while(1) 
{ 
while(SENSOR==0);// wait for high pulse from sensor 
delay_ms(10); // 10ms delay so that it does not listen to any noise 
beatms = 10; // start counting beatms from 10ms since we have delay above 
 
while(SENSOR==1)// wait until signal is high 
{ 
delay_ms(1); //wait 1msec 
beatms++; //keep incrementing counter each 1ms 
} 
 
while(SENSOR==0) //keep looping till signal goes back high, wait for next 
{ 
delay_ms(1); //wait 1msec 
beatms++; //keep incrementing counter each 1ms 
} 
// beatms variable will now have time in ms between two high edge pulse 
bpm = (float)60000/beatms; // see document of #1181 for this calculation 
 
if(bpm > 200) 
{ 
// Invalid, Wait for next cycle 
} else { 
// Display reading in BPM, print variable BPM to LCD Display or Serial port. 
} 
} 
}

So all thats needed is to add a lcd code to view the results I think
 
Top