OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[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
28 //#include "adc.h"
29 #include "adc_int.h"
30 void intadc_init(void)
31 {
32 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22) || \
33      defined(_18F23K22)  || defined(_18F24K22)  || defined(_18F25K22)  || defined(_18F26K22)
34
35     ANSELA = AN_A_VAL;
36     ANSELB = AN_B_VAL;
37     ANSELC = AN_C_VAL;
38 #endif
39 #if defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
40       defined(_18F23K20)  || defined(_18F24K20)  || defined(_18F25K20)  || defined(_18F26K20)
41
42     ANSEL=  _ADC_INITF;
43     ANSELH = _ADCH_INITF;
44 #endif
45 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
46     defined(_18F43K20)  || defined(_18F44K20)  || defined(_18F45K20)  || defined(_18F46K20)
47     ANSEL = 0x00;
48     ANSELH = 0x00;
49     ANSELbits._ADC_INITF = 1;
50 //    ANSELH = 0x00;;
51 #endif
52     PIR1bits.ADIF = 0;
53     PIE1bits.ADIE = 0;
54     IPR1bits.ADIP = 1; // High
55 }
56
57
58
59 unsigned int polladc(void)
60 {
61     unsigned int a;
62     PIE1bits.ADIE = 0;
63     PIR1bits.ADIF = 0;
64     if(ADCON0bits.DONE){ // converting or not enable.
65         return 0xffff;
66     } else { // Done, Clear interrupt
67         a = ((ADRESH << 8)  + ADRESL) & 0x03ff;
68      ADCON0bits.GO_DONE = 0;
69      ADCON0bits.ADON = 0;
70      return a;
71     }
72 }
73
74 /*
75  *    ADCON1bits.VCFG1 = 0;
76  *    ADCON1bits.VCFG0 = 0;
77  *    ADCON2 = 0b10110110;
78  */
79 void startadc(unsigned char ch)
80 {
81     unsigned int ansel_val;
82     unsigned char config1, config2;
83 #ifdef __SDCC
84     adc_open(ch, ADC_FOSC_64, 0, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS | ADC_NVCFG_VSS | ADC_PVCFG_VDD);
85     adc_setchannel(ch);
86     adc_conv();
87 #else
88     PIE1bits.ADIE = 0;
89     PIR1bits.ADIF = 0;
90     ADCON0 = 0;
91     ADCON2 = 0b00000110; // 64OSC, Right
92     ADCON1 = 0b00000000; // VREF+=VDD,VREF-=VSS
93     ADCON2 |= 0b10111000; // TAQ=20TAD, Right
94     if(ch < _REF_ADC) {
95         ADCON0 = ch << 2;
96         ansel_val = 0x0001 << ch;
97 //        ansel_val = 0x0080;
98     } else {
99         ADCON0 = 0b00111100; //0x0f << 2;
100        ansel_val = 0x0000;
101     }
102     ANSELH = ansel_val >> 8;
103     ANSEL = ansel_val & 0xff;
104     ADCON0bits.ADON = 1;
105     idle(160); // 64*20 = 1280clock = 160us
106     ADCON0bits.GO_DONE = 1;
107 #endif
108 }
109
110 #ifdef __SDCC
111 void stopadc(void)
112 {
113     ADCON0bits.GO_DONE = 0;
114     PIR1bits.ADIF = 0;
115     PIE1bits.ADIE = 0;
116 }
117 #endif
118
119 unsigned int polladc2(void)
120 {
121     unsigned int a;
122 //    PIE1bits.ADIE = 0;
123 //    PIR1bits.ADIF = 0;
124     do {
125         idle(50); // wait about 50us
126         a = polladc();
127     } while(a  == 0xffff);
128    return a;
129 }
130
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)
134 {
135
136     // raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
137     return ((b * 120) / reflevel) * 4;
138 }