![](/media/cache/profiles/cff02a74da64d145a4aed3a577a106ab.jpg.50x50_q85.jpg)
test
main.cpp@2:88f866289381, 2014-05-16 (annotated)
- Committer:
- fblanc
- Date:
- Fri May 16 14:05:52 2014 +0000
- Revision:
- 2:88f866289381
- Parent:
- 1:16fae309f85e
- Child:
- 3:7acf6d1a6b1e
correction bug '\0'
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fblanc | 0:92dd9611bf80 | 1 | #include "mbed.h" |
fblanc | 0:92dd9611bf80 | 2 | |
fblanc | 0:92dd9611bf80 | 3 | |
fblanc | 0:92dd9611bf80 | 4 | Serial pc(USBTX, USBRX); |
fblanc | 0:92dd9611bf80 | 5 | |
fblanc | 0:92dd9611bf80 | 6 | DigitalOut led1(LED1); |
fblanc | 0:92dd9611bf80 | 7 | DigitalOut led2(LED2); |
fblanc | 0:92dd9611bf80 | 8 | DigitalOut led3(LED3); |
fblanc | 0:92dd9611bf80 | 9 | DigitalOut led4(LED4); |
fblanc | 0:92dd9611bf80 | 10 | AnalogIn adc_hma(p17); |
fblanc | 0:92dd9611bf80 | 11 | |
fblanc | 0:92dd9611bf80 | 12 | uint16_t T_hma_u16[2][4096]; |
fblanc | 0:92dd9611bf80 | 13 | uint16_t index_hma_write=0; |
fblanc | 0:92dd9611bf80 | 14 | |
fblanc | 0:92dd9611bf80 | 15 | uint16_t index_hma_read=0; |
fblanc | 0:92dd9611bf80 | 16 | bool t_write=0; |
fblanc | 0:92dd9611bf80 | 17 | char str[256]; |
fblanc | 0:92dd9611bf80 | 18 | uint8_t start_str=0; |
fblanc | 0:92dd9611bf80 | 19 | bool flag_write_USB=0; |
fblanc | 0:92dd9611bf80 | 20 | Ticker flipperADC; |
fblanc | 0:92dd9611bf80 | 21 | |
fblanc | 0:92dd9611bf80 | 22 | void flipADC() |
fblanc | 0:92dd9611bf80 | 23 | { |
fblanc | 0:92dd9611bf80 | 24 | led4=1; |
fblanc | 0:92dd9611bf80 | 25 | T_hma_u16[t_write][index_hma_write++]=adc_hma.read_u16(); //read ADC |
fblanc | 0:92dd9611bf80 | 26 | //T_hma_u16[t_write][index_hma_write++]=0x00FF; |
fblanc | 0:92dd9611bf80 | 27 | |
fblanc | 0:92dd9611bf80 | 28 | if(index_hma_write>1000) { |
fblanc | 0:92dd9611bf80 | 29 | flag_write_USB=1; |
fblanc | 0:92dd9611bf80 | 30 | t_write=!t_write; //toggel buffer |
fblanc | 0:92dd9611bf80 | 31 | index_hma_read=--index_hma_write; |
fblanc | 0:92dd9611bf80 | 32 | index_hma_write=0; |
fblanc | 0:92dd9611bf80 | 33 | } |
fblanc | 0:92dd9611bf80 | 34 | led4=0; |
fblanc | 0:92dd9611bf80 | 35 | } |
fblanc | 0:92dd9611bf80 | 36 | |
fblanc | 0:92dd9611bf80 | 37 | Ticker flipperUSB; |
fblanc | 0:92dd9611bf80 | 38 | void flipUSB() |
fblanc | 0:92dd9611bf80 | 39 | { |
fblanc | 0:92dd9611bf80 | 40 | |
fblanc | 0:92dd9611bf80 | 41 | flag_write_USB=1; |
fblanc | 0:92dd9611bf80 | 42 | |
fblanc | 0:92dd9611bf80 | 43 | } |
fblanc | 0:92dd9611bf80 | 44 | |
fblanc | 0:92dd9611bf80 | 45 | void pc_rx(void) |
fblanc | 0:92dd9611bf80 | 46 | { |
fblanc | 0:92dd9611bf80 | 47 | |
fblanc | 0:92dd9611bf80 | 48 | |
fblanc | 0:92dd9611bf80 | 49 | for(; pc.readable()==true; ++start_str) { |
fblanc | 0:92dd9611bf80 | 50 | str[start_str]=pc.getc(); |
fblanc | 0:92dd9611bf80 | 51 | } |
fblanc | 0:92dd9611bf80 | 52 | |
fblanc | 1:16fae309f85e | 53 | if( strcmp(str,"*IDN?")==0 || strcmp(str,"*ID?")==0) { |
fblanc | 0:92dd9611bf80 | 54 | pc.printf("HMA\r"); |
fblanc | 0:92dd9611bf80 | 55 | start_str=0; |
fblanc | 0:92dd9611bf80 | 56 | for(int i=0; i<256; ++i) { |
fblanc | 0:92dd9611bf80 | 57 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 58 | |
fblanc | 0:92dd9611bf80 | 59 | } |
fblanc | 0:92dd9611bf80 | 60 | led4=1; |
fblanc | 0:92dd9611bf80 | 61 | led2=0; |
fblanc | 0:92dd9611bf80 | 62 | led3=0; |
fblanc | 0:92dd9611bf80 | 63 | } else if( strcmp(str,"MEASure")==0 || strcmp(str,"MEAS")==0 || strcmp(str,"measure")==0) { |
fblanc | 0:92dd9611bf80 | 64 | for(int i=0; i<256; ++i) { |
fblanc | 0:92dd9611bf80 | 65 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 66 | |
fblanc | 0:92dd9611bf80 | 67 | } |
fblanc | 0:92dd9611bf80 | 68 | flipperADC.attach_us(&flipADC, 1000); //500us |
fblanc | 0:92dd9611bf80 | 69 | led4=0; |
fblanc | 0:92dd9611bf80 | 70 | led2=1; |
fblanc | 0:92dd9611bf80 | 71 | led3=0; |
fblanc | 0:92dd9611bf80 | 72 | |
fblanc | 0:92dd9611bf80 | 73 | } else if( strcmp(str,"ABORt")==0 || strcmp(str,"ABOR")==0 || strcmp(str,"ABORT")==0) { |
fblanc | 0:92dd9611bf80 | 74 | for(int i=0; i<256; ++i) { |
fblanc | 0:92dd9611bf80 | 75 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 76 | |
fblanc | 0:92dd9611bf80 | 77 | } |
fblanc | 0:92dd9611bf80 | 78 | flipperADC.detach(); |
fblanc | 0:92dd9611bf80 | 79 | t_write=0; |
fblanc | 0:92dd9611bf80 | 80 | index_hma_read=0; |
fblanc | 0:92dd9611bf80 | 81 | index_hma_write=0; |
fblanc | 0:92dd9611bf80 | 82 | led4=0; |
fblanc | 0:92dd9611bf80 | 83 | led2=0; |
fblanc | 0:92dd9611bf80 | 84 | led3=1; |
fblanc | 0:92dd9611bf80 | 85 | } |
fblanc | 0:92dd9611bf80 | 86 | for(int i=0; i<start_str; ++i) { |
fblanc | 0:92dd9611bf80 | 87 | if(str[i]=='\r') { |
fblanc | 0:92dd9611bf80 | 88 | for(int j=0; j<256; ++j) { |
fblanc | 0:92dd9611bf80 | 89 | str[j]='\0'; |
fblanc | 0:92dd9611bf80 | 90 | |
fblanc | 0:92dd9611bf80 | 91 | } |
fblanc | 0:92dd9611bf80 | 92 | start_str=0; |
fblanc | 0:92dd9611bf80 | 93 | |
fblanc | 0:92dd9611bf80 | 94 | } |
fblanc | 0:92dd9611bf80 | 95 | |
fblanc | 0:92dd9611bf80 | 96 | } |
fblanc | 0:92dd9611bf80 | 97 | |
fblanc | 0:92dd9611bf80 | 98 | |
fblanc | 0:92dd9611bf80 | 99 | } |
fblanc | 0:92dd9611bf80 | 100 | |
fblanc | 0:92dd9611bf80 | 101 | int main() |
fblanc | 0:92dd9611bf80 | 102 | { |
fblanc | 0:92dd9611bf80 | 103 | |
fblanc | 0:92dd9611bf80 | 104 | pc.baud(460800); |
fblanc | 0:92dd9611bf80 | 105 | for(int i=0; i<256; ++i) { |
fblanc | 0:92dd9611bf80 | 106 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 107 | |
fblanc | 0:92dd9611bf80 | 108 | } |
fblanc | 0:92dd9611bf80 | 109 | //RUN ADC |
fblanc | 0:92dd9611bf80 | 110 | |
fblanc | 0:92dd9611bf80 | 111 | // flipperADC.attach_us(&flipADC, 500); //500us |
fblanc | 0:92dd9611bf80 | 112 | // flipperUSB.attach(&flipUSB,0.5); //500ms |
fblanc | 0:92dd9611bf80 | 113 | |
fblanc | 0:92dd9611bf80 | 114 | while(1) { |
fblanc | 0:92dd9611bf80 | 115 | led1=0; |
fblanc | 0:92dd9611bf80 | 116 | if (pc.readable()) { |
fblanc | 0:92dd9611bf80 | 117 | pc_rx(); |
fblanc | 0:92dd9611bf80 | 118 | } |
fblanc | 0:92dd9611bf80 | 119 | if(flag_write_USB==1) { |
fblanc | 0:92dd9611bf80 | 120 | flag_write_USB=0; |
fblanc | 0:92dd9611bf80 | 121 | led1=1; |
fblanc | 0:92dd9611bf80 | 122 | |
fblanc | 2:88f866289381 | 123 | pc.printf("%04d%c\r",index_hma_read,'\0'); //'\0'fin de chaine ASCII '\r' fin de transmission |
fblanc | 0:92dd9611bf80 | 124 | |
fblanc | 0:92dd9611bf80 | 125 | char* ptr=(char*)&T_hma_u16[!t_write][0]; |
fblanc | 0:92dd9611bf80 | 126 | |
fblanc | 0:92dd9611bf80 | 127 | for(uint16_t i=0; i<index_hma_read; ++i) { |
fblanc | 0:92dd9611bf80 | 128 | pc.putc(*ptr++); |
fblanc | 0:92dd9611bf80 | 129 | pc.putc(*ptr++); |
fblanc | 0:92dd9611bf80 | 130 | } |
fblanc | 0:92dd9611bf80 | 131 | |
fblanc | 0:92dd9611bf80 | 132 | } |
fblanc | 0:92dd9611bf80 | 133 | |
fblanc | 0:92dd9611bf80 | 134 | |
fblanc | 0:92dd9611bf80 | 135 | |
fblanc | 0:92dd9611bf80 | 136 | } |
fblanc | 0:92dd9611bf80 | 137 | } |