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 CCS Technical Support

ex. watchdog by serial communication or time in while

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



Joined: 02 May 2020
Posts: 73

View user's profile Send private message Send e-mail

ex. watchdog by serial communication or time in while
PostPosted: Wed Mar 16, 2022 11:57 am     Reply with quote

Hello friends, I'm trying a problem with the first program I made and put on a PC system.

The PIC18F46k22 receives a string from the PC, with a fixed number of characters, it happens that, every 50 tests I do, the microcontroller crashes, and it only works again when I turn it off and on again. If it was a different length string, I could handle it, but it's not.

So I thought of WatchDog, but I have no experience. Can anyone give me an example?

I just use Fuse
Code:

#fuses wdt


on the rs232 line:
Code:


#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stop=1, ERRORS, stream=PC, restart_wdt)


the rest the CCS does alone, in case it crashes by serial communication??
Or is it not that simple?

I also saw that it is possible to use a WDT in the while, because if the WatchDog does not burst the bit in a certain time, WDT Reset, the Program would be restarted. This one I didn't find example in the "Examples" folder
_________________
Gradually you will go far with persistence, will and determination!
temtronic



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

View user's profile Send private message

PostPosted: Wed Mar 16, 2022 12:46 pm     Reply with quote

re:
Quote:
The PIC18F46k22 receives a string from the PC,

OK, is this an interrupt driven receive AND do you use a BUFFER for the data ?
'string' usually indicates data then a /0 on the end as a terminator.

You should post a small, compilable program that fails.

Generally speaking , one does NOT enable the WDT , until the program has been verified to be 100% working, then you enable the WDT with a timeout of x2 or x3 the longest 'good loop' time.

When I first got the 46K22 I had a PC send data to com1 then com1 to com2 then com2 to PC. Used minor variation of the CCS supplied ex_sisr. Ran at 115k200 baud for 2 weeks...ZERO problems.
bdeb



Joined: 05 Nov 2010
Posts: 42
Location: Sweden

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

PostPosted: Wed Mar 16, 2022 12:51 pm     Reply with quote

Dear erpgc82,

I'm not a guru, but have used & loved the 46K22 and found it stable.

I assume you followed "ex_sisr.c" example of how to receive data.
May your buffer be too small for a "strange" message from your PC?

Try using other UART for debug or set an LED on buffer overflow.


Best of luck:

/BdeB
erpgc82



Joined: 02 May 2020
Posts: 73

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 16, 2022 3:20 pm     Reply with quote

Hello friend, yes I use a Buffer, I'm working with 26 bytes of characters and the buffer is 64 bytes.

Yes, as soon as the string ends, at the end I enter the '0' or '\0' by the way, it's very similar to the C++ I'm studying at the moment.

ok, i will post a piece here as soon as possible.

That's what I also thought that a Watchdog works, if it doesn't check the "while" of the "main()" function within a certain period of milliseconds, it would restart the program.

In a test I did, I noticed that the program restarts completely, as if it had disconnected from the mains and then reconnected. Is there a way for Watchdog to act, without having to restart the main function? In short, just unlock.

Very good, my God, the 115200, excellent!


temtronic wrote:
re:
Quote:
The PIC18F46k22 receives a string from the PC,

OK, is this an interrupt driven receive AND do you use a BUFFER for the data ?
'string' usually indicates data then a /0 on the end as a terminator.

You should post a small, compilable program that fails.

Generally speaking , one does NOT enable the WDT , until the program has been verified to be 100% working, then you enable the WDT with a timeout of x2 or x3 the longest 'good loop' time.

When I first got the 46K22 I had a PC send data to com1 then com1 to com2 then com2 to PC. Used minor variation of the CCS supplied ex_sisr. Ran at 115k200 baud for 2 weeks...ZERO problems.

_________________
Gradually you will go far with persistence, will and determination!
erpgc82



Joined: 02 May 2020
Posts: 73

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 16, 2022 3:22 pm     Reply with quote

Good tip, I'll do it! The PIC18F46K22 is really big and stable, I really didn't understand until now what's going on, so I thought of Watchdog, which I've never used, I'm new to programming.

bdeb wrote:
Dear erpgc82,

I'm not a guru, but have used & loved the 46K22 and found it stable.

I assume you followed "ex_sisr.c" example of how to receive data.
May your buffer be too small for a "strange" message from your PC?

Try using other UART for debug or set an LED on buffer overflow.


Best of luck:

/BdeB

_________________
Gradually you will go far with persistence, will and determination!
temtronic



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

View user's profile Send private message

PostPosted: Wed Mar 16, 2022 5:28 pm     Reply with quote

Consider the 'Watchdog' as a hardware 'cold reboot' used when the program takes too long to do a normal 'loop' of operations. When it 'times out', it forces the PIC to restart, from the beginning, just like a power off/power on sequence does.
That is how it's supposed to work.

Now there is a solution for when your serial data (26 bytes) doesn't all come in as planned. The CCS manual in the 'commonly asked questions' chapter or FAQ section, has a 'serial input with timeout' Q&A. It allows for a preset time period for the PIC to receive data from some source. You'll need to do some 'math' to fine tune the 'timeout' value. 9600 baud is +-960 characters per second, so a 'stream' of 26 characters takes about 1/40th of a second, say 25ms. double that = 50 ms.
So a timeout of 50 ms will allow the PIC to stay in the receive loop until either all 26 characters have come in OR the 50ms time has elapsed.
You can make the timeout value less (closer to 26ms) so the PIC will exit (return to main program), faster BUT it needs to be long enough for the PIC to store the data into a buffer, set flags, etc.

9600 baud is about 1 character per 1 millisecond. My math be not be correct, will be 69 this year...
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Thu Mar 17, 2022 2:55 am     Reply with quote

and, this is where 'restart_cause' comes in.

If you declare variables 'uninitialised', you can have code like:
Code:

   int myval;

   if (restart_cause() != RESTART_WDT)
   {
       myval=xx; //initialise only on a boot
       //do the same for your other variables
   }
   //code will arrive here on boot with 'myval' set to xx, but
   //on a watchdog, it'll have the old value.


This way you can have a 'warm boot' wake up with a watchdog.
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