Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] how to configure WDT in PIC 18F4520

Status
Not open for further replies.

abc_de

Full Member level 5
Full Member level 5
Joined
Jan 9, 2014
Messages
243
Helped
11
Reputation
22
Reaction score
11
Trophy points
1,298
Location
Ludhiana ਪੰਜਾਬ
Visit site
Activity points
2,939
hello
i am trying to configure WDT in PIC for 16msec but after so efforts i am unable to get reset.


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"p18f4520e.h"
 
#pragma config OSC      = INTIO67       // INTERNAL OSCILLATOR IS ON
#pragma config DEBUG    = OFF           // DEBUGGER IS OFF
#pragma config WDT      = ON        // WATCH DOG TIMER IS OFF
#pragma config LVP      = OFF           // LOW VOLTAGE PROGRAMMING IS OFF 
 
 
//16MSEC WDT TIMER
 
void main(void)
{
OSCCON=0X64;
TRISC=0X00;
LATC=0X00;
 
while(1)
{
 
WDTCONbits.SWDTEN=1;
LATC=0X00;
Delay10KTCYx(10);//100MSEC
LATC=0XFF;
Delay10KTCYx(1);//10MSEC
WDTCONbits.SWDTEN=0;
 
}

 
Last edited by a moderator:

Hi,

You have Set the Watchdog On in the Config line so no need to repeat it again in your main code.

The SWDTEN bit is only used in your code when the Watchdog has been placed OFF by the Config line.

With the 4520 the Watchdog Prescaler is also set by the Config parameters.

Below are the Config parameters and options.

You have an OSCCON setting of 0x64 so thats for 4mhz /4 = 1mhz Clock Speed, will let you work out what Prescaler ratios to get your desired time.

Also try a value of 0x62 for OSCON, what I use OK

You might find this C tutorial of help, though like most tutorials its based on the 16F chips , the 18F have a few differences like the watchdog parameters; thats where you should be looking at the 4520s datasheet for details.
**broken link removed**


Code:
WDT=ON
WDTPS = 2048   ;example value

;
;   Watchdog Timer Enable bit:
;     WDT = OFF            WDT disabled (control is placed on the SWDTEN bit)
;     WDT = ON             WDT enabled
;
;   Watchdog Timer Postscale Select bits:
;     WDTPS = 1            1:1
;     WDTPS = 2            1:2
;     WDTPS = 4            1:4
;     WDTPS = 8            1:8
;     WDTPS = 16           1:16
;     WDTPS = 32           1:32
;     WDTPS = 64           1:64
;     WDTPS = 128          1:128
;     WDTPS = 256          1:256
;     WDTPS = 512          1:512
;     WDTPS = 1024         1:1024
;     WDTPS = 2048         1:2048
;     WDTPS = 4096         1:4096
;     WDTPS = 8192         1:8192
;     WDTPS = 16384        1:16384
;     WDTPS = 32768        1:32768
 

Thank You for your support. problem is solved,
now i am getting interrupt every 16 millisec because loop takes more then 120millisec to complete


Code:
#include"p18f4520e.h"
 
#pragma config OSC      = INTIO67       // INTERNAL OSCILLATOR IS ON
#pragma config DEBUG    = OFF           // DEBUGGER IS OFF
#pragma config WDT      = ON        // WATCH DOG TIMER IS OFF
#pragma config LVP      = OFF           // LOW VOLTAGE PROGRAMMING IS OFF 
 #pragma config WDTPS = 4          //  1:4 4*4=16msec
 
//16MSEC WDT TIMER
 
void main(void)
{
OSCCON=0X64;
TRISC=0X00;
LATC=0X00;
 
while(1)
{
 
LATC=0X00;
Delay10KTCYx(10);//100MSEC
LATC=0XFF;
Delay10KTCYx(1);//10MSEC 
WDTCONbits.SWDTEN=0;
 
}
 

Thank You for your support. problem is solved,
now i am getting interrupt every 16 millisec because loop takes more then 120millisec to complete


Code:
#include"p18f4520e.h"
 
#pragma config OSC      = INTIO67       // INTERNAL OSCILLATOR IS ON
#pragma config DEBUG    = OFF           // DEBUGGER IS OFF
#pragma config WDT      = ON        // WATCH DOG TIMER IS OFF
#pragma config LVP      = OFF           // LOW VOLTAGE PROGRAMMING IS OFF 
 #pragma config WDTPS = 4          //  1:4 4*4=16msec
 
//16MSEC WDT TIMER
 
void main(void)
{
OSCCON=0X64;
TRISC=0X00;
LATC=0X00;
 
while(1)
{
 
LATC=0X00;
Delay10KTCYx(10);//100MSEC
LATC=0XFF;
Delay10KTCYx(1);//10MSEC 
WDTCONbits.SWDTEN=0;
 
}



Hi,

Are you sure ?

The Watchdog starts off on a cycle of 16ms, if it is not Cleared, by the Clear Watchdog instruction, before the end of 16ms it will Reset the program back to the start.

Your program loop first turns Off PortC then starts a 100ms delay, so how can that 100ms delay complete if the Watchdog is Resetting the program every 16 ms , that is taking the program back to the start ?

To allow your Port to flash you would need a minimum watchdog time of 100ms + 10ms + time for the ports to be set, say at least 111ms, then at the end of that you would have to Clear the Watchdog to ensure it could do another loop of your program.

You can now see why turning the Watchdog Off as the last instruction in your loop is not right ?


The whole point of the Watchdog is that if your program code gets stuck in a loop then after the watchdogs set time, if it does not receive a clear Watchdog instruction it will Reset and restart the whole program. It is not an Interrupt, those are something else entirely.

What are you using to test your timings ?
 

Hello
actually i was just testing watch dog timer is reset or not, the total time to complete the loop is more then 120msec and i set the WDT at 16msec it will reset the CPU. if i do some changes like
Code:
#include"p18f4520e.h"
 
#pragma config OSC      = INTIO67       // INTERNAL OSCILLATOR IS ON
#pragma config DEBUG    = OFF           // DEBUGGER IS OFF
#pragma config WDT      = ON        // WATCH DOG TIMER IS OFF
#pragma config LVP      = OFF           // LOW VOLTAGE PROGRAMMING IS OFF 
 #pragma config WDTPS = 8         //  1:8 4*8=32msec
 
//32MSEC WDT TIMER
 
void main(void)
{
OSCCON=0X64;
TRISC=0X00;
LATC=0X00;
 
while(1) // total loop time more then 20msec
{
 
LATC=0X00;
Delay10KTCYx(1);//10MSEC
LATC=0XFF;
Delay10KTCYx(1);//10MSEC 
WDTCONbits.SWDTEN=0;
 
}

the CPU is not get reset. it works

i am using using MPLAB SIM as debugger and STOP watch
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top