OSDN Git Service

[POWER][v2.0] Fix right shutdown sequence.
[openi2cradio/OpenI2CRadio.git] / adc_int.c
index df2c628..b51bf1b 100644 (file)
--- a/adc_int.c
+++ b/adc_int.c
@@ -24,7 +24,8 @@
  *  This exception does not however invalidate any other reasons why
  *  the executable file might be covered by the GNU General Public License.
  */
-#include "adc.h"
+
+//#include "adc.h"
 #include "adc_int.h"
 void intadc_init(void)
 {
@@ -65,13 +66,73 @@ unsigned int polladc(void)
     } else { // Done, Clear interrupt
         a = ((ADRESH << 8)  + ADRESL) & 0x03ff;
      ADCON0bits.GO_DONE = 0;
+     ADCON0bits.ADON = 0;
      return a;
     }
 }
 
+/*
+ *    ADCON1bits.VCFG1 = 0;
+ *    ADCON1bits.VCFG0 = 0;
+ *    ADCON2 = 0b10110110;
+ */
+void startadc(unsigned char ch)
+{
+    unsigned int ansel_val;
+    unsigned char config1, config2;
+#ifdef __SDCC
+    adc_open(ch, ADC_FOSC_64, 0, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS | ADC_NVCFG_VSS | ADC_PVCFG_VDD);
+    adc_setchannel(ch);
+    adc_conv();
+#else
+    PIE1bits.ADIE = 0;
+    PIR1bits.ADIF = 0;
+    ADCON0 = 0;
+    ADCON2 = 0b00000110; // 64OSC, Right
+    ADCON1 = 0b00000000; // VREF+=VDD,VREF-=VSS
+    ADCON2 |= 0b10111000; // TAQ=20TAD, Right
+    if(ch < _REF_ADC) {
+        ADCON0 = ch << 2;
+        ansel_val = 0x0001 << ch;
+//        ansel_val = 0x0080;
+    } else {
+        ADCON0 = 0x0f << 2;
+       ansel_val = 0x0000;
+    }
+    ANSELH = ansel_val >> 8;
+    ANSEL = ansel_val & 0xff;
+    ADCON0bits.ADON = 1;
+    idle(160); // 64*20 = 1280clock = 160us
+    ADCON0bits.GO_DONE = 1;
+#endif
+}
+
+#ifdef __SDCC
+void stopadc(void)
+{
+    ADCON0bits.GO_DONE = 0;
+    PIR1bits.ADIF = 0;
+    PIE1bits.ADIE = 0;
+}
+#endif
 
+unsigned int polladc2(void)
+{
+    unsigned int a;
+//    PIE1bits.ADIE = 0;
+//    PIR1bits.ADIF = 0;
+    do {
+        idle(50); // wait about 50us
+        a = polladc();
+    } while(a  == 0xffff);
+   return a;
+}
+
+// Masure raw battery voltage, Reference = 1.20V±4%
+// Multiply = 39:13 = 4:1.
 unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel)
 {
+
     // raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
-    return ((b >>2) * reflevel) >> 6;
+    return ((b * 120) / reflevel) * 4;
 }
\ No newline at end of file