Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Impedance_Fast_Circuitry by
main.cpp@72:0b554f5026b9, 2017-05-18 (annotated)
- Committer:
- baxterja
- Date:
- Thu May 18 20:19:12 2017 +0000
- Revision:
- 72:0b554f5026b9
- Parent:
- 70:8cd7a8a2c153
- Child:
- 73:b059b5bdc664
ADC0 is now swapping between a0 and a1. Everything else is working great also.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bb128f0e952f | 1 | #include "mbed.h" |
timmey9 | 22:523e316cbe70 | 2 | |
timmey9 | 52:5a40cc58c4c2 | 3 | // Sampling |
bmazzeo | 53:83a90a47c1fd | 4 | #include "DMA_sampling/adc.h" |
bmazzeo | 54:1697dc574b96 | 5 | #include "DMA_sampling/dac.h" |
bmazzeo | 55:2526b3317bc8 | 6 | #include "DMA_sampling/pdb.h" |
timmey9 | 22:523e316cbe70 | 7 | |
bmazzeo | 67:ec0c58490ce6 | 8 | // DSP |
bmazzeo | 67:ec0c58490ce6 | 9 | #include "dsp.h" |
bmazzeo | 67:ec0c58490ce6 | 10 | |
baxterja | 72:0b554f5026b9 | 11 | #define PRINT_BUFFER_LENGTH 10000 |
baxterja | 70:8cd7a8a2c153 | 12 | |
bmazzeo | 67:ec0c58490ce6 | 13 | |
timmey9 | 22:523e316cbe70 | 14 | // for debug purposes |
timmey9 | 18:b17ddeeb1c09 | 15 | Serial pc(USBTX, USBRX); |
timmey9 | 18:b17ddeeb1c09 | 16 | DigitalOut led_red(LED_RED); |
timmey9 | 18:b17ddeeb1c09 | 17 | DigitalOut led_green(LED_GREEN); |
timmey9 | 18:b17ddeeb1c09 | 18 | DigitalOut led_blue(LED_BLUE); |
bmazzeo | 58:4bee89daccff | 19 | DigitalOut status_0(D0); |
bmazzeo | 58:4bee89daccff | 20 | DigitalOut status_1(D1); |
bmazzeo | 66:72c5c24e532c | 21 | DigitalIn sw2(SW2); |
bmazzeo | 66:72c5c24e532c | 22 | DigitalIn sw3(SW3); |
timmey9 | 51:43143a3fc2d7 | 23 | |
timmey9 | 52:5a40cc58c4c2 | 24 | // defined in dma.cpp |
timmey9 | 45:d591d138cdeb | 25 | extern int len; |
bmazzeo | 54:1697dc574b96 | 26 | extern uint16_t static_input_array0[]; |
bmazzeo | 54:1697dc574b96 | 27 | extern uint16_t static_input_array1[]; |
bmazzeo | 55:2526b3317bc8 | 28 | extern uint16_t static_output_array0[]; |
bmazzeo | 57:7b8c49e1c1f6 | 29 | extern uint16_t sampling_status; |
bmazzeo | 57:7b8c49e1c1f6 | 30 | |
bmazzeo | 67:ec0c58490ce6 | 31 | // To set up FIR filtering |
baxterja | 69:014d4bbd4e03 | 32 | #define TOTAL_TAPS 16 |
baxterja | 69:014d4bbd4e03 | 33 | #define STATE_LENGTH 79 |
bmazzeo | 67:ec0c58490ce6 | 34 | #define BUFFER_LENGTH 64 |
bmazzeo | 67:ec0c58490ce6 | 35 | uint16_t numTaps = TOTAL_TAPS; |
bmazzeo | 67:ec0c58490ce6 | 36 | float State[STATE_LENGTH]= {0}; |
baxterja | 69:014d4bbd4e03 | 37 | float Coeffs[TOTAL_TAPS] = {0.0351885543264657, 0.0326718041339497, 0.0447859388579763, 0.0571840193174261, 0.0688188647212066, 0.0786841934234295, 0.0858044659079242, 0.0895870376972757, 0.0895870376972757, 0.0858044659079242, 0.0786841934234295, 0.0688188647212066, 0.0571840193174261, 0.0447859388579763, 0.0326718041339497, 0.0351885543264657}; |
bmazzeo | 67:ec0c58490ce6 | 38 | arm_fir_instance_f32 S = {numTaps, State, Coeffs}; |
bmazzeo | 67:ec0c58490ce6 | 39 | |
bmazzeo | 67:ec0c58490ce6 | 40 | float filter_input_array[BUFFER_LENGTH] = {0}; |
bmazzeo | 67:ec0c58490ce6 | 41 | float filter_output_array[BUFFER_LENGTH] = {0}; |
bmazzeo | 67:ec0c58490ce6 | 42 | |
bmazzeo | 67:ec0c58490ce6 | 43 | |
baxterja | 69:014d4bbd4e03 | 44 | |
baxterja | 69:014d4bbd4e03 | 45 | #define pre_compute_length 500 |
baxterja | 69:014d4bbd4e03 | 46 | #define CarrierFrequency 200 |
baxterja | 69:014d4bbd4e03 | 47 | #define SAMPLEFREQUENCY 100000 |
baxterja | 69:014d4bbd4e03 | 48 | //float i_mod_pre[pre_compute_length]; |
baxterja | 69:014d4bbd4e03 | 49 | //float q_mod_pre[pre_compute_length]; |
baxterja | 69:014d4bbd4e03 | 50 | uint16_t out_val_pre[pre_compute_length]; |
baxterja | 69:014d4bbd4e03 | 51 | float twopi = 3.14159265359 * 2; |
baxterja | 69:014d4bbd4e03 | 52 | |
baxterja | 69:014d4bbd4e03 | 53 | |
baxterja | 69:014d4bbd4e03 | 54 | void pre_compute_tables() { |
baxterja | 69:014d4bbd4e03 | 55 | // This function will precompute the cos and sin tables used in the rest of the program |
baxterja | 69:014d4bbd4e03 | 56 | for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){ |
baxterja | 69:014d4bbd4e03 | 57 | out_val_pre[precompute_counter] = (int) (cos(twopi * CarrierFrequency /SAMPLEFREQUENCY * precompute_counter) * 2046.0 + 2048.0); |
baxterja | 72:0b554f5026b9 | 58 | //if(precompute_counter<len) |
baxterja | 72:0b554f5026b9 | 59 | // static_output_array0[precompute_counter] = out_val_pre[precompute_counter]; |
baxterja | 69:014d4bbd4e03 | 60 | //printf("%d\n\r",out_val_pre[precompute_counter]); |
baxterja | 69:014d4bbd4e03 | 61 | //i_mod_pre[precompute_counter] = (cos(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter)); |
baxterja | 69:014d4bbd4e03 | 62 | //q_mod_pre[precompute_counter] = (-sin(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter)); |
baxterja | 69:014d4bbd4e03 | 63 | |
baxterja | 69:014d4bbd4e03 | 64 | } |
baxterja | 69:014d4bbd4e03 | 65 | } |
baxterja | 69:014d4bbd4e03 | 66 | |
baxterja | 69:014d4bbd4e03 | 67 | |
emilmont | 7:65188f4a8c25 | 68 | int main() { |
timmey9 | 22:523e316cbe70 | 69 | led_blue = 1; |
timmey9 | 35:df40c4566826 | 70 | led_green = 1; |
timmey9 | 18:b17ddeeb1c09 | 71 | led_red = 1; |
timmey9 | 34:44cc9b76a507 | 72 | |
baxterja | 69:014d4bbd4e03 | 73 | int DAC_COUNTER = 0; |
baxterja | 69:014d4bbd4e03 | 74 | //bool is_magnified_sample = true; |
baxterja | 69:014d4bbd4e03 | 75 | pre_compute_tables(); |
baxterja | 69:014d4bbd4e03 | 76 | |
baxterja | 69:014d4bbd4e03 | 77 | |
timmey9 | 18:b17ddeeb1c09 | 78 | pc.baud(230400); |
timmey9 | 34:44cc9b76a507 | 79 | pc.printf("Starting\r\n"); |
timmey9 | 27:8c2b30c855d1 | 80 | |
baxterja | 70:8cd7a8a2c153 | 81 | |
baxterja | 70:8cd7a8a2c153 | 82 | uint16_t output_print_buffer[PRINT_BUFFER_LENGTH]; |
baxterja | 70:8cd7a8a2c153 | 83 | uint16_t output_print_buffer2[PRINT_BUFFER_LENGTH]; |
baxterja | 70:8cd7a8a2c153 | 84 | int print_buffer_count = 0; |
baxterja | 70:8cd7a8a2c153 | 85 | |
bmazzeo | 66:72c5c24e532c | 86 | pdb_init(); // Initalize PDB |
bmazzeo | 66:72c5c24e532c | 87 | dac_init(); // initializes DAC |
timmey9 | 45:d591d138cdeb | 88 | adc_init(); // always initialize adc before dma |
bmazzeo | 53:83a90a47c1fd | 89 | pc.printf("ADC Initialized\r\n"); |
timmey9 | 51:43143a3fc2d7 | 90 | dma_init(); // initializes DMAs |
bmazzeo | 55:2526b3317bc8 | 91 | dma_reset(); // This clears any DMA triggers that may have gotten things into a different state |
bmazzeo | 67:ec0c58490ce6 | 92 | pc.printf("Buffer Size: %i\r\n", len); |
bmazzeo | 54:1697dc574b96 | 93 | |
timmey9 | 46:a015ebf4663b | 94 | led_green = 1; |
timmey9 | 38:ec3b16c130d7 | 95 | |
timmey9 | 40:bd6d8c35e822 | 96 | pc.printf("\r\n\r\n\r\n"); |
timmey9 | 37:8bdc71f3e874 | 97 | |
bmazzeo | 64:bb4a4bd57681 | 98 | pdb_start(); |
baxterja | 70:8cd7a8a2c153 | 99 | //while(print_buffer_count<PRINT_BUFFER_LENGTH) |
baxterja | 70:8cd7a8a2c153 | 100 | while(1) |
bmazzeo | 61:a56cca07d4a6 | 101 | { |
bmazzeo | 61:a56cca07d4a6 | 102 | while(sampling_status == 0) |
bmazzeo | 61:a56cca07d4a6 | 103 | { |
bmazzeo | 61:a56cca07d4a6 | 104 | status_0 = 1; |
bmazzeo | 61:a56cca07d4a6 | 105 | } |
baxterja | 69:014d4bbd4e03 | 106 | /* |
baxterja | 69:014d4bbd4e03 | 107 | if (is_magnified_sample) |
baxterja | 69:014d4bbd4e03 | 108 | { |
baxterja | 69:014d4bbd4e03 | 109 | ADC0_SC1A = 0x0D; |
baxterja | 69:014d4bbd4e03 | 110 | } |
baxterja | 69:014d4bbd4e03 | 111 | else |
baxterja | 69:014d4bbd4e03 | 112 | { |
baxterja | 69:014d4bbd4e03 | 113 | ADC0_SC1A = 0x0C; |
baxterja | 69:014d4bbd4e03 | 114 | } |
baxterja | 69:014d4bbd4e03 | 115 | is_magnified_sample = !is_magnified_sample; |
baxterja | 69:014d4bbd4e03 | 116 | */ |
bmazzeo | 61:a56cca07d4a6 | 117 | sampling_status = 0; |
bmazzeo | 61:a56cca07d4a6 | 118 | status_0 = 0; |
bmazzeo | 66:72c5c24e532c | 119 | |
bmazzeo | 66:72c5c24e532c | 120 | if(sw2) |
bmazzeo | 61:a56cca07d4a6 | 121 | { |
bmazzeo | 66:72c5c24e532c | 122 | if(sw3) |
bmazzeo | 66:72c5c24e532c | 123 | { |
bmazzeo | 66:72c5c24e532c | 124 | //Default PASSTHROUGH Condition |
bmazzeo | 66:72c5c24e532c | 125 | status_1 = 1; |
baxterja | 70:8cd7a8a2c153 | 126 | //printf("%d\n\r",static_input_array0[0]); |
bmazzeo | 66:72c5c24e532c | 127 | for(int i = 0; i < len; i++) |
bmazzeo | 66:72c5c24e532c | 128 | { |
baxterja | 72:0b554f5026b9 | 129 | //static_output_array0[i] = static_input_array0[i] >> 4; |
baxterja | 72:0b554f5026b9 | 130 | static_output_array0[i] = out_val_pre[DAC_COUNTER]; |
baxterja | 72:0b554f5026b9 | 131 | DAC_COUNTER++; |
baxterja | 72:0b554f5026b9 | 132 | if (DAC_COUNTER>=pre_compute_length) |
baxterja | 72:0b554f5026b9 | 133 | { |
baxterja | 72:0b554f5026b9 | 134 | DAC_COUNTER = 0; |
baxterja | 72:0b554f5026b9 | 135 | } |
baxterja | 70:8cd7a8a2c153 | 136 | //printf("%d\n\r",static_input_array0[i]); |
baxterja | 72:0b554f5026b9 | 137 | |
baxterja | 69:014d4bbd4e03 | 138 | |
baxterja | 72:0b554f5026b9 | 139 | //output_print_buffer[print_buffer_count] = static_input_array0[i]; |
baxterja | 72:0b554f5026b9 | 140 | //output_print_buffer2[print_buffer_count] = static_input_array1[i]; |
baxterja | 72:0b554f5026b9 | 141 | |
baxterja | 72:0b554f5026b9 | 142 | |
baxterja | 72:0b554f5026b9 | 143 | //if (i>4&&static_input_array0[i]==static_input_array0[i-1]&&static_input_array0[i]==static_input_array0[i-2]) |
baxterja | 72:0b554f5026b9 | 144 | // printf("Y"); |
baxterja | 72:0b554f5026b9 | 145 | print_buffer_count++; |
baxterja | 72:0b554f5026b9 | 146 | |
baxterja | 72:0b554f5026b9 | 147 | //if (print_buffer_count>=PRINT_BUFFER_LENGTH) |
baxterja | 70:8cd7a8a2c153 | 148 | //{ |
baxterja | 72:0b554f5026b9 | 149 | // break; |
baxterja | 72:0b554f5026b9 | 150 | //} |
baxterja | 69:014d4bbd4e03 | 151 | |
bmazzeo | 66:72c5c24e532c | 152 | } |
baxterja | 70:8cd7a8a2c153 | 153 | //print_buffer_count+=len; |
bmazzeo | 66:72c5c24e532c | 154 | status_1 = 0; |
bmazzeo | 66:72c5c24e532c | 155 | } |
bmazzeo | 66:72c5c24e532c | 156 | else |
bmazzeo | 66:72c5c24e532c | 157 | { |
bmazzeo | 66:72c5c24e532c | 158 | // Can show that buttons are active - Serial link working |
bmazzeo | 66:72c5c24e532c | 159 | status_1 = 1; |
baxterja | 69:014d4bbd4e03 | 160 | pc.printf("DSP: %d\r\n",out_val_pre[DAC_COUNTER]); |
baxterja | 69:014d4bbd4e03 | 161 | //pc.printf("DSP: %d\r\n",out_val_pre[DAC_COUNTER]); |
bmazzeo | 66:72c5c24e532c | 162 | for(int i = 0; i < len; i++) |
bmazzeo | 66:72c5c24e532c | 163 | { |
bmazzeo | 66:72c5c24e532c | 164 | static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 66:72c5c24e532c | 165 | } |
bmazzeo | 66:72c5c24e532c | 166 | status_1 = 0; |
bmazzeo | 66:72c5c24e532c | 167 | } |
bmazzeo | 66:72c5c24e532c | 168 | } |
bmazzeo | 66:72c5c24e532c | 169 | else |
bmazzeo | 66:72c5c24e532c | 170 | { |
bmazzeo | 66:72c5c24e532c | 171 | // Here we can really put our DSP blocks |
bmazzeo | 66:72c5c24e532c | 172 | status_1 = 1; |
bmazzeo | 66:72c5c24e532c | 173 | for(int i = 0; i < len; i++) |
bmazzeo | 66:72c5c24e532c | 174 | { |
bmazzeo | 67:ec0c58490ce6 | 175 | //static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 67:ec0c58490ce6 | 176 | //filter_input_array[i] = (float) (((int) static_input_array0[i]) - 0x8000); |
bmazzeo | 68:e5031c18fefb | 177 | filter_input_array[i] = (float) (((int)static_input_array0[i]) - 0x8000); |
bmazzeo | 67:ec0c58490ce6 | 178 | } |
bmazzeo | 67:ec0c58490ce6 | 179 | |
bmazzeo | 67:ec0c58490ce6 | 180 | //filter_input_array[0] = (float) static_input_array0[0]; |
bmazzeo | 67:ec0c58490ce6 | 181 | |
bmazzeo | 68:e5031c18fefb | 182 | arm_fir_f32(&S, filter_input_array, filter_output_array, len); |
bmazzeo | 68:e5031c18fefb | 183 | |
bmazzeo | 67:ec0c58490ce6 | 184 | |
bmazzeo | 68:e5031c18fefb | 185 | // Scale for output |
bmazzeo | 68:e5031c18fefb | 186 | /* |
bmazzeo | 67:ec0c58490ce6 | 187 | for(int i = 0; i < len; i++) |
bmazzeo | 67:ec0c58490ce6 | 188 | { |
bmazzeo | 67:ec0c58490ce6 | 189 | //static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 67:ec0c58490ce6 | 190 | //filter_output_array[i] = 0.25 * filter_input_array[i]; |
bmazzeo | 68:e5031c18fefb | 191 | filter_output_array[i] = 0.0625 * filter_output_array[i]; |
bmazzeo | 67:ec0c58490ce6 | 192 | } |
bmazzeo | 68:e5031c18fefb | 193 | */ |
bmazzeo | 67:ec0c58490ce6 | 194 | |
bmazzeo | 67:ec0c58490ce6 | 195 | for(int i = 0; i < len; i++) |
bmazzeo | 67:ec0c58490ce6 | 196 | { |
bmazzeo | 67:ec0c58490ce6 | 197 | //static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 67:ec0c58490ce6 | 198 | //static_output_array0[i] = (uint16_t) (( (int) filter_output_array[i] ) + 0x800); |
baxterja | 69:014d4bbd4e03 | 199 | //static_output_array0[i] = (uint16_t) (filter_output_array[i] + 0x8000) >> 4; |
bmazzeo | 67:ec0c58490ce6 | 200 | //static_output_array0[i] = (uint16_t) filter_input_array[i]; |
bmazzeo | 67:ec0c58490ce6 | 201 | //static_output_array0[i] = static_input_array0[i] >> 4; |
baxterja | 69:014d4bbd4e03 | 202 | |
baxterja | 69:014d4bbd4e03 | 203 | |
baxterja | 69:014d4bbd4e03 | 204 | static_output_array0[i] = out_val_pre[DAC_COUNTER]; |
baxterja | 69:014d4bbd4e03 | 205 | DAC_COUNTER++; |
baxterja | 69:014d4bbd4e03 | 206 | if (DAC_COUNTER>=pre_compute_length) |
baxterja | 69:014d4bbd4e03 | 207 | { |
baxterja | 69:014d4bbd4e03 | 208 | DAC_COUNTER = 0; |
baxterja | 69:014d4bbd4e03 | 209 | } |
baxterja | 69:014d4bbd4e03 | 210 | } |
baxterja | 69:014d4bbd4e03 | 211 | |
baxterja | 69:014d4bbd4e03 | 212 | |
bmazzeo | 66:72c5c24e532c | 213 | status_1 = 0; |
bmazzeo | 61:a56cca07d4a6 | 214 | } |
baxterja | 70:8cd7a8a2c153 | 215 | |
bmazzeo | 61:a56cca07d4a6 | 216 | } |
baxterja | 70:8cd7a8a2c153 | 217 | |
baxterja | 70:8cd7a8a2c153 | 218 | for(int i = 0; i<PRINT_BUFFER_LENGTH; i++) |
baxterja | 70:8cd7a8a2c153 | 219 | { |
baxterja | 70:8cd7a8a2c153 | 220 | printf("%d %d\n\r",output_print_buffer[i],output_print_buffer2[i]); |
baxterja | 70:8cd7a8a2c153 | 221 | } |
baxterja | 70:8cd7a8a2c153 | 222 | |
baxterja | 70:8cd7a8a2c153 | 223 | |
bmazzeo | 61:a56cca07d4a6 | 224 | } |
bmazzeo | 63:7903a33e2fd4 | 225 |