1
\$\begingroup\$

I'm new to microcontrollers and the dsPIC33 and want to write a simple program before I get into anything more complex (even though this seems already very complex to me...). I am trying to toggle an LED on my dsPICDEM MCLV-2 development board using my dsPIC33EP256MC506 microcontroller. Right now, my code just makes the LED stay on without me pressing the button. Can anyone help me find whats wrong with my code?

If anyone knows of any sample programs that I can run and play with that would also be very helpful!

Compiler: MPLAB X

Programmer/Debugger: REAL ICE

Microcontroller: dsPIC33EP256MC506

Dev Board: dsPICDEM MCLV-2

main.c

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, port unknown 

while(1)
{
    if(S3 == 0) { //start/stop switch
        uint32_t N = 21000000;
        while(S3) //debounce            
            while(N--);
            LATDbits.LATD6 = 1; //make LED pin high
    }
}
}

user.h

#define S3  !PORTGbits.RG6  //S2 button
#define S2  !PORTGbits.RG7  //S3 button

#define D2  !PORTDbits.RD6  //D2 LED  

#define DEBOUNCE_DELAY  30  //push button debounce delay, expressed in millisecond

/******************************************************************************/
    /* User Function Prototypes                                                   */
    /******************************************************************************/
    /* TODO User level functions prototypes (i.e. InitApp) go here */
    void InitApp(void);         /* I/O and Peripheral Initialization */
\$\endgroup\$
2
  • \$\begingroup\$ Start with this bug: Just because LATDbits.LATD6 = 1; is indented doesn't make it a part of the while(S3) loop. Maybe you should have curly braces around those indented lines. \$\endgroup\$
    – kkrambo
    Commented Oct 25, 2016 at 20:35
  • \$\begingroup\$ I don't see any place where you set the LED pin low. \$\endgroup\$ Commented Oct 25, 2016 at 21:48

1 Answer 1

1
\$\begingroup\$

Try this out. make sure the S3 is the correct Macro definition for the button you are pressing, seems like you commented it wrong in the code.

while(1)
{
   uint32_t N = 21000000;
   uint32_t T = 21000000;

    if(S3 == 1) {
            LATDbits.LATD6 = 1; //make LED pin high
            while(N--); 
                        }
         else {  LATDbits.LATD6 = 0; //make LED pin low
               while(T--);  }

}

This won't check button debounce,(if this works, you can try out debounce) In some microcontroller there is Individual bits associated with the GPIO pins that can check the button debounce. If it is available,just enable it to check debounce.

\$\endgroup\$
4
  • \$\begingroup\$ Do you happen to know how to access analog pins such as the potentiometer on the development board? I want to read the output of the pot but can't figure out how exactly to call for this. I know I have to use ADC, but have no idea what functions to use. If you can point me to documentation or help out with starting this that would be great \$\endgroup\$
    – KP123
    Commented Oct 29, 2016 at 21:29
  • \$\begingroup\$ Yes you will have to use A/D converter for this.Usually in PIC MCs ADCON register is used in setting the A/D. Apart from this you will have to provide maximum and minimum Vref voltage. Reference voltage is used by the microcontroller to set the highest and lowest digital values. Then you will have to give the input V . Calibrate it to get the exact value. Not so difficult. You can search for a/d codes and circuits for PIC ICs.things will be similar except the register bits,pin num etc. \$\endgroup\$ Commented Oct 30, 2016 at 22:28
  • \$\begingroup\$ Really appreciate your feedback. I got some code working in which I use the simulator to set a voltage value on analog pins and can read the value when running. I'm not sure how to implement this on the board's physical potentiometer though. How do I give the input voltage to the potentiometer? Thanks again \$\endgroup\$
    – KP123
    Commented Oct 31, 2016 at 17:32
  • \$\begingroup\$ make sure that your potentiometer meter output is in safe limit of the input.(if you can find the reference voltage,connect +5volt in vref and 0 in -vref). try with the same code. for getting the real values you will have to do calibration.refer this example for more info. youtube.com/watch?v=hcxOs4dKE_E \$\endgroup\$ Commented Nov 1, 2016 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.