Versão limpa em 04/09/2014. Telnet funcionando.

Dependencies:   EthernetInterface mbed-rtos mbed NTPClient

Committer:
rebonatto
Date:
Mon Jan 18 18:01:59 2016 +0000
Revision:
42:d6f9ce115eaf
Parent:
39:9fd8397cbef9
Child:
43:69afea5f5a4d
Aquisi??o pelo AD sem DMA. Com FFT e envio.; Buffer em dobro para ocupar o mesmo. Trava com acesso a telnet e processamento de eventos.

Who changed what in which revision?

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