pic24_adc.c

Go to the documentation of this file.
00001 /*
00002  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
00003  * All rights reserved.
00004  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
00005  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
00006  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
00007  *
00008  * Permission to use, copy, modify, and distribute this software and its
00009  * documentation for any purpose, without fee, and without written agreement is
00010  * hereby granted, provided that the above copyright notice, the following
00011  * two paragraphs and the authors appear in all copies of this software.
00012  *
00013  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
00014  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00015  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
00016  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017  *
00018  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00019  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00020  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
00021  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
00022  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
00023  *
00024  * Please maintain this header in its entirety when copying/modifying
00025  * these files.
00026  *
00027  *
00028  */
00029 
00030 
00031 
00032 // Documentation for this file. If the \file tag isn't present,
00033 // this file won't be documented.
00038 #include "pic24_all.h"
00039 #include <stdio.h>   //for NULL definition
00040 
00041 /*********************************
00042  * Function private to this file *
00043  *********************************/
00044 
00045 
00046 /*********************************************************
00047  * Public functions intended to be called by other files *
00048  *********************************************************/
00049 
00055 uint16 convertADC1(void) {
00056   uint8 u8_wdtState;
00057 
00058   sz_lastTimeoutError = "convertADC1()";
00059   u8_wdtState = _SWDTEN;                  //save WDT state
00060   _SWDTEN = 1;                            //enable WDT since we block
00061   SET_SAMP_BIT_ADC1();                    //start sampling
00062   __delay32(1);                           //takes one clock to clear previous DONE flag, delay before checking.
00063   WAIT_UNTIL_CONVERSION_COMPLETE_ADC1();  //wait for conversion to finish
00064   _SWDTEN = u8_wdtState;                  //restore WDT
00065   sz_lastTimeoutError = NULL;             //reset error message
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;   // turn off ADC (changing setting while ADON is not allowed)
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 #if (defined(__PIC24H__)|| defined(__dsPIC33F__))
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;   //turn on the ADC
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   // compute the number of Channels the user wants to scan over
00139   for (u8_i=0; u8_i<16; u8_i++) {
00140     if (u16_ch0ScanMask & u16_mask)
00141       u8_nChannels++;
00142     u16_mask<<=1;
00143   } //end for
00144 
00145   if (u8_autoSampleTime > 31) u8_autoSampleTime=31;
00146 
00147   AD1CON1bits.ADON = 0;   // turn off ADC (changing setting while ADON is not allowed)
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 #if (defined(__PIC24H__)|| defined(__dsPIC33F__))
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;             // set AD1 interrupt priority
00166   _AD1IF = 0;             // clear AD1 interrupt flag
00167   _AD1IE = 1;             // ENABLE AD1 interrupt
00168 
00169   AD1CON1bits.ADON = 1;   // turn on the ADC
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   // compute the number of Channels the user wants to scan over
00204   for (u8_i=0; u8_i<16; u8_i++) {
00205     if (u16_ch0ScanMask & u16_mask)
00206       u8_nChannels++;
00207     u16_mask<<=1;
00208   } //end for
00209 
00210   if (u8_autoSampleTime > 31) u8_autoSampleTime=31;
00211 
00212   AD1CON1bits.ADON = 0;   // turn off ADC (changing setting while ADON is not allowed)
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 #if (defined(__PIC24H__)|| defined(__dsPIC33F__))
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;             // set AD1 interrupt priority
00231   _AD1IF = 0;             // clear AD1 interrupt flag
00232   _AD1IE = 1;             // ENABLE AD1 interrupt
00233 
00234   AD1CON1bits.ADON = 1;   // turn on the ADC
00235 }
00236 
00237 
00269 void configADC1_Simul4ChanIrq(uint8    u8_ch0Select, \
00270                               uint16   u16_ch123SelectMask, \
00271                               uint16   u16_numTcyMask ) {
00272 
00273   AD1CON1bits.ADON = 0;   // turn off ADC (changing setting while ADON is not allowed)
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 #if (defined(__PIC24H__)|| defined(__dsPIC33F__))
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;             // set AD1 interrupt priority
00287   _AD1IF = 0;             // clear AD1 interrupt flag
00288   _AD1IE = 1;             // ENABLE AD1 interrupt
00289 
00290   AD1CON1bits.ADON = 1;   // turn on the ADC
00291 }
00292 
00293 
00294 
00295 

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9