-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyadc.c
executable file
·62 lines (50 loc) · 1.68 KB
/
myadc.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
57
58
59
60
61
62
//******************************************************************************
//
// Filename : adc.c
// Dependencies: none
// Processor: PIC18
// Complier: MCC18 v3.40 or higher
// Company: HVA.
// Auther: Ferry
// Date: 2012-02-09
// Version: 1.0
//
//
//******************************************************************************
#include <p18f4550.h>
void ADC_init() ;
int getAD(char adcselector) ;
#define AD0SELECTOR 0x03
#define AD1SELECTOR 0x07
#define AD2SELECTOR 0x0B
#define AD3SELECTOR 0x0F
#define AD8SELECTOR 0x23
#define getAD0() getAD(AD0SELECTOR)
#define getAD1() getAD(AD1SELECTOR)
#define getAD2() getAD(AD2SELECTOR)
#define getAD3() getAD(AD3SELECTOR)
#define getAD8() getAD(AD8SELECTOR)
//******************************************************************************
// adc_init
//
// Inits analog ports this is really important because the analog ports
// are multiplexed with the digitalports failing to initialize will
// result in erratic behavior.
//******************************************************************************
void ADC_init() {
//Configure analog pins, voltage reference and digital IO, ADCON1
TRISA = 0xDF; // All input port A5 as output
ADCON1 = 0x06; //Use AN0 to AN8, 8 channel
//Select AD acquisition time and AD conversion clock, ADCON2
ADCON2 = 0xA6;
//Turn on AD module, ADCON0 bit 0;
ADCON0 |= 0x1;
}
int getAD(char adcselector) {
//set channel to ad0, start conversion
ADCON0 = adcselector;
//wait for adconversion to be done
while(ADCON0 & 2) {;} //nop
// 10 bit A/D last 2 bit are higest bits
return ((unsigned int)ADRESH << 8)|(ADRESL) ;
}