1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

"Hello World" in C from dsPIC30F2020

Discussion in 'Microcontrollers' started by kchriste, Sep 9, 2008.

  1. kchriste

    kchriste New Member Forum Supporter

    Joined:
    Jul 23, 2006
    Messages:
    3,677
    Likes:
    47
    Location:
    Victoria BC, Canada
    Since there seems to be very little "simple" code for the dsPIC;
    here is a "Hello World" example for the dsPIC30F2020 for any who may be interested:

    Code (text):
    /* This is a "Hello world" example for the dsPIC30F2020. It will toggle all
    *  IO pins on the chip at a rate which is visible from a LED connected to
    *  one of the IO pins. It uses the slow internal oscillator so all that is
    *  needed is a 10K pullup resistor on MCLR (pin 1 on a 28 pin DIP chip) and
    *  power connected to ALL Vdd, Vss, AVdd, and AVss pins. Don't forget to
    *  put some 0.1uF bypass capacitors across the power pins close to the PIC.
    */

    #include <p30f2020.h>

    _FOSC(CSW_FSCM_OFF & FRC_LO_RANGE & OSC2_IO); //Oscillator Configuration
    _FOSCSEL(FRC);                                //Oscillator Selection
    _FWDT(FWDTEN_OFF);                            //Turn off WatchDog Timer
    _FGS(CODE_PROT_OFF);                          //Turn off code protect
    _FPOR( PWRT_OFF );                            //Turn off power up timer




    int main(void)
    {
       
        unsigned long x;

        ADPCFG = 0xffff;    //make ADC pins all digital
        TRISA = 0;      //Make all PORTs all outputs
        TRISB = 0;
        TRISD = 0;
        TRISE = 0;
        TRISF = 0;

        while(1)
        {
            for( x=0; x<100000; x++){;} //Delay apx 200ms
            LATA = ~LATA;           //Toggle all bits on ALL ports
            LATB = ~LATB;
            LATD = ~LATD;
            LATE = ~LATE;
            LATF = ~LATF;
        }
        return 0;
    }

     
    Edit: The compiler used was MicroChip's C30 student edition which is available for free on their site.
     
    Last edited: Sep 10, 2008
  2. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,642
    Likes:
    109
    Location:
    Michigan, USA
    Thank you. I hope to be experimenting with a dsPIC soon.

    I think Futz may have an example on his web site.

    Regards, Mike
     
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    I have a 30F2012 half wired up on a breadboard so I shall be trying this shortly - probably tomorrow.

    This will be a good start point, thank you.

    Mike.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. kchriste

    kchriste New Member Forum Supporter

    Joined:
    Jul 23, 2006
    Messages:
    3,677
    Likes:
    47
    Location:
    Victoria BC, Canada

    I did check out some of Futz's posts here, but they were for a different dsPIC and were more practical and thus more complex. The PIC programmed and worked perfectly on the first try, though I did debug the code in SIM first. On another note:

    While reading though the C30 Compiler User’s Guide I found this amusing piece of code:

    Code (text):
    void __attribute__((boot)) chuck_cookies()
    {
    int hurl;
    int them = 55;
    char *where = "far";
    splat(where);
    /* ... */
    }
     

Share This Page