// PIC32MX795F512L Configuration Bit Settings
// 'C' source line config statements
// DEVCFG3
#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config FSRSSEL = PRIORITY_7 // SRS Select (SRS Priority 7)
#pragma config FMIIEN = ON // Ethernet RMII/MII Enable (MII Enabled)
#pragma config FETHIO = ON // Ethernet I/O Pin Select (Default Ethernet I/O)
#pragma config FCANIO = ON // CAN I/O Pin Select (Default CAN I/O)
#pragma config FUSBIDIO = ON // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO = ON // USB VBUS ON Selection (Controlled by USB Module)
// DEVCFG2
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_20 // PLL Multiplier (20x Multiplier)
#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_4 // System PLL Output Clock Divider (PLL Divide by 4)
// DEVCFG1
#pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL)
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled)
#pragma config IESO = OFF // Internal/External Switch Over (Disabled)
#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (ICE EMUC1/EMUD1 pins shared with PGC1/PGD1)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<plib.h>
void initADC(void);
int ReadADC(int ch);
void delay_ms (WORD delay);
int main()
{
initADC();
unsigned int channel0;// conversion result as read from result buffer
unsigned int channel1;// conversion result as read from result buffer
unsigned int channel2;// conversion result as read from result buffer
unsigned int channel3;// conversion result as read from result buffer
while(1)
{
channel0=ReadADC(0);
delay_ms(0.5);
channel0=ReadADC(1);
delay_ms(0.5);
channel0=ReadADC(2);
delay_ms(0.5);
channel0=ReadADC(3);
delay_ms(0.5);
}
void initADC(void)
{
AD1PCFGbits.PCFG0 = 0; // Analog input in Analog mode
AD1PCFGbits.PCFG1 = 0;
AD1PCFGbits.PCFG2=0;
AD1PCFGbits.PCFG3=0;
AD1PCFGbits.PCFG4=0;
TRISBbits.TRISB6 = 1; // Pin set as input
TRISBbits.TRISB7 = 1;
TRISBbits.TRISB2=1;
TRISBbits.TRISB3=1;
TRISBbits.TRISB4=1;
AD1CHSbits.CH0NA = 0; // Channel 0 negative input is VR-
AD1CHSbits.CH0SA = 0; // Channel 0 positive input is AN0
AD1CON1bits.FORM = 0; // Integer 16-bit output
AD1CON1bits.SSRC = 0b111; // Internal counter ends sampling and starts conversion
AD1CSSL = 0; // No scanning required
AD1CON2bits.VCFG = 0; // Internal voltage references
AD1CON2bits.CSCNA = 0; // Do not scan inputs
AD1CON2bits.BUFM = 0; // Buffer configured as one 16-word buffer
AD1CON2bits.ALTS = 0; // Always use MUX A input multiplexer settings
AD1CON3bits.ADRC = 0; // Clock derived from PBclock
AD1CON3bits.ADCS = 0b00111111; // TAD
AD1CON3bits.SAMC = 0b11111; // 31 TAD auto-sample time
AD1CON1bits.ON = 1; // A/D converter module is operating
}
int ReadADC(int ch)
{
AD1CHSbits.CH0SA = ch; // Select input channel
AD1CON1bits.SAMP = 1; // Start sampling
while(!AD1CON1bits.DONE); // Wait for conversion to complete
return ADC1BUF0; // Read conversion result
}