• AVR Freaks

dsPIC33EP512MU810 ADC won't sample

Author
syntax3
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/01/02 09:23:44
  • Location: 0
  • Status: offline
2020/01/03 01:47:08 (permalink)
0

dsPIC33EP512MU810 ADC won't sample

Hi I am currently working on dsPIC33EP512MU810. I am trying to read the AN3 pin which is connected to function generator.
However I couldn't get the ADC working. You can find my code below.
 
//----------------CONFIG BITS----------------//

// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = OFF // General Segment Code-Protect bit (General Segment Code protect is disabled)
#pragma config GSSK = OFF // General Segment Key bits (General Segment Write Protection and Code Protection is Disabled)

// FOSCSEL
#pragma config FNOSC = PRIPLL // Initial Oscillator Source Selection Bits (Primary Oscillator (XT, HS, EC) with PLL)
#pragma config IESO = OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)

// FOSC
#pragma config POSCMD = EC // Primary Oscillator Mode Select bits (EC (External Clock) Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)

// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler Bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = OFF // PLL Lock Wait Enable bit (Clock switch will not wait for the PLL lock signal.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)

// FPOR
#pragma config FPWRT = PWR128 // Power-on Reset Timer Value Select bits (128ms)
#pragma config BOREN = ON // Brown-out Reset (BOR) Detection Enable bit (BOR is enabled)
#pragma config ALTI2C1 = OFF // Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
#pragma config ALTI2C2 = OFF // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)

// FICD
#pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
#pragma config RSTPRI = PF // Reset Target Vector Select bit (Device will obtain reset instruction from Primary flash)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)

// FAS
#pragma config AWRP = OFF // Auxiliary Segment Write-protect bit (Auxiliary program memory is not write-protected)
#pragma config APL = OFF // Auxiliary Segment Code-protect bit (Aux Flash Code protect is disabled)
#pragma config APLK = OFF // Auxiliary Segment Key bits (Aux Flash Write Protection and Code Protection is Disabled)

//-------------------------------------------//

#define FCY 70000000UL //Defining Expected Clock frequency for __delay_ functions


#include <xc.h>
#include <libpic30.h> //Delay Function Library



void Initialization(void){
    //----CLOCK SETUP----//
    // Crystal Frequency * (PLLDIV+2)
    //Fosc = --------------------------------
    // (PLLPOST + 2) * (PLLPRE + 2)
    //FCY = Fosc/2
    
    PLLFBDbits.PLLDIV = 54;
    CLKDIVbits.PLLPOST = 0;
    CLKDIVbits.PLLPRE = 0;
    
    //----PORT SETUP----//
    TRISBbits.TRISB3 = 0b1; //SET PORT RB3 to input
    ANSELBbits.ANSB3 = 0b1; //ENAble ANALOG port
    
    AD1CON1bits.FORM = 0b00; //Output Data Integer
    AD1CON1bits.SSRC = 0b111; //Auto End Sampling And Convert
    AD1CON1bits.SSRCG = 0b0;
    AD1CON1bits.ASAM = 0b1; //Sampling begins immediately after the last conversion
    AD1CON1bits.AD12B = 0b1; //12 bit mode
    
    AD1CON2bits.CHPS = 0b0; //Select channel 0
    
    AD1CON3bits.ADRC = 0b0; //ADC Clock from System clock
    AD1CON3bits.ADCS = 63;
    
    
    AD1CHS0bits.CH0SA = 3; //AN3 as positive input
    AD1CHS0bits.CH0NA = 0; //Vref- as inverting input
    
    //IFS0bits.AD1IF = 0;
    IEC0bits.AD1IE = 0; //Interrupt disabled
    AD1CON1bits.ADON = 0b1; //ADC On
    
    
    

}


int main(void) {
    
    Initialization();
    AD1CON1bits.SAMP = 1; //Start Conversion
    
    
    while(1){
       
        
        if (ADC1BUF3 > 2047)
        {
            PORTB = 0x0200;
        }
        else
        {
            PORTB = 0x0000;
        }
        
    }
    
    return 0;
}

 
I am pretty new to dsPIC familiy so any help or information would be appreciated. Thanks in advance. 
 
 
#1
TS9
Super Member
  • Total Posts : 841
  • Reward points : 0
  • Joined: 2010/05/07 10:52:22
  • Status: offline
Re: dsPIC33EP512MU810 ADC won't sample 2020/01/10 02:12:03 (permalink)
0
Hi,
 
Please check below working ADC Init Code for first 12-Channels for Scanning .. AN3 Should worked 
Then modify according ..
 

#include"AnalogInputs.h"
#include"main.h"
#include <xc.h>
void ConfigureAnalogPins()
{
ANSELB = 0xFFF;
}
void InitAdc1(void)
{
// AN0 to AN11 Should be Analog
// Choose First 12-Channel for Scanning

/* Initialize ADC module */
// 22-Aug-2017
AD1CON1 = 0x04EC; // Enable 10-bit mode, auto-sample and auto-conversion

AD1CON2 = 0x040C; // Sample 4 channels alternately using channel scanning
// Choose First 12-Channel for Scanning
// 12-1 = 11
//22-Aug-2017 10-bit Mode
AD1CON1bits.AD12B = 0 ; // For 10-bits
AD1CON2bits.SMPI=11 ; // Choose First 12-Channel for Scanning

// AD1CON3 = 0xF0 ; // Sample for 15*TAD before converting

//22-Aug-2017 ADC at Full Speed - 2 Steps
AD1CON3 = 0x1F02 ;
AD1CON4 = 0x0000 ;

AD1CSSH = 0x0000 ;
AD1CSSL = 0x0FFF ;

// Choose First 12-Channel for Scanning

/* Assign MUXA inputs */
AD1CHS0bits.CH0SA = 0; // CH0SA bits ignored for CH0 +ve input selection
AD1CHS0bits.CH0NA = 0; // Select VREF- for CH0 -ve input

/* Enable ADC module and provide ADC stabilization delay */
AD1CON1bits.ADON = 1;
__delay_us(40);
}
 
 

#2
Jump to:
© 2020 APG vNext Commercial Version 4.5