OSDN Git Service

[ADC] Measure battery with MCU's FIXED-VOLTAGE-REFERENCE, not use AKC6955's ADC.
authorK.Ohta <whatisthis.sowhat@gmail.com>
Tue, 1 Oct 2013 16:43:05 +0000 (01:43 +0900)
committerK.Ohta <whatisthis.sowhat@gmail.com>
Tue, 1 Oct 2013 16:43:05 +0000 (01:43 +0900)
[I2C] Make i2c_writechar() for XC8.

adc_int.c
adc_int.h
i2c_eeprom.c
iodef.h
lcd_acm1602.c
nbproject/Makefile-genesis.properties
radio_getstat.c

index df2c628..f71dd2b 100644 (file)
--- a/adc_int.c
+++ b/adc_int.c
@@ -69,9 +69,53 @@ unsigned int polladc(void)
     }
 }
 
+/*
+ *    ADCON1bits.VCFG1 = 0;
+ *    ADCON1bits.VCFG0 = 0;
+ *    ADCON2 = 0b10110110;
+ */
+void startadc(unsigned char ch)
+{
+#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);
+    ADCON0bits.GO = 1;
+#endif
+}
 
+#ifdef __SDCC
+void stopadc(void)
+{
+    ADCON0bits.GO_DONE = 0;
+    PIR1bits.ADIF = 0;
+    PIE1bits.ADIE = 0;
+}
+#endif
+
+unsigned char polladc2(void)
+{
+    unsigned int a;
+    PIE1bits.ADIE = 0;
+    PIR1bits.ADIF = 0;
+    while (ADCON0bits.DONE != 0)
+    { // converting or not enable.
+        idle_time_ms(1);
+    }
+   a = ((ADRESH << 8)  + ADRESL) & 0x03ff;
+   ADCON0bits.GO_DONE = 0;
+   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 * 4) / reflevel) * (1024/1000)* 120; // Divide = 4
+    reflevel = reflevel / 4;
+    return ((b * 120) / reflevel) * 4 / 4;// Divide = 4
 }
\ No newline at end of file
index 7f7c782..5b7fe0f 100644 (file)
--- a/adc_int.h
+++ b/adc_int.h
@@ -44,56 +44,13 @@ extern "C" {
 #endif
 extern void intadc_init(void);
 extern unsigned int polladc(void);
-#if 0
+extern unsigned char polladc2(void);
 extern void startadc(unsigned char ch);
-extern unsigned int polladc2(void);
-#else
 #ifdef __SDCC
-#define polladc2(a) { \
-    while(adc_busy()); \
-    a = adc_read(); \
-    }
-
-#define stopadc() { \
-    ADCON0bits.GO_DONE = 0; \
-    PIR1bits.ADIF = 0; \
-    PIE1bits.ADIE = 0; \
-    }
-/*
- *    ADCON1bits.VCFG1 = 0;
- *    ADCON1bits.VCFG0 = 0;
- *    ADCON2 = 0b10110110;
- */
-#define startadc(c) { \
-    adc_open(c, ADC_FOSC_64, 0, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS | ADC_NVCFG_VSS | ADC_PVCFG_VDD); \
-    adc_setchannel(c); \
-    adc_conv(); \
-    }
-#else
-#define polladc2(a) { \
-    while(BusyADC()); \
-    a = ReadADC(); \
-    }
-
-#define stopadc() { \
-    CloseADC(); \
-    }
-/*
- *    ADCON1bits.VCFG1 = 0;
- *    ADCON1bits.VCFG0 = 0;
- *    ADCON2 = 0b10110110;
- */
-#define startadc(c) { \
-    OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_20_TAD,  (ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS & (0x0f << 2)) | (c << 2) , 0x0001 << c); \
-    ADCON0bits.GO = 1; \
-    }
-
-#endif
+extern void stopadc(void);
 #endif
 extern unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel);
 
-
-
 #ifdef __cplusplus
 }
 #endif
index cace0ba..916b440 100644 (file)
 
 #include "i2c_eeprom.h"
 
+
+#ifndef __SDCC
+void i2c_writechar(unsigned char c)
+{
+    WriteI2C(c);
+    while(SSPCON2bits.ACKSTAT);
+}
+#endif
+
+
 static unsigned char i2c_eeprom_write_1block(unsigned char i2caddr, unsigned int addr, unsigned char *data, unsigned int size)
 {
     unsigned char i;
@@ -54,22 +64,16 @@ static unsigned char i2c_eeprom_write_1block(unsigned char i2caddr, unsigned int
     StartI2C();
     while(SSPCON2bits.SEN);
 
-    WriteI2C(i2caddr & 0xfe);
-    while(SSPCON2bits.ACKSTAT);
-
-    WriteI2C(page_hi);
-    while(SSPCON2bits.ACKSTAT);
-
-    WriteI2C(page_lo);
-    while(SSPCON2bits.ACKSTAT);
+    i2c_writechar(i2caddr & 0xfe);
+    i2c_writechar(page_hi);
+    i2c_writechar(page_lo);
 
     if(!SSPCON2bits.ACKSTAT){
         for(i = 0; i < size ; i++){
             if(SSPCON1bits.SSPM3) {
-                WriteI2C(*p);
-                while(SSPCON2bits.ACKSTAT);
-            IdleI2C();
-            p++;
+                i2c_writechar(*p);
+                IdleI2C();
+                p++;
             }
         }
     }
@@ -89,6 +93,8 @@ static unsigned char i2c_eeprom_write_1block(unsigned char i2caddr, unsigned int
     return 0xff;
 }
 
+
+
 // I2C_IO
 unsigned char i2c_eeprom_bytewrite(unsigned char i2caddr, unsigned int addr, unsigned char data)
 {
@@ -111,19 +117,13 @@ unsigned char i2c_eeprom_bytewrite(unsigned char i2caddr, unsigned int addr, uns
     StartI2C();
     while(SSPCON2bits.SEN);
 
-    WriteI2C(i2caddr & 0xfe);
-    while(SSPCON2bits.ACKSTAT);
-
-    WriteI2C(page_hi);
-    while(SSPCON2bits.ACKSTAT);
-
-    WriteI2C(page_lo);
-    while(SSPCON2bits.ACKSTAT);
-
-    WriteI2C(data);
-    while(SSPCON2bits.ACKSTAT);
+    i2c_writechar(i2caddr & 0xfe);
+    i2c_writechar(page_hi);
+    i2c_writechar(page_lo);
+    i2c_writechar(data);
 
     StopI2C();
+
     while(SSPCON2bits.PEN);
     do {
         StartI2C();
@@ -187,11 +187,11 @@ static unsigned char i2c_eeprom_read_1block(unsigned char i2caddr, unsigned int
     I2C_START();
     i2c_writechar(addr | 0x01);
     for(i = 0; i < I2C_ROM_PAGE_SIZE - 1 ; i++){
-        *p = i2c_readchar();
-        p++;
+        *data = i2c_readchar();
+        data++;
         I2C_ACK();
     }
-    *p = i2c_readchar();
+    *data = i2c_readchar();
     I2C_NACK();
     I2C_STOP();
     i2c_close();
diff --git a/iodef.h b/iodef.h
index 03e3553..6935d98 100644 (file)
--- a/iodef.h
+++ b/iodef.h
@@ -123,6 +123,7 @@ extern "C" {
 #define TRIS_E_VAL   0b00000100 /* FOR OUTPUT, ADC, PSP=OFF */
 
 #define _BATT_ADC AN7
+#define _REF_ADC 0x0f
 #define _ADC_INITF ANS7
 #define _ADCH_INITF 0x00
 
index 5def4b4..d33af14 100644 (file)
@@ -110,7 +110,7 @@ unsigned char acm1602_getchar(unsigned char addr)
 static void _ACM1602_TC_WAIT(void)
 {
 #ifdef __SDCC
-   delay10tcy(4)
+   delay10tcy(4);
 #else
    __delay_us(5);
 #endif
index 2c44b3d..5c8903a 100644 (file)
@@ -1,5 +1,5 @@
 #
-#Tue Oct 01 21:09:36 JST 2013
+#Wed Oct 02 00:34:28 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
index d60a260..f5f4a84 100644 (file)
@@ -61,6 +61,7 @@ void update_status(void)
 {
 
     unsigned int adc;
+    unsigned int ref;
 
     setup.fm = akc6955_get_fm();
     recv_signal = akc6955_read_level();
@@ -83,12 +84,16 @@ void update_status(void)
     tuneflag = akc6955_tune();
     cnrlevel = akc6955_get_cnr();
     // Battery
-    batlevel_6955 = akc6955_get_battery();
-    // ADC
+//    batlevel_6955 = akc6955_get_battery();
+    //ADC:Battery
     startadc(ADC_BATT_CH);
-    do {
-        idle_time_ms(1);
-        adc = polladc();
-    } while(adc == 0xffff);
-    battlevel = adc_rawtobatt(adc, batlevel_6955);
+    adc = polladc2();
+    //ADC:reference
+    CVRCON2bits.FVREN = 1;
+    while(CVRCON2bits.FVRST == 0);
+    startadc(_REF_ADC);
+    ref = polladc2();
+    CVRCON2bits.FVREN = 0;
+    //ADC:reference
+    battlevel = adc_rawtobatt(adc, ref);
 }