Bluetooth heart rate monitor

Discussion in 'The Projects Forum' started by johnjameson2011, Feb 15, 2014.

  1. johnjameson2011

    Thread Starter New Member

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

    Code ( (Unknown Language)):
    1.  
    2. sbit SENSOR = P1^0; //sensor connected to  pin
    3. unsigned int beatms;
    4. float bpm;
    5. char buf[20];
    6.  
    7. void main()
    8. {
    9. // -=-=- Intialize variables -=-=-=
    10. lcdInit();
    11. // -=-=- Welcome LCD Message -=-=-=
    12. lcdClear();
    13. lcdGotoXY(0,0); // 1st Line of LCD
    14. // "xxxxxxxxxxxxxxxx"
    15. lcdPrint("DIGITAL HEART");
    16. lcdGotoXY(0,1); // 2nd Line of LCD
    17. // "xxxxxxxxxxxxxxxx"
    18. lcdPrint("BEAT MONITOR");
    19. beatms=0; // will store duration between two pulses
    20. // -=-=- Program Loop -=-=-=
    21. while(1)
    22. {
    23. while(SENSOR==0);// wait for high pulse from sensor
    24. DelayNmS(10); // 10ms delay so that it does not listen to any noise
    25. beatms = 10; // start counting beatms from 10ms since we have delay after pulse
    26. while(SENSOR==1)// wait until signal is high
    27. {
    28. DelayNmS(1); //wait 1msec
    29. beatms++; //keep incrementing counter each 1ms
    30. }
    31. while(SENSOR==0) //keep looping till signal goes back high, wait for next
    32. {
    33. DelayNmS(1); //wait 1msec
    34. beatms++; //keep incrementing counter each 1ms
    35. }
    36. // beatms variable will now have time in ms between two high edge pulse
    37. lcdClear();
    38. lcdGotoXY(0,0);
    39. lcdPrint("HEART RATE : ");
    40. bpm = (float)60000/beatms;
    41. if(bpm > 200)
    42. {
    43. lcdGotoXY(0,1);
    44. sprintf (buf, "Processing......"); // Invalid, Wait for next cycle
    45. lcdPrint(buf);
    46. } else {
    47. lcdGotoXY(0,1);
    48. sprintf (buf, "%0.0f BPM", bpm); // Display reading in BPM
    49. lcdPrint(buf);
    50. }
    51. }
    52. }
    53.  
    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
     
  2. johnjameson2011

    Thread Starter New Member

    Feb 17, 2011
    12
    0
    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
     
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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: Feb 19, 2014
  4. johnjameson2011

    Thread Starter New Member

    Feb 17, 2011
    12
    0
    I'm not sure to be honest
    I mean its a sample code from a heart beat sensor datasheet
    http://www.sunrom.com/media/files/p/186/1157-datasheet.pdf

    I mean it looked familiar to stuff I've seen before,data comes in pin 1,lcd intialise's,program loop where calculation is done,output depending on high pulse at pin 1 or not
     
  5. johnjameson2011

    Thread Starter New Member

    Feb 17, 2011
    12
    0
    Decided to try a different than the above,something thats a lot easier to understand
    Code ( (Unknown Language)):
    1. #include reg51.h
    2. //#include
    3. void _nop_(void);
    4. #define delay_us _nop_(); //generates 1 microsecond
    5. #define LCD P3
    6. sbit RS=P2^0; //connect p0.0 to rs pin of lcd
    7. sbit EN=P2^2; //connect p0.1 to en pin of lcd
    8. sbit RW=P2^1;
    9. sbit PULSE=P0^0; //pulse input from heart beat sensor
    10. //sbit LED=P1^0; //led indicator for pulse indication
    11. void integer_lcd(int);
    12. void init_lcd(void);
    13. void cmd_lcd(unsigned char);
    14. void write_lcd(unsigned char);
    15. void delay_ms(unsigned int);
    16. unsigned int num=0;
    17. unsigned int num1=0;
    18. unsigned int dig_1,dig_2,dig_3,dig_4,test=0;
    19. unsigned int dig_11,dig_21,dig_31,dig_41,test1=0,test2=0;
    20. unsigned char k=0;
    21. void main(void)
    22. {
    23. init_lcd();
    24. //LED=0;
    25. write_lcd('H');
    26. write_lcd('E');
    27. write_lcd('A');
    28. write_lcd('R');
    29. write_lcd('T');
    30. write_lcd(' ');
    31. write_lcd('B');
    32. write_lcd('E');
    33. write_lcd('A');
    34. write_lcd('T');
    35. write_lcd(' ');
    36. write_lcd(' ');
    37. write_lcd(' ');
    38. write_lcd(' ');
    39. write_lcd(' ');
    40. write_lcd(' ');
    41. cmd_lcd(0xc0);
    42. write_lcd(' ');
    43. write_lcd(' ');
    44. write_lcd(' ');
    45. write_lcd(' ');
    46. write_lcd(' ');
    47. write_lcd(' ');
    48. write_lcd(' ');
    49. write_lcd(' ');
    50. write_lcd('C');
    51. write_lcd('O');
    52. write_lcd('U');
    53. write_lcd('N');
    54. write_lcd('T');
    55. write_lcd('E');
    56. write_lcd('R');
    57. delay_ms(300);
    58. cmd_lcd(0x01);
    59. write_lcd('B');
    60. write_lcd('e');
    61. write_lcd('a');
    62. write_lcd('t');
    63. write_lcd('s');
    64. write_lcd(':');
    65. write_lcd(48);
    66. write_lcd(48);
    67. write_lcd(48);
    68. write_lcd(48);
    69.     while(1)
    70.     {
    71.     if(PULSE==1) //check for input pulse from sensor
    72.  {
    73. cmd_lcd(0x01);
    74. test++;
    75. num=test;
    76. dig_4=num%10;
    77. num=num/10;
    78. dig_3=num%10;
    79. num=num/10;
    80. dig_2=num%10;
    81. dig_1=num/10;
    82.      if(test==9999)
    83. test=0;
    84. write_lcd('B');
    85. write_lcd('e');
    86. write_lcd('a');
    87. write_lcd('t');
    88. write_lcd('s');
    89. write_lcd(':');
    90. write_lcd(dig_1+48);
    91. write_lcd(dig_2+48);
    92. write_lcd(dig_3+48);
    93. write_lcd(dig_4+48);
    94. //LED=1;
    95. delay_ms(50);
    96. //LED=0;
    97.  }
    98.     }
    99.     }
    100. ////////////////////////////////////////////////////////////////
    101. void init_lcd(void)
    102. {
    103. delay_ms(10); //delay 10 milliseconds
    104. cmd_lcd(0x38); //8 bit initialize, 5x7 character font, 16x2 display
    105. cmd_lcd(0x0e); //lcd on, cursor on
    106. cmd_lcd(0x06); //right shift cursor automatically after each character is displayed
    107. cmd_lcd(0x01); //clear lcd
    108. }
    109. //transmit command or instruction to lcd
    110. void cmd_lcd(unsigned char c)
    111. {
    112. EN=1;
    113. RW=0;//set enable pin
    114. RS=0; //clear register select pin
    115. LCD=c; //load 8 bit data
    116. EN=0; //clear enable pin
    117. delay_ms(2); //delay 2 milliseconds
    118. }
    119. //transmit a character to be displayed on lcd
    120. void write_lcd(unsigned char c)
    121. {
    122. EN=1; //set enable pin
    123. RW=0;
    124. RS=1; //set register select pin
    125. LCD=c; //load 8 bit data
    126. EN=0; //clear enable pin
    127. delay_ms(2); //delay 2 milliseconds
    128. }
    129. //generates delay in milli seconds
    130. void delay_ms(unsigned int i)
    131. {
    132. unsigned int j;
    133.     while(i-->0)
    134.     {
    135.      for(j=0;j<500;j++)
    136.      {
    137.      ;
    138.      }
    139.     }
    140. }
    141.  
    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.
     
  6. johnjameson2011

    Thread Starter New Member

    Feb 17, 2011
    12
    0
    So went back to square one,bought a heartbeat sensor,its one for an arduino but I hope its compatible with the 8051 too.
    [​IMG]
    Using a different code taken from this datasheet
    http://www.sunrom.com/media/files/p/202/1181-datasheet.pdf

    Code ( (Unknown Language)):
    1. // Compiler: Keil, Target Chip AT89S52 or similar
    2. sbit SENSOR = P3^7; //sensor is connected to this pin, can be any other pin also
    3. unsigned int beatms; //Calculate time between two high going pulses in ms
    4. float bpm; // Beats per minute calculated from beatms variable above
    5. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    6. Delay x Milisecond
    7. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
    8. void delay_ms(unsigned int x) // delays x msec (at fosc=11.0592MHz)
    9. {
    10.  unsigned char j=0;
    11. while(x-- > 0)
    12.  {
    13.  for (j=0; j<125; j++){;}
    14.  }
    15. }
    16. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    17. // -=-=-=-=- Main Program -=-=-=-=-=-=-=
    18. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    19. void main()
    20. {
    21. // -=-=- Intialize variables -=-=-=
    22. beatms=0; // will store duration between two pulses
    23.  
    24. // -=-=- Program Loop -=-=-=
    25. while(1)
    26. {
    27. while(SENSOR==0);// wait for high pulse from sensor
    28. delay_ms(10); // 10ms delay so that it does not listen to any noise
    29. beatms = 10; // start counting beatms from 10ms since we have delay above
    30.  
    31. while(SENSOR==1)// wait until signal is high
    32. {
    33. delay_ms(1); //wait 1msec
    34. beatms++; //keep incrementing counter each 1ms
    35. }
    36.  
    37. while(SENSOR==0) //keep looping till signal goes back high, wait for next
    38. {
    39. delay_ms(1); //wait 1msec
    40. beatms++; //keep incrementing counter each 1ms
    41. }
    42. // beatms variable will now have time in ms between two high edge pulse
    43. bpm = (float)60000/beatms; // see document of #1181 for this calculation
    44.  
    45. if(bpm > 200)
    46. {
    47. // Invalid, Wait for next cycle
    48. } else {
    49. // Display reading in BPM, print variable BPM to LCD Display or Serial port.
    50. }
    51. }
    52. }

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