Difference between revisions of "PIC32MX: Analog Inputs"

From Mech
Jump to navigationJump to search
Line 51: Line 51:
#define PARAM5 SKIP_SCAN_AN0 | SKIP_SCAN_AN1 | SKIP_SCAN_AN2 | SKIP_SCAN_AN3 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 | SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14 | SKIP_SCAN_AN15
#define PARAM5 SKIP_SCAN_AN0 | SKIP_SCAN_AN1 | SKIP_SCAN_AN2 | SKIP_SCAN_AN3 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 | SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14 | SKIP_SCAN_AN15
// use ground as neg ref for A | use AN4 (B4) for input A
// use ground as neg ref for A
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF); // use ground as the negative reference
// configure to sample AN4
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF); // configure to sample AN4
OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using parameter define above
OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using parameter define above

Revision as of 16:36, 2 March 2010

Analog-to-Digital Conversion (ADC) is a useful capability of many PIC microcontrollers. This ADC produces a digital value based on the supplied analog voltage, which can then be used with the digital logic of the rest of the PIC. The PIC32 converts analog inputs in the range 0-5V to digital values between 0 and 1023 (10-bit resolution).

Available Pins

The PIC32MX460F512L has 16 available analog pins denoted ANX on the pin list, where x is from 0 to 15.

Analog Inputs Example

This section uses an example to describe how to set up and read digital inputs using a PIC32MX460F512L.

Sample Code

Program to LED output on the NU32 board based on analog input of 2 POTs.

/********************************************************************
Super simple Analog Input with POT. 
********************************************************************/
#include "HardwareProfile.h"
#include <plib.h>

// NOTE THAT BECAUSE WE USE THE BOOTLOADER, NO CONFIGURATION IS NECESSARY
// THE BOOTLOADER PROJECT ACTUALLY CONTROLS ALL OF OUR CONFIG BITS

unsigned short int channel4;	// conversion result as read from result buffer
unsigned short int channel5;	// conversion result as read from result buffer

int main(void)
{
    // Configure the proper PB frequency and the number of wait states
	SYSTEMConfigPerformance(SYS_FREQ);

	// configure and enable the ADC
	CloseADC10();	// ensure the ADC is off before setting the configuration

	// define setup parameters for OpenADC10
				// Turn module on | output 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    | enable scan mode | perform 2 samples | use one buffer | use MUXA mode
	#define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF

	// 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
				// set AN4 and AN5
	#define PARAM4	ENABLE_AN4_ANA | ENABLE_AN5_ANA

	// define setup parameters for OpenADC10
	// do not assign channels to scan
	#define PARAM5	SKIP_SCAN_AN0 | SKIP_SCAN_AN1 | SKIP_SCAN_AN2 | SKIP_SCAN_AN3 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 | SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14 | SKIP_SCAN_AN15

	// use ground as neg ref for A 
	SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF); // use ground as the negative reference
	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 valid data in ADC result registers
	
	//Initialize all of the LED pins
	mInitAllLEDs();
	
	while (1)
	{
		channel4 = ReadADC10(0);  		// read the result of channel 4
		if (channel4 > 511)
		{
			mLED_3_On();
	 	}
		else
		{
			mLED_3_Off();
		}
		
		channel5 = ReadADC10(1);		// read the result of channel 5	
		if (channel5 > 511)
		{
			mLED_0_On();
	 	}
		else
		{
			mLED_0_Off();
		}
		   
	}

	return 0;
}