-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc.c
56 lines (44 loc) · 1.07 KB
/
adc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <xc.h>
#include "adc.h"
void init_adc(void)
{
/* Selecting right justified ADRES Registers order */
ADFM = 1;
/*
* Acqusition time selection bits
* Set for 8 Tad
*/
ACQT2 = 1;
ACQT1 = 0;
ACQT0 = 0;
/*
* Selecting the conversion clock of Fosc / 32 -> 1.6usecs -> 1Tad
* Our device frequency is 20 MHz
*/
ADCS0 = 0;
ADCS1 = 1;
ADCS2 = 0;
/* Stop the conversion to start with */
GODONE = 0;
/* Selecting the channel */
CHS2 = 1;
/* Voltage reference bit as VSS */
VCFG1 = 0;
/* Voltage reference bit as VDD */
VCFG0 = 0;
/* Just clearing the ADRESH & ADRESL registers, for time pass */
ADRESH = 0;
ADRESL = 0;
/* Turn ON the ADC module */
ADON = 1;
}
unsigned short read_adc(unsigned char channel)
{
unsigned short reg_val;
ADCON0 = (ADCON0 & 0xc3) | (channel << 2);
/* Start the conversion */
GO = 1;
while (GO);
reg_val = (ADRESH << 8) | ADRESL;
return reg_val;
}