Simple analog input example for dsPIC30F4011

More information on the dsPIC’s 10-bit ADC can be found in the data sheet and dsPIC30F Family Reference Manual:

This was the example code from today’s class:

// dsPIC30F4011 example - simple analog input
// Written by Ted Burke, Last updated 5-10-2015
#include <xc.h>
#include <libpic30.h>
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

unsigned int read_analog_channel(int n);

int main()
    // Declare a variable for the step time
    // so that it can be changed easily
    int v;
    long step_time = 300000L;
    // Make RD0-3 digital outputs
    TRISD = 0b0000;

    // Configure analog inputs
    TRISB = 0x01FF;      // Port B all inputs
    ADPCFG = 0xFF00;     // Lowest 8 PORTB pins are analog inputs
    ADCON1 = 0;          // Manually clear SAMP to end sampling, start conversion
    ADCON2 = 0;          // Voltage reference from AVDD and AVSS
    ADCON3 = 0x0005;     // Manual Sample, ADCS=5 -> Tad = 3*Tcy = 0.1us
    ADCON1bits.ADON = 1; // Turn ADC ON
    // Cycle through the four windings to make
    // the stepper turn forwards
        // Read the analog channel. The result is an
        // integer between 0 and 1023 inclusive.
        v = read_analog_channel(0);
        // Now, update step time.
        // Because the value get too big for 16-bit ints,
        // the constant values are explicitly marked as
        // long values so that the calculation is carried
        // out using 32-bit ints.
        step_time = 150000L + 200L * V;

        // Cycle through the four stepper windings
        LATD = 0b1000; __delay32(step_time);
        LATD = 0b0100; __delay32(step_time);
        LATD = 0b0010; __delay32(step_time);
        LATD = 0b0001; __delay32(step_time);

    return 0;

// This function reads a single sample from the specified
// analog input. It should take less than 2.5us if the chip
// is running at about 30 MIPS.
unsigned int read_analog_channel(int channel)
    ADCHS = channel;          // Select the requested channel
    ADCON1bits.SAMP = 1;      // start sampling
    __delay32(30);            // 1us delay @ 30 MIPS
    ADCON1bits.SAMP = 0;      // start Converting
    while (!ADCON1bits.DONE); // Should take 12 * Tad = 1.2us
    return ADCBUF0;
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Simple analog input example for dsPIC30F4011

  1. ARYA K P says:

    Sir,can you please give the program to find the FFT of the analog input signal to dsPIC and then to display it in graphical LCD?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s