MIcroblaze Interrupt and timer (embedded processor)

Need some help on a project you are working on or got stuck on a tutorial? Post your questions here.
Post Reply
oneoneone123
Posts: 1
Joined: April 13th, 2015, 1:10 am

MIcroblaze Interrupt and timer (embedded processor)

Post by oneoneone123 » April 13th, 2015, 1:19 am

hai i follow the embedded processor tutorial,

i use the tutorial and edit the code to read data from a pulse sensor,

but somehow it did not work, will someone help please.,

here is my microblaze c code

--------------------------------------------------------------------------------------------------------------

Code: Select all

#include <stdio.h>
#include "platform.h"
#include "xparameters.h" // add
#include "xiomodule.h" // add
#include "math.h"

void print(char *str);
XIOModule gpi;
volatile u32 ct = 0;
u32 input = 0;



volatile u32 rate[10];                    // used to hold last ten IBI values
volatile u32 sampleCounter = 0;          // used to determine pulse timing
volatile u32 lastBeatTime = 0;           // used to find the inter beat interval
volatile u32 P =128;                      // used to find peak in pulse wave
volatile u32 T = 128;                     // used to find trough in pulse wave
volatile u32 thresh = 128;                // used to find instant moment of heart beat
volatile u32 amp = 100;                   // used to hold amplitude of pulse waveform
volatile u32 firstBeat = 1;        // used to seed rate array so we startup with reasonable BPM
volatile u32 secondBeat = 1;       // used to seed rate array so we startup with reasonable BPM
// these variables are volatile because they are used during the interrupt service routine!
volatile u32 BPM;                   // used to hold the pulse rate
volatile u32 Signal;                // holds the incoming raw data
volatile u32 IBI = 600;             // holds the time between beats, the Inter-Beat Interval
volatile u32 Pulse = 0;     // true when pulse wave is high, 0 when it's low
volatile u32 QS = 0;        // becomes true when Arduoino finds a beat.
volatile u32 i;

void timerTick(void* ref) {

  XIOModule_Disable(&gpi, XIN_IOMODULE_FIT_1_INTERRUPT_INTR);
  ct+=2; // increase ct every millisecond
  Signal = XIOModule_DiscreteRead(&gpi, 1);
  sampleCounter = ct;
  u32 N = sampleCounter - lastBeatTime;

  if(Signal<thresh && N>(IBI/5)*3){

	  if(Signal<T){

		  T=Signal;
	  }
  }

  if(Signal>thresh && Signal > P){

	  P = Signal;
  }

if (N > 250){                                   // avoid high frequency noise
  if ( (Signal > thresh) && (Pulse == 0) && (N > (IBI/5)*3)){
    Pulse = 1;                               // set the Pulse flag when we think there is a pulse
    IBI = sampleCounter - lastBeatTime;         // measure time between beats in mS
    lastBeatTime = sampleCounter;               // keep track of time for next pulse

         if(firstBeat){                         // if it's the first time we found a beat, if firstBeat == 1
             firstBeat = 0;                 // clear firstBeat flag
             return;                            // IBI value is unreliable so discard it
            }
         if(secondBeat){                        // if this is the second beat, if secondBeat == 1
            secondBeat = 0;                 // clear secondBeat flag
               for(i=0; i<=9; i++){         // seed the running total to get a realisitic BPM at startup
                    rate[i] = IBI;
                    }
            }

    // keep a running total of the last 10 IBI values
   u32 runningTotal = 0;                   // clear the runningTotal variable

    for(i=0; i<=8; i++){                // shift data in the rate array
          rate[i] = rate[i+1];              // and drop the oldest IBI value
          runningTotal += rate[i];          // add up the 9 oldest IBI values
        }

    rate[9] = IBI;                          // add the latest IBI to the rate array
    runningTotal += rate[9];                // add the latest IBI to runningTotal
    runningTotal /= 10;                     // average the last 10 IBI values
    BPM = 60000/runningTotal;               // how many beats can fit into a minute? that's BPM!
    QS = 1;                              // set Quantified Self flag
    // QS FLAG IS NOT CLEARED INSIDE THIS ISR


    }
}

if (Signal < thresh && Pulse == 1){     // when the values are going down, the beat is over

      Pulse = 0;                         // reset the Pulse flag so we can do it again
      amp = P - T;                           // get amplitude of the pulse wave
      thresh = (amp/2) + T;                    // set thresh at 50% of the amplitude
      P = thresh;                            // reset these for next time
      T = thresh;
     }

  if (N > 2500){                             // if 2.5 seconds go by without a beat
      thresh = 128;                          // set thresh default
      P = 128;                               // set P default
      T = 128;                               // set T default
      lastBeatTime = sampleCounter;          // bring the lastBeatTime up to date
      firstBeat = 1;                      // set these to avoid noise
      secondBeat = 1;                     // when we get the heartbeat back
     }

 XIOModule_Enable(&gpi, XIN_IOMODULE_FIT_1_INTERRUPT_INTR); // enable the interrupt

}

//void sendData(char symbol, int data){
//
//	xil_printf("%c",symbol);
//	xil_printf("%d\n\r",data);
//
//}
int main() {

 init_platform();

  XIOModule_Initialize(&gpi, XPAR_IOMODULE_0_DEVICE_ID); // Initialize the GPi module
  XIOModule_Start(&gpi); // start the GPO module

  microblaze_register_handler(XIOModule_DeviceInterruptHandler,
                              XPAR_IOMODULE_0_DEVICE_ID); // register the interrupt handler



  XIOModule_Connect(&gpi, XIN_IOMODULE_FIT_1_INTERRUPT_INTR, timerTick,
                    NULL); // register timerTick() as interrupt handler
  XIOModule_Enable(&gpi, XIN_IOMODULE_FIT_1_INTERRUPT_INTR); // enable the interrupt

  microblaze_enable_interrupts(); // enable global interrupts


while(1){

	if(QS==1){

		xil_printf("%d",BPM);
		QS=0;
	}

}

cleanup_platform();
return 0;

}

Post Reply