CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to

Problem in delay functions

Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message

Joined: 29 Jan 2019
Posts: 7
Location: Pakistan

View user's profile Send private message

Problem in delay functions
PostPosted: Sun Feb 07, 2021 11:04 am     Reply with quote

Hello everyone,
I want to print few simple strings on serial monitor with a specified time delay in between and for that I write the code using delay function Delay_ms(). But the code was not running properly as on serial monitor strings after delay function were not being printed. I am sharing the code. Please guide me what was the possible issue.

#include <18F46K22.h>
#device ADC=16

#FUSES WDT                      //Watch Dog Timer
#FUSES WDT128                     //Watch Dog Timer uses 1:1 Postscale
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(crystal=16000000)
#use rs232(baud=9600,parity=N,xmit=PIN_E0,bits=8,stream=PORT1,restart_wdt)

void main()


      fprintf(PORT1,"AT+TCPCLIENT Step1");
     fprintf(PORT1, "\r\n");
       fprintf(PORT1,"AT+TCPCLIENT Step2");
       fprintf(PORT1, "\r\n");
      fprintf(PORT1,"AT+TCPCLIENT Step3");
      fprintf(PORT1, "\r\n");

Joined: 28 Sep 2018
Posts: 366
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Sun Feb 07, 2021 11:37 am     Reply with quote

You're using the watchdog timer, but you never reset it. That will reset your micro every time the watchdog timer runs over.

Change the WDT to NOWDT for starters...

Joined: 01 Jul 2010
Posts: 7975
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Feb 07, 2021 11:44 am     Reply with quote

Better yet stop using the WDT !

WDT should never be enabled until 100% of the code is working.

After that, you can determine how long the WDT should be set for.
Say you 'do the math' and 1.5 deconds seems to be reasonable, then make WDT timeout to be say 2 seconds. You have to read the datasheet as most WDT are NOT accurate, some vary +-30% or more.

Joined: 11 Mar 2010
Posts: 16935

View user's profile Send private message

PostPosted: Mon Feb 08, 2021 2:57 am     Reply with quote

People have the habit of thinking that enabling a watchdog is some form
of 'magical fix', but in fact using a watchdog to actually be useful is a complex
thing to do. Having the watchdog enabled, without a proper strategy may
actually make code less likely to work, rather than more likely to work.

For the simplest watchdog, the 'interval' needs to be selected to be longer
than the interval at which you 'test' that the code is running correctly,
and reset the timer. The watchdog should only be reset if things look sensible.
Ideally the reset itself should only be reachable if all the code is doing all the
things it is meant to be doing.
A good discussion of how to structure the watchdog code is here:

Another thing important to understand is that on most PIC's the default
watchdog, is very fast indeed. Only a few mSec. Almost no code could ever
use the watchdog at this setting. Some later PIC's slow this down, because
the manufacturers have realised that this time is just too short. Even the
space shuttle main engine used a watchdog that was over 100* slower
than the default PIC value!.

So first comment is as the two previous posters have said. Don't enable
the watchdog for basic testing. Then if you do want to use the watchdog,
you actually need to design your code to actually do this. Have a watchdog
reset routine, that is called slightly faster than the watchdog interval (be
very careful of the tolerance - as Jay has said, some of the PIC watchdogs
have awful tolerances). Have this routine test values and events that should
be occurring, and only actually reset the watchdog if these are doing what
they should be doing. Then your code needs to be written so that it can
start tidily if a watchdog does trigger. So initialise variables only on a
power on restart, but have the values tested for being in a sensible range.
With this all done, you then have code that can properly use the watchdog.
Simply enabling the watchdog, and 'scattering' watchdog resets, does
nothing to improve the code... Sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group