OSDN Git Service

[ADC][v2.0] Fix correct ADC value.
authorK.Ohta <whatisthis.sowhat@gmail.com>
Thu, 10 Oct 2013 06:22:46 +0000 (15:22 +0900)
committerK.Ohta <whatisthis.sowhat@gmail.com>
Thu, 10 Oct 2013 06:22:46 +0000 (15:22 +0900)
adc_int.c
adc_int.h
main.c
radio_getstat.c

index c58898c..13a21b6 100644 (file)
--- a/adc_int.c
+++ b/adc_int.c
@@ -85,18 +85,30 @@ void startadc(unsigned char ch)
     adc_setchannel(ch);
     adc_conv();
 #else
-    if(ch < 16) {
-        config1 = 0b10101101; //                ADC_RIGHT_JUST & ADC_FOSC_32 & ADC_16_TAD & ADC_INT_OFF,
-        config2 = ch << 3;
+    PIE1bits.ADIE = 0;
+    PIR1bits.ADIF = 0;
+    if(ch < _REF_ADC) {
+        ADCON0 = 0;
+        ADCON2 = 0b00000110; // 64OSC, Right
+        ADCON1 = 0b00000000; // VREF+=VDD,VREF-=VSS
+        ADCON2 |= 0b10111000; // TAQ=20TAD, Right
+        ADCON0 = ch << 2;
+
         ansel_val = 0x0001 << ch;
+//        ansel_val = 0x0080;
     } else {
-        config1 = 0b11101111; //                ADC_RIGHT_JUST & ADC_FOSC_64 & ADC_20_TAD & ADC_INT_OFF,
-        config2 = 0b00111000;
-        ansel_val = 0x00;
+        ADCON0 = 0;
+        ADCON2 = 0b00000110; // 64OSC, Right
+        ADCON1 = 0b00000000; // VREF+=VDD,VREF-=VSS
+        ADCON2 |= 0b10111000; // TAQ=20TAD, Right
+        ADCON0 = 0x0f << 2;
+       ansel_val = 0x0000;
     }
-    OpenADC(config1, config2, ansel_val);
-//    idle_time_ms(2); // 64OSC * 20 = 1280OSC < 1ms
-    ADCON0bits.GO = 1;
+    ANSELH = ansel_val >> 8;
+    ANSEL = ansel_val & 0xff;
+    ADCON0bits.ADON = 1;
+    idle(160); // 64*20 = 1280clock = 160us
+    ADCON0bits.GO_DONE = 1;
 #endif
 }
 
@@ -109,18 +121,15 @@ void stopadc(void)
 }
 #endif
 
-unsigned char polladc2(void)
+unsigned int polladc2(void)
 {
     unsigned int a;
-    PIE1bits.ADIE = 0;
-    PIR1bits.ADIF = 0;
-    do { // converting or not enable.
-        idle_time_ms(1);
-    }     while (ADCON0bits.DONE != 0);
-
-   a = ((ADRESH << 8)  + ADRESL) & 0x03ff;
-   ADCON0bits.GO_DONE = 0;
-   ADCON0bits.ADON = 0;
+//    PIE1bits.ADIE = 0;
+//    PIR1bits.ADIF = 0;
+    do {
+        idle(50); // wait about 50us
+        a = polladc();
+    } while((a  & 0x8000) != 0);
    return a;
 }
 
@@ -130,7 +139,5 @@ unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel)
 {
 
     // raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
-//    return ((b * 4) / reflevel) * (1024/1000)* 120; // Divide = 4
-    return ((b * 120) / reflevel) * 4;// Divide = 4, but input impedance of PIC is lower ;-) ,
-                                                               // about 0.8 (x 4) mesure value, multiply 32/10.
+    return ((b * 120) / reflevel) * 4;
 }
\ No newline at end of file
index 5b7fe0f..bb13f8d 100644 (file)
--- a/adc_int.h
+++ b/adc_int.h
@@ -44,7 +44,7 @@ extern "C" {
 #endif
 extern void intadc_init(void);
 extern unsigned int polladc(void);
-extern unsigned char polladc2(void);
+extern unsigned int polladc2(void);
 extern void startadc(unsigned char ch);
 #ifdef __SDCC
 extern void stopadc(void);
diff --git a/main.c b/main.c
index 23adba6..5e3af67 100644 (file)
--- a/main.c
+++ b/main.c
@@ -286,7 +286,7 @@ int main(void)
     }
     WDTCONbits.SWDTEN = 1; // WDT ON.
     power_on(1);
-    intadc_init();
+    //intadc_init();
     set_powerlamp(1);
     valinit();
     acm1602_init(LCD_I2CADDR, 1); //Init LCD
@@ -303,7 +303,7 @@ int main(void)
     ClrWdt();
     do {
 #if 1
-        if(battlevel < 340) { // 3.4V
+        if(battlevel < 330) { // 3.3V
                 lvcount++;
                 if(lvcount > 4) {
                     if(dispf == 0) {
index 647fb75..ec2357a 100644 (file)
@@ -79,16 +79,25 @@ void update_status(void)
     cnrlevel = akc6955_get_cnr();
     // Battery
 //    batlevel_6955 = akc6955_get_battery();
-    //ADC:Battery - AN7, 28pins PIC should be changed AN number.
-    startadc(_BATT_ADC);
-    adc = polladc2();
+  
     //ADC:reference
     CVRCON2bits.FVREN = 1;
-    while(CVRCON2bits.FVRST == 0) idle_time_ms(1);
+    while(CVRCON2bits.FVRST == 0) idle(100);
+    // Workaround of Errata:
+    // Section 5 of http://ww1.microchip.com/downloads/en/DeviceDoc/80000425K.pdf .
+    // Sample twice on first.
     startadc(_REF_ADC);
     ref = polladc2();
+    startadc(_REF_ADC);
+    ref = polladc2();
+    TRISEbits.TRISE2 = 1;
+    startadc(_BATT_ADC);
+    adc = polladc2();
+    startadc(_BATT_ADC);
+    adc = polladc2();
     CVRCON2bits.FVREN = 0;
-    //ADC:reference
     battlevel = adc_rawtobatt(adc, ref);
-//    battlevel = ref;
+//    battlevel = adc *  32 * 4; // 322 = 1000/1024 * 330
+//    battlevel = (adc * 4 * 32) / 100;
 }
+