OSDN Git Service

[ADC] Use sdcc's adc library, try to reduce program-size.
[openi2cradio/OpenI2CRadio.git] / adc_int.c
index d2d7422..326a0a1 100644 (file)
--- a/adc_int.c
+++ b/adc_int.c
@@ -24,9 +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_int.h"
-
 void intadc_init(void)
 {
 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
@@ -47,6 +46,8 @@ void intadc_init(void)
     IPR1bits.ADIP = 1; // High
 }
 
+
+#if 0
 void startadc(unsigned char ch)
 {
     unsigned char a;
@@ -110,15 +111,28 @@ void stopadc(void)
     PIR1bits.ADIF = 0;
     PIE1bits.ADIE = 0;
 }
+#else // Using sdcc's library.
+
+unsigned int polladc(void)
+{
+    unsigned int a;
+    if(ADCON0bits.DONE == 1){ // converting or not enable.
+        PIE1bits.ADIE = 1;
+        PIR1bits.ADIF = 0;
+        return 0xffff;
+    } else { // Done, Clear interrupt
+        a = ((ADRESH << 8)  + ADRESL) & 0x03ff;
+     PIE1bits.ADIE = 0;
+     PIR1bits.ADIF = 0;
+     ADCON0bits.GO_NOT_DONE = 0;
+     return a;
+    }
+}
+
+#endif
 
 unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel)
 {
-    unsigned int i;
     // raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
-    // Fullscale = 0.5 (1.625V, raw = 6.5V)
-    // raw = b * 0.5 * 4 = b * 2 (b = 1024 6.500V)
-    i = b / 8; // div = 1/8
-    // Fullscale = 13.2v =
-    i = (i * reflevel) / 32;
-    return i;
+    return ((b >>2) * reflevel) >> 6;
 }
\ No newline at end of file