DCS_TEAM / Mbed 2 deprecated Chemical_Sensor_DMA

Dependencies:   mbed

Dependents:   DCS_FINAL_CODE

Fork of Chemical_Sensor_DMA by Jared Baxter

Files at this revision

API Documentation at this revision

Comitter:
baxterja
Date:
Thu Oct 29 17:15:20 2015 +0000
Parent:
1:f0a5690db73f
Child:
3:a85b742be262
Commit message:
Accessible Sample folder

Changed in this revision

AngleEncoder.cpp Show annotated file Show diff for this revision Revisions of this file
AngleEncoder.h Show annotated file Show diff for this revision Revisions of this file
MotorControl.cpp Show annotated file Show diff for this revision Revisions of this file
MotorControl.h Show annotated file Show diff for this revision Revisions of this file
Sample.lib Show diff for this revision Revisions of this file
Sample/adc.cpp Show annotated file Show diff for this revision Revisions of this file
Sample/adc.h Show annotated file Show diff for this revision Revisions of this file
Sample/dma.cpp Show annotated file Show diff for this revision Revisions of this file
Sample/dma.h Show annotated file Show diff for this revision Revisions of this file
Sample/pdb.cpp Show annotated file Show diff for this revision Revisions of this file
Sample/pdb.h Show annotated file Show diff for this revision Revisions of this file
Sample/quad.cpp Show annotated file Show diff for this revision Revisions of this file
Sample/quad.h Show annotated file Show diff for this revision Revisions of this file
SignalProcessing.cpp Show annotated file Show diff for this revision Revisions of this file
SignalProcessing.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
pause.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AngleEncoder.cpp	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,126 @@
+#include "AngleEncoder.h"
+#include "mbed.h"
+ 
+AngleEncoder::AngleEncoder(PinName mosi, PinName miso, PinName sclk, PinName cs, int bits, int mode, int hz) :  
+        _spi(mosi, miso, sclk), 
+        _cs(cs) {
+    // constructor
+    _cs = 1;
+    _spi.format(bits,mode);
+    _spi.frequency(hz);
+    
+    wait_ms(1000); // Angle encoder requires 0.1 seconds to boot up, so I gave it an entire second.
+    
+}
+
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
+ * 
+ * Sends a NOP command.  This is a read command.
+ *
+ * Returns the 8 bit data read from the encoder.  
+ *      0xA5 indicates communication is good, but no data to receive from encoder
+ *      
+ *      0x00 indicates that angle encoder probably wasn't read.  May need to 
+ *      increase SPI_DELAY
+ *  
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 
+int AngleEncoder::nop() { // this function takes about 90us to complete (tested using Timer library)
+    _cs = 0;
+    wait_us(SPI_DELAY);
+    int received = _spi.write(0x00);
+    wait_us(SPI_DELAY);
+    _cs = 1;
+    wait_us(SPI_DELAY);
+    return received;
+}
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
+ * 
+ * Sets the current position as the zero point from which angles are calculated
+ *  
+ * Returns true if successful, false otherwise
+ * 
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+bool AngleEncoder::set_zero() {
+    char received;
+    
+    // send "set_zero_point" command
+    _cs = 0;
+    wait_us(SPI_DELAY);
+    received = _spi.write(0x70); // send the command
+    wait_us(SPI_DELAY);
+    _cs = 1;
+    wait_us(SPI_DELAY);
+    
+    // send NOP until reset is confirmed 
+    while(received == 0xa5) received = nop();
+    
+    // 0x80 indicates that reset is confirmed
+    if(received == 0x80) return true;
+    else return false;
+}
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
+ * 
+ *  Sets the current position as the zero point from which angles are calculated
+ *  @param rotary_count  The address to the rotary_count variable, allowing this 
+ *                       function to reset both the absolute and relative angles.
+ *  
+ *  Returns true if successful, false otherwise
+ *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+bool AngleEncoder::set_zero(int* rotary_count) {
+    
+    *rotary_count = 0;  // reset relative counter
+    return set_zero();  // reset absolute counter
+}
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
+ * 
+ * Reads the absolute angle from the angle encoder via SPI
+ * Returns the 12 bit angle (-2048, 2047)
+ *  
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+int AngleEncoder::absolute_angle() { // this function takes about 500us to complete (tested using Timer library)
+    char received;
+    uint16_t absolute_pos;
+    
+    // send read command
+    _cs = 0;
+    wait_us(SPI_DELAY);
+    received = _spi.write(0x10);
+    wait_us(SPI_DELAY);
+    _cs = 1;
+    
+    // wait for angle encoder to echo the command
+    wait_us(SPI_DELAY);
+    
+    // read until encoder sends 0x10
+    for(uint32_t i = 0; i < 100; i++)
+    {
+        received = nop();
+        if(received == 0x10) break;
+    }
+    
+    //while(received != 0x10) received = nop(); // read until 0x10 is received
+        
+    // the command should have been echoed back.  If so, then read the angle    
+    if(received == 0x10)
+    {
+        // receive the most significatn 4 bits of the 12 bit angle
+        absolute_pos = nop();  // read first 4 bits of absolute angle
+        absolute_pos <<= 8; // shift the 4 bits into the right spot
+        absolute_pos |= nop(); // receive the last 8 bits of absolute angle
+        
+        //make data symmetric around 0.
+        //if(absolute_pos > 2048) absolute_pos -= 4096;
+        //absolute_pos = ~absolute_pos + 1; // inverts the data
+        return absolute_pos;
+        }
+    else return 0x00ff0000; // this is just a random number outside of the range of the encoder
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AngleEncoder.h	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,21 @@
+#ifndef ANGLE_ENCODER_H
+#define ANGLE_ENCODER_H
+ 
+#include "mbed.h"
+ 
+#define SPI_DELAY 4 // must be 4 or greater, otherwise the angle encoder can't keep up with the communication
+ 
+class AngleEncoder {
+public:
+    AngleEncoder(PinName mosi, PinName miso, PinName sclk, PinName cs, int bits, int mode, int hz);
+    int nop();
+    int absolute_angle();
+    bool set_zero();
+    bool set_zero(int*);
+    
+private:  
+    SPI _spi;
+    DigitalOut _cs;
+};
+ 
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MotorControl.cpp	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,76 @@
+#include "MotorControl.h"
+#include "mbed.h"
+
+// constructor 
+MotorControl::MotorControl(PinName cw, PinName ccw, int period, int safetyPeriod) :  
+        _cw(cw),
+        _ccw(ccw) {
+    
+    // turn motor off
+    _cw = 0;
+    _ccw = 0;
+    
+    _cw.period_us(period);
+    _ccw.period_us(period);
+    
+    _period = period;
+    _safetyPeriod = safetyPeriod;
+}
+
+void MotorControl::off() {
+    _cw = 0;
+    _ccw = 0;    
+    pause_us(_safetyPeriod);
+}
+
+void MotorControl::clockwise(float dutyCycle) {
+    _cw = dutyCycle;
+}
+
+void MotorControl::releaseMallet() {
+    // make sure motor is off
+    off();
+    
+    // pulse clockwise to release mallet
+    _cw = 1;
+    pause_ms(10);
+    _cw = 0;
+    pause_ms(75);
+    
+    // pulse counter-clockwise to stop snail cam
+    _ccw = 0.7;
+    pause_ms(8);
+    _ccw = 0;
+    
+    // make sure motor is off
+    off();
+}
+
+void MotorControl::reset() {
+    
+}
+
+void MotorControl::hardReset(int duration) {
+    // make sure motor is off
+    off();
+    
+    // long pulse clockwise to reset mallet
+    _cw = 1;
+    pause_ms(200);
+    _cw = 0;
+    
+    // short pulse counter-clockwise to stop snail cam
+    _ccw = 1;
+    pause_ms(10);
+    _ccw = 0;
+    
+    
+    wait_ms(duration);
+    // short pulse counter-clockwise to set snail cam in groove on mallet
+    _ccw = 1;
+    pause_ms(3);
+    _ccw = 0;
+    
+    // make sure motor is off
+    off();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MotorControl.h	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,26 @@
+#ifndef MOTOR_CONTROL_H
+#define MOTOR_CONTROL_H
+ 
+#include "mbed.h"
+#include "pause.cpp"
+ 
+class MotorControl {
+public:
+    MotorControl(PinName cw, PinName ccw, int period, int safetyPeriod);
+    
+    void off(); // make sure the motor is off
+    void clockwise(float);
+    void releaseMallet(); // release mallet using hard coded timing
+    void reset(); // reset mallet using feedback (NEEDS TO BE IMPLEMENTED)
+    
+    // what we threw together just for testing
+    void hardReset(int);         // reset mallet using hard coded timing
+    
+private:  
+    PwmOut _cw;    // clock wise rotation when _cw = 1
+    PwmOut _ccw;   // counter-clock wise rotation when _ccw = 1
+    int _period;       // the period of the motor switching cycle
+    int _safetyPeriod; // dead time between transition from cw to ccw (and visa versa)
+};
+ 
+#endif
\ No newline at end of file
--- a/Sample.lib	Thu Oct 22 17:10:31 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/Impact-Echo/code/Sample/#9c02c19afd37
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sample/adc.cpp	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,221 @@
+#include "adc.h"
+
+/*
+TODO:   calibration is done, i think
+        change clock speed and hw averaging
+*/
+
+DigitalOut green(LED_GREEN);
+DigitalOut red(LED_RED);
+
+
+
+/* The ADCs are setup so that ADC0 and ADC1 are triggered by the PDB.
+ * When the conversions are complete, ADC0 and ADC1 then trigger DMA0
+ * and DMA1, respectively. ADC0 is using channel B and ADC1 is uing
+ * channel A.  If they are on the same channel, then weird things 
+ * happen.  I think they interfere with each other so they have to be
+ * on separate channels if they're going to be triggered at the same 
+ * time by the PDB.                                                */
+void adc_init()
+{
+    // red, indicating not ready
+    red = 0;
+    green = 1;
+    
+    
+    // Turn on the ADC0 and ADC1 clocks
+    SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;
+    SIM_SCGC3 |= SIM_SCGC3_ADC1_MASK;
+    
+    // Set ADC hardware trigger to PDB0
+    SIM_SOPT7 = SIM_SOPT7_ADC0TRGSEL(0); // Select triggering by PDB and select pre-trigger A
+    SIM_SOPT7 = SIM_SOPT7_ADC1TRGSEL(0); // Select triggering by PDB and select pre-trigger A
+    
+    // calibrate the ADC
+    __disable_irq();
+    if(adc_cal()) {red = 0; green = 0;} // if calibration fails, display yellow
+    __enable_irq();
+    
+    
+    // Setup Configuration Register 1 
+    ADC0_CFG1 = 0; // clear register
+    ADC0_CFG1 |= ADC_CFG1_ADICLK(0);    // select bus clock
+    ADC0_CFG1 |= ADC_CFG1_MODE(3);      // select 16-bit 2's complement output
+    ADC0_CFG1 |= ADC_CFG1_ADIV(0);      // select short sample time
+    ADC0_CFG1 &= ~ADC_CFG1_ADLSMP_MASK; // select short sample time
+    ADC0_CFG1 &= ~ADC_CFG1_ADLPC_MASK;  // select normal power configuration
+    ADC1_CFG1 = 0; // clear register
+    ADC1_CFG1 |= ADC_CFG1_ADICLK(0);    // select bus clock
+    ADC1_CFG1 |= ADC_CFG1_MODE(3);      // select 16-bit 2's complement output
+    ADC1_CFG1 |= ADC_CFG1_ADIV(0);      // select short sample time
+    ADC1_CFG1 &= ~ADC_CFG1_ADLSMP_MASK; // select short sample time
+    ADC1_CFG1 &= ~ADC_CFG1_ADLPC_MASK;  // select normal power configuration
+    
+    // Setup Configuration Register 2 
+    ADC0_CFG2 = 0; // clear register
+    //ADC0_CFG2 |= ADC_CFG2_ADHSC_MASK ;  // select high-speed conversion
+    ADC0_CFG2 &= ~ADC_CFG2_MUXSEL_MASK; // select a channels    
+    ADC1_CFG2 = 0; // clear register
+    //ADC1_CFG2 |= ADC_CFG2_ADHSC_MASK ;  // select high-speed conversion
+    ADC1_CFG2 &= ~ADC_CFG2_MUXSEL_MASK; // select a channels    
+    
+    // Setup Status and Control Register 2 
+    ADC0_SC2 = 0;                    // clear register
+    ADC0_SC2 |= ADC_SC2_REFSEL(0);   // select external voltage reference
+    ADC0_SC2 |= ADC_SC2_DMAEN_MASK;  // enable DMA
+    ADC0_SC2 |= ADC_SC2_ADTRG_MASK; // select hardware trigger
+    ADC1_SC2 = 0;                    // clear register
+    ADC1_SC2 |= ADC_SC2_REFSEL(0);   // select external voltage reference
+    ADC1_SC2 |= ADC_SC2_DMAEN_MASK;  // enable DMA
+    ADC1_SC2 |= ADC_SC2_ADTRG_MASK; // select hardware trigger
+    
+    // Setup Status and Control Register 3 now that calibration is complete
+    ADC0_SC3 = 0; // Hardware Average set to 4 samples averaged
+                  // Hardware Average Disabled
+                  // select single conversion mode
+    ADC1_SC3 = 0; // Hardware Average set to 4 samples averaged
+                  // Hardware Average Disabled
+                  // select single conversion mode
+    
+    // Setup Status and Control Register 1A 
+    ADC0_SC1B = 0; // clear register
+    ADC0_SC1B &= ~ADC_SC1_DIFF_MASK; // select single-ended mode
+    ADC0_SC1B |= ADC_SC1_AIEN_MASK;  // enable interrupt (for debugging)
+    ADC0_SC1B |= ADC_SC1_ADCH(13);   // select channel 13
+    ADC1_SC1A = 0; // clear register
+    ADC1_SC1A &= ~ADC_SC1_DIFF_MASK; // select single-ended mode
+    ADC1_SC1A |= ADC_SC1_AIEN_MASK;  // enable interrupt (for debugging)
+    ADC1_SC1A |= ADC_SC1_ADCH(14);   // select channel 14
+    
+    
+    // Check if ADC is finished initializing  TODO:  This part doesn't seem right, but I did it according to 871
+    while( (ADC0_SC1B&ADC_SC1_COCO_MASK)) {}
+    int gain = ADC0_RA; // read the register to clear SC1A[COCO]
+    while( (ADC1_SC1A&ADC_SC1_COCO_MASK)) {}
+    gain = ADC1_RA; // read the register to clear SC1A[COCO]
+    
+    red = 1;
+    green = 1;
+}
+
+
+/* adc_cal
+ * Calibrates the adc
+ * Returns 0 if successful calibration
+ * Returns 1 otherwise
+ * */
+int adc_cal(void)
+{
+    ADC0_CFG1 |= (ADC_CFG1_MODE(3)  | // 16 bits mode
+                  ADC_CFG1_ADICLK(1)| // Input Bus Clock divided by 2 (20-25 MHz out of reset (FEI mode) / 2)
+                  ADC_CFG1_ADIV(2)) ; // Clock divide by 4 (2.5-3 MHz)
+    
+    ADC0_SC3 |= ADC_SC3_AVGE_MASK |   // Enable HW average
+                ADC_SC3_AVGS(3)   |   // Set HW average of 32 samples
+                ADC_SC3_CAL_MASK;     // Start calibration process
+    
+    while(ADC0_SC3 & ADC_SC3_CAL_MASK); // Wait for calibration to end
+    
+    if(ADC0_SC3 & ADC_SC3_CALF_MASK) return 1;  // Check for successful calibration
+    
+    uint16_t calib = 0; // calibration variable 
+    calib += ADC0->CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
+    calib /= 2;
+    calib |= 0x8000;    // Set MSB 
+    ADC0_PG = calib;
+    calib = 0;
+    calib += ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
+    calib /= 2;
+    calib |= 0x8000;    // Set MSB
+    ADC0_MG = calib;
+    
+    return 0;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+/* * * * * * * * * * * * * * For Debugging Purposes * * * * * * * * * * * * * * * * * * * * */
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
+ * DEBUG:  This is supposed to put the ADC in continuous     *
+ *         mode so it samples without the PCB.  But for      *
+ *         some reason it isn't working.  I haven't deleted  *
+ *         it just in case it is needed for debug purposes   *
+ *         in the future.                                    *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+void adc_start() {
+    // reset DMA
+    dma_reset();
+    
+    // set ADC to continuous mode
+    ADC0_SC3 |= ADC_SC3_ADCO_MASK;
+    ADC1_SC3 |= ADC_SC3_ADCO_MASK;
+    
+    // set ADC to software trigger
+    ADC0_SC2 &= ~ADC_SC2_ADTRG_MASK;
+    ADC1_SC2 &= ~ADC_SC2_ADTRG_MASK;
+    
+    // start ADC conversion (SW trigger)
+    ADC0_SC1B |= ADC_SC1_ADCH(13);   // write to SC1A causing a trigger
+    ADC1_SC1A |= ADC_SC1_ADCH(14);   // write to SC1A causing a trigger
+}
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
+ * DEBUG:  This is supposed to revert back from adc_start()  *
+ *         but because adc_start() isn't working, this       *
+ *         function is good for nothing.  I held on to       *
+ *         it just in case it is needed for debug purposes   *
+ *         in the future.                                    *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+void adc_stop() {
+    // set ADC to hardware trigger
+    ADC0_SC2 |= ADC_SC2_ADTRG_MASK;
+    ADC1_SC2 |= ADC_SC2_ADTRG_MASK;
+    
+    // set to single conversion mode effectively stopping the ADC unless a timer triggers the ADC
+    ADC0_SC3 &= ~ADC_SC3_ADCO_MASK; 
+    ADC1_SC3 &= ~ADC_SC3_ADCO_MASK; 
+}
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
+ * DEBUG:  This is supposed to trigger a software conversion *
+ *         and take a single sample.  However, it only       *
+ *         worked for ADC1 for some reason.  It is here for  *
+ *         possible debug purposes in the future.            *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+void adc_single_sample() {
+    ADC0_SC3 &= ~ADC_SC3_ADCO_MASK;  // single conversion mode
+    ADC1_SC3 &= ~ADC_SC3_ADCO_MASK;  // single conversion mode
+    ADC0_SC2 &= ~ADC_SC2_ADTRG_MASK; // set ADC to software trigger
+    ADC1_SC2 &= ~ADC_SC2_ADTRG_MASK; // set ADC to software trigger
+    ADC0_SC1B |= ADC_SC1_ADCH(13);   // write to SC1B causing a trigger
+    ADC1_SC1A |= ADC_SC1_ADCH(14);   // write to SC1A causing a trigger
+    
+    // Set back to hardware trigger
+    ADC0_SC2 |= ADC_SC2_ADTRG_MASK; // set ADC to software trigger
+    ADC1_SC2 |= ADC_SC2_ADTRG_MASK; // set ADC to software trigger
+}
+
+
+void adc_print_registers() {
+    Serial debug(USBTX,USBRX);
+    debug.printf("ADC0_SC1a: %08x\r\n",ADC0_SC1A);  //(0x0000004d)
+    debug.printf("ADC0_SC1b: %08x\r\n",ADC0_SC1B);  //(0x0000001f)
+    debug.printf("ADC0_CFG1: %08x\r\n",ADC0_CFG1);  //(0x0000000c)
+    debug.printf("ADC0_CFG2: %08x\r\n",ADC0_CFG2);  //(0x00000004)
+    debug.printf("ADC0_RA:   %08x\r\n",ADC0_RA);    //(0x00000000)
+    debug.printf("ADC0_RB:   %08x\r\n",ADC0_RB);    //(0x00000000)
+    debug.printf("ADC0_SC2:  %08x\r\n",ADC0_SC2);   //(0x00000044)
+    debug.printf("ADC0_SC3:  %08x\r\n\n",ADC0_SC3); //(0x00000000)
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sample/adc.h	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,18 @@
+#ifndef ADC_H_
+#define ADC_H_
+
+#include "mbed.h"
+#include "dma.h"
+
+
+/***
+ * Initializes the ADC to 16-bit single-ended, hardware triggered
+ * with DMA enabled.
+ **/
+void adc_init();
+int adc_cal(void);
+//void adc_single_sample();
+//void adc_start();
+//void adc_stop();
+
+#endif /* ADC_H_ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sample/dma.cpp	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,194 @@
+/**
+ *  Setup triggering for DMA2 and PortC
+ */
+#include "dma.h"
+
+DigitalOut toggle_dma0(LED_RED);
+DigitalOut toggle_dma1(LED_BLUE);
+DigitalOut toggle_dma2(LED_GREEN);
+Serial debug3(USBTX,USBRX);
+
+//#define TOTAL_SAMPLES 30000
+//#define FILENAME_SIZE 26 // this matches what is defined in DistanceFirmware's main.cpp
+int len = TOTAL_SAMPLES;
+uint16_t sample_array0[TOTAL_SAMPLES];
+uint16_t sample_array1[TOTAL_SAMPLES];
+uint16_t angle_array[TOTAL_SAMPLES+FILENAME_SIZE];
+bool dma_done = false;
+bool dma_half_done = false;
+
+
+
+/*  DMA0 and DMA1 are triggered by ADC0 and ADC1 (which are triggered
+ *  by the PDB).  However, DMA2 is triggered directly by the PDB.  This
+ *  is becuase DMA2 is reading FTM2, which cannot trigger the DMA.   */
+void dma_init()
+{
+    toggle_dma0 = 1;
+    toggle_dma1 = 1;
+    toggle_dma2 = 1;
+    // Enable clock for DMAMUX and DMA
+    SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK;
+    SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;  
+    SIM_SCGC6 |= SIM_SCGC6_FTM2_MASK; // make sure clock is enabled for FTM2  
+            
+    // Enable DMA channels and select MUX to the correct source (see page 95 of user manual
+    DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(40); // ADC0
+    DMAMUX_CHCFG1 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(41); // ADC1
+    DMAMUX_CHCFG2 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(48); // Set trigger source to PDB (Don't set DMA Trig Enable because that is for the PIT)
+    /* Source number    Source module
+           40                ADC0
+           41                ADC1
+           48                PDB
+    */
+    
+    
+    // Enable request signal for channel 0 
+    DMA_ERQ = DMA_ERQ_ERQ0_MASK | DMA_ERQ_ERQ1_MASK | DMA_ERQ_ERQ2_MASK;
+    
+    // select round-robin arbitration priority
+    DMA_CR |= DMA_CR_ERCA_MASK;
+    
+    // Set memory address for source and destination for DMA0, DMA1, and DMA2
+    DMA_TCD0_SADDR = (uint32_t) &ADC0_RB;
+    DMA_TCD0_DADDR = (uint32_t) sample_array0;
+    DMA_TCD1_SADDR = (uint32_t) &ADC1_RA;
+    DMA_TCD1_DADDR = (uint32_t) sample_array1;
+    DMA_TCD2_SADDR = (uint32_t) &FTM2_CNT;
+    DMA_TCD2_DADDR = (uint32_t) angle_array;
+    
+    // Set an offset for source and destination address
+    DMA_TCD0_SOFF = 0x00; // Source address offset of 2 bits per transaction
+    DMA_TCD0_DOFF = 0x02; // Destination address offset of 1 bit per transaction
+    DMA_TCD1_SOFF = 0x00; // Source address offset of 2 bits per transaction
+    DMA_TCD1_DOFF = 0x02; // Destination address offset of 1 bit per transaction
+    DMA_TCD2_SOFF = 0x00; // Source address offset of 2 bits per transaction
+    DMA_TCD2_DOFF = 0x02; // Destination address offset of 1 bit per transaction
+        
+    // Set source and destination data transfer size
+    DMA_TCD0_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1);
+    DMA_TCD1_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1);
+    DMA_TCD2_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1);
+        
+    // Number of bytes to be transfered in each service request of the channel
+    DMA_TCD0_NBYTES_MLNO = 0x02;
+    DMA_TCD1_NBYTES_MLNO = 0x02;
+    DMA_TCD2_NBYTES_MLNO = 0x02;
+        
+    // Current major iteration count
+    DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len);
+    DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len);
+    DMA_TCD1_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len);
+    DMA_TCD1_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len);
+    DMA_TCD2_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len);
+    DMA_TCD2_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len);
+    
+    // Adjustment value used to restore the source and destiny address to the initial value
+    // After reading 'len' number of times, the DMA goes back to the beginning by subtracting len*2 from the address (going back to the original address)
+    
+    DMA_TCD0_SLAST = 0;      // Source address adjustment
+    DMA_TCD0_DLASTSGA = -len*2;  // Destination address adjustment
+    DMA_TCD1_SLAST = 0;      // Source address adjustment
+    DMA_TCD1_DLASTSGA = -len*2;  // Destination address adjustment
+    DMA_TCD2_SLAST = 0;      // Source address adjustment
+    DMA_TCD2_DLASTSGA = -len*2;  // Destination address adjustment
+    
+    // Setup control and status register
+    DMA_TCD0_CSR = 0;
+    DMA_TCD1_CSR = 0;
+    DMA_TCD2_CSR = 0;
+    
+    // enable interrupt call at end of major loop
+    DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK | DMA_CSR_INTHALF_MASK;
+    
+    // add interrupt handlers to interrupt vector table
+    NVIC_SetVector(DMA0_IRQn, (uint32_t)&DMA_IRQHandler);
+    
+    //enable interrupts
+    NVIC_EnableIRQ(DMA0_IRQn);
+    
+    // dma_init takes 4.09us to run.
+}
+
+void dma_reset() {
+    dma_done = false;
+    dma_half_done = false;
+    
+    // clear all DMA interrupts
+    DMA_CINT = DMA_CINT_CAIR_MASK;
+    
+    //enable interrupts
+    NVIC_EnableIRQ(DMA0_IRQn);
+}
+
+/* The only DMA interrupt is from DMA0.  The interrupts from DMA1 and DMA2
+ * are turned off because they all trigger at the same time.  Actually
+ * DMA2 triggers just before DMA0 and DMA1, but it's a negligible amount 
+ * of time.  By the time the PDB is turned off, the ADCs will have already
+ * been triggered.  */
+void DMA_IRQHandler() {
+    
+    DMA_CINT |= DMA_CINT_CINT(0); // clear interrupt flag
+    //toggle_dma0 = 0;
+    //debug3.printf("DMA ");
+    if(!dma_half_done) {
+        dma_half_done = true;
+        //debug3.printf("half done\r\n");
+        return;
+    }
+    //PDB0_SC &= ~PDB_SC_PDBEN_MASK;  // disable PDB
+    //NVIC_DisableIRQ(DMA0_IRQn); // disable interrupt
+    dma_done = true;
+    //debug3.printf("done\r\n");
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/* * * * * * * * * * * * * * For Debugging Purposes * * * * * * * * * * * * * * * * * * * * */
+
+
+void dma_print_registers() {
+    
+    debug3.printf("SADDR0: 0x%08x\r\n",DMA_TCD0_SADDR);
+    debug3.printf("DADDR0: 0x%08x\r\n",DMA_TCD0_DADDR);
+    debug3.printf("SADDR1: 0x%08x\r\n",DMA_TCD1_SADDR);
+    debug3.printf("DADDR1: 0x%08x\r\n",DMA_TCD1_DADDR);
+    debug3.printf("SADDR2: 0x%08x\r\n",DMA_TCD2_SADDR);
+    debug3.printf("DADDR2: 0x%08x\r\n",DMA_TCD2_DADDR);
+    
+    debug3.printf("CITER0: 0x%08x\r\n",DMA_TCD0_CITER_ELINKNO);
+    debug3.printf("BITER0: 0x%08x\r\n",DMA_TCD0_BITER_ELINKNO);
+    debug3.printf("CITER1: 0x%08x\r\n",DMA_TCD1_CITER_ELINKNO);
+    debug3.printf("BITER1: 0x%08x\r\n",DMA_TCD1_BITER_ELINKNO);
+    debug3.printf("CITER2: 0x%08x\r\n",DMA_TCD2_CITER_ELINKNO);
+    debug3.printf("BITER2: 0x%08x\r\n",DMA_TCD2_BITER_ELINKNO);
+    
+    
+    debug3.printf("DMA_CR: %08x\r\n", DMA_CR);
+    debug3.printf("DMA_ES: %08x\r\n", DMA_ES);
+    debug3.printf("DMA_ERQ: %08x\r\n", DMA_ERQ);
+    debug3.printf("DMA_EEI: %08x\r\n", DMA_EEI);
+    debug3.printf("DMA_CEEI: %02x\r\n", DMA_CEEI);
+    debug3.printf("DMA_SEEI: %02x\r\n", DMA_SEEI);
+    debug3.printf("DMA_CERQ: %02x\r\n", DMA_CERQ);
+    debug3.printf("DMA_SERQ: %02x\r\n", DMA_SERQ);
+    debug3.printf("DMA_CDNE: %02x\r\n", DMA_CDNE);
+    debug3.printf("DMA_SSRT: %02x\r\n", DMA_SSRT);
+    debug3.printf("DMA_CERR: %02x\r\n", DMA_CERR);
+    debug3.printf("DMA_CINT: %02x\r\n", DMA_CINT);
+    debug3.printf("DMA_INT: %08x\r\n", DMA_INT);
+    debug3.printf("DMA_ERR: %08x\r\n", DMA_ERR);
+    debug3.printf("DMA_HRS: %08x\r\n", DMA_HRS);
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sample/dma.h	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,20 @@
+#ifndef DMA_H_
+#define DMA_H_
+
+#include "mbed.h"
+
+#define FILENAME_SIZE 26
+#define TOTAL_SAMPLES 18980 // a multiple of 1460 (tcp payload length within a packet)
+extern uint16_t sample_array0[TOTAL_SAMPLES];
+extern uint16_t sample_array1[TOTAL_SAMPLES];
+extern uint16_t angle_array[TOTAL_SAMPLES+FILENAME_SIZE];
+extern bool dma_done;
+extern bool dma_half_done;
+
+void dma_reset();    
+void dma_init();
+void dma_enable_interrupt();
+void DMA_IRQHandler();
+void dma_print_registers();
+
+#endif /* DMA_H_ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sample/pdb.cpp	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,132 @@
+#include "pdb.h"
+#include "dma.h"
+
+
+DigitalOut toggle_pdb(PTB23);
+
+
+/*  The PDB is setup to run continuous (when enabled) so it will
+ *  periodically trigger the ADCs to sample and trigger DMA2 to 
+ *  save the quadrature angle.  The PDB can be started and stopped. */
+void pdb_init() {
+    
+    // initialize the Programmable Delay Block
+    
+    // turn on the clock to the PDB
+    SIM->SCGC6 |= SIM_SCGC6_PDB_MASK;
+    
+    // set ADC trigger to PDB0
+    SIM_SOPT7 = SIM_SOPT7_ADC0TRGSEL(0); 
+    
+    // input frequency is 6 Mhz?
+    // Configure the Peripheral Delay Block (PDB):
+    //#define SLOW
+    #ifdef SLOW
+    PDB0_IDLY = 0;   // need to trigger interrupt every counter reset which happens when modulus reached
+    PDB0_MOD = 0xffff;//0x257; // period of timer set to 10us
+    PDB0_CH0DLY0 = 0;
+    PDB0_CH0DLY1 = 0;
+    PDB0_CH1DLY0 = 0;
+    PDB0_CH1DLY1 = 0;
+    PDB0_CH0C1 = PDB_C1_EN(2) | PDB_C1_TOS(2); // channel 0 pretrigger 0 and 1 enabled and delayed
+    PDB0_CH1C1 = PDB_C1_EN(1) | PDB_C1_TOS(1); // channel 1 pretrigger 0 and 1 enabled and delayed
+    
+    // Setup Staus and Control Register
+    PDB0_SC = 0; // clear register
+    PDB0_SC = PDB_SC_DMAEN_MASK    // Enable DMA
+             | PDB_SC_PRESCALER(5) // Slow down the period of the PDB for testing
+             | PDB_SC_TRGSEL(0xf)  // Trigger source is Software Trigger to be invoked in this file
+             | PDB_SC_PDBEN_MASK   // PDB enabled
+             | PDB_SC_PDBIE_MASK   // PDB Interrupt Enable
+             | PDB_SC_MULT(1)      // Multiplication factor
+             | PDB_SC_CONT_MASK    // Contintuous, rather than one-shot, mode
+             | PDB_SC_LDOK_MASK;   // Need to ok the loading or it will not load certain regsiters!
+    #else
+    PDB0_IDLY = 0;   // need to trigger interrupt every counter reset which happens when modulus reached
+    PDB0_MOD = 0x257; // period of timer set to 10us
+    PDB0_CH0DLY0 = 0;
+    PDB0_CH0DLY1 = 0;
+    PDB0_CH1DLY0 = 0;
+    PDB0_CH1DLY1 = 0;
+    PDB0_CH0C1 = PDB_C1_EN(2) | PDB_C1_TOS(2); // channel 0 pretrigger 0 and 1 enabled and delayed
+    PDB0_CH1C1 = PDB_C1_EN(1) | PDB_C1_TOS(1); // channel 1 pretrigger 0 and 1 enabled and delayed
+    
+    // Setup Staus and Control Register
+    PDB0_SC = 0; // clear register
+    PDB0_SC = PDB_SC_DMAEN_MASK    // Enable DMA
+             | PDB_SC_PRESCALER(0) // Slow down the period of the PDB for testing
+             | PDB_SC_TRGSEL(0xf)  // Trigger source is Software Trigger to be invoked in this file
+             | PDB_SC_PDBEN_MASK   // PDB enabled
+             | PDB_SC_PDBIE_MASK   // PDB Interrupt Enable
+             | PDB_SC_MULT(0)      // Multiplication factor
+             | PDB_SC_CONT_MASK    // Contintuous, rather than one-shot, mode
+             | PDB_SC_LDOK_MASK;   // Need to ok the loading or it will not load certain regsiters!
+    #endif
+    
+}
+
+void pdb_start() {
+    dma_reset();
+    PDB0_SC |= PDB_SC_PDBEN_MASK;  // PDB enabled
+    PDB0_SC |= PDB_SC_SWTRIG_MASK; // enable software trigger (start the PDB)
+}
+
+void pdb_stop() {
+    PDB0_SC &= ~PDB_SC_PDBEN_MASK;  // PDB disabled
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/* * * * * * * * * * * * * * For Debugging Purposes * * * * * * * * * * * * * * * * * * * * */
+
+// Enables the interrupt for PDB0, which toggles PTB23.
+// Scope PTB23 to verify the frequency.
+void pdb_enable_interrupt() {
+    PDB0_SC &= ~PDB_SC_DMAEN_MASK; // disable DMA, this stops DMA2 from functioning
+    NVIC_SetVector(PDB0_IRQn, (uint32_t)&PDB0_IRQHandler);
+    NVIC_EnableIRQ(PDB0_IRQn);
+}
+void pdb_disable_interrupt() {
+    NVIC_DisableIRQ(PDB0_IRQn); // disable interrupt
+    PDB0_SC |= PDB_SC_DMAEN_MASK; // enable DMA (this lets DMA2 function)
+}
+void PDB0_IRQHandler() {
+    toggle_pdb = !toggle_pdb;
+    PDB0_SC &= ~PDB_SC_PDBIF_MASK; // clear interrupt flag
+}
+
+void pdb_print_registers() {
+    Serial debug2(USBTX,USBRX);
+    debug2.printf("PDB0_SC:       %08x\r\n",PDB0_SC);
+    debug2.printf("PDB0_MOD:      %08x\r\n",PDB0_MOD);
+    debug2.printf("PDB0_CNT:      %08x\r\n",PDB0_CNT);
+    debug2.printf("PDB0_IDLY:     %08x\r\n",PDB0_IDLY);
+    debug2.printf("PDB0_CH0C1:    %08x\r\n",PDB0_CH0C1);
+    debug2.printf("PDB0_CH0S:     %08x\r\n",PDB0_CH0S);
+    debug2.printf("PDB0_CH0DLY0:  %08x\r\n",PDB0_CH0DLY0);
+    debug2.printf("PDB0_CH0DLY1:  %08x\r\n",PDB0_CH0DLY1);
+    debug2.printf("PDB0_CH1C1:    %08x\r\n",PDB0_CH1C1);
+    debug2.printf("PDB0_CH1S:     %08x\r\n",PDB0_CH1S);
+    debug2.printf("PDB0_CH1DLY0:  %08x\r\n",PDB0_CH1DLY0);
+    debug2.printf("PDB0_CH1DLY1:  %08x\r\n",PDB0_CH1DLY1);
+    debug2.printf("PDB0_DACINTC0: %08x\r\n",PDB0_DACINTC0);
+    debug2.printf("PDB0_DACINT0:  %08x\r\n",PDB0_DACINT0);
+    debug2.printf("PDB0_DACINTC1: %08x\r\n",PDB0_DACINTC1);
+    debug2.printf("PDB0_DACINT1:  %08x\r\n",PDB0_DACINT1);
+    debug2.printf("PDB0_POEN:     %08x\r\n",PDB0_POEN);
+    debug2.printf("PDB0_PO0DLY:   %08x\r\n",PDB0_PO0DLY);
+    debug2.printf("PDB0_PO1DLY:   %08x\r\n",PDB0_PO1DLY);
+    debug2.printf("PDB0_PO2DLY:   %08x\r\n\n",PDB0_PO2DLY);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sample/pdb.h	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,21 @@
+/*  This app note explains what needs to be set for a PDB, ADC, DMA setup:  http://cache.freescale.com/files/32bit/doc/app_note/AN4688.pdf
+ *                                                                          http://cache.freescale.com/files/microcontrollers/doc/app_note/AN3844.pdf
+ */
+
+#ifndef PDB_H_
+#define PDB_H_
+
+#include "mbed.h"
+
+void pdb_init();
+void pdb_start();
+void pdb_stop();
+
+
+/* * For Debugging Purposes * */
+void pdb_enable_interrupt();
+void pdb_disable_interrupt();
+void PDB0_IRQHandler();
+void pdb_print_registers();
+
+#endif /* PDB_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sample/quad.cpp	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,57 @@
+#include "quad.h"
+
+void quad_init() {
+    
+    // Enable clock for FTM2
+    SIM_SCGC3 |= SIM_SCGC3_FTM2_MASK; // there are two of them for some reason
+    SIM_SCGC6 |= SIM_SCGC6_FTM2_MASK; // 
+    
+    // Enable clock for PortB
+    SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;
+    
+    //enable the counter
+    FTM2_MODE |= FTM_MODE_WPDIS_MASK; // unlock write protection on certain FTM bits and registers
+    FTM2_MODE |= FTM_MODE_FTMEN_MASK; 
+    
+    //enable the counter to run in the BDM mode
+    FTM2_CONF |= FTM_CONF_BDMMODE(3);
+    
+    //load the Modulo register and counter initial value
+    FTM2_MOD = 4095;
+    FTM2_CNTIN = 0;
+    
+    //configuring FTM for quadrature mode
+    FTM2_QDCTRL = 0;
+    FTM2_QDCTRL |= FTM_QDCTRL_QUADEN_MASK; // enable Quadrature decoding
+    FTM2_QDCTRL &= ~FTM_QDCTRL_QUADMODE_MASK; // select phase A and phase B decoding mode (this is what the angle encoder uses)
+    FTM2_QDCTRL |= FTM_QDCTRL_PHAPOL_MASK; // change direction of counting by changing the phase of input A (for some reason QUADIR wouldn't work)
+    
+    
+    // start the timer clock, source is the external clock
+    FTM2_SC |= FTM_SC_CLKS(3);
+    
+    //configuring the input pins:
+    PORTB_PCR18 = PORT_PCR_MUX(6); // FTM2_QD_PHA  (PTB18) ALT6
+    PORTB_PCR19 = PORT_PCR_MUX(6); // FTM2_QD_PHB  (PTB19) ALT6
+    
+    // Enable write protection
+    FTM2_MODE |= FTM_MODE_WPDIS_MASK; // Lock write protection on certain FTM bits and registers
+    
+}   
+ 
+int32_t quad_read() {
+    int angle = FTM2_CNT;
+    if(angle > 2048) angle -= 4096;
+    return angle;  // this is the register with the counter in it.
+}
+
+void quad_update(int value) {
+    if(value < 0) value = value + 4096; // convert back to raw form (both absolute angle and relative angle are actually integers between 0-4095)
+    FTM2_CNTIN = value; // change reset value to desired value
+    FTM2_CNT = 0; // writing anything to FTM_CNT loads FTM2_CNTIN into FTM_CNT;
+    FTM2_CNTIN = 0; // change reset value back to 0
+}
+
+void quad_invert(){
+    FTM2_QDCTRL &= ~FTM_QDCTRL_PHAPOL_MASK; // change direction of counting by changing the phase of input A
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sample/quad.h	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,17 @@
+/*
+Add this to the Angle Encoder class later
+
+http://cache.freescale.com/files/32bit/doc/app_note/AN4381.pdf
+*/
+
+#ifndef QUAD_H_
+#define QUAD_H_
+
+#include "mbed.h"
+
+void quad_update(int);    
+void quad_init();
+int quad_read();
+void quad_invert();
+
+#endif /* DMA_H_ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SignalProcessing.cpp	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,23 @@
+
+
+#define pre_compute_length 4000
+#define DMA_PERIOD .00001
+#define DMA_FREQUENCY 100000
+#define CARRIERFREQUENCY 10000
+float i_mod_pre[pre_compute_length];
+float q_mod_pre[pre_compute_length];
+int out_val_pre[pre_compute_length]; 
+
+float twopi = 3.14159265359 * 2;
+
+void pre_compute_tables() {
+  /*
+  // This function will precompute the cos and sin tables used in the rest of the program
+  for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){
+    out_val_pre[precompute_counter] = (int) (cos(twopi * CarrierFrequency * DMA_PERIOD * precompute_counter) * 4965.0 + 49650.0);
+    i_mod_pre[precompute_counter] = (cos(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter));
+    q_mod_pre[precompute_counter] = (-sin(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter));
+    
+  }
+  */
+}
\ No newline at end of file
--- a/main.cpp	Thu Oct 22 17:10:31 2015 +0000
+++ b/main.cpp	Thu Oct 29 17:15:20 2015 +0000
@@ -1,2516 +1,318 @@
-
-// TODO: 
-#include "mbed.h"
-#include <string>
-
-#include <stdio.h>
-#include "PeripheralNames.h"
-#include "PeripheralPins.h"
-#include "fsl_adc_hal.h"
-#include "fsl_clock_manager.h"
-#include "fsl_dspi_hal.h"
-
-
-
-Serial pc(USBTX, USBRX);
-
-/*
-#if DEBUG
-#define DEBUG_PRINT(x) pc.printf(x)
-#else
-#define DEBUG_PRINT(x) do {} while(0)
-#endif
-*/
-DigitalOut led_red(LED_RED);
-DigitalOut led_green(LED_GREEN);
-DigitalOut led_blue(LED_BLUE);
-
-
-#define MAX_FADC 6000000
-
-#define TimerInterruptInMicroSeconds 25 // This is for 40 kHz
-
-
-#define CarrierFrequency 200
-
-#define ISR_counter_loop_reset 8 // 10 kHz sampling of the signal
-
-int ISR_counter = 1;
-
-#define MaxNumSamples 0  //set to 0 if want continuous sampling (i.e. run forever)
-#define NumSamplesAverageInADC 32 //hardware averaging default is 2. values 0..32. Higher values reduce noise and maximum sample rate
-#define NumADCbits 16
-
-int CyclesInState = 0;
-// While 32 is the minimum, it seems to take a few cycles to get things to work right. 32 + 18 + 18 + 18
-#define PreCalculateCycles 256
-#define NumCalculatedAverages 500
-#define PRINTTIMER 1
-
-int printCounter = 0;
-
-AnalogOut WaveOut(DAC0_OUT);
-AnalogIn WaveIn1(A0);
-AnalogIn WaveIn2(A2);
-
-
-double previousTime = 0;
-
-
-volatile unsigned long counter = 0; //keep track of how many times the timer interrupt executes
-
-
-volatile int phase_counter = 0;
-float twopi = 3.14159265359 * 2;
-
-#define pre_compute_length 4000
-float i_mod_pre[pre_compute_length];
-float q_mod_pre[pre_compute_length];
-int out_val_pre[pre_compute_length]; 
-
-float current_i_mod;
-float current_q_mod;
-
-
-#define lp_fir_length 2048
-
-
-float x_0_i[lp_fir_length];
-float x_0_q[lp_fir_length];
-float x_1_i[lp_fir_length];
-float x_1_q[lp_fir_length];
-
-float real_ip = 0;
-float real_ip_total = 0;
-float imag_ip = 0;
-float imag_ip_total = 0;
-float y_0_squared = 0;
-float y_1_squared = 0;
-
-int y_0, y_1;
-
-
-int max_0 = -1;
-int min_0 = 65536;
-int max_1 = -1;
-int min_1 = 65536;
-
-
-// Coefficients for FIR low-pass filter
-// Order: 511
-// Fs = 5000
-// Fpass = 5
-// Fstop = 25
-// Density Factor 20
-float lp_filter_coeff[lp_fir_length] = {
-2.25877153377331e-06,
-2.66244224670397e-07,
-2.81092420097884e-07,
-2.97200797125842e-07,
-3.13057205526705e-07,
-3.30279379977406e-07,
-3.47320034430519e-07,
-3.65790534295599e-07,
-3.84037888145959e-07,
-4.03659300931941e-07,
-4.22883429696171e-07,
-4.43414078442301e-07,
-4.63474354546465e-07,
-4.85127823306893e-07,
-5.06629365749489e-07,
-5.30402400375842e-07,
-5.53883059492054e-07,
-5.79062251172147e-07,
-6.00698053897355e-07,
-6.27541356389415e-07,
-6.53603188407001e-07,
-6.79457125689371e-07,
-7.06900952158772e-07,
-7.34607521511639e-07,
-7.63210937527310e-07,
-7.92457626845786e-07,
-8.22319204892483e-07,
-8.52904112434704e-07,
-8.83993876825403e-07,
-9.15965803598408e-07,
-9.48565090704124e-07,
-9.82217633251451e-07,
-1.01643933513151e-06,
-1.05143714735623e-06,
-1.08676147556840e-06,
-1.12300409054529e-06,
-1.16035624735089e-06,
-1.19822784284488e-06,
-1.23612003796729e-06,
-1.27597253424280e-06,
-1.31548405641171e-06,
-1.35656260640872e-06,
-1.39768396625082e-06,
-1.44009364782760e-06,
-1.48263736669695e-06,
-1.52645417739943e-06,
-1.57052770327227e-06,
-1.61590430260585e-06,
-1.66152581672090e-06,
-1.70832466623602e-06,
-1.75531183154730e-06,
-1.80354697649463e-06,
-1.85213180801677e-06,
-1.90190541457320e-06,
-1.95172125266670e-06,
-2.00274982053528e-06,
-2.05440229847222e-06,
-2.10671996122087e-06,
-2.15960953108923e-06,
-2.21344673621392e-06,
-2.26753452245018e-06,
-2.32270367024371e-06,
-2.37811232502338e-06,
-2.43461484557276e-06,
-2.49131860508909e-06,
-2.54904626588095e-06,
-2.60694079790385e-06,
-2.66588370269542e-06,
-2.72502556403521e-06,
-2.78515359568984e-06,
-2.84534746585168e-06,
-2.90651253940365e-06,
-2.96789707905998e-06,
-3.03026815024548e-06,
-3.09250439907414e-06,
-3.15586112065294e-06,
-3.21917116992942e-06,
-3.28329739015491e-06,
-3.34755725225876e-06,
-3.41254082601689e-06,
-3.47759844620301e-06,
-3.54333509849531e-06,
-3.60908996231663e-06,
-3.67551457730312e-06,
-3.74192777791326e-06,
-3.80892935787303e-06,
-3.87581985935145e-06,
-3.94325856674703e-06,
-4.01062965420015e-06,
-4.07851955438422e-06,
-4.14618562570372e-06,
-4.21424927012473e-06,
-4.28219961892930e-06,
-4.35043829085821e-06,
-4.41836938506806e-06,
-4.48672487737745e-06,
-4.55459997845769e-06,
-4.62281849455623e-06,
-4.69055023332375e-06,
-4.75852881449136e-06,
-4.82598777828551e-06,
-4.89357790491499e-06,
-4.96055708038026e-06,
-5.02759963577756e-06,
-5.09401623836012e-06,
-5.16043301207036e-06,
-5.22609616909151e-06,
-5.29163941340404e-06,
-5.35641256312241e-06,
-5.42102571618858e-06,
-5.48469577560834e-06,
-5.54811134917779e-06,
-5.61062044360571e-06,
-5.67265321372291e-06,
-5.73373682457175e-06,
-5.79431080305923e-06,
-5.85377619609459e-06,
-5.91266194714808e-06,
-5.97030085360536e-06,
-6.02728122072791e-06,
-6.08294572989760e-06,
-6.13786180337324e-06,
-6.19132752972891e-06,
-6.24392081949063e-06,
-6.29499279506742e-06,
-6.34512332808420e-06,
-6.39358822958236e-06,
-6.44096494373972e-06,
-6.48662130412141e-06,
-6.53110576502919e-06,
-6.57367526411895e-06,
-6.61504319391059e-06,
-6.65439635608727e-06,
-6.69234997696170e-06,
-6.72823177451458e-06,
-6.76256992228003e-06,
-6.79475276861382e-06,
-6.82527935353772e-06,
-6.85350874920331e-06,
-6.87995435672755e-06,
-6.90400455258515e-06,
-6.92617579276020e-06,
-6.94581178960161e-06,
-6.96341207782413e-06,
-6.97837546570650e-06,
-6.99121320837930e-06,
-7.00126691500350e-06,
-7.00904991575129e-06,
-7.01399146065940e-06,
-7.01649463335420e-06,
-7.01598906903671e-06,
-7.01299335632421e-06,
-7.00682651919766e-06,
-6.99805670472170e-06,
-6.98598387232265e-06,
-6.97115918378061e-06,
-6.95293670913433e-06,
-6.93184262641915e-06,
-6.90721796243191e-06,
-6.87956544369715e-06,
-6.84826575659092e-06,
-6.81383582649206e-06,
-6.77563271791333e-06,
-6.73414912642070e-06,
-6.68879222648781e-06,
-6.64004566255543e-06,
-6.58726001945506e-06,
-6.53097084557196e-06,
-6.47057633363911e-06,
-6.40649492549257e-06,
-6.33820922296577e-06,
-6.26611753900958e-06,
-6.18969752493548e-06,
-6.10938733149237e-06,
-6.02460876343180e-06,
-5.93581228618980e-06,
-5.84243268710826e-06,
-5.74494125723676e-06,
-5.64275663266799e-06,
-5.53633239754493e-06,
-5.42511044796520e-06,
-5.30955851436901e-06,
-5.18909082922882e-06,
-5.06417106483642e-06,
-4.93426738681860e-06,
-4.79981139928378e-06,
-4.66022392940390e-06,
-4.51602630845954e-06,
-4.36660747560401e-06,
-4.21247294402037e-06,
-4.05305110546832e-06,
-3.88878763289571e-06,
-3.71916450048738e-06,
-3.54462712478025e-06,
-3.36465155419938e-06,
-3.17967167761456e-06,
-2.98917613191769e-06,
-2.79361738719780e-06,
-2.59247412473194e-06,
-2.38618565979242e-06,
-2.17426059604335e-06,
-1.95714227397860e-06,
-1.73430496738356e-06,
-1.50620956228030e-06,
-1.27238963764761e-06,
-1.03324761527409e-06,
-7.88323211174950e-07,
-5.38057114445362e-07,
-2.81944291020029e-07,
-2.04836906979221e-08,
--2.46853106248978e-07,
--5.19577878779430e-07,
--7.98197252838720e-07,
--1.08221647391169e-06,
--1.37213415644316e-06,
--1.66747248399997e-06,
--1.96871121194834e-06,
--2.27536237370173e-06,
--2.58791653823994e-06,
--2.90589263830832e-06,
--3.22973451266772e-06,
--3.55896455485854e-06,
--3.89406743396649e-06,
--4.23452884634043e-06,
--4.58080862054126e-06,
--4.93243163040497e-06,
--5.28980705682264e-06,
--5.65249312334948e-06,
--6.02088801710077e-06,
--6.39451744851171e-06,
--6.77379446388097e-06,
--7.15823725534437e-06,
--7.54825910394332e-06,
--7.94336123069663e-06,
--8.34395800204422e-06,
--8.74955757971436e-06,
--9.16054779162674e-06,
--9.57642228943518e-06,
--9.99759144947471e-06,
--1.04235511544729e-05,
--1.08546628466816e-05,
--1.12904336671393e-05,
--1.17312462832730e-05,
--1.21765651542703e-05,
--1.26267966656990e-05,
--1.30813839859992e-05,
--1.35407068370928e-05,
--1.40042432894668e-05,
--1.44723391847100e-05,
--1.49444772391115e-05,
--1.54209897807815e-05,
--1.59013657273958e-05,
--1.63859206132667e-05,
--1.68741290759562e-05,
--1.73663104823494e-05,
--1.78619508850454e-05,
--1.83613352518811e-05,
--1.88639344535055e-05,
--1.93700580786767e-05,
--1.98791701832643e-05,
--2.03915446858796e-05,
--2.09066661112072e-05,
--2.14247833830322e-05,
--2.19453728209874e-05,
--2.24687067756074e-05,
--2.29942189701287e-05,
--2.35221919064648e-05,
--2.40520571098411e-05,
--2.45840821429991e-05,
--2.51176919541455e-05,
--2.56531458365802e-05,
--2.61898800092312e-05,
--2.67281307655500e-05,
--2.72673160896635e-05,
--2.78076813742880e-05,
--2.83486572194740e-05,
--2.88904561028569e-05,
--2.94324977036388e-05,
--2.99750102268205e-05,
--3.05173890710438e-05,
--3.10598620379927e-05,
--3.16018340271438e-05,
--3.21434903845723e-05,
--3.26842631134414e-05,
--3.32243219343559e-05,
--3.37630833740885e-05,
--3.43007203295382e-05,
--3.48366443059829e-05,
--3.53710230750429e-05,
--3.59032455798272e-05,
--3.64334836149489e-05,
--3.69611383814916e-05,
--3.74863645989061e-05,
--3.80085391108475e-05,
--3.85278288122663e-05,
--3.90436126582742e-05,
--3.95560344208977e-05,
--4.00644784803627e-05,
--4.05690860811782e-05,
--4.10692160068759e-05,
--4.15650349494434e-05,
--4.20558816044656e-05,
--4.25419079114848e-05,
--4.30224723840039e-05,
--4.34977023194285e-05,
--4.39669575807467e-05,
--4.44303536014418e-05,
--4.48872654011353e-05,
--4.53378004294169e-05,
--4.57813127757766e-05,
--4.62179102441466e-05,
--4.66469626401911e-05,
--4.70685624133195e-05,
--4.74820663998248e-05,
--4.78875779787031e-05,
--4.82844457389913e-05,
--4.86727623947596e-05,
--4.90518974627560e-05,
--4.94219173993094e-05,
--4.97821954537253e-05,
--5.01328087389260e-05,
--5.04731070890493e-05,
--5.08031767792133e-05,
--5.11223701960069e-05,
--5.14307728917283e-05,
--5.17277278570648e-05,
--5.20133133082317e-05,
--5.22868899566335e-05,
--5.25485330801741e-05,
--5.27975890619123e-05,
--5.30341352214345e-05,
--5.32575308256433e-05,
--5.34678403499526e-05,
--5.36644291832927e-05,
--5.38473696441733e-05,
--5.40160121859755e-05,
--5.41704376199963e-05,
--5.43100072156885e-05,
--5.44347778383721e-05,
--5.45441409303223e-05,
--5.46381421707785e-05,
--5.47161726277983e-05,
--5.47782796545640e-05,
--5.48238615891289e-05,
--5.48529777470327e-05,
--5.48650134478435e-05,
--5.48600304498624e-05,
--5.48374279367193e-05,
--5.47972707257875e-05,
--5.47389540962183e-05,
--5.46625553812279e-05,
--5.45674740408066e-05,
--5.44537801178265e-05,
--5.43208932117335e-05,
--5.41688832854903e-05,
--5.39971664557209e-05,
--5.38058373779503e-05,
--5.35943020304215e-05,
--5.33626591300992e-05,
--5.31103372807938e-05,
--5.28374339049068e-05,
--5.25433860531435e-05,
--5.22282871255255e-05,
--5.18915929674119e-05,
--5.15334084359192e-05,
--5.11531883719458e-05,
--5.07510470767421e-05,
--5.03264565435712e-05,
--4.98795298499818e-05,
--4.94097492621224e-05,
--4.89172468415742e-05,
--4.84015062909820e-05,
--4.78626656356439e-05,
--4.73002336655060e-05,
--4.67143388964721e-05,
--4.61045141628936e-05,
--4.54709069617551e-05,
--4.48130436182427e-05,
--4.41310892863899e-05,
--4.34245803720504e-05,
--4.26937043233061e-05,
--4.19380012096953e-05,
--4.11576670903313e-05,
--4.03522641202012e-05,
--3.95219995716669e-05,
--3.86664427936628e-05,
--3.77858193439538e-05,
--3.68797197976284e-05,
--3.59483680766746e-05,
--3.49913775019667e-05,
--3.40089900319630e-05,
--3.30008251544588e-05,
--3.19671510883658e-05,
--3.09076019921684e-05,
--2.98224428705179e-05,
--2.87113445271020e-05,
--2.75745824745532e-05,
--2.64118441851114e-05,
--2.52234156518926e-05,
--2.40090034267166e-05,
--2.27689200068355e-05,
--2.15028786303040e-05,
--2.02112093356898e-05,
--1.88936525600629e-05,
--1.75505489714928e-05,
--1.61816522718723e-05,
--1.47873283970059e-05,
--1.33673529297333e-05,
--1.19221016313184e-05,
--1.04513793212540e-05,
--8.95557028181646e-06,
--7.43449717788015e-06,
--5.88858138346941e-06,
--4.31765074336527e-06,
--2.72214441335228e-06,
--1.10191546986540e-06,
-5.42576135735981e-07,
-2.21145327612140e-06,
-3.90424732482530e-06,
-5.62105051139952e-06,
-7.36137384262456e-06,
-9.12529459068137e-06,
-1.09123048193875e-05,
-1.27224474678529e-05,
-1.45552032584862e-05,
-1.64105928172265e-05,
-1.82880735840279e-05,
-2.01876489388426e-05,
-2.21087489487933e-05,
-2.40513485984833e-05,
-2.60148760416192e-05,
-2.79992736446810e-05,
-3.00039407582481e-05,
-3.20288053388297e-05,
-3.40732419683153e-05,
-3.61371619724216e-05,
-3.82199078901064e-05,
-4.03213747021350e-05,
-4.24408877592610e-05,
-4.45783101891342e-05,
-4.67329465682261e-05,
-4.89046419944162e-05,
-5.10926798053942e-05,
-5.32968764479913e-05,
-5.55164983346224e-05,
-5.77513323125343e-05,
-6.00006282654158e-05,
-6.22641611239781e-05,
-6.45411398811771e-05,
-6.68313202374356e-05,
-6.91339016622066e-05,
-7.14486074938142e-05,
-7.37746190907099e-05,
-7.61116330506792e-05,
-7.84588166968576e-05,
-8.08158423081462e-05,
-8.31818494755366e-05,
-8.55564956212588e-05,
-8.79389017808088e-05,
-9.03286966625331e-05,
-9.27249827027176e-05,
-9.51273684116554e-05,
-9.75349342814310e-05,
-9.99472748819797e-05,
-0.000102363447520479,
-0.000104783014763168,
-0.000107205025896024,
-0.000109629029400713,
-0.000112054039942443,
-0.000114479594239356,
-0.000116904687981786,
-0.000119328839927821,
-0.000121751025536885,
-0.000124170741112682,
-0.000126586955396263,
-0.000128999137073690,
-0.000131406237902807,
-0.000133807710854935,
-0.000136202494636464,
-0.000138590020743407,
-0.000140969217664355,
-0.000143339493063055,
-0.000145699758496409,
-0.000148049419840621,
-0.000150387365819865,
-0.000152712978884051,
-0.000155025147047118,
-0.000157323233199747,
-0.000159606113351142,
-0.000161873135563338,
-0.000164123164418827,
-0.000166355539329688,
-0.000168569105181148,
-0.000170763192929278,
-0.000172936642681967,
-0.000175088769080921,
-0.000177218404031216,
-0.000179324852197408,
-0.000181406934825622,
-0.000183463950477444,
-0.000185494717916349,
-0.000187498516812910,
-0.000189474160690418,
-0.000191420933229610,
-0.000193337632742739,
-0.000195223538589502,
-0.000197077449370729,
-0.000198898632343484,
-0.000200685889922636,
-0.000202438477037224,
-0.000204155202780923,
-0.000205835317231766,
-0.000207477624265485,
-0.000209081375510424,
-0.000210645375770233,
-0.000212168874797709,
-0.000213650681871823,
-0.000215090046105722,
-0.000216485772585174,
-0.000217837122420945,
-0.000219142904089215,
-0.000220402374841213,
-0.000221614355063568,
-0.000222778105957421,
-0.000223892449138464,
-0.000224956657801864,
-0.000225969559300686,
-0.000226930435535397,
-0.000227838120949022,
-0.000228691904367779,
-0.000229490637305569,
-0.000230233614422397,
-0.000230919699867762,
-0.000231548200624833,
-0.000232117991545114,
-0.000232628392706581,
-0.000233078299941400,
-0.000233467042490252,
-0.000233793529306281,
-0.000234057114622203,
-0.000234256717572874,
-0.000234391707980239,
-0.000234461034429087,
-0.000234464075886551,
-0.000234399804148527,
-0.000234267616643166,
-0.000234066510095474,
-0.000233795906658708,
-0.000233454817877378,
-0.000233042694337553,
-0.000232558571806248,
-0.000232001923306693,
-0.000231371813425932,
-0.000230667742461277,
-0.000229888797324386,
-0.000229034509219026,
-0.000228103997682251,
-0.000227096814542036,
-0.000226012115545807,
-0.000224849486757965,
-0.000223608106379000,
-0.000222287594558100,
-0.000220887169687286,
-0.000219406480827707,
-0.000217844783243759,
-0.000216201753070026,
-0.000214476690763218,
-0.000212669306710065,
-0.000210778935093994,
-0.000208805327647997,
-0.000206747856525214,
-0.000204606309833789,
-0.000202380104774549,
-0.000200069067435948,
-0.000197672653272001,
-0.000195190736945890,
-0.000192622814021790,
-0.000189968793862972,
-0.000187228227821168,
-0.000184401067781124,
-0.000181486906935768,
-0.000178485741364642,
-0.000175397211774938,
-0.000172221367164218,
-0.000168957890495671,
-0.000165606876476172,
-0.000162168061864114,
-0.000158641588326119,
-0.000155027243685390,
-0.000151325219415721,
-0.000147535354241219,
-0.000143657889880845,
-0.000139692722584275,
-0.000135640137861556,
-0.000131500087828142,
-0.000127272919603931,
-0.000122958632897122,
-0.000118557624222376,
-0.000114069956263803,
-0.000109496077969276,
-0.000104836112476280,
-0.000100090556969088,
-9.52595926807594e-05,
-9.03437806713531e-05,
-8.53433537117259e-05,
-8.02589335085685e-05,
-7.50908090508799e-05,
-6.98396606723608e-05,
-6.45058395701152e-05,
-5.90900818618602e-05,
-5.35927957604024e-05,
-4.80147832320940e-05,
-4.23565146651483e-05,
-3.66188412855488e-05,
-3.08022998861741e-05,
-2.49078076002350e-05,
-1.89359578000643e-05,
-1.28877281875480e-05,
-6.76377196319473e-06,
-5.65130739446817e-07,
--5.70747618704037e-06,
--1.20529518122427e-05,
--1.84705123312578e-05,
--2.49589991720766e-05,
--3.15175650599888e-05,
--3.81449885574943e-05,
--4.48403630718286e-05,
--5.16024050889186e-05,
--5.84301376752338e-05,
--6.53222224894314e-05,
--7.22776238052609e-05,
--7.92949306753459e-05,
--8.63730463095441e-05,
--9.35105054098079e-05,
--0.000100706144186103,
--0.000107958436359977,
--0.000115266152994485,
--0.000122627706760917,
--0.000130041811681510,
--0.000137506814203479,
--0.000145021368821121,
--0.000152583759222027,
--0.000160192579466020,
--0.000167846056910789,
--0.000175542718661153,
--0.000183280733901876,
--0.000191058573185037,
--0.000198874346379858,
--0.000206726455104848,
--0.000214612960310208,
--0.000222532206706049,
--0.000230482190676936,
--0.000238461200747511,
--0.000246467177406058,
--0.000254498351409696,
--0.000262552611408044,
--0.000270628125294601,
--0.000278722730787134,
--0.000286834539691710,
--0.000294961336084192,
--0.000303101180604228,
--0.000311251799327546,
--0.000319411202616472,
--0.000327577068996273,
--0.000335747350207106,
--0.000343919676673919,
--0.000352091955603402,
--0.000360261765094418,
--0.000368426959623216,
--0.000376585075147441,
--0.000384733917580314,
--0.000392870977630007,
--0.000400994015796423,
--0.000409100475389566,
--0.000417188075990567,
--0.000425254218742792,
--0.000433296579745569,
--0.000441312521641045,
--0.000449299675354794,
--0.000457255369089255,
--0.000465177193879328,
--0.000473062439153703,
--0.000480908661009415,
--0.000488713116827171,
--0.000496473321124369,
--0.000504186501659088,
--0.000511850143907964,
--0.000519461439038756,
--0.000527017845535591,
--0.000534516525078808,
--0.000541954904421434,
--0.000549330124139864,
--0.000556639582645205,
--0.000563880395792860,
--0.000571049939360574,
--0.000578145303770340,
--0.000585163848349161,
--0.000592102641307673,
--0.000598959022419751,
--0.000605730045666215,
--0.000612413030480144,
--0.000619005017567106,
--0.000625503315016224,
--0.000631904950355812,
--0.000638207216182248,
--0.000644407135922555,
--0.000650501991706910,
--0.000656488796703490,
--0.000662364832864586,
--0.000668127107213612,
--0.000673772895505234,
--0.000679299206549835,
--0.000684703312533797,
--0.000689982228373537,
--0.000695133224872846,
--0.000700153320679537,
--0.000705039793794998,
--0.000709789667273328,
--0.000714400230172114,
--0.000718868516431075,
--0.000723191819632401,
--0.000727367189884889,
--0.000731391938661357,
--0.000735263125592756,
--0.000738978079579033,
--0.000742533884189047,
--0.000745927883252895,
--0.000749157181924490,
--0.000752219147550121,
--0.000755110911309810,
--0.000757829866314622,
--0.000760373168287008,
--0.000762738239234387,
--0.000764922268811481,
--0.000766922708033022,
--0.000768736783543735,
--0.000770361976300053,
--0.000771795549766353,
--0.000773035026336787,
--0.000774077710368219,
--0.000774921156688016,
--0.000775562718410930,
--0.000775999996896670,
--0.000776230384120606,
--0.000776251530463252,
--0.000776060879297139,
--0.000775656128479859,
--0.000775034774979158,
--0.000774194563408219,
--0.000773133050832454,
--0.000771848039257578,
--0.000770337139062027,
--0.000768598212666572,
--0.000766628928149237,
--0.000764427212355194,
--0.000761990800345454,
--0.000759317676212684,
--0.000756405642021792,
--0.000753252754194147,
--0.000749856883052877,
--0.000746216149243278,
--0.000742328500323755,
--0.000738192130147058,
--0.000733805058805418,
--0.000729165555282147,
--0.000724271716466218,
--0.000719121892993695,
--0.000713714263134529,
--0.000708047251625198,
--0.000702119122894178,
--0.000695928386138248,
--0.000689473392683322,
--0.000682752736054815,
--0.000675764850643800,
--0.000668508423585954,
--0.000660981983005567,
--0.000653184299948689,
--0.000645113996101250,
--0.000636769940811193,
--0.000628150846892228,
--0.000619255679141116,
--0.000610083247987995,
--0.000600632615642164,
--0.000590902695998816,
--0.000580892646976487,
--0.000570601481559749,
--0.000560028464530771,
--0.000549172712705324,
--0.000538033593923370,
--0.000526610326399401,
--0.000514902384920966,
--0.000502909102003151,
--0.000490630054751344,
--0.000478064682216253,
--0.000465212674168866,
--0.000452073583482240,
--0.000438647204637503,
--0.000424933205749722,
--0.000410931495060687,
--0.000396641852357854,
--0.000382064302937101,
--0.000367198738062124,
--0.000352045297690400,
--0.000336603996500303,
--0.000320875086144786,
--0.000304858697984160,
--0.000288555200873863,
--0.000271964847867372,
--0.000255088130083280,
--0.000237925412360025,
--0.000220477306560259,
--0.000202744305292592,
--0.000184727135197687,
--0.000166426409808339,
--0.000147842979525945,
--0.000128977579742240,
--0.000109831180556723,
--9.04046425871818e-05,
--7.06990553697872e-05,
--5.07154045972636e-05,
--3.04549042775557e-05,
--9.91865979301451e-06,
-1.08919930592394e-05,
-3.19758215514218e-05,
-5.33313669696652e-05,
-7.49572729406508e-05,
-9.68519634422886e-05,
-0.000119013952872007,
-0.000141441540758312,
-0.000164133122498143,
-0.000187086875132261,
-0.000210301066410341,
-0.000233773755649941,
-0.000257503086406519,
-0.000281486994066025,
-0.000305723503700679,
-0.000330210426059308,
-0.000354945665598255,
-0.000379926915845916,
-0.000405151956507756,
-0.000430618362384964,
-0.000456323792890481,
-0.000482265705856655,
-0.000508441643241767,
-0.000534848940194894,
-0.000561485022897046,
-0.000588347113607660,
-0.000615432518021442,
-0.000642738345529626,
-0.000670261786254066,
-0.000697999835195141,
-0.000725949568737048,
-0.000754107872446179,
-0.000782471708002863,
-0.000811037851152608,
-0.000839803156162613,
-0.000868764285754871,
-0.000897917988557699,
-0.000927260821822552,
-0.000956789425305633,
-0.000986500254535089,
-0.00101638984108537,
-0.00104645454078258,
-0.00107669078473676,
-0.00110709482536606,
-0.00113766299717731,
-0.00116839145262871,
-0.00119927642758690,
-0.00123031398418415,
-0.00126150026162382,
-0.00129283122580216,
-0.00132430292836542,
-0.00135591124431857,
-0.00138765213428446,
-0.00141952138887340,
-0.00145151488073991,
-0.00148362831612719,
-0.00151585748499229,
-0.00154819801195881,
-0.00158064560650847,
-0.00161319581695018,
-0.00164584427382029,
-0.00167858645186783,
-0.00171141790451108,
-0.00174433403822607,
-0.00177733033804577,
-0.00181040213773593,
-0.00184354485348776,
-0.00187675376086131,
-0.00191002421019513,
-0.00194335141256686,
-0.00197673066266553,
-0.00201015711112216,
-0.00204362599689791,
-0.00207713241930943,
-0.00211067156001584,
-0.00214423847287856,
-0.00217782828885456,
-0.00221143601594175,
-0.00224505673956590,
-0.00227868542419834,
-0.00231231711939174,
-0.00234594674933244,
-0.00237956932031571,
-0.00241317972846192,
-0.00244677294956298,
-0.00248034384558351,
-0.00251388736165349,
-0.00254739833774491,
-0.00258087169284408,
-0.00261430224582133,
-0.00264768489443835,
-0.00268101443907401,
-0.00271428576337512,
-0.00274749365520234,
-0.00278063298200053,
-0.00281369852627465,
-0.00284668514688528,
-0.00287958762416115,
-0.00291240080973127,
-0.00294511948107620,
-0.00297773849968884,
-0.00301025264456641,
-0.00304265677662001,
-0.00307494568697706,
-0.00310711424878149,
-0.00313915726402809,
-0.00317106962060174,
-0.00320284613548793,
-0.00323448171662747,
-0.00326597120655615,
-0.00329730953059114,
-0.00332849155900580,
-0.00335951224628230,
-0.00339036649346014,
-0.00342104928540268,
-0.00345155555530025,
-0.00348188032822464,
-0.00351201857965144,
-0.00354196536968445,
-0.00357171571569318,
-0.00360126472814403,
-0.00363060747270192,
-0.00365973910561074,
-0.00368865474551317,
-0.00371734960141723,
-0.00374581885109485,
-0.00377405776271989,
-0.00380206156870805,
-0.00382982560239173,
-0.00385734516541213,
-0.00388461565135489,
-0.00391163243217263,
-0.00393839097201934,
-0.00396488671883422,
-0.00399111521100629,
-0.00401707196633785,
-0.00404275260833588,
-0.00406815273741037,
-0.00409326805585855,
-0.00411809424930169,
-0.00414262710547963,
-0.00416686240098882,
-0.00419079601487805,
-0.00421442381486645,
-0.00423774176962887,
-0.00426074584971528,
-0.00428343212038434,
-0.00430579664814760,
-0.00432783560038011,
-0.00434954515073437,
-0.00437092156951920,
-0.00439196113488749,
-0.00441266022458233,
-0.00443301523239588,
-0.00445302264569759,
-0.00447267896722690,
-0.00449198080207357,
-0.00451092477024544,
-0.00452950759322735,
-0.00454772601412418,
-0.00456557686948679,
-0.00458305702638157,
-0.00460016344989720,
-0.00461689312908165,
-0.00463324315222104,
-0.00464921064199444,
-0.00466479281370150,
-0.00467998692108119,
-0.00469479030849154,
-0.00470920036573083,
-0.00472321457430748,
-0.00473683045364440,
-0.00475004562266464,
-0.00476285774233103,
-0.00477526456757244,
-0.00478726390125808,
-0.00479885363726199,
-0.00481003172038249,
-0.00482079618697185,
-0.00483114513086222,
-0.00484107672645139,
-0.00485058921432468,
-0.00485968091876064,
-0.00486835022461732,
-0.00487659560414803,
-0.00488441559200791,
-0.00489180880914334,
-0.00489877394199243,
-0.00490530975799206,
-0.00491141509702113,
-0.00491708888109120,
-0.00492233009791042,
-0.00492713782472824,
-0.00493151120202513,
-0.00493544945801285,
-0.00493895189278199,
-0.00494201788566977,
-0.00494464688981495,
-0.00494683844085619,
-0.00494859214862692,
-0.00494990770180738,
-0.00495078486760031,
-0.00495122348912271,
-0.00495122348912271,
-0.00495078486760031,
-0.00494990770180738,
-0.00494859214862692,
-0.00494683844085619,
-0.00494464688981495,
-0.00494201788566977,
-0.00493895189278199,
-0.00493544945801285,
-0.00493151120202513,
-0.00492713782472824,
-0.00492233009791042,
-0.00491708888109120,
-0.00491141509702113,
-0.00490530975799206,
-0.00489877394199243,
-0.00489180880914334,
-0.00488441559200791,
-0.00487659560414803,
-0.00486835022461732,
-0.00485968091876064,
-0.00485058921432468,
-0.00484107672645139,
-0.00483114513086222,
-0.00482079618697185,
-0.00481003172038249,
-0.00479885363726199,
-0.00478726390125808,
-0.00477526456757244,
-0.00476285774233103,
-0.00475004562266464,
-0.00473683045364440,
-0.00472321457430748,
-0.00470920036573083,
-0.00469479030849154,
-0.00467998692108119,
-0.00466479281370150,
-0.00464921064199444,
-0.00463324315222104,
-0.00461689312908165,
-0.00460016344989720,
-0.00458305702638157,
-0.00456557686948679,
-0.00454772601412418,
-0.00452950759322735,
-0.00451092477024544,
-0.00449198080207357,
-0.00447267896722690,
-0.00445302264569759,
-0.00443301523239588,
-0.00441266022458233,
-0.00439196113488749,
-0.00437092156951920,
-0.00434954515073437,
-0.00432783560038011,
-0.00430579664814760,
-0.00428343212038434,
-0.00426074584971528,
-0.00423774176962887,
-0.00421442381486645,
-0.00419079601487805,
-0.00416686240098882,
-0.00414262710547963,
-0.00411809424930169,
-0.00409326805585855,
-0.00406815273741037,
-0.00404275260833588,
-0.00401707196633785,
-0.00399111521100629,
-0.00396488671883422,
-0.00393839097201934,
-0.00391163243217263,
-0.00388461565135489,
-0.00385734516541213,
-0.00382982560239173,
-0.00380206156870805,
-0.00377405776271989,
-0.00374581885109485,
-0.00371734960141723,
-0.00368865474551317,
-0.00365973910561074,
-0.00363060747270192,
-0.00360126472814403,
-0.00357171571569318,
-0.00354196536968445,
-0.00351201857965144,
-0.00348188032822464,
-0.00345155555530025,
-0.00342104928540268,
-0.00339036649346014,
-0.00335951224628230,
-0.00332849155900580,
-0.00329730953059114,
-0.00326597120655615,
-0.00323448171662747,
-0.00320284613548793,
-0.00317106962060174,
-0.00313915726402809,
-0.00310711424878149,
-0.00307494568697706,
-0.00304265677662001,
-0.00301025264456641,
-0.00297773849968884,
-0.00294511948107620,
-0.00291240080973127,
-0.00287958762416115,
-0.00284668514688528,
-0.00281369852627465,
-0.00278063298200053,
-0.00274749365520234,
-0.00271428576337512,
-0.00268101443907401,
-0.00264768489443835,
-0.00261430224582133,
-0.00258087169284408,
-0.00254739833774491,
-0.00251388736165349,
-0.00248034384558351,
-0.00244677294956298,
-0.00241317972846192,
-0.00237956932031571,
-0.00234594674933244,
-0.00231231711939174,
-0.00227868542419834,
-0.00224505673956590,
-0.00221143601594175,
-0.00217782828885456,
-0.00214423847287856,
-0.00211067156001584,
-0.00207713241930943,
-0.00204362599689791,
-0.00201015711112216,
-0.00197673066266553,
-0.00194335141256686,
-0.00191002421019513,
-0.00187675376086131,
-0.00184354485348776,
-0.00181040213773593,
-0.00177733033804577,
-0.00174433403822607,
-0.00171141790451108,
-0.00167858645186783,
-0.00164584427382029,
-0.00161319581695018,
-0.00158064560650847,
-0.00154819801195881,
-0.00151585748499229,
-0.00148362831612719,
-0.00145151488073991,
-0.00141952138887340,
-0.00138765213428446,
-0.00135591124431857,
-0.00132430292836542,
-0.00129283122580216,
-0.00126150026162382,
-0.00123031398418415,
-0.00119927642758690,
-0.00116839145262871,
-0.00113766299717731,
-0.00110709482536606,
-0.00107669078473676,
-0.00104645454078258,
-0.00101638984108537,
-0.000986500254535089,
-0.000956789425305633,
-0.000927260821822552,
-0.000897917988557699,
-0.000868764285754871,
-0.000839803156162613,
-0.000811037851152608,
-0.000782471708002863,
-0.000754107872446179,
-0.000725949568737048,
-0.000697999835195141,
-0.000670261786254066,
-0.000642738345529626,
-0.000615432518021442,
-0.000588347113607660,
-0.000561485022897046,
-0.000534848940194894,
-0.000508441643241767,
-0.000482265705856655,
-0.000456323792890481,
-0.000430618362384964,
-0.000405151956507756,
-0.000379926915845916,
-0.000354945665598255,
-0.000330210426059308,
-0.000305723503700679,
-0.000281486994066025,
-0.000257503086406519,
-0.000233773755649941,
-0.000210301066410341,
-0.000187086875132261,
-0.000164133122498143,
-0.000141441540758312,
-0.000119013952872007,
-9.68519634422886e-05,
-7.49572729406508e-05,
-5.33313669696652e-05,
-3.19758215514218e-05,
-1.08919930592394e-05,
--9.91865979301451e-06,
--3.04549042775557e-05,
--5.07154045972636e-05,
--7.06990553697872e-05,
--9.04046425871818e-05,
--0.000109831180556723,
--0.000128977579742240,
--0.000147842979525945,
--0.000166426409808339,
--0.000184727135197687,
--0.000202744305292592,
--0.000220477306560259,
--0.000237925412360025,
--0.000255088130083280,
--0.000271964847867372,
--0.000288555200873863,
--0.000304858697984160,
--0.000320875086144786,
--0.000336603996500303,
--0.000352045297690400,
--0.000367198738062124,
--0.000382064302937101,
--0.000396641852357854,
--0.000410931495060687,
--0.000424933205749722,
--0.000438647204637503,
--0.000452073583482240,
--0.000465212674168866,
--0.000478064682216253,
--0.000490630054751344,
--0.000502909102003151,
--0.000514902384920966,
--0.000526610326399401,
--0.000538033593923370,
--0.000549172712705324,
--0.000560028464530771,
--0.000570601481559749,
--0.000580892646976487,
--0.000590902695998816,
--0.000600632615642164,
--0.000610083247987995,
--0.000619255679141116,
--0.000628150846892228,
--0.000636769940811193,
--0.000645113996101250,
--0.000653184299948689,
--0.000660981983005567,
--0.000668508423585954,
--0.000675764850643800,
--0.000682752736054815,
--0.000689473392683322,
--0.000695928386138248,
--0.000702119122894178,
--0.000708047251625198,
--0.000713714263134529,
--0.000719121892993695,
--0.000724271716466218,
--0.000729165555282147,
--0.000733805058805418,
--0.000738192130147058,
--0.000742328500323755,
--0.000746216149243278,
--0.000749856883052877,
--0.000753252754194147,
--0.000756405642021792,
--0.000759317676212684,
--0.000761990800345454,
--0.000764427212355194,
--0.000766628928149237,
--0.000768598212666572,
--0.000770337139062027,
--0.000771848039257578,
--0.000773133050832454,
--0.000774194563408219,
--0.000775034774979158,
--0.000775656128479859,
--0.000776060879297139,
--0.000776251530463252,
--0.000776230384120606,
--0.000775999996896670,
--0.000775562718410930,
--0.000774921156688016,
--0.000774077710368219,
--0.000773035026336787,
--0.000771795549766353,
--0.000770361976300053,
--0.000768736783543735,
--0.000766922708033022,
--0.000764922268811481,
--0.000762738239234387,
--0.000760373168287008,
--0.000757829866314622,
--0.000755110911309810,
--0.000752219147550121,
--0.000749157181924490,
--0.000745927883252895,
--0.000742533884189047,
--0.000738978079579033,
--0.000735263125592756,
--0.000731391938661357,
--0.000727367189884889,
--0.000723191819632401,
--0.000718868516431075,
--0.000714400230172114,
--0.000709789667273328,
--0.000705039793794998,
--0.000700153320679537,
--0.000695133224872846,
--0.000689982228373537,
--0.000684703312533797,
--0.000679299206549835,
--0.000673772895505234,
--0.000668127107213612,
--0.000662364832864586,
--0.000656488796703490,
--0.000650501991706910,
--0.000644407135922555,
--0.000638207216182248,
--0.000631904950355812,
--0.000625503315016224,
--0.000619005017567106,
--0.000612413030480144,
--0.000605730045666215,
--0.000598959022419751,
--0.000592102641307673,
--0.000585163848349161,
--0.000578145303770340,
--0.000571049939360574,
--0.000563880395792860,
--0.000556639582645205,
--0.000549330124139864,
--0.000541954904421434,
--0.000534516525078808,
--0.000527017845535591,
--0.000519461439038756,
--0.000511850143907964,
--0.000504186501659088,
--0.000496473321124369,
--0.000488713116827171,
--0.000480908661009415,
--0.000473062439153703,
--0.000465177193879328,
--0.000457255369089255,
--0.000449299675354794,
--0.000441312521641045,
--0.000433296579745569,
--0.000425254218742792,
--0.000417188075990567,
--0.000409100475389566,
--0.000400994015796423,
--0.000392870977630007,
--0.000384733917580314,
--0.000376585075147441,
--0.000368426959623216,
--0.000360261765094418,
--0.000352091955603402,
--0.000343919676673919,
--0.000335747350207106,
--0.000327577068996273,
--0.000319411202616472,
--0.000311251799327546,
--0.000303101180604228,
--0.000294961336084192,
--0.000286834539691710,
--0.000278722730787134,
--0.000270628125294601,
--0.000262552611408044,
--0.000254498351409696,
--0.000246467177406058,
--0.000238461200747511,
--0.000230482190676936,
--0.000222532206706049,
--0.000214612960310208,
--0.000206726455104848,
--0.000198874346379858,
--0.000191058573185037,
--0.000183280733901876,
--0.000175542718661153,
--0.000167846056910789,
--0.000160192579466020,
--0.000152583759222027,
--0.000145021368821121,
--0.000137506814203479,
--0.000130041811681510,
--0.000122627706760917,
--0.000115266152994485,
--0.000107958436359977,
--0.000100706144186103,
--9.35105054098079e-05,
--8.63730463095441e-05,
--7.92949306753459e-05,
--7.22776238052609e-05,
--6.53222224894314e-05,
--5.84301376752338e-05,
--5.16024050889186e-05,
--4.48403630718286e-05,
--3.81449885574943e-05,
--3.15175650599888e-05,
--2.49589991720766e-05,
--1.84705123312578e-05,
--1.20529518122427e-05,
--5.70747618704037e-06,
-5.65130739446817e-07,
-6.76377196319473e-06,
-1.28877281875480e-05,
-1.89359578000643e-05,
-2.49078076002350e-05,
-3.08022998861741e-05,
-3.66188412855488e-05,
-4.23565146651483e-05,
-4.80147832320940e-05,
-5.35927957604024e-05,
-5.90900818618602e-05,
-6.45058395701152e-05,
-6.98396606723608e-05,
-7.50908090508799e-05,
-8.02589335085685e-05,
-8.53433537117259e-05,
-9.03437806713531e-05,
-9.52595926807594e-05,
-0.000100090556969088,
-0.000104836112476280,
-0.000109496077969276,
-0.000114069956263803,
-0.000118557624222376,
-0.000122958632897122,
-0.000127272919603931,
-0.000131500087828142,
-0.000135640137861556,
-0.000139692722584275,
-0.000143657889880845,
-0.000147535354241219,
-0.000151325219415721,
-0.000155027243685390,
-0.000158641588326119,
-0.000162168061864114,
-0.000165606876476172,
-0.000168957890495671,
-0.000172221367164218,
-0.000175397211774938,
-0.000178485741364642,
-0.000181486906935768,
-0.000184401067781124,
-0.000187228227821168,
-0.000189968793862972,
-0.000192622814021790,
-0.000195190736945890,
-0.000197672653272001,
-0.000200069067435948,
-0.000202380104774549,
-0.000204606309833789,
-0.000206747856525214,
-0.000208805327647997,
-0.000210778935093994,
-0.000212669306710065,
-0.000214476690763218,
-0.000216201753070026,
-0.000217844783243759,
-0.000219406480827707,
-0.000220887169687286,
-0.000222287594558100,
-0.000223608106379000,
-0.000224849486757965,
-0.000226012115545807,
-0.000227096814542036,
-0.000228103997682251,
-0.000229034509219026,
-0.000229888797324386,
-0.000230667742461277,
-0.000231371813425932,
-0.000232001923306693,
-0.000232558571806248,
-0.000233042694337553,
-0.000233454817877378,
-0.000233795906658708,
-0.000234066510095474,
-0.000234267616643166,
-0.000234399804148527,
-0.000234464075886551,
-0.000234461034429087,
-0.000234391707980239,
-0.000234256717572874,
-0.000234057114622203,
-0.000233793529306281,
-0.000233467042490252,
-0.000233078299941400,
-0.000232628392706581,
-0.000232117991545114,
-0.000231548200624833,
-0.000230919699867762,
-0.000230233614422397,
-0.000229490637305569,
-0.000228691904367779,
-0.000227838120949022,
-0.000226930435535397,
-0.000225969559300686,
-0.000224956657801864,
-0.000223892449138464,
-0.000222778105957421,
-0.000221614355063568,
-0.000220402374841213,
-0.000219142904089215,
-0.000217837122420945,
-0.000216485772585174,
-0.000215090046105722,
-0.000213650681871823,
-0.000212168874797709,
-0.000210645375770233,
-0.000209081375510424,
-0.000207477624265485,
-0.000205835317231766,
-0.000204155202780923,
-0.000202438477037224,
-0.000200685889922636,
-0.000198898632343484,
-0.000197077449370729,
-0.000195223538589502,
-0.000193337632742739,
-0.000191420933229610,
-0.000189474160690418,
-0.000187498516812910,
-0.000185494717916349,
-0.000183463950477444,
-0.000181406934825622,
-0.000179324852197408,
-0.000177218404031216,
-0.000175088769080921,
-0.000172936642681967,
-0.000170763192929278,
-0.000168569105181148,
-0.000166355539329688,
-0.000164123164418827,
-0.000161873135563338,
-0.000159606113351142,
-0.000157323233199747,
-0.000155025147047118,
-0.000152712978884051,
-0.000150387365819865,
-0.000148049419840621,
-0.000145699758496409,
-0.000143339493063055,
-0.000140969217664355,
-0.000138590020743407,
-0.000136202494636464,
-0.000133807710854935,
-0.000131406237902807,
-0.000128999137073690,
-0.000126586955396263,
-0.000124170741112682,
-0.000121751025536885,
-0.000119328839927821,
-0.000116904687981786,
-0.000114479594239356,
-0.000112054039942443,
-0.000109629029400713,
-0.000107205025896024,
-0.000104783014763168,
-0.000102363447520479,
-9.99472748819797e-05,
-9.75349342814310e-05,
-9.51273684116554e-05,
-9.27249827027176e-05,
-9.03286966625331e-05,
-8.79389017808088e-05,
-8.55564956212588e-05,
-8.31818494755366e-05,
-8.08158423081462e-05,
-7.84588166968576e-05,
-7.61116330506792e-05,
-7.37746190907099e-05,
-7.14486074938142e-05,
-6.91339016622066e-05,
-6.68313202374356e-05,
-6.45411398811771e-05,
-6.22641611239781e-05,
-6.00006282654158e-05,
-5.77513323125343e-05,
-5.55164983346224e-05,
-5.32968764479913e-05,
-5.10926798053942e-05,
-4.89046419944162e-05,
-4.67329465682261e-05,
-4.45783101891342e-05,
-4.24408877592610e-05,
-4.03213747021350e-05,
-3.82199078901064e-05,
-3.61371619724216e-05,
-3.40732419683153e-05,
-3.20288053388297e-05,
-3.00039407582481e-05,
-2.79992736446810e-05,
-2.60148760416192e-05,
-2.40513485984833e-05,
-2.21087489487933e-05,
-2.01876489388426e-05,
-1.82880735840279e-05,
-1.64105928172265e-05,
-1.45552032584862e-05,
-1.27224474678529e-05,
-1.09123048193875e-05,
-9.12529459068137e-06,
-7.36137384262456e-06,
-5.62105051139952e-06,
-3.90424732482530e-06,
-2.21145327612140e-06,
-5.42576135735981e-07,
--1.10191546986540e-06,
--2.72214441335228e-06,
--4.31765074336527e-06,
--5.88858138346941e-06,
--7.43449717788015e-06,
--8.95557028181646e-06,
--1.04513793212540e-05,
--1.19221016313184e-05,
--1.33673529297333e-05,
--1.47873283970059e-05,
--1.61816522718723e-05,
--1.75505489714928e-05,
--1.88936525600629e-05,
--2.02112093356898e-05,
--2.15028786303040e-05,
--2.27689200068355e-05,
--2.40090034267166e-05,
--2.52234156518926e-05,
--2.64118441851114e-05,
--2.75745824745532e-05,
--2.87113445271020e-05,
--2.98224428705179e-05,
--3.09076019921684e-05,
--3.19671510883658e-05,
--3.30008251544588e-05,
--3.40089900319630e-05,
--3.49913775019667e-05,
--3.59483680766746e-05,
--3.68797197976284e-05,
--3.77858193439538e-05,
--3.86664427936628e-05,
--3.95219995716669e-05,
--4.03522641202012e-05,
--4.11576670903313e-05,
--4.19380012096953e-05,
--4.26937043233061e-05,
--4.34245803720504e-05,
--4.41310892863899e-05,
--4.48130436182427e-05,
--4.54709069617551e-05,
--4.61045141628936e-05,
--4.67143388964721e-05,
--4.73002336655060e-05,
--4.78626656356439e-05,
--4.84015062909820e-05,
--4.89172468415742e-05,
--4.94097492621224e-05,
--4.98795298499818e-05,
--5.03264565435712e-05,
--5.07510470767421e-05,
--5.11531883719458e-05,
--5.15334084359192e-05,
--5.18915929674119e-05,
--5.22282871255255e-05,
--5.25433860531435e-05,
--5.28374339049068e-05,
--5.31103372807938e-05,
--5.33626591300992e-05,
--5.35943020304215e-05,
--5.38058373779503e-05,
--5.39971664557209e-05,
--5.41688832854903e-05,
--5.43208932117335e-05,
--5.44537801178265e-05,
--5.45674740408066e-05,
--5.46625553812279e-05,
--5.47389540962183e-05,
--5.47972707257875e-05,
--5.48374279367193e-05,
--5.48600304498624e-05,
--5.48650134478435e-05,
--5.48529777470327e-05,
--5.48238615891289e-05,
--5.47782796545640e-05,
--5.47161726277983e-05,
--5.46381421707785e-05,
--5.45441409303223e-05,
--5.44347778383721e-05,
--5.43100072156885e-05,
--5.41704376199963e-05,
--5.40160121859755e-05,
--5.38473696441733e-05,
--5.36644291832927e-05,
--5.34678403499526e-05,
--5.32575308256433e-05,
--5.30341352214345e-05,
--5.27975890619123e-05,
--5.25485330801741e-05,
--5.22868899566335e-05,
--5.20133133082317e-05,
--5.17277278570648e-05,
--5.14307728917283e-05,
--5.11223701960069e-05,
--5.08031767792133e-05,
--5.04731070890493e-05,
--5.01328087389260e-05,
--4.97821954537253e-05,
--4.94219173993094e-05,
--4.90518974627560e-05,
--4.86727623947596e-05,
--4.82844457389913e-05,
--4.78875779787031e-05,
--4.74820663998248e-05,
--4.70685624133195e-05,
--4.66469626401911e-05,
--4.62179102441466e-05,
--4.57813127757766e-05,
--4.53378004294169e-05,
--4.48872654011353e-05,
--4.44303536014418e-05,
--4.39669575807467e-05,
--4.34977023194285e-05,
--4.30224723840039e-05,
--4.25419079114848e-05,
--4.20558816044656e-05,
--4.15650349494434e-05,
--4.10692160068759e-05,
--4.05690860811782e-05,
--4.00644784803627e-05,
--3.95560344208977e-05,
--3.90436126582742e-05,
--3.85278288122663e-05,
--3.80085391108475e-05,
--3.74863645989061e-05,
--3.69611383814916e-05,
--3.64334836149489e-05,
--3.59032455798272e-05,
--3.53710230750429e-05,
--3.48366443059829e-05,
--3.43007203295382e-05,
--3.37630833740885e-05,
--3.32243219343559e-05,
--3.26842631134414e-05,
--3.21434903845723e-05,
--3.16018340271438e-05,
--3.10598620379927e-05,
--3.05173890710438e-05,
--2.99750102268205e-05,
--2.94324977036388e-05,
--2.88904561028569e-05,
--2.83486572194740e-05,
--2.78076813742880e-05,
--2.72673160896635e-05,
--2.67281307655500e-05,
--2.61898800092312e-05,
--2.56531458365802e-05,
--2.51176919541455e-05,
--2.45840821429991e-05,
--2.40520571098411e-05,
--2.35221919064648e-05,
--2.29942189701287e-05,
--2.24687067756074e-05,
--2.19453728209874e-05,
--2.14247833830322e-05,
--2.09066661112072e-05,
--2.03915446858796e-05,
--1.98791701832643e-05,
--1.93700580786767e-05,
--1.88639344535055e-05,
--1.83613352518811e-05,
--1.78619508850454e-05,
--1.73663104823494e-05,
--1.68741290759562e-05,
--1.63859206132667e-05,
--1.59013657273958e-05,
--1.54209897807815e-05,
--1.49444772391115e-05,
--1.44723391847100e-05,
--1.40042432894668e-05,
--1.35407068370928e-05,
--1.30813839859992e-05,
--1.26267966656990e-05,
--1.21765651542703e-05,
--1.17312462832730e-05,
--1.12904336671393e-05,
--1.08546628466816e-05,
--1.04235511544729e-05,
--9.99759144947471e-06,
--9.57642228943518e-06,
--9.16054779162674e-06,
--8.74955757971436e-06,
--8.34395800204422e-06,
--7.94336123069663e-06,
--7.54825910394332e-06,
--7.15823725534437e-06,
--6.77379446388097e-06,
--6.39451744851171e-06,
--6.02088801710077e-06,
--5.65249312334948e-06,
--5.28980705682264e-06,
--4.93243163040497e-06,
--4.58080862054126e-06,
--4.23452884634043e-06,
--3.89406743396649e-06,
--3.55896455485854e-06,
--3.22973451266772e-06,
--2.90589263830832e-06,
--2.58791653823994e-06,
--2.27536237370173e-06,
--1.96871121194834e-06,
--1.66747248399997e-06,
--1.37213415644316e-06,
--1.08221647391169e-06,
--7.98197252838720e-07,
--5.19577878779430e-07,
--2.46853106248978e-07,
-2.04836906979221e-08,
-2.81944291020029e-07,
-5.38057114445362e-07,
-7.88323211174950e-07,
-1.03324761527409e-06,
-1.27238963764761e-06,
-1.50620956228030e-06,
-1.73430496738356e-06,
-1.95714227397860e-06,
-2.17426059604335e-06,
-2.38618565979242e-06,
-2.59247412473194e-06,
-2.79361738719780e-06,
-2.98917613191769e-06,
-3.17967167761456e-06,
-3.36465155419938e-06,
-3.54462712478025e-06,
-3.71916450048738e-06,
-3.88878763289571e-06,
-4.05305110546832e-06,
-4.21247294402037e-06,
-4.36660747560401e-06,
-4.51602630845954e-06,
-4.66022392940390e-06,
-4.79981139928378e-06,
-4.93426738681860e-06,
-5.06417106483642e-06,
-5.18909082922882e-06,
-5.30955851436901e-06,
-5.42511044796520e-06,
-5.53633239754493e-06,
-5.64275663266799e-06,
-5.74494125723676e-06,
-5.84243268710826e-06,
-5.93581228618980e-06,
-6.02460876343180e-06,
-6.10938733149237e-06,
-6.18969752493548e-06,
-6.26611753900958e-06,
-6.33820922296577e-06,
-6.40649492549257e-06,
-6.47057633363911e-06,
-6.53097084557196e-06,
-6.58726001945506e-06,
-6.64004566255543e-06,
-6.68879222648781e-06,
-6.73414912642070e-06,
-6.77563271791333e-06,
-6.81383582649206e-06,
-6.84826575659092e-06,
-6.87956544369715e-06,
-6.90721796243191e-06,
-6.93184262641915e-06,
-6.95293670913433e-06,
-6.97115918378061e-06,
-6.98598387232265e-06,
-6.99805670472170e-06,
-7.00682651919766e-06,
-7.01299335632421e-06,
-7.01598906903671e-06,
-7.01649463335420e-06,
-7.01399146065940e-06,
-7.00904991575129e-06,
-7.00126691500350e-06,
-6.99121320837930e-06,
-6.97837546570650e-06,
-6.96341207782413e-06,
-6.94581178960161e-06,
-6.92617579276020e-06,
-6.90400455258515e-06,
-6.87995435672755e-06,
-6.85350874920331e-06,
-6.82527935353772e-06,
-6.79475276861382e-06,
-6.76256992228003e-06,
-6.72823177451458e-06,
-6.69234997696170e-06,
-6.65439635608727e-06,
-6.61504319391059e-06,
-6.57367526411895e-06,
-6.53110576502919e-06,
-6.48662130412141e-06,
-6.44096494373972e-06,
-6.39358822958236e-06,
-6.34512332808420e-06,
-6.29499279506742e-06,
-6.24392081949063e-06,
-6.19132752972891e-06,
-6.13786180337324e-06,
-6.08294572989760e-06,
-6.02728122072791e-06,
-5.97030085360536e-06,
-5.91266194714808e-06,
-5.85377619609459e-06,
-5.79431080305923e-06,
-5.73373682457175e-06,
-5.67265321372291e-06,
-5.61062044360571e-06,
-5.54811134917779e-06,
-5.48469577560834e-06,
-5.42102571618858e-06,
-5.35641256312241e-06,
-5.29163941340404e-06,
-5.22609616909151e-06,
-5.16043301207036e-06,
-5.09401623836012e-06,
-5.02759963577756e-06,
-4.96055708038026e-06,
-4.89357790491499e-06,
-4.82598777828551e-06,
-4.75852881449136e-06,
-4.69055023332375e-06,
-4.62281849455623e-06,
-4.55459997845769e-06,
-4.48672487737745e-06,
-4.41836938506806e-06,
-4.35043829085821e-06,
-4.28219961892930e-06,
-4.21424927012473e-06,
-4.14618562570372e-06,
-4.07851955438422e-06,
-4.01062965420015e-06,
-3.94325856674703e-06,
-3.87581985935145e-06,
-3.80892935787303e-06,
-3.74192777791326e-06,
-3.67551457730312e-06,
-3.60908996231663e-06,
-3.54333509849531e-06,
-3.47759844620301e-06,
-3.41254082601689e-06,
-3.34755725225876e-06,
-3.28329739015491e-06,
-3.21917116992942e-06,
-3.15586112065294e-06,
-3.09250439907414e-06,
-3.03026815024548e-06,
-2.96789707905998e-06,
-2.90651253940365e-06,
-2.84534746585168e-06,
-2.78515359568984e-06,
-2.72502556403521e-06,
-2.66588370269542e-06,
-2.60694079790385e-06,
-2.54904626588095e-06,
-2.49131860508909e-06,
-2.43461484557276e-06,
-2.37811232502338e-06,
-2.32270367024371e-06,
-2.26753452245018e-06,
-2.21344673621392e-06,
-2.15960953108923e-06,
-2.10671996122087e-06,
-2.05440229847222e-06,
-2.00274982053528e-06,
-1.95172125266670e-06,
-1.90190541457320e-06,
-1.85213180801677e-06,
-1.80354697649463e-06,
-1.75531183154730e-06,
-1.70832466623602e-06,
-1.66152581672090e-06,
-1.61590430260585e-06,
-1.57052770327227e-06,
-1.52645417739943e-06,
-1.48263736669695e-06,
-1.44009364782760e-06,
-1.39768396625082e-06,
-1.35656260640872e-06,
-1.31548405641171e-06,
-1.27597253424280e-06,
-1.23612003796729e-06,
-1.19822784284488e-06,
-1.16035624735089e-06,
-1.12300409054529e-06,
-1.08676147556840e-06,
-1.05143714735623e-06,
-1.01643933513151e-06,
-9.82217633251451e-07,
-9.48565090704124e-07,
-9.15965803598408e-07,
-8.83993876825403e-07,
-8.52904112434704e-07,
-8.22319204892483e-07,
-7.92457626845786e-07,
-7.63210937527310e-07,
-7.34607521511639e-07,
-7.06900952158772e-07,
-6.79457125689371e-07,
-6.53603188407001e-07,
-6.27541356389415e-07,
-6.00698053897355e-07,
-5.79062251172147e-07,
-5.53883059492054e-07,
-5.30402400375842e-07,
-5.06629365749489e-07,
-4.85127823306893e-07,
-4.63474354546465e-07,
-4.43414078442301e-07,
-4.22883429696171e-07,
-4.03659300931941e-07,
-3.84037888145959e-07,
-3.65790534295599e-07,
-3.47320034430519e-07,
-3.30279379977406e-07,
-3.13057205526705e-07,
-2.97200797125842e-07,
-2.81092420097884e-07,
-2.66244224670397e-07,
-2.25877153377331e-06
-};
-
-
-
-
-int lp_fifo_num = 1;
-int bp_fifo_num = 1;
-
-
-int value[] = {0,0};
-
-float mag = 0;
-float calc_mag = 0;
-double mag_total = 0;
-float phase = 0;
-float calc_phase = 0;
-float phase_total = 0;
-int NumInBlock = 0;
-
-float mag_result = 0;
-float phase_result = 0;
-/*
-int diff_0[3] = {0, 0, 0};
-int diff_1[3] = {0, 0, 0};
-*/
-double final_mag = 0;
-float final_phase = 0;
-
-// Finite State Machine
-int current_state = 0;
-
-// Flag
-bool ADC_reading = false;
-bool isTransmitting = false;
-
-
-//IntervalTimer timer0;
-Ticker timer0;
-Timer t1;
-
-int adc_cal(void)
-{
-    ADC0_CFG1 |= (ADC_CFG1_MODE(3)  | // 16 bits mode
-                  ADC_CFG1_ADICLK(1)| // Input Bus Clock divided by 2 (20-25 MHz out of reset (FEI mode) / 2)
-                  ADC_CFG1_ADIV(2)) ; // Clock divide by 4 (2.5-3 MHz)
-    
-    ADC0_SC3 |= ADC_SC3_AVGE_MASK |   // Enable HW average
-                ADC_SC3_AVGS(3)   |   // Set HW average of 32 samples
-                ADC_SC3_CAL_MASK;     // Start calibration process
-    
-    while(ADC0_SC3 & ADC_SC3_CAL_MASK); // Wait for calibration to end
-    
-    if(ADC0_SC3 & ADC_SC3_CALF_MASK) return 1;  // Check for successful calibration
-    
-    uint16_t calib = 0; // calibration variable 
-    calib += ADC0->CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
-    calib /= 2;
-    calib |= 0x8000;    // Set MSB 
-    ADC0_PG = calib;
-    calib = 0;
-    calib += ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
-    calib /= 2;
-    calib |= 0x8000;    // Set MSB
-    ADC0_MG = calib;
-    
-    return 0;
-}
-
-void adc_init()
-{
-    // red, indicating not ready
-
-    
-    // Turn on the ADC0 and ADC1 clocks
-    SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;
-    SIM_SCGC3 |= SIM_SCGC3_ADC1_MASK;
-    
-    
-     __disable_irq();
-     adc_cal();
-    //if(adc_cal()) {red = 0; green = 0;} // if calibration fails, display yellow
-    __enable_irq();
-
-
-    
-    // Setup Configuration Register 1 
-    ADC0_CFG1 = 0; // clear register
-    ADC0_CFG1 |= ADC_CFG1_ADICLK(0);    // select bus clock
-    ADC0_CFG1 |= ADC_CFG1_MODE(3);      // select 16-bit 2's complement output  
-    ADC0_CFG1 |= ADC_CFG1_ADIV(0);      // clock divisor = 0.
-    ADC0_CFG1 &= ~ADC_CFG1_ADLSMP_MASK; // select short sample time   //QUESTION
-    ADC0_CFG1 &= ~ADC_CFG1_ADLPC_MASK;  // select normal power configuration
-    ADC1_CFG1 = 0; // clear register
-    ADC1_CFG1 |= ADC_CFG1_ADICLK(0);    // select bus clock
-    ADC1_CFG1 |= ADC_CFG1_MODE(3);      // select 16-bit 2's complement output
-    ADC1_CFG1 |= ADC_CFG1_ADIV(0);      // clock divisor = 0.
-    ADC1_CFG1 &= ~ADC_CFG1_ADLSMP_MASK; // select short sample time
-    ADC1_CFG1 &= ~ADC_CFG1_ADLPC_MASK;  // select normal power configuration
-    
-    // Setup Configuration Register 2 
-    ADC0_CFG2 = 0; // clear register
-    //ADC0_CFG2 |= ADC_CFG2_ADHSC_MASK ;  // select high-speed conversion
-    ADC0_CFG2 |= ADC_CFG2_MUXSEL_MASK; // select a channels    //possibly change to channel b.
-    ADC1_CFG2 = 0; // clear register
-    //ADC1_CFG2 |= ADC_CFG2_ADHSC_MASK ;  // select high-speed conversion
-    ADC1_CFG2 &= ~ADC_CFG2_MUXSEL_MASK; // select a channels    
-    
-    // Setup Status and Control Register 2 
-    ADC0_SC2 = 0;                    // clear register
-    ADC0_SC2 |= ADC_SC2_REFSEL(0);   // select external voltage reference   //QUESTION
-    //ADC0_SC2 |= ADC_SC2_DMAEN_MASK;  // enable DMA
-    //ADC0_SC2 &= ~ADC_SC2_ADTRG_MASK; // select hardware trigger  //QESTION
-    ADC1_SC2 = 0;                    // clear register
-    ADC1_SC2 |= ADC_SC2_REFSEL(0);   // select external voltage reference
-    //ADC1_SC2 |= ADC_SC2_DMAEN_MASK;  // enable DMA
-    //ADC1_SC2 &= ~ADC_SC2_ADTRG_MASK; // select hardware trigger
-   
-   
-    ADC0_SC3 = ADC_SC3_AVGS(0) | ADC_SC3_AVGE_MASK; // Hardware Average set to 16 samples averaged
-                                                    // select single conversion mode
-    ADC1_SC3 = ADC_SC3_AVGS(0) | ADC_SC3_AVGE_MASK; // Hardware Average set to 16 samples averaged 
-    // Setup Status and Control Register 3 now that calibration is complete
-    ADC0_SC3 = 0; // Hardware Average set to 4 samples averaged
-                  // Hardware Average Disabled
-                  // select single conversion mode
-    ADC1_SC3 = 0; // Hardware Average set to 4 samples averaged
-                  // Hardware Average Disabled
-                  // select single conversion mode
-    //ADC1_SC3 = ADC_SC3_AVGS(0) |  ADC_SC3_AVGE_MASK;//JARED COME BACK AND LOOK AT THIS
-    
-    // Setup Status and Control Register 1A 
-    ADC0_SC1A = 0; // clear register
-    ADC0_SC1A &= ~ADC_SC1_DIFF_MASK; // select single-ended mode
-    ADC0_SC1A |= ADC_SC1_AIEN_MASK;  // enable interrupt (for debugging)
-    ADC0_SC1A |= ADC_SC1_ADCH(12);   // select channel 13
-    ADC1_SC1A = 0; // clear register
-    ADC1_SC1A &= ~ADC_SC1_DIFF_MASK; // select single-ended mode
-    ADC1_SC1A |= ADC_SC1_AIEN_MASK;  // enable interrupt (for debugging)
-    ADC1_SC1A |= ADC_SC1_ADCH(14);   // select channel 14
-    
-    
-    // Check if ADC is finished initializing  TODO:  This part doesn't seem right, but I did it according to 871
-    while( (ADC0_SC1B&ADC_SC1_COCO_MASK)) {}
-    int gain = ADC0_RA; // read the register to clear SC1A[COCO]  //possibly a
-    while( (ADC1_SC1A&ADC_SC1_COCO_MASK)) {}
-    gain = ADC1_RA; // read the register to clear SC1A[COCO]
-    
-    
-//QUESTION
-    ADC0_SC3 &= ~ADC_SC3_ADCO_MASK;  // single conversion mode
-    ADC1_SC3 &= ~ADC_SC3_ADCO_MASK;  // single conversion mode
-    ADC0_SC2 &= ~ADC_SC2_ADTRG_MASK; // set ADC to software trigger
-    ADC1_SC2 &= ~ADC_SC2_ADTRG_MASK; // set ADC to software trigger
-    ADC0_SC1A |= ADC_SC1_ADCH(12);   // write to SC1B causing a trigger
-    ADC1_SC1A |= ADC_SC1_ADCH(14);   // write to SC1A causing a trigger
-}
-
-
-
-void pre_compute_tables() {
-  // This function will precompute the cos and sin tables used in the rest of the program
-  for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){
-    out_val_pre[precompute_counter] = (int) (cos(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter) * 4965.0 + 49650.0);
-    i_mod_pre[precompute_counter] = (cos(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter));
-    q_mod_pre[precompute_counter] = (-sin(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter));
-
-  }
-} //pre_compute_tables
-
-
-
-
-void ISR_repeat() {//40 KHz
-  // Calculation of loop for generating signal to output on DAC
-  
-  WaveOut.write_u16(out_val_pre[phase_counter]);  //creates a wave that bounces between 0 & 3.3 V
-
-  // ISR counter reset and increment
-  if (ISR_counter >= ISR_counter_loop_reset)//10KHz
-  {
-    ISR_counter = 1;
-  //JARED ADD THESE 2 LINES BACK IN
-    ADC0_SC1A |= ADC_SC1_ADCH(12);   // start ADC process Possibly change to B.
-    ADC1_SC1A |= ADC_SC1_ADCH(14);   // start ADC process
-    
-    ADC_reading = true;
-    current_i_mod = i_mod_pre[phase_counter];
-    current_q_mod = q_mod_pre[phase_counter];
-  }
-  else {
-    ISR_counter++;
-  }
-  
-  phase_counter++;
-  if (phase_counter >= pre_compute_length) phase_counter = 0;
-  
-} //ISR_repeat
-
-
-void execute_DSP() {
-/*
-bp_fifo_num++;  
-  if (bp_fifo_num >= bp_fir_length) bp_fifo_num = 0;
-
-   x_0[bp_fifo_num] = (float) (value[1] - value[0]);
-   x_1[bp_fifo_num] = (float) (value[0] - 32768);
-
-   // Band pass filter of incoming data
-  float y_0 = x_0[bp_fifo_num] * bp_filter_coeff[0];
-  float y_1 = x_1[bp_fifo_num] * bp_filter_coeff[0];
-  
-  for(int fir_counter = 1; fir_counter < bp_fir_length; fir_counter++){
-    int fir_index = bp_fifo_num + fir_counter;
-    if (fir_index >= bp_fir_length) {
-      fir_index -= bp_fir_length;
-    }
-    y_0 += x_0[fir_index] * bp_filter_coeff[fir_counter];
-    y_1 += x_1[fir_index] * bp_filter_coeff[fir_counter];
-  }
-*/
-y_0 = (value[0]);
-y_1 = (value[1]);
-  //y_0 = (value[0] - 32768);
-  //y_1 = (value[1] - 32768);
-  current_i_mod = i_mod_pre[phase_counter];
-  current_q_mod = q_mod_pre[phase_counter];
-
-  // Demodulation of the signal
-  x_0_i[lp_fifo_num] = (y_0 * current_i_mod);
-  x_0_q[lp_fifo_num] = (y_0 * current_q_mod);
-  x_1_i[lp_fifo_num] = (y_1 * current_i_mod);
-  x_1_q[lp_fifo_num] = (y_1 * current_q_mod); 
-
-  lp_fifo_num++;  
-  if (lp_fifo_num >= lp_fir_length) lp_fifo_num = 0;
-
-  // Low pass filter of demodulated signal
-  float y_0_i = x_0_i[lp_fifo_num] * lp_filter_coeff[0];
-  float y_0_q = x_0_q[lp_fifo_num] * lp_filter_coeff[0];
-  float y_1_i = x_1_i[lp_fifo_num] * lp_filter_coeff[0];
-  float y_1_q = x_1_q[lp_fifo_num] * lp_filter_coeff[0];
-
-  for(int fir_counter = 1; fir_counter < lp_fir_length; fir_counter++){
-    int fir_index = lp_fifo_num + fir_counter;
-    if (fir_index >= lp_fir_length) {
-      fir_index -= lp_fir_length;
-    }
-    y_0_i += x_0_i[fir_index] * lp_filter_coeff[fir_counter];
-    y_0_q += x_0_q[fir_index] * lp_filter_coeff[fir_counter];
-    y_1_i += x_1_i[fir_index] * lp_filter_coeff[fir_counter];
-    y_1_q += x_1_q[fir_index] * lp_filter_coeff[fir_counter];
-  }  
-   
-  real_ip = y_0_i * y_1_i + y_0_q * y_1_q;
-  imag_ip = y_0_q * y_1_i - y_0_i * y_1_q;
-  y_0_squared = y_0_i * y_0_i + y_0_q * y_0_q; 
-  y_1_squared = y_1_i * y_1_i + y_1_q * y_1_q;
-
-//  float mag = sqrt((real_ip * real_ip + imag_ip * imag_ip) / y_0_squared;
-  mag = sqrt(((float) real_ip * (float) real_ip + (float) imag_ip * (float) imag_ip)) / ((float) y_0_squared);
-  mag_total += mag;
-  calc_mag = mag_total / NumInBlock;
-
-  real_ip_total += real_ip;
-  imag_ip_total += imag_ip;
-  phase = -atan2(imag_ip_total, real_ip_total);
-  calc_phase = phase_total / NumInBlock;
-}
-
-
-
-void setup() { 
-printf("Starting UP");
-  adc_init();
-  pre_compute_tables();
-  wait(1); 
-} //setup
-
-using namespace std;
- 
-int main() {
-    led_blue = 1;
-    led_green = 1;
-    led_red = 0;
-    
-    setup();
-    int count = 0;
-    t1.start();
-    
-    pc.baud(115200);
-    
-    for(int i = 0; i < 86; i++) 
-    {
-        if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2);
-    }
-    
-    // Give hardware associated with 
-    // sampling the highest priority
-    NVIC_SetPriority(ADC1_IRQn,0);
-    NVIC_SetPriority(ADC0_IRQn,0);
-    NVIC_SetPriority(PDB0_IRQn,0);
-    NVIC_SetPriority(DMA0_IRQn,0);
-    NVIC_SetPriority(DMA1_IRQn,0);
-    NVIC_SetPriority(DMA2_IRQn,0);
-    
-    NVIC_SetPriority(ENET_1588_Timer_IRQn,1);
-    NVIC_SetPriority(ENET_Transmit_IRQn,1);
-    NVIC_SetPriority(ENET_Receive_IRQn,1);
-    NVIC_SetPriority(ENET_Error_IRQn,1);
-
-    led_green = 1;
-    led_red = 1;
-    led_blue = 1;
-
-    timer0.attach_us(&ISR_repeat, TimerInterruptInMicroSeconds);
-    while(true) {
-                
-                count++;
-                //printf("Loop");
-                
-                while (!ADC_reading)  
-                {
-                    
-                led_blue = 0;
-                }
-
-            
-              counter++;
-              
-              CyclesInState++;
-              
-              
-              execute_DSP();
-              
-
-                NumInBlock++; 
-                if (NumInBlock >= NumCalculatedAverages) {
-                  printCounter++;
-                  mag_result = calc_mag;
-                  phase_result = calc_phase;
-                  final_mag = mag_result;
-                  final_phase = phase_result;
-                  NumInBlock = 0;
-                  mag_total = 0;
-                  real_ip_total = 0;
-                  imag_ip_total = 0;
-                  if (printCounter>PRINTTIMER)
-                  {
-                      printf("Coeficient%f Phase%f\n\r", final_mag, final_phase);
-                      printCounter = 0;
-                  }
-                  
-                }
-
-              
-                      
-          value[0] = ADC0_RA;//Read adc
-          value[1] = ADC1_RA;//Read adc
-          //printf("%f %f\n\r", value[0], value[1]);
-          ADC_reading = false;//lower flag
-
-    }// end while(true) 
-}// end main
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *      
+ *         
+ *          Pinout for FRDM-k64f                                    
+ *                                  J2
+ *                                  X X 
+ *                                  X X 
+ *                                  X X 
+ *          J3                      X X GND
+ *          X X                     X X SCLK
+ *          X X                     X X MISO
+ *          X X                     X X MOSI
+ *          X X                     X X CS
+ *          X X                 GND X X 
+ *      GND X X                     X X 
+ *      GND X X                         
+ *     5Vin X X                     J1  
+ *                                  X X 
+ *          J4                      X X motorA
+ *          X X                     X X motorB
+ *     mic1 X X                     X X 
+ *     mic2 X X                     X X 
+ *          X X                     X X 
+ *          X X               quadA X X 
+ *          X X               quadB X X 
+ *  
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+#include "mbed.h"
+#include "pause.cpp"
+
+// Sampling
+#include "Sample/adc.h"
+#include "Sample/pdb.h"
+#include "Sample/quad.h"
+
+#include "AngleEncoder.h"
+#include "MotorControl.h"
+
+// for debug purposes
+Serial pc(USBTX, USBRX);
+DigitalOut led_red(LED_RED);
+DigitalOut led_green(LED_GREEN);
+DigitalOut led_blue(LED_BLUE);
+//DigitalIn switch1(PTA4);
+//DigitalIn switch2(PTC6);
+//DigitalOut TotalInd(PTC4);
+//DigitalOut SampleInd(PTC5);
+
+MotorControl motor(PTC2, PTA2, 2000, 25); // cw, ccw, period_us, safetyPeriod_us
+AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz
+
+// defined in dma.cpp
+extern int len;
+extern uint16_t sample_array0[];
+extern uint16_t sample_array1[];
+extern uint16_t angle_array[];
+extern bool dma_done;
+extern bool dma_half_done;
+
+int motorVal = 10;
+
+// Declaration of functions
+void output_data();
+
+Timer t1;
+using namespace std;
+ 
+int main() {
+    
+    led_blue = 1;
+    led_green = 1;
+    led_red = 1;
+    
+    pc.baud(230400);
+    pc.printf("Starting...\r\n");
+    
+    for(int i = 0; i < 86; i++) 
+    {
+        if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2);
+    }
+    
+    // Give hardware associated with 
+    // sampling the highest priority
+    NVIC_SetPriority(ADC1_IRQn,0);
+    NVIC_SetPriority(ADC0_IRQn,0);
+    NVIC_SetPriority(PDB0_IRQn,0);
+    NVIC_SetPriority(DMA0_IRQn,0);
+    NVIC_SetPriority(DMA1_IRQn,0);
+    NVIC_SetPriority(DMA2_IRQn,0);
+    
+    NVIC_SetPriority(ENET_1588_Timer_IRQn,1);
+    NVIC_SetPriority(ENET_Transmit_IRQn,1);
+    NVIC_SetPriority(ENET_Receive_IRQn,1);
+    NVIC_SetPriority(ENET_Error_IRQn,1);
+    
+    //quad_init(); // initialize FTM2 quadrature decoder
+    //quad_invert(); // invert the direction of counting
+    adc_init(); // initialize ADCs (always initialize adc before dma)
+    dma_init(); // initializes DMAs
+    pdb_init(); // initialize PDB0 as the timer for ADCs and DMA2 
+    
+    // flash green led indicating startup complete
+    led_red = 1;
+    led_blue = 1;
+    led_green = 0;
+    pause_ms(500);
+    led_green = 1;
+    pause_ms(200);
+    led_green = 0;
+    pause_ms(500);
+    led_green = 1;
+    pdb_start();
+    t1.start();
+    t1.reset();
+    int startAddress = (int)&sample_array0[0];
+    while (1)
+    {
+        if (t1.read()>2)
+        {
+            //pdb_start();
+            pc.printf("%i\t%i\t%i\r\n", sample_array0[1], sample_array1[1],(DMA_TCD0_DADDR-startAddress)/2);
+            t1.reset();
+        }
+    }
+ //   while(1) {
+//        if(pc.readable() > 0) {
+//            char temp = pc.getc();
+//            
+//            switch(temp) {
+//                case '1': // run motor and sample
+//                {
+//                    // wait for angle to set to zero
+//                    led_blue = 0;
+//                    while(!angle_encoder.set_zero()) {pc.printf("AngleNotSet");}
+//                    quad_reset();
+//                    led_blue = 1;
+//                    
+//                    // release mallet                                
+//                    motor.releaseMallet();
+//                    
+//                    // wait for mallet to drop to certin point before beginning to sample
+//                    /*
+//                    led_red = 0;
+//                    int angleVal;
+//                    do {
+//                        angleVal = angle_encoder.absolute_angle();
+//                        }
+//                    while(!(angleVal > 150 && angleVal < 400));
+//                    led_red = 1;
+//                    */
+//                    
+//                    // begin sampling
+//                    pdb_start();
+//                    //pause_us(TOTAL_SAMPLES*10);
+//                    //while(!dma_done) {led_green = 0;}
+//                    led_green = 1;
+//                    
+//                    // reset mallet
+//                    motor.reset();
+//                    output_data();
+//                    
+//                    
+//                    led_red = 1;
+//                    led_blue = 1;
+//                    led_green = 1;
+//                    }
+//                    break;
+//                
+//                case '2':
+//                {
+//                    motor.releaseMallet();
+//                    pause(1);
+//                    
+//                    while(!angle_encoder.set_zero()) {}
+//                    quad_reset();
+//                    
+//                    pdb_start();
+//                    motor.clockwise(1);
+//                    pause(1);
+//                    motor.off();
+//                    output_data();
+//                    
+//                    }
+//                    break;
+//                
+//                case '3':
+//                {    
+//                    /*
+//                    while(angleVar)
+//                    {
+//                        counter++;
+//                        angleVar = angle_encoder.absolute_angle();
+//                        angleVar -= motorVal;
+//                        if(angleVar == 0x00ff0000) {} // do nothing
+//                        //else if(angleVar > 340) motorB.pulsewidth_us(8000); // max speed 
+//                        else if(angleVar < 43) {
+//                            angleVar = 0; // exit loop
+//                            //motorB.pulsewidth_us(0);} // min speed
+//                        //else motorB.pulsewidth_us(angleVar*800/34);
+//                        }
+//                    motor.off();
+//                    */
+//                    }
+//                    break;    
+//                
+//                
+//                /********************************************************************
+//                * The code below is for testing and troubleshooting.  Don't delete. *
+//                ********************************************************************/
+//                case 'B':
+//                case 'b':
+//                    led_blue = !led_blue;
+//                    pc.printf("Blue LED\r\n");
+//                    break;
+//                case 'R':
+//                case 'r':
+//                    led_red = !led_red;
+//                    pc.printf("Red LED\r\n");
+//                    break;
+//                 
+//                // test sampling
+//                case 'Q':
+//                case 'q':
+//                    quad_reset();
+//                    pdb_start();
+//                    
+//                    while(!dma_half_done) {
+//                        pc.printf("first half\r\n");
+//                        pause_ms(1);
+//                        }
+//                    
+//                    while(!dma_done) {
+//                        pc.printf("second half\r\n");
+//                        pause_ms(1);
+//                        }
+//                    for(int i = 0; i < len; i++) pc.printf("%i\t %i\t %i\r\n", sample_array0[i],sample_array1[i], angle_array[i]);
+//                    pc.printf("\r\n");
+//                    
+//                    led_red = 1;
+//                    led_green = 1;
+//                    led_blue = 1;
+//                    
+//                    
+//                    /*
+//                    while(!angle_encoder.set_zero()) {}
+//                    quad_reset();
+//                    
+//                    
+//                    pdb_start();
+//                    pause_us(TOTAL_SAMPLES*10);
+//                    output_data();*/
+//                    
+//                    break;
+//                case 'W':
+//                case 'w': // reads 0 unless the counter is running
+//                    pc.printf("PDB: %i\r\n",PDB0_CNT);
+//                    break;
+//                
+//                // test angle encoder
+//                case 'A':
+//                case 'a': // set zero
+//                    if(angle_encoder.set_zero()) {
+//                        pc.printf("Zero set\r\n");
+//                    }
+//                    else {
+//                        pc.printf("Zero NOT set\r\n");
+//                    }
+//                    break;
+//                case 'S':
+//                case 's': // perform "no operation", which returns 0xa5
+//                {
+//                    pc.printf("NOP: 0x%02x\r\n", angle_encoder.nop());
+//                    break;
+//                }
+//                case 'D':
+//                case 'd': // read absolute and relative angles
+//                {
+//                    pc.printf("Angle: %i %i\r\n", angle_encoder.absolute_angle(), quad_read());
+//                    break;
+//                }
+//                case 'F':
+//                case 'f':
+//                    pc.printf("Quad Cnt: %i\r\n", quad_read());
+//                    break;
+//                    
+//                // test motor
+//                case 'Z':
+//                case 'z': // release the mallet
+//                    motor.releaseMallet();
+//                    break;
+//                case 'X':
+//                case 'x':    
+//                    motor.hardReset(motorVal);
+//                    break;
+//                case '+':
+//                case '=':
+//                    motorVal++;
+//                    if(motorVal > 8000) motorVal = 8000;
+//                    pc.printf("MotorVal: %i\r\n", motorVal);
+//                    break;
+//                case '-':
+//                {
+//                    motorVal--;
+//                    if(motorVal < 0) motorVal = 0;
+//                    pc.printf("MotorVal: %i\r\n", motorVal);
+//                    break;
+//                }
+//            }
+//        }
+//    }
+}
+
+void output_data()
+{
+    for (int n = 0; n < len; n++) {
+        pc.printf("%i\t%i\r\n", sample_array0[n], sample_array1[n]);
+        
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pause.cpp	Thu Oct 29 17:15:20 2015 +0000
@@ -0,0 +1,17 @@
+#ifndef PAUSE_CPP
+#define PAUSE_CPP
+#include "mbed.h"
+// these are various delays
+inline void pause(uint32_t seconds) {
+    for(seconds; seconds > 0; seconds--) for(uint32_t i = 0x1ffffff; i > 0; i--) asm("nop");
+}
+
+inline void pause_ms(uint32_t milliseconds) {
+    for(milliseconds; milliseconds > 0; milliseconds--) for(uint32_t i = 0x8312; i > 0; i--) asm("nop");
+}
+
+inline void pause_us(uint32_t microseconds) {
+    for(microseconds; microseconds > 0; microseconds--) for(uint32_t i = 0x21; i > 0; i--) asm("nop");
+}
+
+#endif 
\ No newline at end of file