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 :
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?
-
1\$\begingroup\$ Wow, that's one confusing ratsnest of connections! \$\endgroup\$Olin Lathrop– Olin Lathrop2017年07月09日 22:05:49 +00:00Commented Jul 9, 2017 at 22:05
1 Answer 1
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.
-
\$\begingroup\$ Hei, I have changed the clock to 16000000 (16 Mhz) and the delay time to 15000000, so it should delayed for about 0.937 second, but the led still lighted up all the time. Any idea? \$\endgroup\$Wildan S Nahar– Wildan S Nahar2017年05月05日 05:31:41 +00:00Commented May 5, 2017 at 5:31
-
\$\begingroup\$ @WildanSNahar Hmm. For troubleshooting, I would do a quick test and set the led OFF first, then on. If it lights up anyway, then that points to the circuit... \$\endgroup\$bitsmack– bitsmack2017年05月05日 05:37:50 +00:00Commented May 5, 2017 at 5:37
-
\$\begingroup\$ Yes, I did that. That works. First, in while(1) I changed the initial condition to
0xff
and it lights, and then in while(1) I changed again to0x00
and it turned off. So what's wrong then? \$\endgroup\$Wildan S Nahar– Wildan S Nahar2017年05月05日 05:42:23 +00:00Commented May 5, 2017 at 5:42 -
\$\begingroup\$ @WildanSNahar Well, that's good! It verifies your hardware :) Next, I would try a different function to make the delay. There is a
__delay_ms()
you may want to try. The "16-Bit Language Tools Libraries Reference Manual" says that you must first defineFCY
as your crystal frequency. And it must be defined before your#include "libpic30.h"
statement. Hmm. I wonder if you just move your#define _XTAL_FREQ 16000000
line to the very top of the file, maybe your original `__delay32() function might start working... \$\endgroup\$bitsmack– bitsmack2017年05月05日 05:57:23 +00:00Commented May 5, 2017 at 5:57 -
\$\begingroup\$ I tried that. But it doesn't work. How should I write
FCY
? And how can I use__delay_ms()
instead of__delay32()
? \$\endgroup\$Wildan S Nahar– Wildan S Nahar2017年05月05日 06:16:12 +00:00Commented May 5, 2017 at 6:16