1,27 → 1,52 |
#include "maindefs.h" |
#include "adc.h" |
#include <adc.h> |
|
void adc_init() { |
static ADC_DATA adc_data; |
|
void ADC_Init(unsigned char TAD, unsigned char FOSC) { |
TRISAbits.TRISA0 = 1; |
|
OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_0_TAD, |
ADC_CH0 & ADC_INT_ON & ADC_REF_VDD_VSS, 0, |
ADC_1ANA); |
TRISAbits.TRISA1 = 1; |
TRISAbits.TRISA2 = 1; |
|
adc_data.last_channel = 0; |
adc_data.result = 0; |
|
ADCON0bits.VCFG1 = 0; // VRef- = AVss |
ADCON0bits.VCFG0 = 0; // VRef+ = AVdd |
ADCON1bits.ADFM = 1; // Right justified result |
ADCON1bits.ADCAL = 1; // Calibrate A/D |
ADCON1bits.ACQT = TAD; |
ADCON1bits.ADCS = FOSC; |
ADCON0bits.ADON = 1; // Enable A/D module |
|
ADCON0bits.GO_DONE = 1; // Start calibration |
while (ADCON0bits.GO_DONE); // Wait for calibration to finish |
PIR1bits.ADIF = 0; // Clear the IF flag |
ADCON1bits.ADCAL = 0; // Normal A/D operation |
|
PIE1bits.ADIE = 1; // Enable A/D interrupt |
|
} |
|
void adc_start() { |
ConvertADC(); |
void ADC_Start(unsigned char channel) { |
adc_data.last_channel = channel; |
ADCON0bits.CHS = channel; // Set A/D channel |
ADCON0bits.GO_DONE = 1; // Start A/D conversion |
} |
|
void adc_stop() { |
CloseADC(); |
void ADC_Stop() { |
ADCON0bits.ADON = 0; // Disable A/D module |
} |
|
void adc_interrupt_handler() { |
// Sends the ADC value to main() |
unsigned int ret; |
unsigned char length; |
ret = ReadADC(); |
length = 2; |
// MQ_sendmsg_ToMainFromLow(length, MSGTYPE_ADC_NEWVALUE, &ret); |
void ADC_Interrupt_Handler() { |
adc_data.result = ADRES; |
} |
|
char ADC_Get_Result(unsigned int* ret) { |
if (ADCON0bits.GO_DONE) { |
return 0; |
} else { |
*ret = adc_data.result; |
return 1; |
} |
} |