![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Versão do protegemed que calcula o tempo em ms da fuga, calcula o numero de onverflow (valores muito baixo) e underflow (valores muito altos). Além disso, calcula um valor médio a partir dos valores capturados e não apenas pela fft.
Dependencies: EthernetInterface mbed-rtos mbed
Codes/Capture.cpp@1:917ca6b5d9d9, 2014-07-18 (annotated)
- Committer:
- rebonatto
- Date:
- Fri Jul 18 20:24:45 2014 +0000
- Revision:
- 1:917ca6b5d9d9
- Parent:
- 0:c64e1194230b
- Child:
- 2:86c3cb25577b
Nova vers?o: problemas com excesso de mem?ria.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rebonatto | 0:c64e1194230b | 1 | /* |
rebonatto | 0:c64e1194230b | 2 | * Capture.h |
rebonatto | 0:c64e1194230b | 3 | * |
rebonatto | 0:c64e1194230b | 4 | * Created on: |
rebonatto | 0:c64e1194230b | 5 | * Author: |
rebonatto | 0:c64e1194230b | 6 | */ |
rebonatto | 0:c64e1194230b | 7 | #include "Capture.h" |
rebonatto | 0:c64e1194230b | 8 | |
rebonatto | 0:c64e1194230b | 9 | Semaphore Capture::m_CaptureSemaphore(1); //used to alert the capture thread about a ready capture |
rebonatto | 0:c64e1194230b | 10 | int Capture::m_BufferIndex; |
rebonatto | 0:c64e1194230b | 11 | dmaLinkedListNode Capture::m_Nodes[2] __attribute__((section("AHBSRAM0"))); //this list holds the buffer configuration for the DMA peripheral |
rebonatto | 0:c64e1194230b | 12 | unsigned short int Capture::m_AdcBuffers[2][NUMBER_OF_SAMPLES][NUMBER_OF_CHANNELS] __attribute__((section("AHBSRAM0"))); |
rebonatto | 1:917ca6b5d9d9 | 13 | //Serial rfid_serial(p28,p27); |
rebonatto | 0:c64e1194230b | 14 | |
rebonatto | 0:c64e1194230b | 15 | //This function prepares the capture buffers and starts the DMA peripheral |
rebonatto | 0:c64e1194230b | 16 | void Capture::Start() |
rebonatto | 0:c64e1194230b | 17 | { |
rebonatto | 0:c64e1194230b | 18 | m_Nodes[0].destAddr = (unsigned int)m_AdcBuffers[0];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval; |
rebonatto | 0:c64e1194230b | 19 | m_Nodes[0].sourceAddr = (unsigned int)&LPC_ADC->ADGDR; |
rebonatto | 0:c64e1194230b | 20 | m_Nodes[0].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT; |
rebonatto | 0:c64e1194230b | 21 | m_Nodes[0].nextNode = (unsigned int)&m_Nodes[1]; |
rebonatto | 0:c64e1194230b | 22 | |
rebonatto | 0:c64e1194230b | 23 | m_Nodes[1].destAddr = (unsigned int)m_AdcBuffers[1];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval2; |
rebonatto | 0:c64e1194230b | 24 | m_Nodes[1].sourceAddr = (unsigned int)&LPC_ADC->ADGDR; |
rebonatto | 0:c64e1194230b | 25 | m_Nodes[1].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT; |
rebonatto | 0:c64e1194230b | 26 | m_Nodes[1].nextNode = (unsigned int)&m_Nodes[0]; |
rebonatto | 0:c64e1194230b | 27 | |
rebonatto | 0:c64e1194230b | 28 | m_BufferIndex=0; |
rebonatto | 0:c64e1194230b | 29 | |
rebonatto | 0:c64e1194230b | 30 | while(!(LPC_ADC->ADDR0&(1U<<31))); //waits the completion of the ADC Channel 0 capture - for synchronization purposes |
rebonatto | 0:c64e1194230b | 31 | while(!(LPC_ADC->ADDR0&(1U<<31))); //waits the completion of the ADC Channel 0 capture - for synchronization purposes |
rebonatto | 0:c64e1194230b | 32 | |
rebonatto | 0:c64e1194230b | 33 | setup_channel(&m_Nodes[0],0,DMA_PERIPHERAL_ADC,DMA_MEMORY); |
rebonatto | 0:c64e1194230b | 34 | } |
rebonatto | 0:c64e1194230b | 35 | |
rebonatto | 0:c64e1194230b | 36 | //This function initializes the ADC and DMA peripherals |
rebonatto | 0:c64e1194230b | 37 | void Capture::Initialize() |
rebonatto | 0:c64e1194230b | 38 | { |
rebonatto | 0:c64e1194230b | 39 | init_adc(Settings::get_FreqBase()*NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS); |
rebonatto | 0:c64e1194230b | 40 | select_channels(ADC_CH_0|ADC_CH_1|ADC_CH_2|ADC_CH_3|ADC_CH_4|ADC_CH_5); |
rebonatto | 0:c64e1194230b | 41 | LPC_ADC->ADCR |= ADC_MODE_BURST; |
rebonatto | 0:c64e1194230b | 42 | |
rebonatto | 0:c64e1194230b | 43 | init_dma(); |
rebonatto | 0:c64e1194230b | 44 | |
rebonatto | 0:c64e1194230b | 45 | Start(); |
rebonatto | 0:c64e1194230b | 46 | |
rebonatto | 0:c64e1194230b | 47 | m_CaptureSemaphore.wait(); |
rebonatto | 0:c64e1194230b | 48 | } |
rebonatto | 0:c64e1194230b | 49 | |
rebonatto | 0:c64e1194230b | 50 | void Capture::Stop() |
rebonatto | 0:c64e1194230b | 51 | { |
rebonatto | 0:c64e1194230b | 52 | m_CaptureSemaphore.release(); //release semaphore |
rebonatto | 0:c64e1194230b | 53 | stop_channel(); |
rebonatto | 0:c64e1194230b | 54 | } |
rebonatto | 0:c64e1194230b | 55 | |
rebonatto | 0:c64e1194230b | 56 | void Capture::Wait() |
rebonatto | 0:c64e1194230b | 57 | { |
rebonatto | 0:c64e1194230b | 58 | m_CaptureSemaphore.wait(osWaitForever); |
rebonatto | 0:c64e1194230b | 59 | } |
rebonatto | 0:c64e1194230b | 60 | |
rebonatto | 0:c64e1194230b | 61 | unsigned short int Capture::GetValue(int nsamples, int nchannel) |
rebonatto | 0:c64e1194230b | 62 | { |
rebonatto | 0:c64e1194230b | 63 | return ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]); |
rebonatto | 0:c64e1194230b | 64 | } |
rebonatto | 0:c64e1194230b | 65 | |
rebonatto | 0:c64e1194230b | 66 | void Capture::CopyBuffer(int channel, unsigned short int *dest) |
rebonatto | 0:c64e1194230b | 67 | { |
rebonatto | 0:c64e1194230b | 68 | for(int i=0;i<Settings::get_Samples();i++) |
rebonatto | 0:c64e1194230b | 69 | { |
rebonatto | 0:c64e1194230b | 70 | dest[i] = GetValue(i,channel); |
rebonatto | 0:c64e1194230b | 71 | } |
rebonatto | 0:c64e1194230b | 72 | } |
rebonatto | 0:c64e1194230b | 73 | |
rebonatto | 0:c64e1194230b | 74 | //DMA ISR signals the capture thread about the end of capture event |
rebonatto | 0:c64e1194230b | 75 | extern "C" void DMA_IRQHandler(void) |
rebonatto | 0:c64e1194230b | 76 | { |
rebonatto | 0:c64e1194230b | 77 | Capture::ISRHandler(); |
rebonatto | 0:c64e1194230b | 78 | } |
rebonatto | 0:c64e1194230b | 79 | |
rebonatto | 0:c64e1194230b | 80 | void Capture::ISRHandler() |
rebonatto | 0:c64e1194230b | 81 | { |
rebonatto | 0:c64e1194230b | 82 | Capture::m_BufferIndex = (~Capture::m_BufferIndex)&1; |
rebonatto | 0:c64e1194230b | 83 | |
rebonatto | 0:c64e1194230b | 84 | Capture::m_CaptureSemaphore.release(); |
rebonatto | 0:c64e1194230b | 85 | LPC_GPDMA->DMACIntTCClear = 0xFF; |
rebonatto | 1:917ca6b5d9d9 | 86 | } |
rebonatto | 1:917ca6b5d9d9 | 87 | |
rebonatto | 1:917ca6b5d9d9 | 88 | void Capture::ReadRFID(int channel,char *rfid) |
rebonatto | 1:917ca6b5d9d9 | 89 | { |
rebonatto | 1:917ca6b5d9d9 | 90 | /* |
rebonatto | 1:917ca6b5d9d9 | 91 | char cmd[4]; |
rebonatto | 1:917ca6b5d9d9 | 92 | cmd[0] = 'S'; |
rebonatto | 1:917ca6b5d9d9 | 93 | cmd[1] = '0'+channel; |
rebonatto | 1:917ca6b5d9d9 | 94 | cmd[2] = '\n'; |
rebonatto | 1:917ca6b5d9d9 | 95 | cmd[3] = '\0'; |
rebonatto | 1:917ca6b5d9d9 | 96 | |
rebonatto | 1:917ca6b5d9d9 | 97 | //send |
rebonatto | 1:917ca6b5d9d9 | 98 | rfid_serial.puts(cmd); |
rebonatto | 1:917ca6b5d9d9 | 99 | |
rebonatto | 1:917ca6b5d9d9 | 100 | //receive |
rebonatto | 1:917ca6b5d9d9 | 101 | char ch=0; |
rebonatto | 1:917ca6b5d9d9 | 102 | char ans[10]; |
rebonatto | 1:917ca6b5d9d9 | 103 | int cnt=0; |
rebonatto | 1:917ca6b5d9d9 | 104 | int tmout=1000; |
rebonatto | 1:917ca6b5d9d9 | 105 | while(ch != '\n' && tmout-- && cnt<9) |
rebonatto | 1:917ca6b5d9d9 | 106 | { |
rebonatto | 1:917ca6b5d9d9 | 107 | if(rfid_serial.readable()) |
rebonatto | 1:917ca6b5d9d9 | 108 | { |
rebonatto | 1:917ca6b5d9d9 | 109 | ch = rfid_serial.getc(); |
rebonatto | 1:917ca6b5d9d9 | 110 | if(!((ch>='0' && ch<='9') || (ch >= 'A' && ch <= 'F')))ch='0'; |
rebonatto | 1:917ca6b5d9d9 | 111 | ans[cnt++] = ch; |
rebonatto | 1:917ca6b5d9d9 | 112 | } |
rebonatto | 1:917ca6b5d9d9 | 113 | else |
rebonatto | 1:917ca6b5d9d9 | 114 | wait_ms(1); |
rebonatto | 1:917ca6b5d9d9 | 115 | |
rebonatto | 1:917ca6b5d9d9 | 116 | } |
rebonatto | 1:917ca6b5d9d9 | 117 | ans[cnt-1] = '\0'; |
rebonatto | 1:917ca6b5d9d9 | 118 | for(int i=0;i<8;i++) |
rebonatto | 1:917ca6b5d9d9 | 119 | rfid[i] = ans[i]; |
rebonatto | 1:917ca6b5d9d9 | 120 | */ |
rebonatto | 0:c64e1194230b | 121 | } |