Test2

Dependencies:   MAX30003 max32630fthr DS1307

Revision:
17:4bf0775c1b38
Parent:
16:8fc65c8fa0e8
Child:
18:462e396f9e5a
--- a/main.cpp	Tue May 18 23:43:34 2021 +0000
+++ b/main.cpp	Thu May 20 01:40:51 2021 +0000
@@ -1,8 +1,10 @@
 
-#include "MAX30003.h"
+
 #include "mbed.h"
 #include "max32630fthr.h"
 #include "ds1307.h"
+#include "MAX30003.h"
+
 //#include <BufferedSerial.h>
 #include <string>
 //#include <Serial.h>
@@ -18,6 +20,10 @@
 DigitalOut ledA(LED2);
 
 void ecg_config(MAX30003 &ecgAFE);
+
+void ecg_config_off(MAX30003& ecgAFE);
+
+
 //BufferedSerial pc(P3_1,P3_0);            // Use USB debug probe for serial link static Unbuffered
 BufferedSerial pc(P0_1,P0_0);
 
@@ -26,7 +32,7 @@
 //Serial uart_1(USBTX, USBRX);            // Use USB debug probe for serial link static Unbuffered
 //
 //rial pc(P0_1,P0_0);
-volatile bool ecgFIFOIntFlag = 0; 
+
 volatile bool timerflag = 0;
 
 FileHandle *mbed::mbed_override_console(int fd)
@@ -34,11 +40,18 @@
     return &pc;
 }
 
-void ecgFIFO_callback_1()  { // Triggered when the ECG FIFO is about to be full
-    
+volatile bool ecgFIFOIntFlag = 0; 
+void ecgFIFO_callback()  
+{
     ecgFIFOIntFlag = 1;
 }  
 
+volatile bool ecgFIFOIntFlag_1 = 0; 
+void ecgFIFO_callback_1()  
+{
+    ecgFIFOIntFlag_1 = 1;
+} 
+
 time_t asUnixTime(int year, int mon, int mday, int hour, int min, int sec) {
     struct tm   t;
     t.tm_year = year - 1900;
@@ -51,6 +64,7 @@
  
     return mktime(&t);          // returns seconds elapsed since January 1, 1970 (begin of the Epoch)
 }
+
 int main(void)
 {   
    // Constants
@@ -61,8 +75,7 @@
     const int ETAG_BITS_MASK = 0x7;
     
     timer_fast.start();
-    DigitalOut rLed(LED2, LED_ON);
-    
+    DigitalOut rLed(LED2, LED_ON);    
 //    pc.baud(9600);
     
     //pc.set_baud(115200);                    // Baud rate = 115200
@@ -72,8 +85,13 @@
     //    /* stop bit */ 1 //1
     //);
     //uart_1.baud(115200);  
+    
+//----------------------------------------------------------------------------------------------//
+    
     InterruptIn ecgFIFO_int(P5_4);          // Config P5_4 as int. in for the
-    ecgFIFO_int.fall(&ecgFIFO_callback_1);    // ecg FIFO interrupt at falling edge
+    ecgFIFO_int.fall(&ecgFIFO_callback);    // ecg FIFO interrupt at falling edge
+    
+    DigitalOut bLed(LED3, LED_ON);
     
     SPI spiBus(SPI2_MOSI, SPI2_MISO, SPI2_SCK);     // SPI bus, P5_1 = MOSI, 
                                                     // P5_2 = MISO, P5_0 = SCK
@@ -86,7 +104,7 @@
     ecgAFE.writeRegister( MAX30003::SYNCH , 0);
     
     uint32_t ecgFIFO, readECGSamples, idx, ETAG[32], status;
-    int16_t ecgSample[32];
+    int16_t ecgSample[32]; int8_t x;
 //-----------------------------------------------------------------------------------------------// 
     
     //bool timerflag = false;
@@ -137,7 +155,7 @@
       {
         if (pc.readable())  // For nordic - Readiing time
         {     
-        scanf("%s",buf);
+      scanf("%s",buf);
      //printf("The entered string is %s : \n ",buf);
      
      packet_1 = atoi(buf);  // converting ascii to integer
@@ -206,7 +224,8 @@
       else
             Channel_Num = 0;      
       //pc.write((uint8_t *)Channel_Num, sizeof(Channel_Num));     
-      printf("%6d\r\n", Channel_Num);                                                    //Channel no
+      //printf("%6d\r\n", Channel_Num);              
+      printf("channel No is  %d \n",Channel_Num);                                                                               //Channel no
       
       onesec_counter_temp = onesec_counter * 2;
       data_len_1 = onesec_counter_temp & 0xff;
@@ -241,44 +260,35 @@
     //  timer_fast.reset();
        } 
        
-
-       // Read back ECG samples from the FIFO        
-       else if((ecgFIFOIntFlag==1))// && (timerflag == 0)) 
-        {
+       do {  
+        // Read back ECG samples from the FIFO 
+        if( ecgFIFOIntFlag) {
+            
             ecg_1 = 1;
             ecgFIFOIntFlag = 0; 
             status = ecgAFE.readRegister( MAX30003::STATUS );      // Read the STATUS register
              
             // Check if EINT interrupt asserted
-            if ( ( status & EINT_STATUS_MASK ) == EINT_STATUS_MASK ) 
-            {     
+            if ( ( status & EINT_STATUS_MASK ) == EINT_STATUS_MASK ) {     
             
-                readECGSamples = 0;                        // Reset sample counter
+                readECGSamples = 0; // Reset sample counter
                 
                 do {
-                    ecgFIFO = ecgAFE.readRegister( MAX30003::ECG_FIFO );       // Read FIFO
+                    ecgFIFO = ecgAFE.readRegister( MAX30003::ECG_FIFO );       // Read FIFO                                            
                     ecgSample[readECGSamples] = ecgFIFO >> 8;                  // Isolate voltage data
-                    ecgSample[readECGSamples] = ((ecgSample[readECGSamples]<<8)&0xFF00)|((ecgSample[readECGSamples]>>8)&0x00FF);
                     ETAG[readECGSamples] = ( ecgFIFO >> 3 ) & ETAG_BITS_MASK;  // Isolate ETAG
-                    readECGSamples++;                                          // Increment sample counter
+                    readECGSamples++;
+                                                             // Increment sample counter
                 
                 // Check that sample is not last sample in FIFO                                              
                 } while ( ETAG[readECGSamples-1] == FIFO_VALID_SAMPLE_MASK || 
                           ETAG[readECGSamples-1] == FIFO_FAST_SAMPLE_MASK ); 
                 
                 // Check if FIFO has overflowed
-                if( ETAG[readECGSamples - 1] == FIFO_OVF_MASK )
-                {                  
+                if( ETAG[readECGSamples - 1] == FIFO_OVF_MASK ){                  
                     ecgAFE.writeRegister( MAX30003::FIFO_RST , 0); // Reset FIFO
-                    rLed = 1;//notifies the user that an over flow occured
+                    bLed = 1;//notifies the user that an over flow occured
                 }
-                //uint8_t header_ecg_checksum[2] = {'%','%'};
-                //pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum));
-                
-                //pc.write((int16_t *)ecgSample,readECGSamples * sizeof(int16_t ));
-                //memcpy(ecgSample_1sec , ecgSample, sizeof(ecgSample));
-                
-                //memcpy(ecgSample_1sec + (onesec_counter * sizeof(int16_t)), ecgSample, sizeof(ecgSample));
                 
                 if (flip)
                 {
@@ -289,23 +299,114 @@
                  {
                      sampleps = 13;
                     // printf("Flipped 13\r\n");     
-                  }
+                  }                
                 
-                 for( idx = 0; idx < sampleps; idx++ ) 
+                // Print results 
+                for( idx = 0; idx < readECGSamples; idx++ ) 
                 {
-                    //pc.printf("%6d\r\n", ecgSample[idx]);     
+                   // pc.printf("%6d\r\n", ecgSample[idx]); 
+                   ecgSample_1sec[onesec_counter] = ecgSample[idx];
+                    
+                    cksm_1 = ecgSample[idx] & 0xff;
+                    cksm_2 = (ecgSample[idx] >> 8) & 0xff;
+                    checksum_ += cksm_1 + cksm_2;
+                    onesec_counter++;          
+                } 
+                      //rLed = 1;    
+                        flip =!flip;   
+                        bLed = ! bLed;
+                        x++;      
+                        //if (x==100)
+                        //ecg_config_off(ecgAFE);                       
+             }
+        }
+    }while(x<50);
+    ecg_config_off(ecgAFE); 
+    bLed = ~bLed; 
+    x=0;
+    
+//--------------------------------------channal 2-----------------------------------------------//     
+    InterruptIn ecgFIFO_2_int(P1_5);          // Config P5_4 as int. in for the
+    ecgFIFO_2_int.fall(&ecgFIFO_callback_1);    // ecg FIFO almost full interrupt
+
+    DigitalOut gLed(LED2, LED_ON);
+    
+    SPI spiBus2(P0_5,P0_6,P0_4);     // SPI bus, P5_1 = MOSI, 
+                                                    // P5_2 = MISO, P5_0 = SCK
+    
+    MAX30003 ecgAFE_2(spiBus2, P0_7);          // New MAX30003 on spiBus, CS = P5_3 
+    ecg_config(ecgAFE_2);                    // Config ECG 
+     
+   // uint32_t ecgFIFO, readECGSamples, idx, ETAG[32], status;
+   //int16_t ecgSample[32], x = 0;
+    
+    ecgAFE_2.writeRegister( MAX30003::SYNCH , 0);
+   
+        do {        
+        // Read back ECG samples from the FIFO 
+        if( ecgFIFOIntFlag_1) {
+            
+            ecg_2 = 1;
+            ecgFIFOIntFlag_1 = 0; 
+            status = ecgAFE_2.readRegister( MAX30003::STATUS );      // Read the STATUS register
+             
+            // Check if EINT interrupt asserted
+            if ( ( status & EINT_STATUS_MASK ) == EINT_STATUS_MASK ) {     
+            
+                readECGSamples = 0; // Reset sample counter
+                
+                do {
+                    ecgFIFO = ecgAFE_2.readRegister( MAX30003::ECG_FIFO );       // Read FIFO                                             
+                    ecgSample[readECGSamples] = ecgFIFO >> 8;                  // Isolate voltage data
+                    ETAG[readECGSamples] = ( ecgFIFO >> 3 ) & ETAG_BITS_MASK;  // Isolate ETAG
+                    readECGSamples++;
+                                                             // Increment sample counter
+                
+                // Check that sample is not last sample in FIFO                                              
+                } while ( ETAG[readECGSamples-1] == FIFO_VALID_SAMPLE_MASK || 
+                          ETAG[readECGSamples-1] == FIFO_FAST_SAMPLE_MASK ); 
+                
+                // Check if FIFO has overflowed
+                if( ETAG[readECGSamples - 1] == FIFO_OVF_MASK ){                  
+                    ecgAFE_2.writeRegister( MAX30003::FIFO_RST , 0); // Reset FIFO
+                    gLed = 1;//notifies the user that an over flow occured
+                }
+                
+                                if (flip)
+                {
+                 sampleps = 12;
+                 //printf("Flipped 12\r\n");     
+                 }
+                 else
+                 {
+                     sampleps = 13;
+                    // printf("Flipped 13\r\n");     
+                  }                
+                
+                
+                // Print results 
+                for( idx = 0; idx < readECGSamples; idx++ )
+                 {
+                    //pc.printf("%6d\r\n", ecgSample[idx]);  
                     ecgSample_1sec[onesec_counter] = ecgSample[idx];
                     
                     cksm_1 = ecgSample[idx] & 0xff;
                     cksm_2 = (ecgSample[idx] >> 8) & 0xff;
                     checksum_ += cksm_1 + cksm_2;
-                    onesec_counter++;      
-                } 
-                  flip =!flip; 
-                  rLed = ! rLed;           
-            }
-           
+                    onesec_counter++; 
+                             
+                }       
+                        gLed = ! gLed;
+                        x++;      
+                        //if (x==100)
+                        //ecg_config_off(ecgAFE);                              
+             }
         }
+    }while(x<50);
+    ecg_config_off(ecgAFE_2); 
+    gLed = ~gLed;
+    x=0; 
+        
     }
 }
 
@@ -374,3 +475,16 @@
     return;
 }  
 
+
+void ecg_config_off(MAX30003& ecgAFE)
+{
+    // General config register setting
+    MAX30003::GeneralConfiguration_u CNFG_GEN_r;
+    CNFG_GEN_r.bits.en_ecg = 0;     // Enable ECG channel
+   // CNFG_GEN_r.bits.rbiasn = 1;     // Enable resistive bias on negative input
+   // CNFG_GEN_r.bits.rbiasp = 1;     // Enable resistive bias on positive input
+   // CNFG_GEN_r.bits.en_rbias = 1;   // Enable resistive bias
+   // CNFG_GEN_r.bits.imag = 2;       // Current magnitude = 10nA
+   // CNFG_GEN_r.bits.en_dcloff = 1;  // Enable DC lead-off detection   
+    ecgAFE.writeRegister( MAX30003::CNFG_GEN , CNFG_GEN_r.all);
+}