OSDN Git Service

[UI][DISP][v1.0] Refine display, printstr().
[openi2cradio/OpenI2CRadio.git] / adc_int.c
1 /*
2  * OpenI2CRADIO
3  * Internal ADCONVERTER Handler
4  * Copyright (C) 2013-06-20 K.Ohta <whatisthis.sowhat ai gmail.com>
5  *
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.
14  *
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,
18  *  MA 02110-1301, USA.
19  *
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.
26  */
27 #include "adc.h"
28 #include "adc_int.h"
29 void intadc_init(void)
30 {
31 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22) || \
32      defined(_18F23K22)  || defined(_18F24K22)  || defined(_18F25K22)  || defined(_18F26K22)
33
34     ANSELA = AN_A_VAL;
35     ANSELB = AN_B_VAL;
36     ANSELC = AN_C_VAL;
37 #endif
38 #if defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
39       defined(_18F23K20)  || defined(_18F24K20)  || defined(_18F25K20)  || defined(_18F26K20)
40
41     ANSEL=  _ADC_INITF;
42     ANSELH = _ADCH_INITF;
43 #endif
44 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
45     defined(_18F43K20)  || defined(_18F44K20)  || defined(_18F45K20)  || defined(_18F46K20)
46     ANSEL = 0x00;
47     ANSELH = 0x00;
48     ANSELbits._ADC_INITF = 1;
49 //    ANSELH = 0x00;;
50 #endif
51     PIR1bits.ADIF = 0;
52     PIE1bits.ADIE = 0;
53     IPR1bits.ADIP = 1; // High
54 }
55
56
57
58 unsigned int polladc(void)
59 {
60     unsigned int a;
61     if(ADCON0bits.DONE){ // converting or not enable.
62         PIE1bits.ADIE = 1;
63         PIR1bits.ADIF = 0;
64         return 0xffff;
65     } else { // Done, Clear interrupt
66         a = ((ADRESH << 8)  + ADRESL) & 0x03ff;
67      PIE1bits.ADIE = 0;
68      PIR1bits.ADIF = 0;
69      ADCON0bits.GO_DONE = 0;
70      return a;
71     }
72 }
73
74
75 unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel)
76 {
77     // raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
78     return ((b >>2) * reflevel) >> 6;
79 }