Jared Baxter
/
Impedance_Fast_Circuitry
Fork of DSP_200kHz by
main.cpp
- Committer:
- timmey9
- Date:
- 2015-01-25
- Revision:
- 34:44cc9b76a507
- Parent:
- 33:9806eb964362
- Child:
- 35:df40c4566826
File content as of revision 34:44cc9b76a507:
// Server code #include "mbed.h" #include <stdio.h> // Analog sampling #include "PeripheralNames.h" #include "PeripheralPins.h" #include "fsl_adc_hal.h" #include "fsl_clock_manager.h" #include "fsl_dspi_hal.h" #include "AngleEncoder.h" #include "dma.h" // Analog sampling #define MAX_FADC 6000000 #define SAMPLING_RATE 1000 // In microseconds, so 10 us will be a sampling rate of 100 kHz #define TOTAL_SAMPLES 10 // originally 30000 for 0.3 ms of sampling. #define LAST_SAMPLE_INDEX (TOTAL_SAMPLES-1) // If sampling time is 25 us, then 2000 corresponds to 50 ms #define FIRST_SAMPLE_INDEX 0 #define BEGIN_SAMPLING 0xFFFFFFFF #define WAITING_TO_BEGIN (BEGIN_SAMPLING-1) // for debug purposes Serial pc(USBTX, USBRX); DigitalOut led_red(LED_RED); DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder // Analog sampling AnalogIn A0_pin(A0); AnalogIn A2_pin(A2); Ticker Sampler; uint32_t current_sample_index = WAITING_TO_BEGIN; uint16_t sample_array1[TOTAL_SAMPLES]; uint16_t sample_array2[TOTAL_SAMPLES]; uint16_t angle_array[TOTAL_SAMPLES]; float currA0 = 0; float currA2 = 0; // Declaration of functions void analog_initialization(PinName pin); void timed_sampling(); // Important globabl variables necessary for the sampling every interval int rotary_count = 0; uint32_t last_AMT20_AB_read = 0; using namespace std; int main() { led_blue = 1; led_green = 0; led_red = 1; pc.baud(230400); pc.printf("Starting\r\n"); analog_initialization(A0); analog_initialization(A2); dma_init(sample_array1,TOTAL_SAMPLES); // Start the sampling loop current_sample_index = WAITING_TO_BEGIN; Sampler.attach_us(&timed_sampling, SAMPLING_RATE); while(1) { if(pc.readable() > 0) { char temp = pc.getc(); switch(temp) { case 's': for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: ",i,sample_array1[i]); break; case 'f': for(int i = 0; i < TOTAL_SAMPLES; i++) sample_array1[i] = 0; break; } } pc.printf("A1: %f\tA2: %f\r\n", currA0, currA2); wait(1); } } void timed_sampling() { __disable_irq(); // Disable Interrupts // The following performs analog-to-digital conversions - first reading the last conversion - then initiating another uint32_t A0_value = adc_hal_get_conversion_value(0, 0); uint32_t A2_value = adc_hal_get_conversion_value(1, 0); BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2) BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10) currA0 = (float) A0_value*3.3/65535; currA2 = (float) A2_value*3.3/65535; // The following updates the rotary counter for the AMT20 sensor // Put A on PTC0 // Put B on PTC1 uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03; //AMT20_AB = ~last_AMT20_AB_read; // Used to force a count - extend time. if (AMT20_AB != last_AMT20_AB_read) { // change "INVERT_ANGLE" to change whether relative angle counts up or down. if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U) #if INVERT_ANGLE == 1 {rotary_count--;} else {rotary_count++;} #else {rotary_count++;} else {rotary_count--;} #endif last_AMT20_AB_read = AMT20_AB; } //current_sample_index = BEGIN_SAMPLING; // Used to force extra time. if (current_sample_index == WAITING_TO_BEGIN) {} else { if (current_sample_index == BEGIN_SAMPLING) { current_sample_index = FIRST_SAMPLE_INDEX; } sample_array1[current_sample_index] = A0_value; sample_array2[current_sample_index] = A2_value; angle_array[current_sample_index] = rotary_count; if (current_sample_index == LAST_SAMPLE_INDEX) { current_sample_index = WAITING_TO_BEGIN; } else { current_sample_index++; } } __enable_irq(); // Enable Interrupts } void analog_initialization(PinName pin) { ADCName adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); // MBED_ASSERT(adc != (ADCName)NC); uint32_t instance = adc >> ADC_INSTANCE_SHIFT; clock_manager_set_gate(kClockModuleADC, instance, true); uint32_t bus_clock; clock_manager_get_frequency(kBusClock, &bus_clock); uint32_t clkdiv; for (clkdiv = 0; clkdiv < 4; clkdiv++) { if ((bus_clock >> clkdiv) <= MAX_FADC) break; } if (clkdiv == 4) { clkdiv = 0x7; //Set max div } // adc is enabled/triggered when reading. adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2)); adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3)); adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref); adc_hal_set_resolution_mode(instance, kAdcSingleDiff16); adc_hal_configure_continuous_conversion(instance, false); adc_hal_configure_hw_trigger(instance, false); // sw trigger adc_hal_configure_hw_average(instance, false); adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4); adc_hal_set_group_mux(instance, kAdcChannelMuxB); // only B channels are avail pinmap_pinout(pin, PinMap_ADC); }