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

Committer:
viniciushl
Date:
Wed Jan 13 18:53:25 2016 +0000
Revision:
1:8129536051df
Parent:
0:fac116e94d44
Alterada captura para aquisi??o simples do A/D utilizando interrup??o do timer;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rebonatto 0:fac116e94d44 1 /*
rebonatto 0:fac116e94d44 2 * Capture.h
rebonatto 0:fac116e94d44 3 *
rebonatto 0:fac116e94d44 4 * Created on:
rebonatto 0:fac116e94d44 5 * Author:
rebonatto 0:fac116e94d44 6 */
rebonatto 0:fac116e94d44 7 #include "Capture.h"
viniciushl 1:8129536051df 8 #include "Settings.h"
viniciushl 1:8129536051df 9
rebonatto 0:fac116e94d44 10
rebonatto 0:fac116e94d44 11 //Semaphore Capture::m_CaptureSemaphore(1); //used to alert the capture thread about a ready capture
rebonatto 0:fac116e94d44 12 //int Capture::m_BufferIndex;
rebonatto 0:fac116e94d44 13
rebonatto 0:fac116e94d44 14 //__attribute((section("AHBSRAM1"),aligned)) dmaLinkedListNode Capture::m_Nodes[2];// __attribute__((section("AHBSRAM0"))); //this list holds the buffer configuration for the DMA peripheral
rebonatto 0:fac116e94d44 15 __attribute((section("AHBSRAM1"),aligned)) short int Capture::m_AdcBuffers[NUMBER_OF_CHANNELS][NUMBER_OF_SAMPLES];// __attribute__((section("AHBSRAM0")));
viniciushl 1:8129536051df 16 bool Capture::flag_capture = 0;
viniciushl 1:8129536051df 17 int Capture::cnt_buffer=0;
viniciushl 1:8129536051df 18 int Capture::channel_number=0;
viniciushl 1:8129536051df 19 int Capture::cnt_interrupt=0;
viniciushl 1:8129536051df 20
viniciushl 1:8129536051df 21
rebonatto 0:fac116e94d44 22 Serial rfid_serial(p28,p27);
viniciushl 1:8129536051df 23 /*
rebonatto 0:fac116e94d44 24 AnalogIn AdcChannel0(CHANNEL0);
rebonatto 0:fac116e94d44 25 AnalogIn AdcChannel1(CHANNEL1);
rebonatto 0:fac116e94d44 26 AnalogIn AdcChannel2(CHANNEL2);
rebonatto 0:fac116e94d44 27 AnalogIn AdcChannel3(CHANNEL3);
rebonatto 0:fac116e94d44 28 AnalogIn AdcChannel4(CHANNEL4);
rebonatto 0:fac116e94d44 29 AnalogIn AdcChannel5(CHANNEL5);
viniciushl 1:8129536051df 30 */
viniciushl 1:8129536051df 31 //ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]);
rebonatto 0:fac116e94d44 32
viniciushl 1:8129536051df 33 Ticker timer_interrupt;
viniciushl 1:8129536051df 34
viniciushl 1:8129536051df 35 Timer timer1;
viniciushl 1:8129536051df 36
viniciushl 1:8129536051df 37 DigitalOut led1(LED1);
rebonatto 0:fac116e94d44 38
rebonatto 0:fac116e94d44 39 short int Capture::GetValue(int nsamples, int nchannel)
rebonatto 0:fac116e94d44 40 {
rebonatto 0:fac116e94d44 41 return m_AdcBuffers[nchannel][nsamples];
rebonatto 0:fac116e94d44 42 }
rebonatto 0:fac116e94d44 43
rebonatto 0:fac116e94d44 44 void Capture::PutValue(int nsamples, int nchannel, short int value)
rebonatto 0:fac116e94d44 45 {
rebonatto 0:fac116e94d44 46 m_AdcBuffers[nchannel][nsamples] = value;
rebonatto 0:fac116e94d44 47 }
rebonatto 0:fac116e94d44 48
rebonatto 0:fac116e94d44 49 void Capture::CopyBuffer(int channel, short int *dest)
rebonatto 0:fac116e94d44 50 {
rebonatto 0:fac116e94d44 51 memcpy(dest, &m_AdcBuffers[channel][0], sizeof(short int) * NUMBER_OF_SAMPLES);
rebonatto 0:fac116e94d44 52 /*
rebonatto 0:fac116e94d44 53 for(int i=0;i<NUMBER_OF_SAMPLES;i++)
rebonatto 0:fac116e94d44 54 {
rebonatto 0:fac116e94d44 55 dest[i] = GetValue(i,channel);
rebonatto 0:fac116e94d44 56 }
rebonatto 0:fac116e94d44 57 */
rebonatto 0:fac116e94d44 58 }
rebonatto 0:fac116e94d44 59
viniciushl 1:8129536051df 60 void Capture::InitializeAD()
viniciushl 1:8129536051df 61 {
viniciushl 1:8129536051df 62
viniciushl 1:8129536051df 63 init_adc(200000); // Inicializa o AD com a frequencia de amostragem de 200 kHz
viniciushl 1:8129536051df 64 LPC_ADC->ADCR &= ~ADC_MODE_BURST; // Desabilito o modo burst de captura para utilizar apenas a captura simples
viniciushl 1:8129536051df 65 NVIC_DisableIRQ(TIMER3_IRQn); // Desabilita a interrupção do Timer3
viniciushl 1:8129536051df 66
viniciushl 1:8129536051df 67 }
viniciushl 1:8129536051df 68
viniciushl 1:8129536051df 69
viniciushl 1:8129536051df 70 void Capture::AcquireValues()
viniciushl 1:8129536051df 71 {
viniciushl 1:8129536051df 72 //timer1.start();
viniciushl 1:8129536051df 73 NVIC_DisableIRQ(TIMER3_IRQn); // Desabilita a interrupção do Timer3
viniciushl 1:8129536051df 74 timer_interrupt.attach_us(&Capture::FcnTimerInt,11); // Seta a interrupção do timer3 para cada 11us apontando para a função FcnTimerInt
viniciushl 1:8129536051df 75 cnt_buffer = 0;
viniciushl 1:8129536051df 76 cnt_interrupt = 0;
viniciushl 1:8129536051df 77 flag_capture = 0;
viniciushl 1:8129536051df 78 channel_number = 0;
viniciushl 1:8129536051df 79 LPC_ADC->ADCR |= ADC_START_NOW; // Habilita a captura do A/D -> Captura Simples
viniciushl 1:8129536051df 80 //printf("\r\nPASSOU do init \n");
viniciushl 1:8129536051df 81
viniciushl 1:8129536051df 82 select_channels(1<<channel_number); //
viniciushl 1:8129536051df 83
viniciushl 1:8129536051df 84
rebonatto 0:fac116e94d44 85
viniciushl 1:8129536051df 86 NVIC_EnableIRQ(TIMER3_IRQn); // Habilita a interrupção do timer 3
viniciushl 1:8129536051df 87 for(volatile int i=0; i<100000; i++); // Gasta tempo
viniciushl 1:8129536051df 88
viniciushl 1:8129536051df 89 while(!flag_capture);
viniciushl 1:8129536051df 90
viniciushl 1:8129536051df 91
viniciushl 1:8129536051df 92 /*for (int i=0;i<NUMBER_OF_SAMPLES;i++)
viniciushl 1:8129536051df 93 {
viniciushl 1:8129536051df 94 for (int j=0;j<6;j++)
viniciushl 1:8129536051df 95 {
viniciushl 1:8129536051df 96 m_AdcBuffers[j][i] -= get_Offset(j);
viniciushl 1:8129536051df 97 m_AdcBuffers[j][i] /= get_Gain(j);
viniciushl 1:8129536051df 98 }
viniciushl 1:8129536051df 99 }*/
viniciushl 1:8129536051df 100
viniciushl 1:8129536051df 101 /*for (int i=0;i<NUMBER_OF_SAMPLES;i++)
viniciushl 1:8129536051df 102 {
viniciushl 1:8129536051df 103 printf("\r\n%d", i);
viniciushl 1:8129536051df 104
viniciushl 1:8129536051df 105 for (int j=0;j<6;j++)
viniciushl 1:8129536051df 106 {
viniciushl 1:8129536051df 107 float val = 0;
viniciushl 1:8129536051df 108 val = (float)m_AdcBuffers[j][i] - (float)Settings::get_Offset(j);
viniciushl 1:8129536051df 109 val /= (float)Settings::get_Gain(j);
viniciushl 1:8129536051df 110 //printf("\t%f", m_AdcBuffers[j][i]);
viniciushl 1:8129536051df 111 printf("\t%2.2f", val);
viniciushl 1:8129536051df 112 }
viniciushl 1:8129536051df 113 }*/
viniciushl 1:8129536051df 114 //printf("\n");
rebonatto 0:fac116e94d44 115 }
rebonatto 0:fac116e94d44 116
rebonatto 0:fac116e94d44 117 //void Capture::Initialize(){ //AnalogIn adcChannel0; }
rebonatto 0:fac116e94d44 118
rebonatto 0:fac116e94d44 119
rebonatto 0:fac116e94d44 120 void Capture::ReadRFID(int channel,char *rfid)
rebonatto 0:fac116e94d44 121 {
rebonatto 0:fac116e94d44 122
rebonatto 0:fac116e94d44 123 char cmd[4];
rebonatto 0:fac116e94d44 124 cmd[0] = 'S';
rebonatto 0:fac116e94d44 125 cmd[1] = '0'+channel;
rebonatto 0:fac116e94d44 126 cmd[2] = '\n';
rebonatto 0:fac116e94d44 127 cmd[3] = '\0';
rebonatto 0:fac116e94d44 128
rebonatto 0:fac116e94d44 129 //send
rebonatto 0:fac116e94d44 130 rfid_serial.puts(cmd);
rebonatto 0:fac116e94d44 131
rebonatto 0:fac116e94d44 132 //receive
rebonatto 0:fac116e94d44 133 char ch=0;
rebonatto 0:fac116e94d44 134 char ans[10];
rebonatto 0:fac116e94d44 135 int cnt=0;
rebonatto 0:fac116e94d44 136 int tmout=1000;
rebonatto 0:fac116e94d44 137 while(ch != '\n' && tmout-- && cnt<9)
rebonatto 0:fac116e94d44 138 {
rebonatto 0:fac116e94d44 139 if(rfid_serial.readable())
rebonatto 0:fac116e94d44 140 {
rebonatto 0:fac116e94d44 141 ch = rfid_serial.getc();
rebonatto 0:fac116e94d44 142 if(!((ch>='0' && ch<='9') || (ch >= 'A' && ch <= 'F')))ch='0';
rebonatto 0:fac116e94d44 143 ans[cnt++] = ch;
rebonatto 0:fac116e94d44 144 }
rebonatto 0:fac116e94d44 145 else
rebonatto 0:fac116e94d44 146 wait_ms(1);
rebonatto 0:fac116e94d44 147
rebonatto 0:fac116e94d44 148 }
rebonatto 0:fac116e94d44 149 ans[cnt-1] = '\0';
rebonatto 0:fac116e94d44 150 for(int i=0;i<9;i++)
rebonatto 0:fac116e94d44 151 rfid[i] = ans[i];
rebonatto 0:fac116e94d44 152
viniciushl 1:8129536051df 153 }
viniciushl 1:8129536051df 154
viniciushl 1:8129536051df 155 extern "C" void Capture::FcnTimerInt(void)
viniciushl 1:8129536051df 156 {
viniciushl 1:8129536051df 157 if (cnt_interrupt < 120) // Descarta as primeiras leituras para estabilizar as leituras do A/D
viniciushl 1:8129536051df 158 {
viniciushl 1:8129536051df 159 while(!(LPC_ADC->ADGDR>>31));
viniciushl 1:8129536051df 160 m_AdcBuffers[channel_number][0] = ((LPC_ADC->ADGDR>>4)&0xfff);
viniciushl 1:8129536051df 161 if (channel_number < 5)
viniciushl 1:8129536051df 162 {
viniciushl 1:8129536051df 163 channel_number++;
viniciushl 1:8129536051df 164 }
viniciushl 1:8129536051df 165 else
viniciushl 1:8129536051df 166 {
viniciushl 1:8129536051df 167 channel_number = 0;
viniciushl 1:8129536051df 168 }
viniciushl 1:8129536051df 169 cnt_buffer = 0;
viniciushl 1:8129536051df 170 cnt_interrupt++;
viniciushl 1:8129536051df 171 select_channels(1<<channel_number);
viniciushl 1:8129536051df 172 LPC_ADC->ADCR |= ADC_START_NOW;
viniciushl 1:8129536051df 173
viniciushl 1:8129536051df 174
viniciushl 1:8129536051df 175 }
viniciushl 1:8129536051df 176 else //Armazena as leituras no buffer dos canais
viniciushl 1:8129536051df 177 {
viniciushl 1:8129536051df 178 while(!(LPC_ADC->ADGDR>>31)); // Aguarda o bit de DONE ser setado no registrador ADGDR sinalizando o final de uma captura
viniciushl 1:8129536051df 179
viniciushl 1:8129536051df 180 if (cnt_buffer < NUMBER_OF_SAMPLES)
viniciushl 1:8129536051df 181 {
viniciushl 1:8129536051df 182 m_AdcBuffers[channel_number][cnt_buffer] =((LPC_ADC->ADGDR>>4)&0xfff);
viniciushl 1:8129536051df 183 if (channel_number < 5)
viniciushl 1:8129536051df 184 {
viniciushl 1:8129536051df 185 channel_number++;
viniciushl 1:8129536051df 186 }
viniciushl 1:8129536051df 187 else
viniciushl 1:8129536051df 188 {
viniciushl 1:8129536051df 189 channel_number = 0;
viniciushl 1:8129536051df 190 cnt_buffer++;
viniciushl 1:8129536051df 191 }
viniciushl 1:8129536051df 192 }
viniciushl 1:8129536051df 193 else
viniciushl 1:8129536051df 194 {
viniciushl 1:8129536051df 195
viniciushl 1:8129536051df 196 flag_capture = 1;
viniciushl 1:8129536051df 197 NVIC_DisableIRQ(TIMER3_IRQn);
viniciushl 1:8129536051df 198 //timer_interrupt.detach();
viniciushl 1:8129536051df 199 }
viniciushl 1:8129536051df 200 select_channels(1<<channel_number);
viniciushl 1:8129536051df 201 LPC_ADC->ADCR |= ADC_START_NOW;
viniciushl 1:8129536051df 202 }
viniciushl 1:8129536051df 203 }
viniciushl 1:8129536051df 204