Can someone explain me what this code is doing

Discussion in 'Programmer's Corner' started by kai hiwatari, Apr 24, 2015.

  1. kai hiwatari

    Thread Starter New Member

    Feb 20, 2015
    3
    0
    Its 8051 code for heart rate monitor. I found it here: http://www.zembedded.com/heart-rate...rocontroller-at89c51-based-heartbeat-monitor/
    I want someone to explain me it in normal language. Please help

    Code (Text):
    1. #include<at89x52.h> // plz ad the reg51 . h file
    2. #include<string.h> // plz ad the string . h file
    3. //heart beat monitor 8051 based
    4. #define lcdport P2 // chnage it for ur hardware
    5. sbit rw = P3^7; // LCD connection may be different
    6. sbit rs=P3^6; // LCD interface with microcontroller
    7. sbit en=P3^5; // Enable pin of LCD
    8. unsigned char sec,sec100;
    9. unsigned int bt,tick,r,bpm;
    10. void lcdinit();
    11. void lcdcmd(unsigned char);
    12. void lcddata(unsigned char);
    13. void send_string(unsigned char *s);
    14. void msdelay(unsigned int);
    15. void extrint (void) interrupt 0 // external Interrupt to detect the heart pulse
    16. {
    17. bt=tick; // number of ticks are picked
    18. tick=0; // reset for next counting
    19. }
    20. void timer0 (void) interrupt 1 using 1 // Timer 0 for one second time
    21. {
    22. TH0 = 0xdc; //The value is taken for Ssc/100 at crystal 11.0592MHz
    23. sec100++; // It is incremented every Ssc/100 at crystal 11.0592MHz
    24. tick++; // This variable counts the time period of incoming pulse in Sec/100
    25. if(tick>=3500){tick=0;} // tick are limited to less trhan 255 for valid calculation
    26. if(sec100 >=100) // 1 sec = sec100 * 100
    27. {
    28. sec++;
    29. sec100=0;
    30. }
    31. }
    32. void main()
    33. {
    34. P0=0xff;
    35. P1=0xff;
    36. P2=0xff;
    37. P3=0xff;
    38. rw=0;
    39. EA = 1;
    40. TMOD = 0x21;
    41. IT0 = 1;
    42. EX0 = 1;
    43. ET0 = 1;
    44. TR0 = 1;
    45. msdelay(1000);
    46. lcdinit();
    47. msdelay(1000);
    48. send_string("Heart beat ");
    49. msdelay(1500);
    50. msdelay(500);
    51. //delay(15000);
    52. bpm=0;bt=0;
    53. while(1)
    54. {
    55. if(sec >=1)
    56. {
    57. sec=0;
    58. /*
    59. The sampling time is fixed 1 sec.
    60. A variable "tick" is incremented with one tick per 100mSc in the timer 0 interrupt routine.
    61. Each on occurring of external interrupt the value in the "tick" is picked up
    62. and it is set to zero for recounting.
    63. The process continues till next external interrupt.
    64. Formula for calculating beats per minutes (microcontroller based heartbeat monitor ) is
    65. as tick is the time period in Sec/100. so extract the frequency of pulses at external interrupt
    66. Frequency = (1/tick)* 100 i.e pulses /sec
    67. Then
    68. bpm = frequency * 60 for one minutes i.e pulses per minute
    69. in short we can do it as
    70. bpm = 6000/ bt
    71. */
    72. lcdcmd(0x02);
    73. if(bt >=7){
    74. bpm = 6000/bt; // for valid output bt is limited so that it should be greater than 6
    75. msdelay(500);
    76. send_string("Pulse. ");
    77. lcddata((bpm/100)+0x30);
    78. r=bpm%100;
    79. lcddata((r/10)+0x30);
    80. lcddata((r%10)+0x30);
    81. send_string(" bpm ");
    82. }
    83. else {
    84. send_string("out of range");} // otherwise bpm will be shown zero, if limit does not fit for your project you can change it.
    85. }
    86. }
    87. }
    88. void lcdinit()
    89. {
    90. msdelay(100);
    91. lcdcmd(0x01);
    92. msdelay(500);
    93. lcdcmd(0x38);
    94. msdelay(500);
    95. lcdcmd(0x38);
    96. msdelay(500);
    97. lcdcmd(0x38);
    98. msdelay(500);
    99. lcdcmd(0x06);
    100. msdelay(500);
    101. lcdcmd(0x0c);
    102. msdelay(500);
    103. lcdcmd(0x03);
    104. msdelay(500);
    105. msdelay(500);
    106. }
    107. void lcdcmd(unsigned char value)
    108. {
    109. rs=0;
    110. lcdport=value;
    111. msdelay(100);
    112. en=1;
    113. msdelay(100);
    114. en=0;
    115. msdelay(100);
    116. rs=1;
    117. }
    118. void lcddata(unsigned char value)
    119. //heart beat monitoring system using microcontroller
    120. {
    121. rs=1;
    122. lcdport=value;
    123. msdelay(10);
    124. en=1;
    125. msdelay(100);
    126. en=0;
    127. rs=0;
    128. }
    129. void msdelay(unsigned int i)
    130. {
    131. //unsigned int i;
    132. while(i --);
    133. }
    134. void send_string(unsigned char *s)
    135. {
    136. unsigned char l,i;
    137. l = strlen(s); // get the length of string
    138. for(i=1;i <=l;i++)
    139. {
    140. lcddata(*s); // write every char one by one
    141. s++;
    142. }
    143. }
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    So you find a program off the Internet and you want to learn what it does or how it does what it is supposed to do.

    That is a terrible way to learn programming.

    A better way is to write code yourself. Start with something simple such as making an LED flash on and off.
     
    cmartinez likes this.
  3. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,790
    Timer 0 is counting basic time units of the processors oscillator frequency.
    The external interrupt causes the accumulated number of ticks to be evaluated and reset for the next interval.
    The main program interprets the number and formats it for the LCD display.

    Reading other people's code can be valuable as long as it is known to be "good code". Reading other people's bad code can be worst thing you could do. You will pick up their bad habits.
     
  4. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,790
    I haven't written any MCU code in C (all in assembly, to date), but there are things in this code that don't seem to make sense.

    The statements:

    Code (Text):
    1.  
    2. sbit rw = P3^7; // LCD connection may be different
    3. sbit rs=P3^6; // LCD interface with microcontroller
    4. sbit en=P3^5; // Enable pin of LCD
    5.  
    Presumably P3 is a constant declared in one of the header files. Okay.

    But then how does XORing P3 with 5, for instance, set the variable en to something related to the enable pin of the LCD?

    And en is just a variable and every place it is used it either has 0 or 1 written to it, so whatever P3^5 is intended to do is irrelevant because it is never used.

    This is C, right?
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,790
    It is not standard C. It is a C variant specifically for the bit addressable functions in the 8051 microcontroller.

    sbit is a decalration that declares a bit address. P3^7 refers to bit 7 of Port3. Similarly with the other two declarations. The code generator in the compiler will use bit manipulation instructions whenever the identifiers rw, rs, or en appear in an expression. This syntax predated the later variant that used a dot to separate the register name from the bit number. The IAR compiler for example would use P3.7 to indicate the same bit. The original C standard did not cover bit addressing except in terms of register operations on larger quantities.

    Example
    Code (Text):
    1.  
    2. #define MYBIT (1<<3)
    3. unsigned int flag ;
    4.     flag &= ~MYBIT  ;  //clear MYBIT in flag
    5.     flag |= MYBIT ;  //set MYBIT in flag
    6.  
     
    Last edited: Apr 24, 2015
  6. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,790
    Thanks.

    So it's a non-standard variant compiler that is overloading the assignment operator in both the variable declaration and expression evaluation?

    To the TS: You can see that it might be valuable to indicate what language and what compiler the code you post is associated with.
     
  7. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,790
    I think that is an accurate characterization of what is happening. I don't think any of the followup standards for C have tackled the bit addressing question either.
    C89 talks about bitfields as a variation on unsigned, but it is not quite the same thing as addressable bits in RAM or registers of a microcontroller. That is why C for those devices has to be an extended version.
     
    Last edited: Apr 24, 2015
Loading...