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.
29 void intadc_init(void)
31 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22) || \
32 defined(_18F23K22) || defined(_18F24K22) || defined(_18F25K22) || defined(_18F26K22)
38 #if defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
39 defined(_18F23K20) || defined(_18F24K20) || defined(_18F25K20) || defined(_18F26K20)
44 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
45 defined(_18F43K20) || defined(_18F44K20) || defined(_18F45K20) || defined(_18F46K20)
48 ANSELbits._ADC_INITF = 1;
53 IPR1bits.ADIP = 1; // High
58 unsigned int polladc(void)
63 if(ADCON0bits.DONE){ // converting or not enable.
65 } else { // Done, Clear interrupt
66 a = ((ADRESH << 8) + ADRESL) & 0x03ff;
67 ADCON0bits.GO_DONE = 0;
73 unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel)
75 // raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
76 return ((b >>2) * reflevel) >> 6;