Bumped by Community user
    Bumped by Community user
    Bumped by Community user
    Bumped by Community user
    Bumped by Community user
    Bumped by Community user
    Bumped by Community user
    Bumped by Community user
    Bumped by Community user
    Bumped by Community user
3 updated for new program code
source | link
/* Device header file */
#if defined(__XC16__)
#include <xc<p33Exxxx.h>
#elif defined(__C30__)/****************************CONFIGURATION****************************/
#if defined
_FOSCSEL(__dsPIC33E__FNOSC_FRC);
_FOSC(FCKSM_CSECMD & POSCMD_XT & #includeOSCIOFNC_OFF <p33Exxxx.h>
#elif& definedIOL1WAY_OFF);
_FWDT(__dsPIC33F__FWDTEN_OFF);
 _FPOR(ALTI2C1_ON & ALTI2C2_ON);
_FICD(ICS_PGD1 #include& <p33Fxxxx.h>
#endif
#endif
JTAGEN_OFF);

#include <stdint.h>void initAdc1(void);
void Delay_us(unsigned int);
int  ADCValue, i;
int  main(void)
{
/* Includes uint16_t definition            / Configure the device PLL to obtain 40 */
#includeMIPS <stdbooloperation.h>  The crystal frequency is 8 MHz.
/* Includes true/false definition Divide 8 MHz by 2, multiply by 40 and divide by 2. This results in Fosc of */
#include80 <stdioMHz.h>

#include "system.h"    // The CPU clock /*frequency Systemis funct/params,Fcy like= oscFosc/peripheral2 config= */
#include40 "userMHz.h"  
PLLFBD = 38;      /* User funct/params, such as InitApp         /* M  = 40 */
#include "adcCLKDIVbits.h"     

/******************************************************************************/
/* Global Variable Declaration                   PLLPOST = 0;         /* N1 = 2  */
CLKDIVbits.PLLPRE = 0;          /* N2 = 2  */
/******************************************************************************/

/* i.e. uint16_tOSCTUN <variable_name>;= */

/******************************************************************************/0;
/* Main Program                                                            Initiate Clock Switch to Primary Oscillator with */
/******************************************************************************/

int16_tPLL main(void)
{
    /* Configure the oscillator forNOSC the= device0x3) */
    ConfigureOscillator__builtin_write_OSCCONH(0x03);
     /* Initialize IO ports and peripherals */
    InitApp__builtin_write_OSCCONL(0x01);
   while TRISDbits(OSCCONbits.TRISD6COSC != 0; //set LED as output 0x3);
while (_LOCK == 0); TRISGbits.TRISG6 = 1; //set Button as input
    TRISEbits.TRISE13 = 1; //set Potentiometer as input

//ADC Init
ANSELA = 0;
ANSELB = 0;
ANSELAbits.ANSA0 = 1;  * //Wait for Monitor 2
ANSELAbits.ANSA1PLL =lock 1;at 40 MIPS /*/ for Monitor 
initAdc1();
while(1)
ANSELBbits.ANSB0 = 1;   // for{
 Monitor 3
ANSELE = 0;
ANSELEbitsAD1CON1bits.ANSE13SAMP = 1;  // POT

AD1CON1 = 0x006C;   //ADC is off
                    //Continue module operation in IdleStart modesampling
                    //Delay_us(10-bit, 4-channel ADC operation
   );                 //Data OutputWait Formatfor bitssampling Integertime (0000 00dd dddd10 ddddus)
         AD1CON1bits.SAMP = 0;         //PWM Special Event TriggerStart comparatorthe triggeredconversion
               while (!AD1CON1bits.DONE);    //Samples CH0, CH1, CH2, CH3Wait simultaneouslyfor whenthe CHPS<1:0>conversion =to 1xcomplete
        ADCValue = ADC1BUF0;          //Sampling begins immediatelyRead afterthe lastADC conversion SAMP bit is auto-set.result
}
AD1CHS0 = 0x000D;   //MUX B Channel 0 negative input is VREF-}
             void initAdc1(void) {
     //MUX B Channel* 0Set positiveport inputconfiguration is*/ AN0
       ANSELA = ANSELB = ANSELC  = ANSELE = 0x0000;
    //MUX A Channel 0 negative input is VREF-
       ANSELBbits.ANSB5 = 1;           //MUX A Channel 0 positiveEnsure inputAN5/RB5 is AN8analog
                    //just a startup sequence to read the POT (ANSELBbits.ANSB0 AN0= )
1;
    AD1CON1bitsANSELAbits.ADONANSA0 = 1;   //Enabling the ADC module


int pot1;

while(1) {
    if(S3 == 1) { //start/stop switch
    A0 and B0 are LATDbitsinputs
TRISAbits.LATD6TRISA0 = 1; //make LED pin high 
    } else {
        LATDbitsTRISBbits.LATD6TRISB0 = 0; //make LED pin low1;
    
/* Initialize and enable pot1ADC =module 0;*/

AD1CON1 = 0x0000;
AD1CON2 = 0x0000;
AD1CON3 = 0x000F;
AD1CON4 = 0x0000;
AD1CHS0 = 13;       0x0000;
//Select Potentiometer on0000 AN8
0000 0000 0101
AD1CHS123 = 0x0000;
AD1CSSH = 0x0000;
AD1CSSL = pot10x0000;
AD1CON1bits.ADON = PORTEbits.RE13;1;
Delay_us(20);
        if}
void Delay_us(pot1unsigned >int 1delay)  
{
           for LATDbits.LATD6(i = 1;
    0; i < delay; }i++)
{
    }    //end__asm__ ofvolatile if("repeat #39");
}    //end__asm__ ofvolatile while("nop");
}
}
/* Device header file */
#if defined(__XC16__)
#include <xc.h>
#elif defined(__C30__)
#if defined(__dsPIC33E__)
    #include <p33Exxxx.h>
#elif defined(__dsPIC33F__)
    #include <p33Fxxxx.h>
#endif
#endif


#include <stdint.h>        /* Includes uint16_t definition                    */
#include <stdbool.h>       /* Includes true/false definition                  */
#include <stdio.h>

#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"          /* User funct/params, such as InitApp              */
#include "adc.h"     

/******************************************************************************/
/* Global Variable Declaration                                                    */
/******************************************************************************/

/* i.e. uint16_t <variable_name>; */

/******************************************************************************/
/* Main Program                                                                   */
/******************************************************************************/

int16_t main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();
     /* Initialize IO ports and peripherals */
    InitApp();
    TRISDbits.TRISD6 = 0; //set LED as output 
    TRISGbits.TRISG6 = 1; //set Button as input
    TRISEbits.TRISE13 = 1; //set Potentiometer as input

//ADC Init
ANSELA = 0;
ANSELB = 0;
ANSELAbits.ANSA0 = 1;   // for Monitor 2
ANSELAbits.ANSA1 = 1;   // for Monitor 1
ANSELBbits.ANSB0 = 1;   // for Monitor 3
ANSELE = 0;
ANSELEbits.ANSE13 = 1;  // POT

AD1CON1 = 0x006C;   //ADC is off
                    //Continue module operation in Idle mode
                    //10-bit, 4-channel ADC operation
                    //Data Output Format bits Integer (0000 00dd dddd dddd)
                    //PWM Special Event Trigger comparator triggered
                    //Samples CH0, CH1, CH2, CH3 simultaneously when CHPS<1:0> = 1x
                    //Sampling begins immediately after last conversion SAMP bit is auto-set.

AD1CHS0 = 0x000D;   //MUX B Channel 0 negative input is VREF-
                    //MUX B Channel 0 positive input is AN0
                    //MUX A Channel 0 negative input is VREF-
                    //MUX A Channel 0 positive input is AN8
                    //just a startup sequence to read the POT ( AN0 )

    AD1CON1bits.ADON = 1;   //Enabling the ADC module


int pot1;

while(1) {
    if(S3 == 1) { //start/stop switch
        LATDbits.LATD6 = 1; //make LED pin high 
    } else {
        LATDbits.LATD6 = 0; //make LED pin low
        pot1 = 0;

        AD1CHS0 = 13;       //Select Potentiometer on AN8
        pot1 = PORTEbits.RE13;

        if (pot1 > 1) {
            LATDbits.LATD6 = 1;
        }

    }    //end of if()
}    //end of while()
}
#include <p33Exxxx.h>
/****************************CONFIGURATION****************************/

_FOSCSEL(FNOSC_FRC);
_FOSC(FCKSM_CSECMD & POSCMD_XT & OSCIOFNC_OFF & IOL1WAY_OFF);
_FWDT(FWDTEN_OFF);
_FPOR(ALTI2C1_ON & ALTI2C2_ON);
_FICD(ICS_PGD1 & JTAGEN_OFF);

void initAdc1(void);
void Delay_us(unsigned int);
int  ADCValue, i;
int  main(void)
{
// Configure the device PLL to obtain 40 MIPS operation. The crystal frequency is 8 MHz.
// Divide 8 MHz by 2, multiply by 40 and divide by 2. This results in Fosc of 80 MHz.
// The CPU clock frequency is Fcy = Fosc/2 = 40 MHz.
PLLFBD = 38;                    /* M  = 40 */
CLKDIVbits.PLLPOST = 0;         /* N1 = 2  */
CLKDIVbits.PLLPRE = 0;          /* N2 = 2  */
OSCTUN = 0;
/* Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0x3) */
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(0x01);
while (OSCCONbits.COSC != 0x3);
while (_LOCK == 0);             /* Wait for PLL lock at 40 MIPS */
initAdc1();
while(1)
{
    AD1CON1bits.SAMP = 1;         // Start sampling
    Delay_us(10);                 // Wait for sampling time (10 us)
    AD1CON1bits.SAMP = 0;         // Start the conversion
    while (!AD1CON1bits.DONE);    // Wait for the conversion to complete
    ADCValue = ADC1BUF0;          // Read the ADC conversion result
}
}
    void initAdc1(void) {
     /* Set port configuration */ 
     ANSELA = ANSELB = ANSELC  = ANSELE = 0x0000;
    //ANSELBbits.ANSB5 = 1;           // Ensure AN5/RB5 is analog
    ANSELBbits.ANSB0 = 1;
    ANSELAbits.ANSA0 = 1;

// A0 and B0 are inputs
TRISAbits.TRISA0 = 1;
TRISBbits.TRISB0 = 1;

/* Initialize and enable ADC module */

AD1CON1 = 0x0000;
AD1CON2 = 0x0000;
AD1CON3 = 0x000F;
AD1CON4 = 0x0000;
AD1CHS0 = 0x0000;
// 0000 0000 0000 0101
AD1CHS123 = 0x0000;
AD1CSSH = 0x0000;
AD1CSSL = 0x0000;
AD1CON1bits.ADON = 1;
Delay_us(20);
}
void Delay_us(unsigned int delay) 
{
for (i = 0; i < delay; i++)
{
    __asm__ volatile ("repeat #39");
    __asm__ volatile ("nop");
}
}
2 added 140 characters in body
source | link

How to readdisplay potentiometer value from dspic33 microcontroller

I am new to the microcontroller world and am using a dsPIC33EP256MC506 controller and dsPICDEM -2 MCLV development board for a project. I am trying to read the value of the potentiometer that is on the dev board, but am having trouble. I just want to be able to run the program and move turn the potentiometer and see the program output the value. I know that I have to use ADC ports somehow to do this, but I'm not exactly sure how to use it. Documentation has not been very helpful on how to do this in the program either. If anyone can direct me to documentation that uses ADC to control the potentiometer or help me get started with this that would be great.

Also, does anyone know where this value will be displayed? It makes sense to me that it would be displayed in the Variables output tab, but no values seem to be populating for any variable.

Microcontroller: dsPIC33EP256MC506

Development Board: dsPICDEM -2 MCLV

Programmer/Debugger: REAL ICE

Compiler: MPLAB X - XC16

How to read potentiometer value from dspic33 microcontroller

I am new to the microcontroller world and am using a dsPIC33EP256MC506 controller and dsPICDEM -2 MCLV development board for a project. I am trying to read the value of the potentiometer that is on the dev board, but am having trouble. I just want to be able to run the program and move turn the potentiometer and see the program output the value. I know that I have to use ADC ports somehow to do this, but I'm not exactly sure how to use it. Documentation has not been very helpful on how to do this in the program either.

Also, does anyone know where this value will be displayed? It makes sense to me that it would be displayed in the Variables output tab, but no values seem to be populating for any variable.

How to display potentiometer value from dspic33 microcontroller

I am new to the microcontroller world and am using a dsPIC33EP256MC506 controller and dsPICDEM -2 MCLV development board for a project. I am trying to read the value of the potentiometer that is on the dev board, but am having trouble. I just want to be able to run the program and move turn the potentiometer and see the program output the value. I know that I have to use ADC ports somehow to do this, but I'm not exactly sure how to use it. Documentation has not been very helpful on how to do this in the program either. If anyone can direct me to documentation that uses ADC to control the potentiometer or help me get started with this that would be great.

Also, does anyone know where this value will be displayed? It makes sense to me that it would be displayed in the Variables output tab, but no values seem to be populating for any variable.

Microcontroller: dsPIC33EP256MC506

Development Board: dsPICDEM -2 MCLV

Programmer/Debugger: REAL ICE

Compiler: MPLAB X - XC16

1
source | link

How to read potentiometer value from dspic33 microcontroller

I am new to the microcontroller world and am using a dsPIC33EP256MC506 controller and dsPICDEM -2 MCLV development board for a project. I am trying to read the value of the potentiometer that is on the dev board, but am having trouble. I just want to be able to run the program and move turn the potentiometer and see the program output the value. I know that I have to use ADC ports somehow to do this, but I'm not exactly sure how to use it. Documentation has not been very helpful on how to do this in the program either.

Also, does anyone know where this value will be displayed? It makes sense to me that it would be displayed in the Variables output tab, but no values seem to be populating for any variable.

Thanks again!

/* Device header file */
#if defined(__XC16__)
#include <xc.h>
#elif defined(__C30__)
#if defined(__dsPIC33E__)
    #include <p33Exxxx.h>
#elif defined(__dsPIC33F__)
    #include <p33Fxxxx.h>
#endif
#endif


#include <stdint.h>        /* Includes uint16_t definition                    */
#include <stdbool.h>       /* Includes true/false definition                  */
#include <stdio.h>

#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"          /* User funct/params, such as InitApp              */
#include "adc.h"     

/******************************************************************************/
/* Global Variable Declaration                                                    */
/******************************************************************************/

/* i.e. uint16_t <variable_name>; */

/******************************************************************************/
/* Main Program                                                                   */
/******************************************************************************/

int16_t main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();
     /* Initialize IO ports and peripherals */
    InitApp();
    TRISDbits.TRISD6 = 0; //set LED as output 
    TRISGbits.TRISG6 = 1; //set Button as input
    TRISEbits.TRISE13 = 1; //set Potentiometer as input

//ADC Init
ANSELA = 0;
ANSELB = 0;
ANSELAbits.ANSA0 = 1;   // for Monitor 2
ANSELAbits.ANSA1 = 1;   // for Monitor 1
ANSELBbits.ANSB0 = 1;   // for Monitor 3
ANSELE = 0;
ANSELEbits.ANSE13 = 1;  // POT

AD1CON1 = 0x006C;   //ADC is off
                    //Continue module operation in Idle mode
                    //10-bit, 4-channel ADC operation
                    //Data Output Format bits Integer (0000 00dd dddd dddd)
                    //PWM Special Event Trigger comparator triggered
                    //Samples CH0, CH1, CH2, CH3 simultaneously when CHPS<1:0> = 1x
                    //Sampling begins immediately after last conversion SAMP bit is auto-set.

AD1CHS0 = 0x000D;   //MUX B Channel 0 negative input is VREF-
                    //MUX B Channel 0 positive input is AN0
                    //MUX A Channel 0 negative input is VREF-
                    //MUX A Channel 0 positive input is AN8
                    //just a startup sequence to read the POT ( AN0 )

    AD1CON1bits.ADON = 1;   //Enabling the ADC module


int pot1;

while(1) {
    if(S3 == 1) { //start/stop switch
        LATDbits.LATD6 = 1; //make LED pin high 
    } else {
        LATDbits.LATD6 = 0; //make LED pin low
        pot1 = 0;

        AD1CHS0 = 13;       //Select Potentiometer on AN8
        pot1 = PORTEbits.RE13;

        if (pot1 > 1) {
            LATDbits.LATD6 = 1;
        }

    }    //end of if()
}    //end of while()
}