Jared Baxter / Mbed 2 deprecated Impedance_Fast_Circuitry_print_V_I

Dependencies:   mbed-dsp mbed

Fork of Impedance_Fast_Circuitry by Jared Baxter

Revision:
45:d591d138cdeb
Parent:
44:41c262caf898
Child:
46:a015ebf4663b
--- a/main.cpp	Fri Jan 30 14:56:58 2015 +0000
+++ b/main.cpp	Sat Jan 31 07:25:52 2015 +0000
@@ -1,57 +1,32 @@
 // 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 "adc.h"
-#include "dma.h"
-#include "pit.h"
-
-// Analog sampling
-#define MAX_FADC 6000000
-#define SAMPLING_RATE       10 // In microseconds, so 10 us will be a sampling rate of 100 kHz
-#define TOTAL_SAMPLES       100 // originally 30000 for 0.3 ms of sampling.
+#include "pdb.h"
+#include "quadrature.h"
 
 // for debug purposes
 Serial pc(USBTX, USBRX);
 DigitalOut led_red(LED_RED);
 DigitalOut led_green(LED_GREEN);
 DigitalOut led_blue(LED_BLUE);
-DigitalOut test1(PTB19);
-DigitalOut test2(PTB18);
+//DigitalOut test1(PTB19);
+//DigitalOut test2(PTB18);
 
 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
-Ticker Sampler;
-
-uint16_t sample_array1[TOTAL_SAMPLES];
-uint16_t sample_array2[TOTAL_SAMPLES];
-uint16_t angle_array[TOTAL_SAMPLES];
-float currA0 = 0;
-float currA2 = 0;
-
+extern int len;
+extern uint16_t sample_array0[];
+extern uint16_t sample_array1[];
+extern uint16_t angle_array[];
+extern DigitalIn AMT20_A; // input for quadrature encoding from angle encoder
+extern DigitalIn AMT20_B; // input for quadrature encoding from angle encoder
 
 
 // Declaration of functions
 
-void timed_sampling();
-
-// Important globabl variables necessary for the sampling every interval
 int rotary_count = 0;
-uint32_t last_AMT20_AB_read = 0;
-
-DMA dma(sample_array1, sample_array2, angle_array, TOTAL_SAMPLES, &rotary_count);
-
-
 using namespace std;
  
 int main() {
@@ -62,10 +37,11 @@
     pc.baud(230400);
     pc.printf("Starting\r\n");
     
-    dma.reset();
-    adc_init(A1);
-    adc_init(A2);
-    pit_init(pc);
+    quad_init();
+    adc_init(); // always initialize adc before dma
+    dma_init();
+    pdb_init();
+    
     
     pc.printf("\r\n\r\n\r\n");
     
@@ -81,46 +57,42 @@
             char temp = pc.getc();
             
             switch(temp) {
+                case 'G':
+                case 'g':
+                    pc.printf("Quad Cnt: %i\r\n", quad_read());
+                    break;
+                case 'A':
                 case 'a':
-                    //adc_start();
-                    
-                    //adc_stop();
+                    adc_start();
+                    for(int i = 0; i < 1500; i++) asm("nop");
+                    adc_stop();
                     // then proceed to 's' to display the array
+                case 'S':
                 case 's':
-                    for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: %f\t %f\t %i%i\r\n",i,sample_array1[i]*3.3/65535,sample_array2[i]*3.3/65535, (angle_array[i]>>1)&0x01, angle_array[i]&0x01);
+                    for(int i = 0; i < len; i++) pc.printf("%i: %f\t %f\t %i%i\r\n",i,sample_array0[i]*3.3/65535,sample_array1[i]*3.3/65535, (angle_array[i]>>1)&0x01, angle_array[i]&0x01);
                     pc.printf("\r\n");
                     break;
+                case 'D':
+                case 'd':
+                    adc_single_sample();
+                    break;
+                case 'F':
                 case 'f':
-                    for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array1[i] = 0; sample_array2[i] = 0; angle_array[i] = 0;}
+                    for(int i = 0; i < len; i++) {sample_array0[i] = 0; sample_array1[i] = 0; angle_array[i] = 0;}
                     pc.printf("Arrays cleared\r\n");
                     break;
+                case 'R':
                 case 'r':
                     pc.printf("Quadrature: %i%i \r\n", (HW_GPIO_PDIR_RD(HW_PORTC)>>1)&0x01, HW_GPIO_PDIR_RD(HW_PORTC)&0x01);
+                case 'B':
+                case 'b':
+                    pdb_start();
+                    break;
+                case 'K':
+                case 'k':
+                    pdb_stop();
+                    break;
             }
         }
     }
-}
-
-void timed_sampling() {
-    
-    // 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;
-    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;        
-    }
 }
\ No newline at end of file