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