WDT

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
richardb
Posts: 307
Joined: Tue Oct 03, 2006 8:54 pm

WDT

Post by richardb » Wed Oct 04, 2006 12:05 pm

I've suddenly started to have a problem with the WDT resetting with the following code.

Repeat
tempchar = USART.ReadByte
LCD.WriteAt(2,2,"*")
Until tempchar = "G"

If i disble when programming, then the program works fine.

Rich.
Hmmm..

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Wed Oct 04, 2006 1:32 pm

You need to add a special compiler directive at the beginning of your main program before any include files. For example,

Code: Select all

device = 18F452
clock = 20

#option WDT = true
include "usart.bas"

dim Value as byte
SetBaudrate(br19200)
Value = ReadByte
...
EXPLANATION

Just using the standard config directive 'WDT = ON' will only set the MCUs fuse. For example,

Code: Select all

config
   OSC = HSPLL,
   BOR = ON,
   WDT = ON
   
include "usart.bas"
...
However, many library calls are blocking or they may take quite some time to execute. To stop the WDT resetting the MCU, you have to 'tickle' it. By default, swordfish libraries will not do this (as it adds a little extra code overhead). Including the directive

Code: Select all

#option WDT = true
will do two things

(1) Automatically set the config 'WDT = ON'
(2) Link in the extra code needed to tickle the WDT

richardb
Posts: 307
Joined: Tue Oct 03, 2006 8:54 pm

Post by richardb » Wed Oct 04, 2006 2:13 pm

OK this is the top of my prog


Device = 18f458
Clock = 20
'PicBasic program for stepper motor
#option WDT = true
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTB.4
#option LCD_EN = PORTB.5

Include "LCD.bas"
Include "utils.bas"
Include "usart.bas"
Include "convert.bas"
Include "math.bas"
Include "string.bas"

It still seems to reset.



the help files need improving in this respect as i had looked up WDT and it comes up with nothing.



Device = 18f458
Clock = 20
Config
WDT = OFF

even doing this doesnt work i can only get it to run if explicitly turn off the wdt fuse in the epicwin programmer.
Hmmm..

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Wed Oct 04, 2006 3:03 pm

> even doing this doesnt work i can only get it to run if
> explicitly turn off the wdt fuse in the epicwin programmer.

This is my fault. If you select FILE...OPEN, then browse the 'Library' folder for 'system.bas'. Open it in the IDE.

Double click on the 'lock' icon to enable editing.

about halfway down the page you will see

Code: Select all

{
****************************************************************************
* Name    : ClrWDT                                                         *
* Purpose : Clear the watch dog timer                                      *
****************************************************************************
}
#option WDT = true //false
As you can see, I accidently left the default behaviour to TRUE when debugging. Just change to

Code: Select all

#option WDT = false
Save the file, then close it. Your EPIC screen will now default to WDT = OFF, unless

(a) You use the #option WDT = true in your program
or
(b) You use config WDT = ON in your program

Let me know if the above fixes the default behaviour problem.

> It still seems to reset.

With #option WDT = true, the libraries here get built correctly with the ClrWDT mnemonic. It can only be one of two things

(a) One of the swordfish libraries is missing a call
(b) You program is causing the problem, by taking too long to execute some code without resetting the WDT.

Remember, you are responsible for periodically clearing the WDT in you main code. For example, if you have a looping construct that performs some computationally expensive task (floating point, strings) you will need to reset the watchdog. To do this, just include "system.bas" in your main program and call ClrWDT(). For example,

Code: Select all

device = 18f458
clock = 20
include "system.bas"

while true
   // some code here...
   ClrWDT // prevent reset
wend
If you still belive it is a swordfish problem, please post some code to demonstrate and we will take a look...

richardb
Posts: 307
Joined: Tue Oct 03, 2006 8:54 pm

Post by richardb » Thu Oct 05, 2006 7:22 am

ok i'm confused as to why this became a problem as i didnt change anything in the program when the wdt became a problem.
anyway...

i changes the system.bas file as you suggested that stopped it resetting. so now i've reenabled.

the following code doesnt reset. and has 6 references to clrwdt in the asm code

Code: Select all

Device = 18f458
Clock = 20
'Config WDT  = OFF
 'PicBasic program for stepper motor 
#option WDT = true
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTB.4
#option LCD_EN = PORTB.5

Include "LCD.bas" 
Include "utils.bas"
Include "usart.bas"
Include "convert.bas"
Include "math.bas"
Include "string.bas"
Dim t As Char
SetBaudrate(br38400)
LCD.Cls
LCD.WriteAt(1,1,"start")
    While true
        Repeat 
            LCD.WriteAt(2,1,"1")
            t = USART.ReadByte
            LCD.WriteAt(3,1,"2")
        Until t = "G" 
    Wend    
End



the following code resets after 2 is displayed and has 3 ref's to clrwdt in the asm

Code: Select all


Device = 18f458
Clock = 20
'Config WDT  = OFF
 'PicBasic program for stepper motor 
#option WDT = true
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTB.4
#option LCD_EN = PORTB.5

Include "LCD.bas" 
Include "utils.bas"
Include "usart.bas"
Include "convert.bas"
Include "math.bas"
Include "string.bas"

Dim AVCURRENT1 As PORTA.0  
Dim PKCURRENT1 As PORTA.1 
Dim AVCURRENT2 As PORTA.2 
Dim PKCURRENT2 As PORTA.3 
Dim LED As PORTA.4         
'*******************************************************************************
'Dim DEBUGTX As PORTC.0
Dim PWM2 As PORTC.1       
Dim PWM1 As PORTC.2      
'Dim DEBUGRX As PORTC.3    
'Dim RTS As PORTC.4        
'Dim CTS As PORTC.5       
Dim RS232TX As PORTC.6     
Dim RS232RX As PORTC.7 
'*******************************************************************************
Dim CCWLIMIT As PORTD.0    
Dim CWLIMIT As PORTD.1     
Dim MARKER As PORTD.2      
'Dim SPAREMARKER As PORTD.3 
'*******************************************************************************
Const motormax =7
Dim motorindex As Byte
Dim tempchar As Char
Dim tempbyte As Byte
Dim tempstring As String(10)
Dim stageposition As LongInt
Dim ok As Boolean 
'*******************************************************************************

Sub steponce(direction As Bit,delay As Word)
    Const motorbits(motormax+1) As Byte = (%00001000,%00001010,%00000010,%00000110,%00000100,%00000101,%00000001,%00001001)
    Dim temp As Byte
    If direction = 1 Then         'stepplus
        If motorindex >= motormax Then 
            motorindex = 0
        Else
            motorindex = motorindex + 1
        EndIf
    Else                        'stepminus
        If motorindex <= 0 Then
            motorindex = motormax
        Else
            motorindex = motorindex - 1
        EndIf
    EndIf
    temp =PORTB And %11110000  ' store the port values for the upper bits of the port lower 4bits all zero
    PORTB = temp Or motorbits(motorindex)' upper bits of motor.. should be zero
    DelayUS(delay)
    PORTB = temp And %11110000' sets all lower bits to 0
End Sub

'*******************************************************************************
Sub move(distance As LongInt)    '1 = clockwise
    Dim dir As Bit
    Dim t As Word
    Dim i As Word
    'Dim dist As Word
    i = 1
    t = 5000 '2000ms
    
    If distance = 0 Then 
        Exit
    EndIf
    If distance > 0 Then
        dir =1
    Else
        dir = 0
    EndIf
    Repeat
    'USART.Write(DecToStr(i)," ")
    'USART.Write(DecToStr(distance)," ")
    If dir = 1 Then
        If  CCWLIMIT = 0  Then 
            USART.Write("CCWLIMIT ")
            Exit
        EndIf 
    Else
        If  CWLIMIT = 0  Then 
            USART.Write("CWLIMIT ")
            Exit
        EndIf  
    EndIf
    i=i+1
    If t > 300 Then
        t = t-(1+(t/100))
    EndIf
    steponce(dir,t)
    Until i >= abs(distance)

End Sub 

'*******************************************************************************

  
'*******************************************************************************
main:
SetAllDigital 
ADCON1 = %0000001'; AN0-4 = AN; AN5-7 = DIG
TRISA = %11101111
TRISB = %11000000
TRISC = %10101000
TRISD = %00001111
motorindex = 0
stageposition = 0
'*******************************************************************************
SetBaudrate(br38400)
DelayMS(200)
USART.Write("Start ")
DelayMS(100)
PWM1 = 1  'output enabled
PWM2 = 1  'output enabled
PORTB = 0' set drive outputs low(ie no current
LED = 1  ' no pullup on port a.4!!!!!!!!!!!!!!!!!!!!!!!!!
LCD.Cls
LCD.WriteAt(1,1,"Started")
ReadTerminator = 13
While true
    LCD.WriteAt(4,1,"1")
    Repeat
    LCD.WriteAt(4,3,"2") 
    tempchar = USART.ReadByte
    LCD.WriteAt(4,5,"3")
    Until tempchar = "G"     '?<cast from char to byte
    LCD.WriteAt(4,7,"4")
   ' tempchar = ReadByte
    'LCD.WriteAt(2,2,tempchar)
    
    USART.Read(tempstring) '< reads in a string with a chr13 terminator
    tempchar = tempstring(0)
    
    
    
     LCD.WriteAt(2,1,tempchar)
Wend
    
    Repeat 
        Select tempchar
            Case "1","2","3","4","5","6","7","8","9","0","-" 
                LCD.WriteAt(2,1,tempstring)
                LCD.WriteAt(1,1,"           ")
                move(StrToDec(tempstring))
                ok = true
            Case "HOME"    
                LCD.WriteAt(1,1,"should home")
                ok = true
            Else
                LCD.WriteAt(1,1,"else")
                tempbyte = 0
                Repeat 
                    tempbyte = tempbyte + 1
                    tempstring(tempbyte) = tempstring(tempbyte+1)
                Until tempstring(tempbyte) <>Char(0)
                ok = false
        EndSelect    
     Until ok



End
    
Hmmm..

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Thu Oct 05, 2006 9:07 am

Thanks for the code. I have identified a problem with the compiler, which has now been fixed. It was caused by the line

Code: Select all

   Case "HOME"   
      LCD.WriteAt(1,1,"should home")
      ...
this part of the case statement is always FALSE, as a single char (tempchar) can never be equal to a string ("HOME"). Removing the dead code was causing the problem. This is related to something I believe we discussed via email when the forum was offline.

You should

(a) Download the latest version (1.1.9.9) from http://www.sfcompiler.co.uk/swordfish/download

(b) Install the new build. You won't need to uninstall your current version, the files will be overwritten.

When you have done this, verify the version from HELP...ABOUT. It should read 1.1.9.9

I would appreciate it if you could let me know if this build fixes the problem for you.

richardb
Posts: 307
Joined: Tue Oct 03, 2006 8:54 pm

Post by richardb » Thu Oct 05, 2006 9:33 am

That worked.

Thanks for the help.
Hmmm..

Richard
Posts: 5
Joined: Sun Nov 05, 2006 10:19 pm
Location: michigan

newest compiler

Post by Richard » Wed Nov 08, 2006 2:19 am

I have been following this thread. When I go to download page for latest compile, I still get version 1.2.0.8! Where is the latest version??
Richard in Michigan

User avatar
mister_e
Posts: 28
Joined: Fri Oct 06, 2006 2:02 pm
Location: Montréal, Canada
Contact:

Post by mister_e » Wed Nov 08, 2006 4:03 am

euh.. 1.2.0.8 is newer than 1.1.9.9 so you have the latest?

Or i miss something?

Richard
Posts: 5
Joined: Sun Nov 05, 2006 10:19 pm
Location: michigan

Post by Richard » Wed Nov 08, 2006 4:13 am

:oops:

In my haste, I miss read...thanx for the corrective reply!
Richard in Michigan

Post Reply