www.pudn.com > All_PIC32_Code_examples.zip > adc10_basic.c, change:2007-10-25,size:5523b


/********************************************************************* 
 * 
 *                  Simple ADC10 API Example 
 * 
 *	This example demonstrated the use of the ADC10 peripheral library. 
 *	In this example two channels, AN4 & AN5, are alternately sampled. 
 * 
 * 
 * 
 ********************************************************************* 
 * FileName:        adc10_basic.c 
 * Dependencies:	plib.h 
 * 
 * 
 * Processor:       PIC32MX360512L 
 * 
 * Complier:        MPLAB C32 
 *                  MPLAB IDE v8.0+ 
 * Company:         Microchip Technology, Inc. 
 * 
 * Software License Agreement 
 * 
 * The software supplied herewith by Microchip Technology Incorporated 
 * (the “Company”) for its PIC32 Microcontroller is intended 
 * and supplied to you, the Company’s customer, for use solely and 
 * exclusively on Microchip PIC32 Microcontroller products. 
 * The software is owned by the Company and/or its supplier, and is 
 * protected under applicable copyright laws. All rights are reserved. 
 * Any use in violation of the foregoing restrictions may subject the 
 * user to criminal sanctions under applicable laws, as well as to 
 * civil liability for the breach of the terms and conditions of this 
 * license. 
 * 
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, 
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 
 * 
 ****************************************************************** 
 * This program Demonstrates the operation of the Analog to Digital converter 
 * operating in Alternate AutoSample mode. 
 * 
 * The Hardware setup consist of a PIC32 pim on an Explorer16 board 
 * Analog input 4 (the temperature sensor) and Analog input 5 (the pot) are converted 
 * The ADC Alternate mode is used to automatically sample the 
 * 2 channels without CPU intervention. The most current results can be 
 * read from the channel4 and channel5 variables 
 * 
 * $Id: adc10_basic.c 4851 2007-09-17 16:39:06Z rajbhartin $ 
 * 
 ********************************************************************/ 
// Master header file for all peripheral library includes 
#include <plib.h> 
 
 
// Configuration Bit settings 
// SYSCLK = 72 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV) 
// PBCLK = 36 MHz 
// Primary Osc w/PLL (XT+,HS+,EC+PLL) 
// WDT OFF 
// Other options are don't care 
// 
#pragma config FPLLMUL = MUL_18, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF 
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2 
 
 
unsigned int channel4;	// conversion result as read from result buffer 
unsigned int channel5;	// conversion result as read from result buffer 
unsigned int offset;	// buffer offset to point to the base of the idle buffer 
 
int main(void) 
{ 
	// Configure the device for maximum performance. 
	// This macro sets flash wait states, PBCLK divider and DRM wait states based on the specified 
	// clock frequency. It also turns on the cache mode if avaialble. 
	// Based on the current frequency, the PBCLK divider will be set at 1:2. This knoweldge 
	// is required to correctly set UART baud rate, timer reload value and other time sensitive 
	// setting. 
	SYSTEMConfigPerformance(72000000L); 
 
	// configure and enable the ADC 
	CloseADC10();	// ensure the ADC is off before setting the configuration 
 
	// define setup parameters for OpenADC10 
				// Turn module on | ouput in integer | trigger mode auto | enable autosample 
	#define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON 
 
	// define setup parameters for OpenADC10 
			    // ADC ref external    | disable offset test    | disable scan mode | perform 2 samples | use dual buffers | use alternate mode 
	#define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON 
 
	// define setup parameters for OpenADC10 
	// 				  use ADC internal clock | set sample time 
	#define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15 
 
 
	// define setup parameters for OpenADC10 
	// do not assign channels to scan 
	#define PARAM4	SKIP_SCAN_ALL 
 
 
	// define setup parameters for OpenADC10 
				// set AN4 and AN5 as analog inputs 
	#define PARAM5	ENABLE_AN4_ANA | ENABLE_AN5_ANA 
 
		// use ground as neg ref for A | use AN4 for input A      |  use ground as neg ref for A | use AN5 for input B 
 
	 // configure to sample AN4 & AN5 
	SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 |  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN5); // configure to sample AN4 & AN5 
	OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using parameter define above 
 
	EnableADC10(); // Enable the ADC 
 
 
	while ( ! mAD1GetIntFlag() ) { } // wait for the first conversion to complete so there will be vaild data in ADC result registers 
 
	// the results of the conversions are available in channel4 and channel5 
	while (1) 
	{ 
		offset = 8 * ((~ReadActiveBufferADC10() & 0x01));  // determine which buffer is idle and create an offset 
 
		channel4 = ReadADC10(offset);  		// read the result of channel 4 conversion from the idle buffer 
		channel5 = ReadADC10(offset + 1);  	// read the result of channel 5 conversion from the idle buffer 
	} 
 
	return 0; 
}