
Impedance Fast Circuitry Software
Fork of DSP_200kHz by
main.cpp@42:52a92a8d2cc7, 2015-01-30 (annotated)
- Committer:
- timmey9
- Date:
- Fri Jan 30 07:22:00 2015 +0000
- Revision:
- 42:52a92a8d2cc7
- Parent:
- 41:3e0623d81b9a
- Child:
- 43:c593a8b9688f
Works for ADC0_SE13 and ADC1_SE14 with their respective DMAs. Try reading port C directly into the DMA.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
timmey9 | 20:f533b3c9296f | 1 | // Server code |
donatien | 0:bb128f0e952f | 2 | #include "mbed.h" |
timmey9 | 24:a5891669afc5 | 3 | #include <stdio.h> |
timmey9 | 22:523e316cbe70 | 4 | |
timmey9 | 22:523e316cbe70 | 5 | // Analog sampling |
timmey9 | 22:523e316cbe70 | 6 | #include "PeripheralNames.h" |
timmey9 | 22:523e316cbe70 | 7 | #include "PeripheralPins.h" |
timmey9 | 22:523e316cbe70 | 8 | #include "fsl_adc_hal.h" |
timmey9 | 22:523e316cbe70 | 9 | #include "fsl_clock_manager.h" |
timmey9 | 22:523e316cbe70 | 10 | #include "fsl_dspi_hal.h" |
timmey9 | 22:523e316cbe70 | 11 | #include "AngleEncoder.h" |
timmey9 | 39:82dc3daecf32 | 12 | #include "adc.h" |
timmey9 | 34:44cc9b76a507 | 13 | #include "dma.h" |
timmey9 | 39:82dc3daecf32 | 14 | #include "pdb.h" |
timmey9 | 28:4a833d59897b | 15 | |
timmey9 | 22:523e316cbe70 | 16 | // Analog sampling |
timmey9 | 22:523e316cbe70 | 17 | #define MAX_FADC 6000000 |
timmey9 | 36:07d8a3143967 | 18 | #define SAMPLING_RATE 10 // In microseconds, so 10 us will be a sampling rate of 100 kHz |
timmey9 | 40:bd6d8c35e822 | 19 | #define TOTAL_SAMPLES 100 // originally 30000 for 0.3 ms of sampling. |
timmey9 | 22:523e316cbe70 | 20 | |
timmey9 | 22:523e316cbe70 | 21 | // for debug purposes |
timmey9 | 18:b17ddeeb1c09 | 22 | Serial pc(USBTX, USBRX); |
timmey9 | 18:b17ddeeb1c09 | 23 | DigitalOut led_red(LED_RED); |
timmey9 | 18:b17ddeeb1c09 | 24 | DigitalOut led_green(LED_GREEN); |
timmey9 | 18:b17ddeeb1c09 | 25 | DigitalOut led_blue(LED_BLUE); |
timmey9 | 18:b17ddeeb1c09 | 26 | |
timmey9 | 22:523e316cbe70 | 27 | AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz |
timmey9 | 22:523e316cbe70 | 28 | DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder |
timmey9 | 22:523e316cbe70 | 29 | DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder |
timmey9 | 22:523e316cbe70 | 30 | |
timmey9 | 22:523e316cbe70 | 31 | // Analog sampling |
timmey9 | 34:44cc9b76a507 | 32 | Ticker Sampler; |
timmey9 | 22:523e316cbe70 | 33 | |
timmey9 | 40:bd6d8c35e822 | 34 | uint16_t sample_array1[TOTAL_SAMPLES]; |
timmey9 | 25:abbc19af13f9 | 35 | uint16_t sample_array2[TOTAL_SAMPLES]; |
timmey9 | 22:523e316cbe70 | 36 | uint16_t angle_array[TOTAL_SAMPLES]; |
timmey9 | 34:44cc9b76a507 | 37 | float currA0 = 0; |
timmey9 | 34:44cc9b76a507 | 38 | float currA2 = 0; |
timmey9 | 22:523e316cbe70 | 39 | |
timmey9 | 41:3e0623d81b9a | 40 | |
timmey9 | 41:3e0623d81b9a | 41 | |
timmey9 | 22:523e316cbe70 | 42 | // Declaration of functions |
timmey9 | 39:82dc3daecf32 | 43 | |
timmey9 | 22:523e316cbe70 | 44 | void timed_sampling(); |
timmey9 | 22:523e316cbe70 | 45 | |
timmey9 | 22:523e316cbe70 | 46 | // Important globabl variables necessary for the sampling every interval |
timmey9 | 22:523e316cbe70 | 47 | int rotary_count = 0; |
timmey9 | 22:523e316cbe70 | 48 | uint32_t last_AMT20_AB_read = 0; |
timmey9 | 23:9e5141647775 | 49 | |
timmey9 | 41:3e0623d81b9a | 50 | DMA dma(sample_array1, sample_array2, angle_array, TOTAL_SAMPLES, &rotary_count); |
timmey9 | 41:3e0623d81b9a | 51 | |
timmey9 | 39:82dc3daecf32 | 52 | |
timmey9 | 22:523e316cbe70 | 53 | using namespace std; |
timmey9 | 17:2f978f823020 | 54 | |
emilmont | 7:65188f4a8c25 | 55 | int main() { |
timmey9 | 22:523e316cbe70 | 56 | led_blue = 1; |
timmey9 | 35:df40c4566826 | 57 | led_green = 1; |
timmey9 | 18:b17ddeeb1c09 | 58 | led_red = 1; |
timmey9 | 34:44cc9b76a507 | 59 | |
timmey9 | 18:b17ddeeb1c09 | 60 | pc.baud(230400); |
timmey9 | 34:44cc9b76a507 | 61 | pc.printf("Starting\r\n"); |
timmey9 | 27:8c2b30c855d1 | 62 | |
timmey9 | 41:3e0623d81b9a | 63 | dma.reset(); |
timmey9 | 42:52a92a8d2cc7 | 64 | adc_init(A1); |
timmey9 | 42:52a92a8d2cc7 | 65 | adc_init(A2); |
timmey9 | 38:ec3b16c130d7 | 66 | |
timmey9 | 40:bd6d8c35e822 | 67 | pc.printf("\r\n\r\n\r\n"); |
timmey9 | 37:8bdc71f3e874 | 68 | |
timmey9 | 34:44cc9b76a507 | 69 | while(1) { |
timmey9 | 36:07d8a3143967 | 70 | rotary_count++; |
timmey9 | 34:44cc9b76a507 | 71 | if(pc.readable() > 0) { |
timmey9 | 34:44cc9b76a507 | 72 | char temp = pc.getc(); |
timmey9 | 34:44cc9b76a507 | 73 | |
timmey9 | 34:44cc9b76a507 | 74 | switch(temp) { |
timmey9 | 40:bd6d8c35e822 | 75 | case 'a': |
timmey9 | 41:3e0623d81b9a | 76 | adc_start(); |
timmey9 | 40:bd6d8c35e822 | 77 | wait(1); |
timmey9 | 41:3e0623d81b9a | 78 | adc_stop(); |
timmey9 | 42:52a92a8d2cc7 | 79 | // then proceed to 's' to display the array |
timmey9 | 42:52a92a8d2cc7 | 80 | case 's': |
timmey9 | 42:52a92a8d2cc7 | 81 | for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("% 1i: %f\t% 1i: %f\r\n",i,sample_array1[i]*3.3/65535,i,sample_array2[i]*3.3/65535); |
timmey9 | 42:52a92a8d2cc7 | 82 | pc.printf("\r\n"); |
timmey9 | 42:52a92a8d2cc7 | 83 | break; |
timmey9 | 42:52a92a8d2cc7 | 84 | case 'f': |
timmey9 | 42:52a92a8d2cc7 | 85 | for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array1[i] = 0; sample_array2[i] = 0;} |
timmey9 | 42:52a92a8d2cc7 | 86 | pc.printf("Arrays cleared\r\n"); |
timmey9 | 40:bd6d8c35e822 | 87 | break; |
timmey9 | 34:44cc9b76a507 | 88 | } |
timmey9 | 34:44cc9b76a507 | 89 | } |
timmey9 | 17:2f978f823020 | 90 | } |
timmey9 | 22:523e316cbe70 | 91 | } |
timmey9 | 23:9e5141647775 | 92 | |
timmey9 | 22:523e316cbe70 | 93 | void timed_sampling() { |
timmey9 | 22:523e316cbe70 | 94 | |
timmey9 | 22:523e316cbe70 | 95 | // The following updates the rotary counter for the AMT20 sensor |
timmey9 | 22:523e316cbe70 | 96 | // Put A on PTC0 |
timmey9 | 22:523e316cbe70 | 97 | // Put B on PTC1 |
timmey9 | 22:523e316cbe70 | 98 | uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03; |
timmey9 | 22:523e316cbe70 | 99 | if (AMT20_AB != last_AMT20_AB_read) |
timmey9 | 22:523e316cbe70 | 100 | { |
timmey9 | 22:523e316cbe70 | 101 | // change "INVERT_ANGLE" to change whether relative angle counts up or down. |
timmey9 | 22:523e316cbe70 | 102 | if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U) |
timmey9 | 22:523e316cbe70 | 103 | #if INVERT_ANGLE == 1 |
timmey9 | 22:523e316cbe70 | 104 | {rotary_count--;} |
timmey9 | 22:523e316cbe70 | 105 | else |
timmey9 | 22:523e316cbe70 | 106 | {rotary_count++;} |
timmey9 | 22:523e316cbe70 | 107 | #else |
timmey9 | 22:523e316cbe70 | 108 | {rotary_count++;} |
timmey9 | 22:523e316cbe70 | 109 | else |
timmey9 | 22:523e316cbe70 | 110 | {rotary_count--;} |
timmey9 | 22:523e316cbe70 | 111 | #endif |
timmey9 | 22:523e316cbe70 | 112 | |
timmey9 | 22:523e316cbe70 | 113 | last_AMT20_AB_read = AMT20_AB; |
timmey9 | 22:523e316cbe70 | 114 | } |
timmey9 | 42:52a92a8d2cc7 | 115 | } |