protegemed, aquisição via A/D simples utilizando interrupção do timer

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Fork of ptgm_semDMA by Marcelo Rebonatto

Files at this revision

API Documentation at this revision

Comitter:
viniciushl
Date:
Wed Jan 13 18:53:25 2016 +0000
Parent:
0:fac116e94d44
Commit message:
Alterada captura para aquisi??o simples do A/D utilizando interrup??o do timer;

Changed in this revision

Codes/Capture.cpp Show annotated file Show diff for this revision Revisions of this file
Headers/Capture.h Show annotated file Show diff for this revision Revisions of this file
Headers/Settings.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Codes/Capture.cpp	Tue Jan 05 11:47:35 2016 +0000
+++ b/Codes/Capture.cpp	Wed Jan 13 18:53:25 2016 +0000
@@ -5,22 +5,36 @@
  *      Author: 
  */
 #include "Capture.h"
+#include "Settings.h"
+
 
 //Semaphore Capture::m_CaptureSemaphore(1); //used to alert the capture thread about a ready capture 
 //int Capture::m_BufferIndex;
 
 //__attribute((section("AHBSRAM1"),aligned)) dmaLinkedListNode Capture::m_Nodes[2];// __attribute__((section("AHBSRAM0"))); //this list holds the buffer configuration for the DMA peripheral
 __attribute((section("AHBSRAM1"),aligned)) short int Capture::m_AdcBuffers[NUMBER_OF_CHANNELS][NUMBER_OF_SAMPLES];// __attribute__((section("AHBSRAM0")));
+bool Capture::flag_capture = 0;
+int Capture::cnt_buffer=0;
+int Capture::channel_number=0;
+int Capture::cnt_interrupt=0;
+
+
 Serial rfid_serial(p28,p27);
-
+/*
 AnalogIn AdcChannel0(CHANNEL0);
 AnalogIn AdcChannel1(CHANNEL1);
 AnalogIn AdcChannel2(CHANNEL2);
 AnalogIn AdcChannel3(CHANNEL3);
 AnalogIn AdcChannel4(CHANNEL4);
 AnalogIn AdcChannel5(CHANNEL5);
+*/
+//ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]);
 
-//ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]);
+Ticker timer_interrupt;
+
+Timer timer1;
+
+DigitalOut led1(LED1);
 
 short int Capture::GetValue(int nsamples, int nchannel)
 {
@@ -43,24 +57,61 @@
     */
 }
 
-
-void Capture::AcquireValues(){
-    int i;
+void Capture::InitializeAD()
+{
+    
+    init_adc(200000);                   // Inicializa o AD com a frequencia de amostragem de 200 kHz
+    LPC_ADC->ADCR &= ~ADC_MODE_BURST;   // Desabilito o modo burst de captura para utilizar apenas a captura simples
+    NVIC_DisableIRQ(TIMER3_IRQn);   // Desabilita a interrupção do Timer3
+    
+}
+    
+    
+void Capture::AcquireValues()
+{
+    //timer1.start();
+    NVIC_DisableIRQ(TIMER3_IRQn);   // Desabilita a interrupção do Timer3
+    timer_interrupt.attach_us(&Capture::FcnTimerInt,11);    // Seta a interrupção do timer3 para cada 11us apontando para a função FcnTimerInt
+    cnt_buffer = 0;
+    cnt_interrupt = 0;
+    flag_capture = 0;   
+    channel_number = 0;
+    LPC_ADC->ADCR |= ADC_START_NOW; // Habilita a captura do A/D -> Captura Simples
+    //printf("\r\nPASSOU do init \n"); 
+     
+    select_channels(1<<channel_number); // 
+     
+    
     
-    for(i=0; i < NUMBER_OF_SAMPLES; i++){
-         m_AdcBuffers[0][i] = ADC_CONVERT(AdcChannel0.read_u16());
-         //wait_us(10);
-         m_AdcBuffers[1][i] = ADC_CONVERT(AdcChannel1.read_u16());
-         //wait_us(10);
-         m_AdcBuffers[2][i] = ADC_CONVERT(AdcChannel2.read_u16());
-         //wait_us(10);
-         m_AdcBuffers[3][i] = ADC_CONVERT(AdcChannel3.read_u16());
-         //wait_us(10);
-         m_AdcBuffers[4][i] = ADC_CONVERT(AdcChannel4.read_u16());
-         //wait_us(10);
-         m_AdcBuffers[5][i] = ADC_CONVERT(AdcChannel5.read_u16());
-         wait_us(13);
-    }    
+    NVIC_EnableIRQ(TIMER3_IRQn); // Habilita a interrupção do timer 3
+    for(volatile int i=0; i<100000; i++); // Gasta tempo
+
+    while(!flag_capture);
+
+    
+    /*for (int i=0;i<NUMBER_OF_SAMPLES;i++)
+    {
+        for (int j=0;j<6;j++)
+        {
+             m_AdcBuffers[j][i] -= get_Offset(j);
+             m_AdcBuffers[j][i] /= get_Gain(j);
+        }
+    }*/
+
+    /*for (int i=0;i<NUMBER_OF_SAMPLES;i++)
+    {
+        printf("\r\n%d", i);
+        
+        for (int j=0;j<6;j++)
+        {
+            float val = 0;
+            val = (float)m_AdcBuffers[j][i] - (float)Settings::get_Offset(j);
+            val /= (float)Settings::get_Gain(j);
+            //printf("\t%f", m_AdcBuffers[j][i]);
+            printf("\t%2.2f", val);
+        }
+    }*/
+    //printf("\n");
 }
 
 //void Capture::Initialize(){     //AnalogIn adcChannel0; }
@@ -99,4 +150,55 @@
     for(int i=0;i<9;i++)
         rfid[i] = ans[i];
     
-}
\ No newline at end of file
+}
+
+extern "C" void Capture::FcnTimerInt(void)
+{
+    if (cnt_interrupt < 120)    // Descarta as primeiras leituras para estabilizar as leituras do A/D
+    {
+        while(!(LPC_ADC->ADGDR>>31));
+        m_AdcBuffers[channel_number][0] = ((LPC_ADC->ADGDR>>4)&0xfff);
+        if (channel_number < 5)
+            {
+                channel_number++;
+            }
+            else
+            {
+                channel_number = 0;
+            }
+        cnt_buffer = 0;   
+        cnt_interrupt++;
+        select_channels(1<<channel_number);
+        LPC_ADC->ADCR |= ADC_START_NOW;
+        
+        
+    }
+    else    //Armazena as leituras no buffer dos canais
+    {
+        while(!(LPC_ADC->ADGDR>>31));  // Aguarda o bit de DONE ser setado no registrador ADGDR sinalizando o final de uma captura
+        
+        if (cnt_buffer < NUMBER_OF_SAMPLES)
+        {
+            m_AdcBuffers[channel_number][cnt_buffer] =((LPC_ADC->ADGDR>>4)&0xfff);
+            if (channel_number < 5)
+            {
+                channel_number++;
+            }
+            else
+            {
+                channel_number = 0;
+                cnt_buffer++;
+            }   
+        }
+        else
+        {
+            
+            flag_capture = 1;
+            NVIC_DisableIRQ(TIMER3_IRQn);
+            //timer_interrupt.detach();
+        }
+        select_channels(1<<channel_number);
+        LPC_ADC->ADCR |= ADC_START_NOW;
+   }
+}
+
--- a/Headers/Capture.h	Tue Jan 05 11:47:35 2016 +0000
+++ b/Headers/Capture.h	Wed Jan 13 18:53:25 2016 +0000
@@ -33,15 +33,20 @@
     
     //static Semaphore m_CaptureSemaphore; //used to alert the capture thread about a ready capture 
     //static int m_BufferIndex;
+    static bool flag_capture;
+    static int cnt_buffer;
+    static int channel_number;  
+    static int cnt_interrupt;
 
-    
+
     //static dmaLinkedListNode m_Nodes[2]; //this list holds the buffer configuration for the DMA peripheral
 public:
-    static short int m_AdcBuffers[NUMBER_OF_CHANNELS][NUMBER_OF_SAMPLES];    
+    static short int m_AdcBuffers[NUMBER_OF_CHANNELS][NUMBER_OF_SAMPLES];
+  
     // matrix m_AdcBuffers[6][128]
     
 public:
-
+    
     static short int GetValue(int nsamples, int nchannel);
     static void      PutValue(int nsamples, int nchannel, short int value);
     static void      CopyBuffer(int channel, short int *dest);    
@@ -51,11 +56,16 @@
 
     //static void Initialize();
     static void AcquireValues();
+    static void InitializeAD();
+    
     //static void Start();
     //static void Stop();
     //static void Wait();   
     
     static void ReadRFID(int channel,char *rfid);
+    
+    static void FcnTimerInt(void);
+    //static void AdInitialize();
 
 };
 
--- a/Headers/Settings.h	Tue Jan 05 11:47:35 2016 +0000
+++ b/Headers/Settings.h	Wed Jan 13 18:53:25 2016 +0000
@@ -23,7 +23,7 @@
 #define NUMBER_OF_CHANNELS  6U
 #define NUMBER_OF_OUTLETS   3U
 #define NUMBER_OF_HARMONICS 12U
-#define NUMBER_OF_SAMPLES   128U
+#define NUMBER_OF_SAMPLES   256U
 #define LENGTH_MAIL_BOX     10U
 #define MAXTRIES            10U
 
--- a/main.cpp	Tue Jan 05 11:47:35 2016 +0000
+++ b/main.cpp	Wed Jan 13 18:53:25 2016 +0000
@@ -4,7 +4,7 @@
     
 */
 
-#define MSGWINDOW "Version 4"
+#define MSGWINDOW "Version 5"
 
 #include <stdio.h>
 
@@ -51,10 +51,12 @@
 }
 
 int main() {
+    
+    
     float inicio, fim, maior, menor, dif;
     float inicioc, fimc, maiorc, menorc, difc;
     Timer t,tc;
-        
+    /* 
     if ((LPC_WDT->WDMOD >> 2) & 1){
         //myled4 = 1; 
         PmedLog::WriteEntry(PMEDLOG_INITIALIZINGWDT);
@@ -62,19 +64,22 @@
     else{        
         //myled3 = 1;
         PmedLog::WriteEntry(PMEDLOG_INITIALIZING);
-    }
+    }*/
 
     printf("\r\n %s \r\n", MSGWINDOW);
+    
     FILE *f;
     
-    Settings::ReadFile();    
-    
+    Settings::ReadFile();
+    printf("\r\n\nCANAL\tOFFSET\t\tGAIN");
+
     InitializeEthernetLink();
     printf("Inicializou link Ethernet\n");
     
     //Start HTTP POST service
     Thread http_post(HttpPost::HttpPost_Thread);        
-           
+    
+        
     //Start Telnet Service
     //Thread telnetserver(TelnetServer::TelnetServer_Thread);
     
@@ -93,25 +98,26 @@
     printf(PMEDLOG_INITIALIZINGWDT);
     printf("\n\n");
             
-    PmedLog::WriteEntry(PMEDLOG_STARTED);
+    //PmedLog::WriteEntry(PMEDLOG_STARTED);
     
-    /* start test WahtchDog */
-    DigitalOut led1(LED1);
-    int i, n = 0;
+    /* start test WatchDog */
+
+    int n = 0;
     //int tatual, tnovo;
     float rms[NUMBER_OF_CHANNELS], mv2[NUMBER_OF_CHANNELS];
     int under[NUMBER_OF_CHANNELS], over[NUMBER_OF_CHANNELS];
     
-    wdt.kick(10.0);  
+    //wdt.kick(10.0);  
     
-    //Capture::Initialize();
-       
+    Capture::InitializeAD();
+    int cont1 = 0; 
+    
     while(1)    
     {
-        tranca.wait();
+        /*tranca.wait();
         t.start();
         inicio = t.read();
-                
+          */      
         //get sample values from 6 channels
         Capture::AcquireValues();
         
@@ -127,13 +133,29 @@
             menor = dif;         
         t.reset();
         t.stop();           
-        tranca.release();
+        //tranca.release();
         
-        tranca.wait();
+        //tranca.wait();
         tc.start();
         inicioc = tc.read();
         // Calcula o RMS dos 6 canais
         SignalProcessor::CalculateRMSBulk(rms, mv2, under, over);
+        /*
+        printf("\r\n\nCANAL\tOFFSET\t\tGAIN");
+        for (int i=0;i<6;i++)
+        {
+            printf("\r\n%d\t%d\t\t%4.2f", i, Settings::get_Offset(i), Settings::get_Gain(i));
+        }
+        
+        
+        printf("\r\n\n\tRMS\t\tMV2\t\tUNDER\t\tOVER");
+        for (int i=0;i<6;i++)
+        {
+            printf("\r\n%d\t%4.2f\t\t%4.2f\t\t%d\t\t%d", i, rms[i], mv2[i], under[i], over[i]);
+        }
+        printf("\n");
+        */
+        
         fimc = tc.read();
         difc = fimc-inicioc;
         
@@ -145,7 +167,7 @@
         if (difc < menorc)
             menorc = difc;                    
                 
-        for(i=0;i<6;i++){
+        for(int i=0;i<6;i++){
             EventDetector::get_Detector(i).ProcessEvent(rms[i], mv2[i], under[i], over[i]);
         }
 
@@ -153,7 +175,7 @@
         if(n % 60 == 0)
         {
             printf("%.2f %.0f %.2f %.0f\t%.2f %.0f %.2f %.0f\t%.2f %.0f %.2f %.0f\n",rms[0], mv2[0],rms[1],mv2[1],rms[2],mv2[2],rms[3],mv2[3],rms[4],mv2[4],rms[5],mv2[5]);
-            led1 = !led1;            
+            //led1 = !led1;            
          //   n=0;    
                                     
             PmedLog::Mark();            
@@ -164,10 +186,10 @@
         }
         
         // End of main loop so "kick" to reset watchdog timer and avoid a reset
-        wdt.kick();        
+        //wdt.kick();        
         tc.reset();
         tc.stop();
-        tranca.release();
+        //tranca.release();
     }    
        
     while(1){//never reaches here