// 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.
}
}
}