00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00038 #include "pic24_all.h"
00039 #include <stdio.h>
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00055 uint16 convertADC1(void) {
00056 uint8 u8_wdtState;
00057
00058 sz_lastTimeoutError = "convertADC1()";
00059 u8_wdtState = _SWDTEN;
00060 _SWDTEN = 1;
00061 SET_SAMP_BIT_ADC1();
00062 __delay32(1);
00063 WAIT_UNTIL_CONVERSION_COMPLETE_ADC1();
00064 _SWDTEN = u8_wdtState;
00065 sz_lastTimeoutError = NULL;
00066 return(ADC1BUF0);
00067 }
00068
00084 void configADC1_ManualCH0(uint16 u16_ch0PositiveMask, \
00085 uint8 u8_autoSampleTime, \
00086 uint8 u8_use12bit) {
00087
00088 if (u8_autoSampleTime > 31) u8_autoSampleTime=31;
00089 AD1CON1bits.ADON = 0;
00090
00092 AD1CON1 = ADC_CLK_AUTO | ADC_AUTO_SAMPLING_OFF;
00093 #ifdef _AD12B
00094 if (u8_use12bit)
00095 AD1CON1bits.AD12B = 1;
00096 else
00097 AD1CON1bits.AD12B = 0;
00098 #endif
00099 AD1CON3 = ADC_CONV_CLK_INTERNAL_RC | (u8_autoSampleTime<<8);
00100 AD1CON2 = ADC_VREF_AVDD_AVSS;
00101 #ifdef __PIC24H__
00102 AD1CHS0 = ADC_CH0_NEG_SAMPLEA_VREFN | u16_ch0PositiveMask;
00103 #else
00104 AD1CHS = ADC_CH0_NEG_SAMPLEA_VREFN | u16_ch0PositiveMask;
00105 #endif
00106 AD1CON1bits.ADON = 1;
00107 }
00108
00132 void configADC1_AutoScanIrqCH0(uint16 u16_ch0ScanMask, \
00133 uint8 u8_autoSampleTime, \
00134 uint8 u8_use12bit) {
00135 uint8 u8_i, u8_nChannels=0;
00136 uint16 u16_mask = 0x0001;
00137
00138
00139 for (u8_i=0; u8_i<16; u8_i++) {
00140 if (u16_ch0ScanMask & u16_mask)
00141 u8_nChannels++;
00142 u16_mask<<=1;
00143 }
00144
00145 if (u8_autoSampleTime > 31) u8_autoSampleTime=31;
00146
00147 AD1CON1bits.ADON = 0;
00149 AD1CON1 = ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON;
00150 #ifdef _AD12B
00151 if (u8_use12bit)
00152 AD1CON1bits.AD12B = 1;
00153 else
00154 AD1CON1bits.AD12B = 0;
00155 #endif
00156 AD1CON3 = ADC_CONV_CLK_INTERNAL_RC | (u8_autoSampleTime<<8);
00157 AD1CON2 = ADC_VREF_AVDD_AVSS | ADC_CONVERT_CH0 | ADC_SCAN_ON | ((u8_nChannels-1)<<2);
00158 #ifdef __PIC24H__
00159 AD1CHS0 = ADC_CH0_NEG_SAMPLEA_VREFN;
00160 #else
00161 AD1CHS = ADC_CH0_NEG_SAMPLEA_VREFN;
00162 #endif
00163 AD1CSSL = u16_ch0ScanMask;
00164
00165 _AD1IP = 7;
00166 _AD1IF = 0;
00167 _AD1IE = 1;
00168
00169 AD1CON1bits.ADON = 1;
00170 }
00171
00172
00197 void configADC1_AutoHalfScanIrqCH0(uint16 u16_ch0ScanMask, \
00198 uint8 u8_autoSampleTime, \
00199 uint8 u8_use12bit) {
00200 uint8 u8_i, u8_nChannels=0;
00201 uint16 u16_mask = 0x0001;
00202
00203
00204 for (u8_i=0; u8_i<16; u8_i++) {
00205 if (u16_ch0ScanMask & u16_mask)
00206 u8_nChannels++;
00207 u16_mask<<=1;
00208 }
00209
00210 if (u8_autoSampleTime > 31) u8_autoSampleTime=31;
00211
00212 AD1CON1bits.ADON = 0;
00214 AD1CON1 = ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON;
00215 #ifdef _AD12B
00216 if (u8_use12bit)
00217 AD1CON1bits.AD12B = 1;
00218 else
00219 AD1CON1bits.AD12B = 0;
00220 #endif
00221 AD1CON3 = ADC_CONV_CLK_INTERNAL_RC | (u8_autoSampleTime<<8);
00222 AD1CON2 = ADC_VREF_AVDD_AVSS | ADC_CONVERT_CH0 | ADC_ALT_BUF_ON | ADC_SCAN_ON | ((u8_nChannels-1)<<2);
00223 #ifdef __PIC24H__
00224 AD1CHS0 = ADC_CH0_NEG_SAMPLEA_VREFN;
00225 #else
00226 AD1CHS = ADC_CH0_NEG_SAMPLEA_VREFN;
00227 #endif
00228 AD1CSSL = u16_ch0ScanMask;
00229
00230 _AD1IP = 7;
00231 _AD1IF = 0;
00232 _AD1IE = 1;
00233
00234 AD1CON1bits.ADON = 1;
00235 }
00236
00237
00269 void configADC1_Simul4ChanIrq(uint8 u8_ch0Select, \
00270 uint16 u16_ch123SelectMask, \
00271 uint16 u16_numTcyMask ) {
00272
00273 AD1CON1bits.ADON = 0;
00275 AD1CON1 = ADC_CLK_TMR | ADC_SAMPLE_SIMULTANEOUS;
00276 AD1CON3 = (u16_numTcyMask & 0x00FF);
00277 AD1CON2 = ADC_VREF_AVDD_AVSS | ADC_CONVERT_CH0123 | ADC_ALT_BUF_ON;
00278 #ifdef __PIC24H__
00279 AD1CHS0 = ADC_CH0_NEG_SAMPLEA_VREFN | (u8_ch0Select & 0x1F);
00280 AD1CHS123 = u16_ch123SelectMask;
00281 #else
00282 AD1CHS = ADC_CH0_NEG_SAMPLEA_VREFN | (u8_ch0Select & 0x1F);
00283 #endif
00284 AD1CSSL = 0;
00285
00286 _AD1IP = 7;
00287 _AD1IF = 0;
00288 _AD1IE = 1;
00289
00290 AD1CON1bits.ADON = 1;
00291 }
00292
00293
00294
00295