// program to test PIC32MX360F512L ADC
#include <stdio.h>
#include "hardware.h"
#include "uart2.h"
#include "timer.h"
// *****************************************************************************
// Section: Configuration bits
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 40 MHz
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1
// WDT ON, prescaler bits set
#pragma config FWDTEN = ON, WDTPS = PS16384
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
// **********************************************************************************
// ADC code taken from Microchip's example PIC32_ADC_CodeExample on http://www.microchip.com/CodeExamplesByFunc.aspx
// Define setup parameters for OpenADC10 function
// Turn module on | Ouput in integer format | Trigger mode auto | Enable autosample
#define config1 ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
// ADC ref external | Disable offset test | Disable scan mode | Perform 2 samples | Use dual buffers | Use alternate mode
#define config2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON
// Use ADC internal clock | Set sample time
#define config3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
// Do not assign channels to scan
#define configscan SKIP_SCAN_ALL
// AN4 = Temperature Sensor, AN2 = POT
#define configport ENABLE_AN4_ANA | ENABLE_AN2_ANA
unsigned int tempSensor; // Connected to AN4
unsigned int pot; // Connected to AN2 or AN5 (depending on device)
unsigned int offset; // Buffer offset to point to the base of the idle buffer
void ADCinit(void)
{
CloseADC10(); // Ensure the ADC is off before setting the configuration
// Configure to sample AN4 & AN5
// Use ground as neg ref for A | use AN4 for input A | use ground as neg ref for A | use AN5 for input B
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 | ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN5);
// Configure ADC using the parameters defined above
OpenADC10( config1, config2, config3, configport, configscan );
EnableADC10(); // Enable the ADC
}
// read Explorer 16 temperature and potentiometer ADC values
int ADCread(void)
{
while ( !mAD1GetIntFlag() )
{
// Wait for the first conversion to complete so there will be vaild data in ADC result registers
}
// Determine which buffer is idle and create an offset
offset = 8 * ((~ReadActiveBufferADC10() & 0x01));
// Read the result of temperature sensor conversion from the idle buffer
tempSensor = ReadADC10(offset);
// Read the result of pot conversion from the idle buffer
pot = ReadADC10(offset + 1);
printf("temperature= %d Potentiometer = %d\n", tempSensor, pot);
mAD1ClearIntFlag();
}
int main(void)
{
int i;
// Explorer16 LEDs are on lower 8-bits of PORTA and to use all LEDs, JTAG port must be disabled.
mJTAGPortEnable(DEBUG_JTAGPORT_OFF);
// Configure the device for maximum performance but do not change the PBDIV
// Given the options, this function will change the flash wait states, RAM
// wait state and enable prefetch cache but will not change the PBDIV.
// The PBDIV value is already set via the pragma FPBDIV option above..
SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
mPORTAClearBits(0xff); // clear LEDs
mPORTASetPinsDigitalOut(0xff); // make LEDs outputt.
UART2Init(PC_BAUDRATE); // initialise UART2 to PC host
Timer1init(); // initialise timer 1
for(i=0;i<10;i++) // blink LEDs 0.1sec using timer
{ mPORTAToggleBits(0xff); mSecDelay(100); }
UART2PrintString("*** UART and ADC tests ***\r\n");
printf("\nExplorer 16 test \n");
mPORTAClearBits(0xff); // clear LEDs
ADCinit();
// loop
while (1)
{
// if .5 second elapsed read ADC and toggle LED
mSecDelay(500);
mPORTAToggleBits(BIT_0);
ADCread(); // read temperature and potentiometer value
ClearWDT(); // clear watchdog timer - comment out to test
}
return 0;
}