blob: 801fed09a44e55a48184ec450a50550e2ec4ce24 (
plain)
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
|
#include "adc.h"
/* ADCON0: bit 7 - bit 0 (11-1 pg 111)
* ADCS1:ADCS0:CHS2:CHS1:CHS0:GO/DONE:-:ADON
*
* ADCON1: bit 7 - bit 0
* ADFM:-:-:-:PCFG3:PCFG2:PCFG1:PCFG0
*/
void Adc_Init(void) {
ADCON0 = 0x00; // A/D Off, GO_DONE not in progress, Channel 0, Fosc/2
ADCS0 = 0; // A/D Conversion Fosc/32 ADCS1:ADCS0 (0b10) (Table 11-1)
ADCS1 = 1;
//ADCON1
ADFM = 1; // results right justified
PCFG0 = PCFG1 = PCFG2 = PCFG3 = 0; // AN0-AN7 Analog input, Vref+/- Vdd/Vss
}
unsigned int Adc_Read(unsigned int channel) {
ADCON0bits.CHS = channel; // Set CHS bits for channel (pg111)
ADCON0bits.ADON = 1; // Turn on ADC
__delay_us(5); // Enough time for A/D acquisition
ADCON0bits.GO_DONE = 1; // A/D Conversion bit set 1 (conversion in progress)
while (ADCON0bits.GO_DONE == 1); // Wait for conversion to finish
return ((ADRESH << 8) + ADRESL); // return combined 10 bits of conversion
}
|