Jared Baxter / Mbed 2 deprecated Impedance_Fast_Circuitry_print_V_I

Dependencies:   mbed-dsp mbed

Fork of Impedance_Fast_Circuitry by Jared Baxter

Revision:
40:bd6d8c35e822
Parent:
39:82dc3daecf32
Child:
41:3e0623d81b9a
--- a/adc.cpp	Thu Jan 29 16:18:54 2015 +0000
+++ b/adc.cpp	Fri Jan 30 06:16:39 2015 +0000
@@ -1,14 +1,19 @@
 #include "adc.h"
+#include "dma.h"
+
+DigitalOut blah(LED_BLUE);
+DigitalOut toggle(PTC16);
 
 void analog_initialization(PinName pin, Serial &pc)
 {
     
+    
     // Turn on the ADC0 and ADC1 clocks
     SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;
     SIM_SCGC3 |= SIM_SCGC3_ADC1_MASK;
     
-    ADC0->CFG1 |= ADC_CFG1_ADLPC_MASK; // high power mode for faster frequencies
     
+    /*
     // Configure System Integration Module for defaults as far as ADC
     SIM_SOPT7 &= ~(SIM_SOPT7_ADC1ALTTRGEN_MASK  | // selects PDB not ALT trigger
                 SIM_SOPT7_ADC1PRETRGSEL_MASK |
@@ -16,6 +21,10 @@
                 SIM_SOPT7_ADC0ALTTRGEN_MASK) ;
     SIM_SOPT7 = SIM_SOPT7_ADC0TRGSEL(0);       // applies only in case of ALT trigger, in which case PDB external pin input trigger for ADC
     SIM_SOPT7 = SIM_SOPT7_ADC1TRGSEL(0);       // same for both ADCs
+    */
+    
+    // enable interrupt
+    ADC0_SC1A |= ADC_SC1_AIEN_MASK;
     
     // enable the DMA
     ADC0->SC2 |= ADC_SC2_DMAEN_MASK;
@@ -33,16 +42,9 @@
     
     ADC0->CFG1 |= ADC_CFG1_ADLPC_MASK; // high power mode for faster frequencies
     
-    
-    
-    
     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;
@@ -58,8 +60,8 @@
     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); // true=continuous conversion mode, false = single conversion mode
-    adc_hal_configure_hw_trigger(instance, true); // true=hw trigger, false=sw trigger
+    adc_hal_configure_continuous_conversion(instance, true); // true=continuous conversion mode, false = single conversion mode
+    adc_hal_configure_hw_trigger(instance, false); // true=hw trigger, 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 
@@ -74,4 +76,29 @@
     pc.printf("ADC0_RB:   %08x\r\n",ADC0_RB);
     pc.printf("ADC0_SC2:  %08x\r\n",ADC0_SC2); // hw trigger and dma enabled.  Compare function disabled and Vref set to external pin
     pc.printf("ADC0_SC3:  %08x\r\n\n",ADC0_SC3);
+    
+    // Enable the ISR vector
+    NVIC_SetVector (ADC0_IRQn, (uint32_t)&ADC0_IRQHandler);
+    NVIC_EnableIRQ(ADC0_IRQn);
+}
+
+void start_adc() {
+    // reset DMA
+    //reset_dma();
+    dma_init(100);
+    
+    // set ADC to continuous mode
+    ADC0_SC3 |= ADC_SC3_ADCO_MASK;
+    
+    // start ADC conversion (SW trigger)
+    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)
+}
+
+void stop_adc() {
+    ADC0_SC3 &= ~ADC_SC3_ADCO_MASK; // set to single conversion mode effectively stopping the ADC
+}
+
+void ADC0_IRQHandler() {
+    blah = !blah;
 }
\ No newline at end of file