3 * Internal ADCONVERTER Handler
4 * Copyright (C) 2013-06-20 K.Ohta <whatisthis.sowhat ai gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
10 * This library / program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; see the file COPYING. If not, write to the
17 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 * As a special exception, if you link this(includeed from sdcc) library
21 * with other files, some of which are compiled with SDCC,
22 * to produce an executable, this library does not by itself cause
23 * the resulting executable to be covered by the GNU General Public License.
24 * This exception does not however invalidate any other reasons why
25 * the executable file might be covered by the GNU General Public License.
30 void intadc_init(void)
32 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22) || \
33 defined(_18F23K22) || defined(_18F24K22) || defined(_18F25K22) || defined(_18F26K22)
39 #if defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
40 defined(_18F23K20) || defined(_18F24K20) || defined(_18F25K20) || defined(_18F26K20)
45 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
46 defined(_18F43K20) || defined(_18F44K20) || defined(_18F45K20) || defined(_18F46K20)
49 ANSELbits._ADC_INITF = 1;
54 IPR1bits.ADIP = 1; // High
59 unsigned int polladc(void)
64 if(ADCON0bits.DONE){ // converting or not enable.
66 } else { // Done, Clear interrupt
67 a = ((ADRESH << 8) + ADRESL) & 0x03ff;
68 ADCON0bits.GO_DONE = 0;
75 * ADCON1bits.VCFG1 = 0;
76 * ADCON1bits.VCFG0 = 0;
77 * ADCON2 = 0b10110110;
79 void startadc(unsigned char ch)
81 unsigned int ansel_val;
82 unsigned char config1, config2;
84 adc_open(ch, ADC_FOSC_64, 0, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS | ADC_NVCFG_VSS | ADC_PVCFG_VDD);
89 config1 = 0b10101101; // ADC_RIGHT_JUST & ADC_FOSC_32 & ADC_16_TAD & ADC_INT_OFF,
91 ansel_val = 0x0001 << ch;
93 config1 = 0b11101111; // ADC_RIGHT_JUST & ADC_FOSC_64 & ADC_20_TAD & ADC_INT_OFF,
97 OpenADC(config1, config2, ansel_val);
98 // idle_time_ms(2); // 64OSC * 20 = 1280OSC < 1ms
106 ADCON0bits.GO_DONE = 0;
112 unsigned char polladc2(void)
117 do { // converting or not enable.
119 } while (ADCON0bits.DONE != 0);
121 a = ((ADRESH << 8) + ADRESL) & 0x03ff;
122 ADCON0bits.GO_DONE = 0;
127 // Masure raw battery voltage, Reference = 1.20V±4%
128 // Multiply = 39:13 = 4:1.
129 unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel)
132 // raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
133 // return ((b * 4) / reflevel) * (1024/1000)* 120; // Divide = 4
134 return ((b * 120) / reflevel) * 4;// Divide = 4, but input impedance of PIC is lower ;-) ,
135 // about 0.8 (x 4) mesure value, multiply 32/10.