Difference between revisions of "PIC32MX: Analog Inputs"

From Mech
Jump to navigationJump to search
(New page: 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 ...)
 
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
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).
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-3.3V to digital values between 0 and 1023 (10-bit resolution).


==Available Pins==
==Available Pins==
The PIC32MX460F512L has 16 available analog pins denoted ANX on the pin list, where x is from 0 to 15.
<br><br>
<br><br>


==Analog Inputs Example==
==Analog Inputs Example==
This section uses an example to describe how to set up and read digital inputs using a PIC32MX460F512L.
This section uses an example to describe how to set up and read analog inputs using a PIC32MX460F512L.


===Sample Code===
===Sample Code===
Program to LED output on the UBW32 board based on analog input of 2 POTs.
Program to LED output on the NU32 board based on analog input of 2 POTs.


/********************************************************************
First include header files with definitions for generic type definitions, compiler, and for specific PIC. Also include the plib header file.
Super simple Analog Input with POT.

********************************************************************/
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "HardwareProfile.h"
#include "HardwareProfile.h"
#include <plib.h>
#include <plib.h>
// NOTE THAT BECAUSE WE USE THE BOOTLOADER, NO CONFIGURATION IS NECESSARY

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

unsigned short int channel4; // conversion result as read from result buffer
Define the system frequency
unsigned short int channel5; // conversion result as read from result buffer
#define SYS_FREQ (80000000L)

int main(void)
Define global variables for analog results
{
unsigned short int channel4; // conversion result as read from result buffer
// Configure the proper PB frequency and the number of wait states
unsigned short int channel5; // conversion result as read from result buffer
SYSTEMConfigPerformance(SYS_FREQ);

Begin main function
// configure and enable the ADC
int main(void)
CloseADC10(); // ensure the ADC is off before setting the configuration
{

// define setup parameters for OpenADC10
Configure the proper PB frequency and the number of wait states
// Turn module on | output in integer | trigger mode auto | enable autosample
SYSTEMConfigPerformance(SYS_FREQ);
#define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON

configure and enable the ADC
// define setup parameters for OpenADC10
CloseADC10(); // ensure the ADC is off before setting the configuration
// ADC ref external | disable offset test | enable scan mode | perform 2 samples | use one buffer | use MUXA mode

// note: to read X number of pins you must set ADC_SAMPLES_PER_INT_X
Define setup parameters for OpenADC10 (the parameter options are located in adc10.h located in the include peripheral folder) Below is an example:
#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

// Turn module on | output in integer | trigger mode auto | enable autosample
// define setup parameters for OpenADC10
#define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
// use ADC internal clock | set sample time

#define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
// 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
// set AN4 and AN5
// use ADC internal clock | set sample time
#define PARAM4 ENABLE_AN4_ANA | ENABLE_AN5_ANA
#define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15

// define setup parameters for OpenADC10
// set AN4 and AN5
// do not assign channels to scan
#define PARAM4 ENABLE_AN4_ANA | ENABLE_AN5_ANA
#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

// do not assign channels to scan
// use ground as neg ref for A
#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
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF); // use ground as the negative reference

OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using parameter define above
// use ground as neg ref for A
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF); // configure to sample AN4
EnableADC10(); // Enable the ADC
configure ADC using parameter define above
while ( ! mAD1GetIntFlag() ) { } // wait for the first conversion to complete so there will be valid data in ADC result registers
OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 );

//Initialize all of the LED pins
Enable the ADC
EnableADC10();
mInitAllLEDs();

while (1)
wait for the first conversion to complete so there will be valid data in ADC result registers
{
while ( ! mAD1GetIntFlag() ) { }
channel4 = ReadADC10(0); // read the result of channel 4

if (channel4 > 511)
Initialize all of the LED pins on the UBW32
{
mInitAllLEDs();
mLED_3_On();

}
main loop
else
while (1)
{
{
mLED_3_Off();

}
Read the result of channel 4 (the first channel to be read) and turn on / off the LED
channel4 = ReadADC10(0);
channel5 = ReadADC10(1); // read the result of channel 5
if (channel4 > 511)
if (channel5 > 511)
{
{
mLED_3_On();
mLED_0_On();
}
else
}
else
{
{
mLED_3_Off();
mLED_0_Off();
}
}

Read the result of channel 5 (the first channel to be read) and turn on / off the LED
}
channel5 = ReadADC10(1);
if (channel5 > 511)
return 0;
{
}
mLED_4_On();
}
else
{
mLED_4_Off();
}

end the main function
return 0;
}

Latest revision as of 09:53, 12 April 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-3.3V 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 analog 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
       // note: to read X number of pins you must set ADC_SAMPLES_PER_INT_X
	#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;
}