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);
91 ADCON2 = 0b00000110; // 64OSC, Right
92 ADCON1 = 0b00000000; // VREF+=VDD,VREF-=VSS
93 ADCON2 |= 0b10111000; // TAQ=20TAD, Right
96 ansel_val = 0x0001 << ch;
97 // ansel_val = 0x0080;
102 ANSELH = ansel_val >> 8;
103 ANSEL = ansel_val & 0xff;
105 idle(160); // 64*20 = 1280clock = 160us
106 ADCON0bits.GO_DONE = 1;
113 ADCON0bits.GO_DONE = 0;
119 unsigned int polladc2(void)
122 // PIE1bits.ADIE = 0;
123 // PIR1bits.ADIF = 0;
125 idle(50); // wait about 50us
127 } while(a == 0xffff);
131 // Masure raw battery voltage, Reference = 1.20V±4%
132 // Multiply = 39:13 = 4:1.
133 unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel)
136 // raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
137 return ((b * 120) / reflevel) * 4;