UAVX Multicopter Flight Controller.

Dependencies:   mbed

Revision:
1:1e3318a30ddd
Parent:
0:62a1c91a859a
Child:
2:90292f8bd179
--- a/analog.c	Fri Feb 18 22:28:05 2011 +0000
+++ b/analog.c	Fri Feb 25 01:35:24 2011 +0000
@@ -20,6 +20,7 @@
 
 #include "UAVXArm.h"
 
+real32 ADC(uint8);
 void GetBattery(void);
 void BatteryTest(void);
 void InitBattery(void);
@@ -28,19 +29,95 @@
 real32 BatteryCharge, BatteryCurrent;
 real32 BatteryVoltsScale;
 
+void DirectAnalog(void) {
+// Framework Simon Ford
+
+    static uint32 data;
+
+    // Select channel and start conversion
+    LPC_ADC->ADCR &= ~0xFF;
+    LPC_ADC->ADCR |= 1 << 5; // ADC0[5]
+    LPC_ADC->ADCR |= 1 << 24;
+
+    // Repeatedly get the sample data until DONE bit
+    do {
+        data = LPC_ADC->ADGDR;
+    } while ((data & ((uint32)1 << 31)) == 0);
+
+    // Stop conversion
+    LPC_ADC->ADCR &= ~(1 << 24);
+
+} // DirectAnalog
+
+void InitDirectAnalog(void) {
+
+// power on, clk divider /4
+    LPC_SC->PCONP |= (1 << 12);
+    LPC_SC->PCLKSEL0 &= ~(0x3 << 24);
+
+    // software-controlled ADC settings
+    LPC_ADC->ADCR = (0 << 0) // SEL: 0 = no channels selected
+                    | (25 << 8)    // CLKDIV: PCLK max ~= 25MHz, /25 to give safe 1MHz
+                    | (0 << 16)    // BURST: 0 = software control
+                    | (0 << 17)    // CLKS: not applicable
+                    | (1 << 21)    // PDN: 1 = operational
+                    | (0 << 24)    // START: 0 = no start
+                    | (0 << 27);   // EDGE: not applicable
+
+    // setup P1_31 as sel 3 (ADC), mode 2 (no pull)
+    LPC_PINCON->PINSEL3 &= ~((uint32)0x3 << 30);
+    LPC_PINCON->PINSEL3 |= (uint32)0x3 << 30;
+
+    LPC_PINCON->PINMODE3 &= ~((uint32)0x3 << 30);
+    LPC_PINCON->PINMODE3 |= (uint32)0x2 << 30;
+
+} // InitDirectAnalog
+
+real32 ADC(uint8 p) {
+
+    static real32 r;
+
+DebugPin = 1;
+    switch (p) {
+        case ADCPitch:
+            r = PitchADC.read();
+            break;
+        case ADCRoll:
+            r = RollADC.read();
+            break;
+        case ADCYaw:
+            r = YawADC.read();
+            break;
+        case ADCRangefinder:
+            r = RangefinderADC.read();
+            break;
+        case ADCBatteryCurrent:
+            r = BatteryCurrentADC.read();
+            break;
+        case ADCBatteryVolts:
+            r =  BatteryVoltsADC.read();
+            break;
+    }
+    
+  DebugPin = 0;
+
+    return ( r );
+
+} // ADC
+
 void GetBattery(void) {
     //  AttoPilot Voltage and Current Sense Breakout SparkFun Part: SEN-09028
 
     const real32 BatteryCurrentScale = 90.15296;    // Amps FS
 
     if ( F.HaveBatterySensor ) {
-        BatteryCurrent = BatteryCurrentADC.read() * BatteryCurrentScale;
+        BatteryCurrent = ADC(ADCBatteryCurrent) * BatteryCurrentScale;
         BatteryChargeUsedAH  += BatteryCurrent * (real32)(mSClock() - mS[LastBattery]) * 2.777777e-7;
         mS[LastBattery] = mSClock();
     } else
         BatteryCurrent =  BatteryChargeUsedAH = 0;
 
-    BatteryVolts = BatteryVoltsADC.read() *  BatteryVoltsScale;
+    BatteryVolts = ADC(ADCBatteryVolts) *  BatteryVoltsScale;
     F.LowBatt = BatteryVolts < K[LowVoltThres];
 
 } // GetBattery
@@ -92,7 +169,7 @@
 void GetRangefinderAltitude(void) {
 
     if ( F.RangefinderAltitudeValid ) {
-        RangefinderAltitude = RangefinderADC.read() * RangefinderScale;
+        RangefinderAltitude = ADC(ADCRangefinder) * RangefinderScale;
         if ( F.RFInInches )
             RangefinderAltitude *= 2.54;