Subversion Repositories Code-Repo

Rev

Rev 126 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 126 Rev 128
Line 1... Line 1...
1
#include "maindefs.h"
1
#include "maindefs.h"
2
#include "adc.h"
2
#include "adc.h"
3
 
3
 
4
static ADC_DATA adc_data;
4
static ADC_DATA adc_data;
-
 
5
static ADC_DATA *adc_data_p = &adc_data;
5
 
6
 
6
void ADC_Init(unsigned char TAD, unsigned char FOSC) {
7
void ADC_Init(unsigned char TAD, unsigned char FOSC) {
7
    TRISAbits.TRISA0 = 1;
8
    TRISAbits.TRISA0 = 1;
8
    TRISAbits.TRISA1 = 1;
9
    TRISAbits.TRISA1 = 1;
9
    TRISAbits.TRISA2 = 1;
10
    TRISAbits.TRISA2 = 1;
10
 
11
 
11
    adc_data.last_channel = 0;
12
    adc_data_p->last_channel = 0;
12
    adc_data.result = 0;
13
    adc_data_p->result = 0;
13
 
14
 
14
    ADCON0bits.VCFG1 = 0; // VRef- = AVss
15
    ADCON0bits.VCFG1 = 0; // VRef- = AVss
15
    ADCON0bits.VCFG0 = 1; // VRef+ != AVdd
16
    ADCON0bits.VCFG0 = 1; // VRef+ != AVdd
16
    ADCON1bits.ADFM = 1; // Right justified result
17
    ADCON1bits.ADFM = 1; // Right justified result
17
    ADCON1bits.ADCAL = 1; // Calibrate A/D
18
    ADCON1bits.ADCAL = 1; // Calibrate A/D
Line 27... Line 28...
27
    PIE1bits.ADIE = 1; // Enable A/D interrupt
28
    PIE1bits.ADIE = 1; // Enable A/D interrupt
28
 
29
 
29
}
30
}
30
 
31
 
31
void ADC_Start(unsigned char channel) {
32
void ADC_Start(unsigned char channel) {
32
    adc_data.last_channel = channel;
33
    adc_data_p->last_channel = channel;
33
    ADCON0bits.CHS = channel; // Set A/D channel
34
    ADCON0bits.CHS = channel; // Set A/D channel
34
    ADCON0bits.GO_DONE = 1; // Start A/D conversion
35
    ADCON0bits.GO_DONE = 1; // Start A/D conversion
35
}
36
}
36
 
37
 
37
void ADC_Stop() {
38
void ADC_Stop() {
38
    ADCON0bits.ADON = 0; // Disable A/D module
39
    ADCON0bits.ADON = 0; // Disable A/D module
39
}
40
}
40
 
41
 
41
void ADC_Interrupt_Handler() {
42
void ADC_Interrupt_Handler() {
42
    adc_data.result = ADRES;
43
    adc_data_p->result = ADRES;
43
}
44
}
44
 
45
 
45
char ADC_Get_Result(unsigned int* ret) {
46
char ADC_Get_Result(unsigned int* ret) {
46
    if (ADCON0bits.GO_DONE) {
47
    if (ADCON0bits.GO_DONE) {
47
        return 0;
48
        return 0;
48
    } else {
49
    } else {
49
        *ret = adc_data.result;
50
        *ret = adc_data_p->result;
50
        return 1;
51
        return 1;
51
    }
52
    }
52
}
53
}
53
54