Subversion Repositories Code-Repo

Rev

Rev 284 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
284 Kevin 1
#include "defines.h"
2
#include "ADC.h"
3
 
4
void ADC_Init(void) {
5
    ADCON0bits.ADON = 0;        // Turn off ADC module
6
    ADCON1bits.ADFM = 1;        // Right justified data
7
    ADCON1bits.ADCS = 0b110;    // A/D conversion clock = FOSC/64
8
    ADCON1bits.ADNREF = 0;      // Negative reference is VSS
9
    ADCON1bits.ADPREF = 0b00;   // Positive reference is VDD
10
}
11
 
12
uint16_t ADC_Read(uint8_t channel) {
13
    uint16_t ret;
14
 
15
    ADCON0bits.CHS = channel;   // Set channel
16
    ADCON0bits.ADON = 1;        // Turn ADC on
17
    __delay_us(10);             // Wait the acquisition time
18
    ADCON0bits.GO_nDONE = 1;    // Start conversion
19
    while (ADCON0bits.GO_nDONE == 1);   // Wait for conversion to finish
20
    ret  = ADRESH << 8;         // Read ADC value
21
    ret |= ADRESL;
22
 
23
    return ret;
24
}