Heart Rate Project Help

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Heart Rate Project Help

910 Views
Chubz
Contributor I

I'm currently working on a heart rate device and I would like to connect it to the HCS12 board and used the input capture function. The main thing I need the HCS12 board to due is to be able to count how many beats per minute and store that data as a variable and also display it on a computer screen. I'm not a really good programmer and fairly new to using the HCS12 board, I just wanted to know if someone can give me examples or tutorials on the input capture function. Anything will help.

Labels (1)
Tags (1)
0 Kudos
4 Replies

537 Views
bigmac
Specialist III

Hello, and welcome to the forum.

 

Using the forum search engine, I found the following threads.

https://community.freescale.com/message/62776#62776

http://forums.freescale.com/t5/CodeWarrior-for-8-and-16-bit/Project-code-advice-tips/m-p/51888

These actually refer to HCS08 devices and use of the TPM module, but should be workable on the HCS12, with some code adjustment for the different timer module.  Maybe you will be able to glean a few ideas?

 

How do you intend to communicate with the host computer?

 

Regards,

Mac

 

0 Kudos

537 Views
Chubz45
Contributor I

Thanks MAC for the information. Im no to sure on how I'm going to display it on the computer due to this being a group project and one of my other group members is going to send data from the HCS12 to the computer wrieless. All he told me to due is to calculate the Beats Per MInute (bpm) and store that data as a variable and from there he will grap the variable and send it. So on that note I'm completly lost from the inforamtion that you gave me. I am getting errors popping up saying C2801: ' ; ' missing. Here is a copy of my code on the bottom please help me in anyway you can. Thanks

 

 

#include <hidef.h>              /* common defines and macros */
#include "derivative.h"         /* derivative-specific definitions */
#include "main_asm.h"           /* interface to the assembly module */
#include <mc9s12dg256.h>        /* derivative information */
#include "TERMIO.h"
#include <stdio.h>


#define OFFTIME 3000
#define NUMREADINGS 4

int readings[NUMREADINGS];
int index = 0;
int total = 0;
int average = 0;
int period;

void init_timerIC(void);
void init(void)


/************main**************/

void main()

{

  init_timerIC( );                    // initializations for timer input capture
    TSCR1 = 0x90;                        //  enable timer counter, enable fast flag clear
    TIOS &= ~I0S0;                      //  enable input-capture 0
    TCTL4 = 0x01;                     //  Capture rising edge of the PT0 pin
    TFLG1 = 0x01;                     //  clear the COF flag
    while(!(TFLG1 & COF));              //  wait for the arrival of the first rising edge
   
    for (int i = 0; i++)
    {
        readings[i] = 0;                 // set readings to 0
    }
}

// Calculate BPM
void bpm(void)
{
    int frequence;

    frequence = (60000 / period);    //calculate bpm

    if ((frequence >= 40) && (frequence <= 220)) // limit for bpm
    {   
        total -= readings[index];    // subtract the last reading
        readings[index] = frequence;
        total += readings[index];    // add the reading to the total
        index = (index + 1);    // advance to the next index
   
        if (index >= NUMREADINGS)    // if at the end of the array
        { 
          index = 0;    // go back to beginning of array
          average = total / NUMREADINGS;    // calculate average bpm
        }
    }
   
    // Set boundries for period
    if (period > OFFTIME)
    {
        average = 0;
        index = 0;
    }
}


    EnableInterrupts;

 asm_main(); /* call the assembly function */


  for(;:smileywink: {
    _FEED_COP(); /* feeds the dog */
  } /* loop forever */
  /* please make sure that you never leave main */
}

0 Kudos

537 Views
CrasyCat
Specialist III

Hello

 

There are a bunch of basic programming errors in this code snippet.

May be you need a refresher course in ANSI C programming.

 

First issue:

  The Prototype for function init must be ended with a ";" character. It should look like

   void init(void); 

 

Second issue:

  The for instruction in function main is not correct.

   You cannot define  the loop counter i inside of the for loop.

  So you need to define i as a local variable at the beginning of the main function.

  Then the for should probably look as follows:

    for ( i = 0; i < NUMREADINGS; i++)

 

Third issue:

  You ended the function bmp right after

    if (period > OFFTIME)
    {
        average = 0;
        index = 0;
    }

  That means the code afterward is not located in any function. Which is invalid in ANSI C.

 

I stopped checking the source code at that point.....

 

CrasyCat

0 Kudos

537 Views
Chubz45
Contributor I

Thanks for your help. I re-did my code and now I have encountered a new problem. I would like to implement in my code to allow the input capture function to only work for 10 seconds and then times the number of captures by 6 so i can get a person beats per minute. Also I need this top be a continous loop.

 

#program LINK_INFO DERIVATIVE "mc9s12dg256b"

#include <hidef.h>                      /* common defines and macros */
#include <mc9s12dg256.h>                /* derivative information */
#include "Terminal.h"
#include "termio.h"
#include <stdio.h>

unsigned int event1, event2, event3, event4, period = 1;

void init_timerIC(void);
int get_period(void);
void init_PLL(void);  

/************main**************/
void main(){
   int c=0;
   init_PLL( );                         // added - initialize PLL for 24 MHz CPU clock
   EnableInterrupts;

   TERMIO_Init();
   SCI0BDL = 156;                       // added to slow down baudrate to 9600 for hardware

    DDRT &= ~0x01;                      // PT0 inputs (input capture ch. 0)

    init_timerIC( );                      // initializations for timer input capture
    c = get_period( );                    // measure waveform period, store to memory
   // printf("period = %d\n\r",c);        // display period to monitor as decimal     
     
    for(;:smileywink: {}                          /* never ends  */
}

void init_timerIC( ){
     TSCR1 = 0x90;                        // timer counter on, FFCA='1'(auto flag clear on TC0 readout)
     TIOS &= 0xFE;                          // Ch. 0 configured for input capture
     TSCR2 = 0x00;                        // no overflow interrupt, prescale = 1 (24 MHz main timer)
     TCTL4 = 0x01;                          // Ch. 0 captures on rising edge
     TFLG1 |= 0x01;                       // clear C0F flag by writing a '1' to it
}                          

int get_period( ){
     while((TFLG1 & 0x01) == 0){          // waiting for CF0='1' (edge capture)
     }
     event1 = TC0;                          // read out arrival time of 1st edge
     while((TFLG1 & 0x01) == 0){          // waiting for 2nd edge
     }
     event2 = TC0;                          // read out arrive time of 2nd edge
     while((TFLG1 & 0x01) ==0){           // waiting for 3rd edge
     }
     event3 = TC0;                        // read out arrival time of 3rd edge
     while((TFLG1 & 0x01) ==0){           // waiting for 4th edge
     }
     event4 - TC0;                        // read out 4th edge
     period = (event2 - event1) - (event3 - event4);            // find time between 2 edges
     return period;
}   

void init_PLL( ){                       // initializes phase-locked loop for 24MHz CPU clock
   asm(sei);                            // for running board standalone w/out Codewarrior
   CLKSEL &= ~0x80;
   PLLCTL |= 0x40;
   SYNR = 0x05;
   REFDV = 0x01;
   while((CRGFLG & 0x08) == 0){         // wait here for lock
      CRGFLG |= 0x08;
   }
   CLKSEL |= 0x80;
   asm(cli);
}

/******** end of file *****************/

0 Kudos