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:
#configWDTE=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):
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.
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!):
#CHIPPIC18F16Q40
#CONFIGLVP=ON
#optionexplicit
#configWDTE=SWDTEN'from page 237 of the Datasheet Software enable watchdog timer
#configWDTCCS=LFINTOSC'Variables found in \chipdata\18f16q40.dat
#configWDTCPS=WDTCPS_10'Derived after noticing a pattern and taking educated guesses
#DefineLEDPORTB.7DirPORTB.7outDimCountasWordDimintervalasWordforCount=1to5LED=1wait50msLED=0wait50msnextiDoForeverLED=0Wait1secLED=1interval=1'interval is total wait in secondsSleepTime(1)LoopsubSleepTime(intervalasWord)count=0backtosleep:
WDTSEN=1SLEEPWDTSEN=0count=count+1Ifcount<intervalthengotobacktosleependsub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
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:
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:
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:
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!):
Jeff - look at PICINFO in your installation. There is an icon to the application. PICInfo shows you the default values the compiler will use.
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
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