# dsPIC30F2010 - 30 I/SP restarts at interval of 20seconds for no known reason

#### londoner

##### Newbie level 4
I have a simple code for blinking LEDs in dsPIC30F2010. I am using MPLABX v5.20 and XC16 v1.36.

I notice the dsPIC restarts at approximately 20seconds. Please, what could be the cause? I have tried on different boards and same result.

Here is my code:

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <xc.h>
#include <libpic30.h>
#include <p30F2010.h>

_FOSC(CSW_FSCM_OFF & XT_PLL16);
_FWDT(WDT_OFF);
_FBORPOR(MCLR_DIS & PWRT_OFF & PBOR_ON & BORV_27 & RST_PWMPIN & PWMxH_ACT_HI & PWMxL_ACT_HI);
_FGS(CODE_PROT_OFF);

void main( void )
{
TRISEbits.TRISE4 = 0;
TRISEbits.TRISE5 = 0;

_LATE4 = 1;
_LATE5 = 1;                          //used this to know when there is restart
__delay32(2400000);

_LATE5 = 0;

while( 1 )
{
__delay32(2400000);
_LATE4 = 1 - _LATE4;
_LATE5 = 1 - _LATE5;          //toggle LEDs
}
}

Xtal is 6MHz external. Is there something I need to turn off or on in the SFRs?

#### andre_teprom

##### Super Moderator
_LATE5 = 1 - _LATE5; //toggle LED s
Although at first glance this should work, surely doing a logical inversion by an algebraic operation is not the usual way of toggling a bit. Consider instead of doing a toogle, just forcing to a fixed value, .

#### londoner

##### Newbie level 4
Code:
_LATE5 = 1 - _LATE5;          //toggle LED s
Although at first glance this should work, surely doing a logical inversion by an algebraic operation is not the usual way of toggling a bit. Consider instead of doing a toogle, just forcing to a fixed value, .
I tried all stuff. I just changed it to below as suggested. The restart still happens exactly 20 seconds. It is frustrating.

Code:
    __delay32(2400000);
_LATE4 = 0;
_LATE5 = 1;
__delay32(2400000);

_LATE4 = 1;
_LATE5 = 0;
I also changed the void main() to int main() with a return 0; still no show.

I still wonder if there is a register or something else I need to set. The uniformity of interval of the restart is exact, Meaning something internal or by default is resetting.

#### betwixt

##### Super Moderator
I'm not that familiar with dsPIC30 series code but a restart at fixed intervals regardless of the coding strongly suggests a watchdog issue. I assume the line "_FWDT(WDT_OFF);" turns it off, is there another control for the watchdog, maybe in the device config that needs attention?

Brian.

londoner

### londoner

points: 2

#### FvM

##### Super Moderator
Besides code review, you should also learn to read reset reason register.

points: 2

### c_mitra

points: 2

#### londoner

##### Newbie level 4
I'm not that familiar with dsPIC30 series code but a restart at fixed intervals regardless of the coding strongly suggests a watchdog issue. I assume the line "_FWDT(WDT_OFF);" turns it off, is there another control for the watchdog, maybe in the device config that needs attention?

Brian.
Strangely, the WDT was the culprit. It remains enabled despite turning it off in the config and confirming that on the programmer config bits too.

Apply CLRWDT stopped the reset.

Could a bad chip be the cause or errata issue or something I don't know yet?

Besides code review, you should also learn to read reset reason register.
Since I don't have any readout attached yet to the dsPIC30F, I was able to use LED pattern to confirm the cause of reset via the WDTO bit of RCON. Please, is there anyway RCONbits check can be integrated in code to resolve issues automatically besides development test?

londoner

### londoner

points: 2

#### londoner

##### Newbie level 4
Are you sure that the compiler supports this syntax?

Code:
_FWDT(WDT_OFF);
According to Microchip's datasheet (View attachment 154359), the correct one for 16-bits microcontrollers seems like being rather that:

Code:
#pragma config WDT=OFF
Thanks,
I tried the new config as below with no success. The reset was ongoing unless I use CLRWDT. I believe the chip is bad. I will be receiving new dsPIC from my supplier soon to confirm if it is chip level failure.

Code:
#pragma config FWPSB = WDTPSB_10        // WDT Prescaler B (1:10)
#pragma config FWPSA = WDTPSA_512       // WDT Prescaler A (1:512)
#pragma config WDT = WDT_OFF            // Watchdog Timer (Disabled)
The prescaler A and B setting explains the >10seconds reset interval I was getting. Strange indeed! Let's assume it is chip partial failure.

I am running at 6MHz and PLL_16 which should be 24 MIPS Tcy = 41.6ns. That means 1 sec delay should be __delay32(24000000) but __delay32(2400000) was the correct value on the physical chip against my math.

#### Aussie Susan

##### Advanced Member level 4
The answer, as so often happens, is in the data sheet. The last paragraph of Section 19.4.2 says that, if you turn off the config bit, then the WDT is controlled by software with the SWDTEN bit. Make sure this is bit is 0.
Susan

andre_teprom

### andre_teprom

points: 2

#### londoner

##### Newbie level 4
The answer, as so often happens, is in the data sheet. The last paragraph of Section 19.4.2 says that, if you turn off the config bit, then the WDT is controlled by software with the SWDTEN bit. Make sure this is bit is 0.
Susan
I appreciate your input. This is a useful pointer. However, my test code got that bit cleared as depicted below:

Code:
 //read RCON bits for reset conditions
if(RCONbits.WDTO)
{
_LATE4 = 0;
_LATE5 = 0;
__delay32(9600000);

}
RCON = 0x0000;
I believe clearing the entire RCON bits does the job of ensuring SWDTEN = 0.
Update will be made when I receive new dsPIC chips, as I have agreed my dsPIC chip is faulty. It will be interesting to find out.