brightjoey
New Member
I'm using the 68HC11 microcontroller. Writing a heart rate monitor program
currently I am outputing the heart rate per minute (bpm) every 1 second. how do I average the datalog received over previous 5 seconds and print it?
I know it's using a array, somehow adding up the previous bpm and divide it. A guide would be helpful. Do I save 5 bpm every second, and then divide by 5 and print?
This is my current code.
currently I am outputing the heart rate per minute (bpm) every 1 second. how do I average the datalog received over previous 5 seconds and print it?
I know it's using a array, somehow adding up the previous bpm and divide it. A guide would be helpful. Do I save 5 bpm every second, and then divide by 5 and print?
This is my current code.
Code:
// Some further examples of setting timer unit
// No. 4 - Minimum code to count pulses ina 5 second period and display
// I.T. - 25/10/04
#include <stdio.h>
/* Heart Rate Monitor - Assignment 1 2001 I.T. 25/10/01 */
void timer(void);
void calc(void);/* Function Prototype for ISR */
unsigned char calcnow,count,enable; /* GLOBAL VARIABLES FOR RETURNING VALUES*/
unsigned int data,data1,secs,ticks,ticks1,ticks2,mins,hours,calcn,cycle,pulse,bpm,interval,rollovers,rollovers2,rollovers1;
float freq;
unsigned long int result;
unsigned char *padr,*paddr,*tflg2,*pactl,*tmsk2,*tflg1,*tctl2,*tmsk1;
unsigned int *tcnt,*tic1;
void main()
{
int log[2][100],avrate=0,eventno=0;
unsigned char selection=0,run=1;
calcnow=0;
padr=(unsigned char*)0x0;
paddr=(unsigned char*)0x1;
tctl2=(unsigned char*)0x21;
tflg1=(unsigned char*)0x23;
tmsk2=(unsigned char*)0x24;
tflg2=(unsigned char*)0x25;
pactl=(unsigned char*)0x26;
tcnt= (unsigned int*)0x0e;
tic1= (unsigned int*)0x10;
tmsk1=( unsigned char*)0x22;
*paddr=0x00; /*Port A all inputs*/
*pactl=0x03; /*Set prescaler to maximum*/
*tctl2=0xaa; /* One edge capture on all TICs*/
*tflg1=0x04; /* Clear TIC1 Flag*/
*tmsk2=0x40; /*Enable RTI interrupt*/
*tflg2=0x40;
*tmsk1=0x04;
for(;;)
{
/*
// printf("interval was %u Frequency is %5.0f \n\r",result,freq);
*/
if (calcnow!=0 && run ==1)
{
eventno++;
calcnow=0;
result=((rollovers1-rollovers2)-1)*65536+interval ;
freq=2.0e6/(float)result;
cycle=(rollovers1-rollovers2);
//pulse=(data-data1)+(ticks-ticks1)*65536;
bpm=freq*60;
printf("Interval was %u Frequency is: %4.2f Cycle:%i bpm=%3i\n\r ",interval,freq,cycle,bpm) ;
// printf("Data:%i Data1:%i ticks1:%i ticks:%i\n\n\r",data,data1,ticks1,ticks);
printf("Data:%i Data1:%i rollovers1:%i rollovers2:%i\n\n\r",data,data1,rollovers1,rollovers2);
count=1;
enable=0;
}
}
}
@interrupt void timer(void)
{
ticks++;
rollovers++;
if (ticks==30)
{
ticks=0;
secs++;
printf("%60Time: %2i:%2i:%2i\n\n",hours,mins,secs);
calcnow=1;
}
if (secs==60)
{
secs=0;
mins++;
}
if (mins==60)
{
mins=0;
hours++;
}
if (hours==24)
{
hours=0;
}
*tflg2=0x40;
}
@interrupt void calc(void)
{
/* Copy tic1 to data and data1 */
// printf("asdas");
// count++ ;
data1=data;
data=*tic1;
interval=(data-data1);
rollovers2=rollovers1;
rollovers1=rollovers;
Last edited: