OSDN Git Service

[ADC][Battery] Fix stability, but still not collect.
authorK.Ohta <whatisthis.sowhat@gmail.com>
Thu, 3 Oct 2013 05:23:33 +0000 (14:23 +0900)
committerK.Ohta <whatisthis.sowhat@gmail.com>
Thu, 3 Oct 2013 05:23:33 +0000 (14:23 +0900)
adc_int.c
iodef.h
main.c
nbproject/Makefile-genesis.properties
power.c
radio_getstat.c

index f71dd2b..c58898c 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,6 +66,7 @@ unsigned int polladc(void)
     } else { // Done, Clear interrupt
         a = ((ADRESH << 8)  + ADRESL) & 0x03ff;
      ADCON0bits.GO_DONE = 0;
+     ADCON0bits.ADON = 0;
      return a;
     }
 }
@@ -76,12 +78,24 @@ unsigned int polladc(void)
  */
 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
-    OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_20_TAD,  (ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS & (0x0f << 2)) | (ch << 2) , 0x0001 << ch);
+    if(ch < 16) {
+        config1 = 0b10101101; //                ADC_RIGHT_JUST & ADC_FOSC_32 & ADC_16_TAD & ADC_INT_OFF,
+        config2 = ch << 3;
+        ansel_val = 0x0001 << ch;
+    } else {
+        config1 = 0b11101111; //                ADC_RIGHT_JUST & ADC_FOSC_64 & ADC_20_TAD & ADC_INT_OFF,
+        config2 = 0b00111000;
+        ansel_val = 0x00;
+    }
+    OpenADC(config1, config2, ansel_val);
+//    idle_time_ms(2); // 64OSC * 20 = 1280OSC < 1ms
     ADCON0bits.GO = 1;
 #endif
 }
@@ -100,12 +114,13 @@ unsigned char polladc2(void)
     unsigned int a;
     PIE1bits.ADIE = 0;
     PIR1bits.ADIF = 0;
-    while (ADCON0bits.DONE != 0)
-    { // converting or not enable.
+    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;
    return a;
 }
 
@@ -116,6 +131,6 @@ 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
-    reflevel = reflevel / 4;
-    return ((b * 120) / reflevel) * 4 / 4;// 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.
 }
\ No newline at end of file
diff --git a/iodef.h b/iodef.h
index 6935d98..2aecfa1 100644 (file)
--- a/iodef.h
+++ b/iodef.h
@@ -122,8 +122,8 @@ extern "C" {
 
 #define TRIS_E_VAL   0b00000100 /* FOR OUTPUT, ADC, PSP=OFF */
 
-#define _BATT_ADC AN7
-#define _REF_ADC 0x0f
+#define _BATT_ADC 0x07
+#define _REF_ADC 0x10
 #define _ADC_INITF ANS7
 #define _ADCH_INITF 0x00
 
diff --git a/main.c b/main.c
index f9a40f7..23adba6 100644 (file)
--- a/main.c
+++ b/main.c
@@ -302,7 +302,8 @@ int main(void)
     update_display();
     ClrWdt();
     do {
-            if(battlevel <= 340) { // 3.4V
+#if 1
+        if(battlevel < 340) { // 3.4V
                 lvcount++;
                 if(lvcount > 4) {
                     if(dispf == 0) {
@@ -314,6 +315,7 @@ int main(void)
             } else {
                 lvcount = 0;
             }
+#endif
         /* Main routine*/
             c = pollkey_single_timeout(41, 1); // 23*41 = 943ms
            p = 0;
index 5c8903a..6d821c5 100644 (file)
@@ -1,5 +1,5 @@
 #
-#Wed Oct 02 00:34:28 JST 2013
+#Thu Oct 03 11:16:09 JST 2013
 default.languagetoolchain.dir=/opt/microchip/xc8/v1.20/bin
 com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=0d2b1469ad71adb787c711a416386331
 default.languagetoolchain.version=1.20
diff --git a/power.c b/power.c
index 5cd64c7..3d3c060 100644 (file)
--- a/power.c
+++ b/power.c
@@ -137,18 +137,18 @@ unsigned char chk_powerbutton(void)
     return 0; // Not pressed
 }
 
-void shutdown(unsigned char save)
+void shutdown(unsigned char savef)
 {
     rfamp_power(RFAMP_OFF);
     acm1602_suspend(LCD_I2CADDR);
-    power_off(save);
+    power_off(savef);
 }
 
-void power_off(unsigned char save)
+void power_off(unsigned char savef)
 {
     unsigned char sts;
 
-    if(save != 0) {
+    if(savef != 0) {
         save_eeprom();
         save_userbands();
     }
index 43ef5ae..647fb75 100644 (file)
@@ -90,4 +90,5 @@ void update_status(void)
     CVRCON2bits.FVREN = 0;
     //ADC:reference
     battlevel = adc_rawtobatt(adc, ref);
+//    battlevel = ref;
 }