00001
00006 #include "adc1_config.h"
00007
00008 volatile uint16 au16_ADC1Buffer[ADC1_BUFFER_SIZE];
00009
00010 void configureADC1_AutoScanIRQCH0(uint16 u16_ch0ScanMask, \
00011 uint8 u8_autoSampleTime, \
00012 uint8 u8_use12bit) {
00013 uint8 u8_i, u8_nChannels = 0;
00014 uint16 u16_mask = 0x0001;
00015
00016
00017 for (u8_i = 0; u8_i < 16; u8_i++) {
00018 if (u16_ch0ScanMask & u16_mask)
00019 u8_nChannels++;
00020 u16_mask <<= 1;
00021 }
00022
00023 if (u8_autoSampleTime > 31) u8_autoSampleTime = 31;
00024
00025 AD1CON1bits.ADON = 0;
00027 AD1CON1 = ADC_CLK_AUTO + ADC_AUTO_SAMPLING_ON;
00028 #ifdef _AD12B
00029 if (u8_use12bit)
00030 AD1CON1bits.AD12B = 1;
00031 else
00032 AD1CON1bits.AD12B = 0;
00033 #endif
00034 AD1CON3 = ADC_CONV_CLK_INTERNAL_RC + (u8_autoSampleTime << 8);
00035 AD1CON2 = ADC_VREF_AVDD_AVSS + ADC_CONVERT_CH0 + ADC_SCAN_ON + \
00036 ((u8_nChannels - 1) << 2);
00037 #ifdef __PIC24H__
00038 AD1CHS0 = ADC_CH0_NEG_SAMPLEA_VREFN;
00039 #else
00040 AD1CHS = ADC_CH0_NEG_SAMPLEA_VREFN;
00041 #endif
00042 AD1CSSL = u16_ch0ScanMask;
00043
00044 AD1CON1bits.ADON = 1;
00045 }
00046
00047 void initialize_ADC1(void) {
00048 #ifdef ADC1_USE_AN0
00049 CONFIG_AN0_AS_ANALOG();
00050 #endif
00051 #ifdef ADC1_USE_AN1
00052 CONFIG_AN1_AS_ANALOG();
00053 #endif
00054 #ifdef ADC1_USE_AN4
00055 CONFIG_AN4_AS_ANALOG();
00056 #endif
00057 #ifdef ADC1_USE_AN5
00058 CONFIG_AN5_AS_ANALOG();
00059 #endif
00060 #ifdef ADC1_USE_AN10
00061 CONFIG_AN10_AS_ANALOG();
00062 #endif
00063 #ifdef ADC1_USE_AN11
00064 CONFIG_AN11_AS_ANALOG();
00065 #endif
00066 #ifdef ADC1_USE_AN12
00067 CONFIG_AN12_AS_ANALOG();
00068 #endif
00069 ESOS_REGISTER_PIC24_USER_INTERRUPT(ESOS_IRQ_PIC24_AD1, \
00070 ESOS_USER_IRQ_LEVEL1, _ADC1Interrupt);
00071 ESOS_ENABLE_PIC24_USER_INTERRUPT(ESOS_IRQ_PIC24_AD1);
00072 configureADC1_AutoScanIRQCH0(USE_ADC1_CHANNELS, 31, ADC_12BIT_FLAG);
00073 }
00074
00075 ESOS_USER_INTERRUPT(ESOS_IRQ_PIC24_AD1) {
00076 uint8 u8_i;
00077 uint16* au16_adcHWBuff = (uint16*) &ADC1BUF0;
00078
00079 for (u8_i = 0; u8_i < 16; u8_i++) {
00080 au16_ADC1Buffer[u8_i] = au16_adcHWBuff[u8_i];
00081 }
00082 ESOS_MARK_PIC24_USER_INTERRUPT_SERVICED(ESOS_IRQ_PIC24_AD1);
00083 }