Difference between revisions of "PIC32MX: Analog Inputs"

From Mech
Jump to navigationJump to search
Line 3: Line 3:
==Available Pins==
==Available Pins==
The PIC32MX460F512L has 16 available analog pins denoted ANX on the pin list, where x is from 0 to 15.
The PIC32MX460F512L has 16 available analog pins denoted ANX on the pin list, where x is from 0 to 15.

<br><br>
<br><br>



Revision as of 13:43, 30 July 2009

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 UBW32 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.

  #include "GenericTypeDefs.h"
  #include "Compiler.h"
  #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.

Define the system frequency

  #define SYS_FREQ 				(80000000L)

Define global variables for analog results

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

Begin main function

  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 (the parameter options are located in adc10.h located in the include peripheral folder) Below is an example:

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

configure ADC using parameter define above

  OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 );

Enable the ADC

  EnableADC10();  

wait for the first conversion to complete so there will be valid data in ADC result registers

  while ( ! mAD1GetIntFlag() ) { } 	

Initialize all of the LED pins on the UBW32

  mInitAllLEDs();

main loop

  while (1)
  {

Read the result of channel 4 (the first channel to be read) and turn on / off the LED

  channel4 = ReadADC10(0);
  if (channel4 > 511)
  {
      mLED_3_On();
  }
  else
  {
      mLED_3_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)
  {
      mLED_4_On();
  }
  else
  {
      mLED_4_Off();
  }

end the main function

  return 0;
  }