Electrical – wrong with this dsPIC30F2020 blink example

cembeddedmicrocontrollerpic

I have coded a simple blink example for a dsPIC30F2020. I think that the schematic is correct, but I am unsure of the configuration bits.

Tools :

  • MCU : dsPIC30F2020
  • Programmer : PICKit 2
  • IDE : MPLAB X IDE v3.60

Here is the code:

#include "xc.h"
#include "libpic30.h"
#include "stdio.h"
#include "stdlib.h"
#include "p30F2020.h"

// xtal
#define _XTAL_FREQ 16000000

// DSPIC30F2020 Configuration Bit Settings
// 'C' source line config statements

// FBS
#pragma config BWRP = BWRP_OFF          // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_BOOT_CODE       // Boot Segment Program Flash Code Protection (No Boot Segment)

// FGS
#pragma config GWRP = GWRP_OFF          // General Code Segment Write Protect (General Segment may be written)
#pragma config GSS = GSS_OFF            // General Segment Code Protection (Disabled)

// FOSCSEL
#pragma config FNOSC = PRIOSC           // Oscillator Mode (Primary Oscillator (HS, EC))

// FOSC
#pragma config POSCMD = HS              // Primary Oscillator Source (HS Oscillator Mode)
#pragma config OSCIOFNC = OSC2_CLKO     // OSCI/OSCO Pin Function (OSCO pin has clock out function)
#pragma config FRANGE = FRC_HI_RANGE    // Frequency Range Select (High Range)
#pragma config FCKSM = CSW_FSCM_OFF     // Clock Switching and Monitor (Sw Disabled, Mon Disabled)

// FWDT
#pragma config WDTPS = WDTPOST_PS32768  // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA0 = WDTPRE_PR128    // WDT Prescaler (1:128)
#pragma config WWDTEN = WINDIS_OFF      // Watchdog Timer Window (Non-Window mode)
#pragma config FWDTEN = FWDTEN_OFF      // Watchdog Timer Enable (Disable)

// FPOR
#pragma config FPWRT = PWRT_128         // POR Timer Value (128ms)

// FICD
#pragma config ICS = ICS_PGD            // Comm Channel Select (Use PGC/EMUC and PGD/EMUD)

int main(){
    _TRISD0 = 0x00;

    while(1){
        _RD0 = 0xff;
        __delay32(150000000);
        _RD0 = 0x00;
        __delay32(150000000);
    }
    return 0;
}

And here is the schematic:
schematic for blink example

Pickit 2 screenshot :

pic kit 2

When I connect the LED to RD0 (pin 15), the LED lights up all the time; it does not blink.

What might be wrong?

[UPDATE]
I think the __delay32() is the problem. Any idea?

Best Answer

It looks like you're using a 16 MHz crystal, but telling the compiler that your frequency is 160 MHz:

#define _XTAL_FREQ 160000000

Also, at 16 MHz, each clock cycle is \$1/16,000,000 = 62.5ns.\$

You are calling __delay32(150000000);, which would equate to \$150000000\cdot62.5ns=9.37s\$. So the LED will be on for about ten seconds, then off for about 10 seconds.