• AVR Freaks

PIC24 ADC example code for PIC24FJ1024GB610 on the 16/32 Explorer Board

New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/11/26 12:12:42
  • Location: 0
  • Status: offline
2020/01/15 09:57:01 (permalink)

PIC24 ADC example code for PIC24FJ1024GB610 on the 16/32 Explorer Board

I thought I would post some working ADC code with some helpful hints for those using the PIC24FJ1024GB610 on the 16/32 Explorer board. Please read the comments in the code and let me know if something is not clear. It should compile and load without errors.
Generated main.c file from MPLAB Code Configurator to configure UART only.
The UART connection is for troubleshooting purposes only, so you can see what
is happening with the ADC. To view the UART communication, you can download PuTTY
for free. Choose "Serial" as the connection type. Change the COM port to whatever
is showing in device manager. The baud rate should have defaulted to 9600 when using
MCC to set up the UART. Then, just click "Open" to start the session. Once the program
is running, you will see the serial communication in the PuTTY window.
The ADC code can work independently.
The ADC setup is explicitly written in the main file and was not configured
using MCC
Section: Included Files
#include "mcc_generated_files/system.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <xc.h>
Main application
int main(void)
// initialize the device

//MCC UART configuration will not work unless the following two lines are
//included. DON'T FORGET THEM!!!!
//connect a second USB cable to the UART serial-to-USB port on the
//16/32 Explorer Board (J40)
//The standard USB connection at J18 for the PICkit on board and power remains
RPINR18bits.U1RXR = 10; //UART
RPOR8bits.RP17R = 3; //UART

int ADCValue =7; //will store ADC result from ADC1BUF0
//given an initial value of "7" in conjunction
//with a printf statement via UART as a troubleshooting
//aid. All such printf statements are for troubleshooting
//and calibration purposes only.

//LED setup
TRISAbits.TRISA2 =0; //RA2 pin 58 (LED) on 16/32 Explorer Board as output

pin 20, the POT on 16/32 Explorer Board is connected to pin 32 (RB8/AN8)
which will be set up as the analog input for the ADC
NOTE: trying to use AN6 and AN7 (pins 26 and 27) was unsuccessful. Still not
sure why this is the case
The maximum value for the A/D conversion from the POT is 897 for 10-bit resolution,
not 1023 as would be expected because the POT only outputs a maximum of 3.2 volts
and there appears to be a further voltage drop when it is connected to the analog pin.
As a check, you can connect VDD to the analog pin and the A/D conversion will give
a value of 1023 for 10-bit resolution. */

TRISBbits.TRISB8 = 1; //RB8/AN8 (pin 32) as input
ANSBbits.ANSB8 = 1; //RB8/AN8 (pin 32) as analog

AD1CON1 = 0;
AD1CON1bits.MODE12 = 0; //10-bit operation
AD1CON1bits.FORM = 0; //absolute decimal result
AD1CON1bits.SSRC = 0b0111; //Auto-Convert mode
AD1CON1bits.ASAM = 1; //Sampling begins immediately after last converstion
//SAMP bit is auto-set
AD1CON2 = 0; //interrupts at the completion of the conversion
//for each sample
//always starts filling buffer at ADC1BUF0
//always uses channel input selects for Sample A
AD1CON3 = 0;
AD1CON3bits.ADRC = 1; //dedicated ADC RC clock generator (4 MHz nominal)
AD1CON3bits.SAMC = 2; //auto-sample time = 2*TAD
AD1CON3bits.ADCS = 2; //A/D conversion clock = 2*TAD

AD1CON5 = 0;

AD1CHS = 0;
AD1CHSbits.CH0SA = 8; //sample A Channel 0 positive input = RB8/AN8 (pin 32)

printf("pot value = %d\r\n",ADCValue); //test string (for troubleshooting purposes only)

AD1CON1bits.ADON = 1; //A/D converter is operating

while (1){

printf("sampling \r\n"); //print output to verify sampling (for troubleshooting purposes only)
while(!AD1CON1bits.DONE ){ //wait until conversion done
printf("converting \r\n"); //print output to verify converting (for troubleshooting purposes only)

printf("pot value = %d\r\n",ADC1BUF0); //print result

ADCValue = ADC1BUF0;

printf("ADCValue = %d\r\n",ADCValue); //print result

if (ADCValue > 400){ //LED on
LATAbits.LATA2 = 1;
else {
LATAbits.LATA2 = 0; //LED off


return (EXIT_SUCCESS);

post edited by DanielPLongo - 2020/01/15 12:58:29

0 Replies Related Threads

    Jump to:
    © 2020 APG vNext Commercial Version 4.5