Menu

Config Bit and Control Bit Name Guessing for Watch dog timer and SLEEP

Help
2022-08-26
2022-08-27
  • Jeff Weinmann

    Jeff Weinmann - 2022-08-26

    I was looking to explore my chip's power consumption capability utilizing SLEEP and Watch dog timer. I noticed that I was starting blind on what syntax to use to configure the Watch dog timer.

    As with all chips we start by reading the Datasheet. The chip I'm using, PIC18F16Q40 has the capability of enabling the Watch dog timer by software or the WDTE config bit.

    Current examples refer to syntax like : WDT = ON. This is just not compatible for the chip I'm using.

    I was completely at a loss on syntax until I came across a most important fact about GCB to date. Every chip it can compile for has a .dat file that contains reserved words that can be used in code! This fact I would suggest be emphasized in the documentation.

    For Example, my chip's specific Config and Control bit names can be found in:

    \chipdata\18F16Q40.DAT

    This is a great resource. But this leads to another Easter egg hunt: There is little explanation of reserved names within the .dat file, so you must make your best educated guess as to what the variable name meanings are.

    I totally understand why this is so. the guys at GCB do not have time to re-write the datasheet inside the .dat file! I get it.

    So back to my example I needed to set the WDTE configuration bit to allow the Watch dog timer to be enabled by software.

    This text in my .dat file saved me a lot of headache:

    [ConfigOps]
    'For details of the config options see the microcontroller datasheet
    'The first parameter is the configuration field that can be used to expose specific configuration bits
    'The other parameters are the configuration field options that can be set.

    The config item, WDTE was defined as:

    WDTE=OFF,SWDTEN,NSLEEP,ON

    So piecing together this information I took a guess that I had to add:

    #config WDTE = SWDTEN 'set configuation bit to allow for software enabling of WDT
    

    Similarly for the Watchdog clock source and watchdog prescale (how long before WDT is triggered):

    .dat file:

    WDTCCS=LFINTOSC,MFINTOSC,SOSC,SC

    WDTCPS=WDTCPS_0,WDTCPS_1,WDTCPS_2,WDTCPS_3,WDTCPS_4,WDTCPS_5,WDTCPS_6,WDTCPS_7,WDTCPS_8,WDTCPS_9,WDTCPS_10,WDTCPS_11,WDTCPS_12,WDTCPS_13,WDTCPS_14,WDTCPS_15,WDTCPS_16,WDTCPS_17,WDTCPS_18,WDTCPS_19,WDTCPS_20,WDTCPS_21,WDTCPS_22,WDTCPS_23,WDTCPS_24,WDTCPS_25,WDTCPS_26,WDTCPS_27,WDTCPS_28,WDTCPS_29,WDTCPS_30,WDTCPS_31

    More educated guesses where made: LFINTOSC for the 32khz internal clock is what I wanted, and for the prescale a value of 0b01010, or decimal value of 10. gives me a 1 second interval, so I guessed : WDTCPS_10 only because the number of options and they way they were closely matched the bit values in the datasheet.

    So more config syntax construction:

    #config WDTCCS = LFINTOSC  
    #config WDTCPS = WDTCPS_10  
    

    Software enabling the Watch dog timer in code was a bit trickier. The last bit of register WDTCON0, according to the datasheet was SEN or WDTCON0.0. Setting this bit to 1 enables the Watch dog timer in code. I was able to finally call the SLEEP command and watch the PIC wake up based on my 1 second prescaller by this syntax:

    WDTCON0.0 = 1
    SLEEP
    WDTCON0.0 = 0
    

    My Program finally came to life!(or went to sleep that is). I also considered there HAD to be reserved syntax for such an important bit so again I reviewed the datasheet and Microchip referred to this bit as the SEN (software enable) bit.

    So I did a text search on my .dat file for 'SEN' and found: WDTSEN . One would guess, Watch Dog Timer Software Enable. So after syntax replacement, the code now looked like:

     WDTSEN = 1
        SLEEP
     WDTSEN = 0
    

    And sure enough it worked.

    In summary possibly adding a video to your library to explain how to use the chip datasheet along with the .dat file would be quite useful for beginners/intermediates.

    Here is a listing of the working code - pretty short and simple (once you know the syntax!):

    #CHIP PIC18F16Q40
    #CONFIG LVP=ON
    #option explicit
    
    #config WDTE = SWDTEN       'from page 237 of the Datasheet Software enable watchdog timer
    #config WDTCCS = LFINTOSC   'Variables found in \chipdata\18f16q40.dat
    #config WDTCPS = WDTCPS_10  'Derived after noticing a pattern and taking educated guesses
    
    #Define LED PORTB.7
    Dir PORTB.7 out
    
    Dim Count as Word
    Dim interval as Word
    
    for Count = 1 to 5
      LED = 1
      wait 50 ms
      LED = 0
      wait 50 ms
    next i
    
    Do Forever
        LED = 0
        Wait 1 sec
        LED = 1
        interval = 1  'interval is total wait in seconds
    
        SleepTime(1)
    Loop
    
    sub SleepTime(interval as Word)
      count = 0
    
    backtosleep:
    
      WDTSEN = 1
        SLEEP
      WDTSEN = 0
    
      count = count + 1
      If count < interval then goto backtosleep
    end sub
    
     
  • Anobium

    Anobium - 2022-08-26

    Jeff - look at PICINFO in your installation. There is an icon to the application. PICInfo shows you the default values the compiler will use.

     
  • William Roth

    William Roth - 2022-08-26

    While the .dat file is quite useful, it is the long way of looking up most of these settings.

    Picinfo uses the .dat file as the source then shows the most relevant data in a user friendly format. Config bits (Options), register names, bit names, interrupts names, and chip capabilities (Chip Data). I find the bit names most useful since Microchip is inconsistent on the bit name conventions from one chip family to another. eg. the same register.bit on one family of chips can have a different name on another family.

    I seldom look at the .dat files any more.

    And don't forget the demos. These give useful examples.

     

    Last edit: William Roth 2022-08-26
    • Jeff Weinmann

      Jeff Weinmann - 2022-08-27

      Well that one flew over my head. The PICInfo Icon is now staring at me saying why didn't you start here? lol

      Very nice tool. thanks for the education.

      regards,

      Jeff

       

Log in to post a comment.