Jared Baxter / Mbed 2 deprecated Impedance_Fast_Circuitry_print_V_I

Dependencies:   mbed-dsp mbed

Fork of Impedance_Fast_Circuitry by Jared Baxter

Committer:
timmey9
Date:
Thu Jan 29 16:18:54 2015 +0000
Revision:
39:82dc3daecf32
Parent:
38:ec3b16c130d7
Child:
40:bd6d8c35e822
Cleaned up the code.  PDB still won't work.

Who changed what in which revision?

UserRevisionLine numberNew 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 36:07d8a3143967 19 #define TOTAL_SAMPLES 3 // 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 39:82dc3daecf32 34
timmey9 39:82dc3daecf32 35 uint16_t sample_array1[TOTAL_SAMPLES] = {0xa,0xb,0xc};
timmey9 25:abbc19af13f9 36 uint16_t sample_array2[TOTAL_SAMPLES];
timmey9 22:523e316cbe70 37 uint16_t angle_array[TOTAL_SAMPLES];
timmey9 34:44cc9b76a507 38 float currA0 = 0;
timmey9 34:44cc9b76a507 39 float currA2 = 0;
timmey9 22:523e316cbe70 40
timmey9 22:523e316cbe70 41 // Declaration of functions
timmey9 39:82dc3daecf32 42
timmey9 22:523e316cbe70 43 void timed_sampling();
timmey9 22:523e316cbe70 44
timmey9 22:523e316cbe70 45 // Important globabl variables necessary for the sampling every interval
timmey9 22:523e316cbe70 46 int rotary_count = 0;
timmey9 22:523e316cbe70 47 uint32_t last_AMT20_AB_read = 0;
timmey9 23:9e5141647775 48
timmey9 39:82dc3daecf32 49 void PIT0_IRQHandler(void);
timmey9 39:82dc3daecf32 50
timmey9 22:523e316cbe70 51 using namespace std;
timmey9 17:2f978f823020 52
emilmont 7:65188f4a8c25 53 int main() {
timmey9 22:523e316cbe70 54 led_blue = 1;
timmey9 35:df40c4566826 55 led_green = 1;
timmey9 18:b17ddeeb1c09 56 led_red = 1;
timmey9 34:44cc9b76a507 57
timmey9 18:b17ddeeb1c09 58 pc.baud(230400);
timmey9 34:44cc9b76a507 59 pc.printf("Starting\r\n");
timmey9 27:8c2b30c855d1 60
timmey9 39:82dc3daecf32 61 //for(int i = 0; i < 3; i++) pc.printf("Sample[%i]: %x\r\n", i, sample_array1[i]);
timmey9 38:ec3b16c130d7 62
timmey9 39:82dc3daecf32 63 dma_init(sample_array1, sample_array2, angle_array, TOTAL_SAMPLES, pc);
timmey9 39:82dc3daecf32 64 analog_initialization(A0,pc);
timmey9 38:ec3b16c130d7 65
timmey9 39:82dc3daecf32 66 //pdb_init(pc);
timmey9 39:82dc3daecf32 67 //pdb_start();
timmey9 37:8bdc71f3e874 68
timmey9 39:82dc3daecf32 69 //Sampler.attach_us(&timed_sampling, SAMPLING_RATE);
timmey9 27:8c2b30c855d1 70
timmey9 35:df40c4566826 71 pc.printf("\r\n\r\n\r\n");
timmey9 39:82dc3daecf32 72 //while(1) {pc.printf("CNT: %04x\r\n",PDB0->CNT);}
timmey9 34:44cc9b76a507 73 while(1) {
timmey9 36:07d8a3143967 74 rotary_count++;
timmey9 34:44cc9b76a507 75 if(pc.readable() > 0) {
timmey9 34:44cc9b76a507 76 char temp = pc.getc();
timmey9 34:44cc9b76a507 77
timmey9 34:44cc9b76a507 78 switch(temp) {
timmey9 34:44cc9b76a507 79 case 's':
timmey9 35:df40c4566826 80 for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: %f\t",i,sample_array1[i]*3.3/65535);
timmey9 36:07d8a3143967 81
timmey9 35:df40c4566826 82 pc.printf("\r\n");
timmey9 34:44cc9b76a507 83 break;
timmey9 34:44cc9b76a507 84 case 'f':
timmey9 39:82dc3daecf32 85 for(int i = 0; i < TOTAL_SAMPLES; i++) sample_array1[i] = 0xf;
timmey9 34:44cc9b76a507 86 break;
timmey9 39:82dc3daecf32 87 case 'd':
timmey9 39:82dc3daecf32 88 for(int i = 0; i < 3; i++) pc.printf("Sample[%i]: %x\r\n", i, sample_array1[i]);
timmey9 39:82dc3daecf32 89 break;
timmey9 34:44cc9b76a507 90 }
timmey9 34:44cc9b76a507 91 }
timmey9 36:07d8a3143967 92 for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("A%i: %f ",i,sample_array1[i]*3.3/65535);
timmey9 39:82dc3daecf32 93
timmey9 39:82dc3daecf32 94 pc.printf("ADC0_RA: %08x\r\n",ADC0_RA);
timmey9 39:82dc3daecf32 95 pc.printf("ADC0_RB: %08x\r\n",ADC0_RB);
timmey9 39:82dc3daecf32 96 //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("B%i: %f ",i,sample_array2[i]*3.3/65535);
timmey9 39:82dc3daecf32 97 //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("C%i: %i ",i,angle_array[i]);
timmey9 35:df40c4566826 98 pc.printf("\r");
timmey9 35:df40c4566826 99 //pc.printf("DMA_DADDR: %08x \r", *dma_daddr);
timmey9 35:df40c4566826 100 //pc.printf("A1: %f\tA2: %f\r\n", currA0, currA2);
timmey9 34:44cc9b76a507 101 wait(1);
timmey9 17:2f978f823020 102 }
timmey9 22:523e316cbe70 103 }
timmey9 23:9e5141647775 104
timmey9 22:523e316cbe70 105 void timed_sampling() {
timmey9 39:82dc3daecf32 106 // start ADC conversion
timmey9 22:523e316cbe70 107 BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2)
timmey9 22:523e316cbe70 108 BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10)
timmey9 22:523e316cbe70 109
timmey9 22:523e316cbe70 110 // The following updates the rotary counter for the AMT20 sensor
timmey9 22:523e316cbe70 111 // Put A on PTC0
timmey9 22:523e316cbe70 112 // Put B on PTC1
timmey9 22:523e316cbe70 113 uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
timmey9 22:523e316cbe70 114 if (AMT20_AB != last_AMT20_AB_read)
timmey9 22:523e316cbe70 115 {
timmey9 22:523e316cbe70 116 // change "INVERT_ANGLE" to change whether relative angle counts up or down.
timmey9 22:523e316cbe70 117 if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
timmey9 22:523e316cbe70 118 #if INVERT_ANGLE == 1
timmey9 22:523e316cbe70 119 {rotary_count--;}
timmey9 22:523e316cbe70 120 else
timmey9 22:523e316cbe70 121 {rotary_count++;}
timmey9 22:523e316cbe70 122 #else
timmey9 22:523e316cbe70 123 {rotary_count++;}
timmey9 22:523e316cbe70 124 else
timmey9 22:523e316cbe70 125 {rotary_count--;}
timmey9 22:523e316cbe70 126 #endif
timmey9 22:523e316cbe70 127
timmey9 22:523e316cbe70 128 last_AMT20_AB_read = AMT20_AB;
timmey9 22:523e316cbe70 129 }
timmey9 39:82dc3daecf32 130 }