PDA

View Full Version : Sleep & nap



keymuu
- 2nd December 2008, 01:39
Hi!

Using picbasicpro 2,50b and having
DEFINE WDT_OFF ; one of the first statements
and then later

Both SLEEP 1
and
NAP 1
hangs, get stucked, bog down :eek:

Is the define wrong somehow or what do I miss here?
Never used these commands before. Tried also
@ DEVICE pic18F4550 WDT_OFF
but that produced error 122.
Tried some other things also but DEFINE WDT_OFF was the only one (I found) that compiled without errors.

I'm missing something, but what?

Thank's for all hints:)

Jerson
- 2nd December 2008, 01:57
From the manual


The Watchdog Timer is used in conjunction with the SLEEP and NAP
instructions to wake the PICmicro MCU after a certain period of time.

You need to enable the WDT. How about trying this
@device 18F4550, WDT_ON

I do not know if this will work as I've not checked

mackrackit
- 2nd December 2008, 02:10
Also


@device 18F4550, WDT_ON

is PM syntax.

Must use MPASM for 18Fs


@ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H


A good late night read.
http://www.picbasic.co.uk/forum/showthread.php?t=543

keymuu
- 2nd December 2008, 08:38
Also


@device 18F4550, WDT_ON

is PM syntax.

Must use MPASM for 18Fs


@ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H


A good late night read.
http://www.picbasic.co.uk/forum/showthread.php?t=543
Thank you Dave and Jerson! :)

Of course, it seems to me that I can't read:mad:
That is also clearly written in the manual "...If the Watchdog Timer is not enabled, ther processor will sleep forever..., page 105", so I did not see the small word "not"... :o

However, changing that to WDT_ON does not produce the wanted result.
Added also WDTPS 128 but again without success.

Is there something else involved here?

That "543" is not solving this issue? It is showing that there can be many things that can go wrong... when you are not extremely familiar with this all...

mackrackit
- 2nd December 2008, 08:47
there can be many things that can go wrong
That is when the fun starts:D

Can you post your whole code?
The actual problem might be someplace else.

keymuu
- 2nd December 2008, 10:49
That is when the fun starts:D

Can you post your whole code?
The actual problem might be someplace else.

Here, the fun has alredy started :p


DEFINE LOADER_USED 1
DEFINE RESET_ORG 1000h ' For Microchip USB Bootloader
DEFINE INTERRUPT_ORG 1008h ' For Microchip USB Bootloader
define WDT_ON
define WDTPS 128

DEFINE LCD_DREG PORTD ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4 if 4-bit bus)
DEFINE LCD_RSREG PORTC ' Set LCD Register Select port
DEFINE LCD_RSBIT 2 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTC ' Set LCD Enable port
DEFINE LCD_EBIT 1 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 4 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us, 2000 us = 2ms
DEFINE LCD_DATAUS 50 ' Set data delay time in us, 50 us = 0,05ms

powerOut var PORTE.0
onOffPressed var PORTD.2
b var byte
clear

TRISE = %00001000
TRISC = %10110000
TRISD = %00001100
powerOut = 1

pause 1
lcdout $FE, 1
pause 1

pstart:
powerout = not onoffpressed
lcdout $FE,2, "b=", dec b
pause 100
b = b + 1
'nap 5
goto pstart

So, I can see b living with nap or sleep commented, but it hangs when not commented.:(

Acetronics2
- 2nd December 2008, 11:22
Hi,

From " The Manual" $ 5.46




The 16-bit
core devices, including the 17Cxxx and 18Xxxxx parts use a postscaler
set at programming time to configure the Watchdog timeout period.

The compiler will disregard the Period set in the NAP instruction for the 16-
bit core devices.



Alain

keymuu
- 2nd December 2008, 12:56
Hi,

From " The Manual" $ 5.46

Alain

TRUE!

But SLEEP should work, shouldn't it?
The problem is somewhere else, but where?

Acetronics2
- 2nd December 2008, 13:00
Hi,

You want a ~1 sec PAUSE ...

use PAUSE 1000 !!!

a PAUSE can be "interrupted" so, there are no problem to use it ...

Alain

Bruce
- 2nd December 2008, 15:43
If you're using the Microchip USB loader, it may have WDT disabled in config.

Try inserting WDTCON.0=1 before any NAP or SLEEP commands to enable WDT.

keymuu
- 2nd December 2008, 17:26
If you're using the Microchip USB loader, it may have WDT disabled in config.

Try inserting WDTCON.0=1 before any NAP or SLEEP commands to enable WDT.

One would have think that would work, but it did not :mad:
What might be wrong?

WDTCON.0=1
sleep 1
lcdout $FE, $C0, "ok"

Can't still se the "ok" :confused:

Acetronics2
- 2nd December 2008, 17:51
Can't still see the "ok"



Could the problem come from the LCD setup ???



pause 1
lcdout $FE, 1
pause 1


Generally, we allow 500 ms min for the LCD Power on ...

Alain

Bruce
- 2nd December 2008, 19:14
How long are you waiting? If the WDT poscaler in config is set to 32,768 it's going to take around 131 seconds before it wakes-up.

keymuu
- 2nd December 2008, 20:11
How long are you waiting? If the WDT poscaler in config is set to 32,768 it's going to take around 131 seconds before it wakes-up.

the WDTPS is 128 as in #9, and actually it takes several seconds before that sleep instrucion is reached in the demo code (#9) as well as in the "big" code where I spotted this phenomena/error/failure at first.

I have never seen sleep working, yet...:rolleyes:
but I would really like to see it :)

Bruce
- 2nd December 2008, 20:33
How are you setting the WDT postscaler to 128?

keymuu
- 2nd December 2008, 20:49
How are you setting the WDT postscaler to 128?

Like this:
define WDT_ON
define WDTPS 128

Is that ok?

Bruce
- 2nd December 2008, 21:17
The watchdog timer postscaler & on/off options are configuration options. These are set at
program time.

PBP allows you to DEFINE pretty much anything without throwing an error, but that doesn't
mean your defines are doing anything.

If you're using the older version Microchip USB boot-loader firmware, these are set in the C
code.



#pragma config WDT = OFF
#pragma config WDTPS = 32768

You can open the loader .HEX file with your programmer, change WDTPS from 32768 to 128
then save the .HEX file & re-program your 4550 with the modified loader firmware. Then you
will have a postscaler of 128. I would leave WDT=OFF, and use the software enable option
WDTCON.0=1.

You'll also want to use DEFINE OSC 48 so PBP will use the correct timing. Without defining
the osc speed PBP defaults to 4MHz.

keymuu
- 3rd December 2008, 19:58
Thank you Bruce!


The watchdog timer postscaler & on/off options are configuration options. These are set at program time.

PBP allows you to DEFINE pretty much anything without throwing an error, but that doesn't mean your defines are doing anything.
So, this means that you can not override them later with your running program?

If you're using the older version Microchip USB boot-loader firmware, these are set in the C code.
The date for the "USB Device - HID - HID Bootloader - C18 - PIC18F4450.hex" is 19.6.2008. I opened the zip-file maybe 1,5-2 month ago.




#pragma config WDT = OFF
#pragma config WDTPS = 32768

You can open the loader .HEX file with your programmer, change WDTPS from 32768 to 128 then save the .HEX file & re-program your 4550 with the modified loader firmware. Then you will have a postscaler of 128. I would leave WDT=OFF, and use the software enable option WDTCON.0=1.

I don't grasp this. How can I see or found WDTPS in a .HEX file?


You'll also want to use DEFINE OSC 48 so PBP will use the correct timing. Without defining the osc speed PBP defaults to 4MHz.
My program has DEFINE OSC 48, so that can not be the reason for this sleep-error.

Bruce
- 3rd December 2008, 20:11
So, this means that you can not override them later with your running program?

Some 18F parts allow you to change config settings at run-time, but I wouldn't advise trying this until you're a bit more familiar with how to set them & what they all do. If you're using a boot-loader, you'll need to know every setting the loader expects, or you may end up doing a lot of loader firmware re-programming. If you search here for modifying config settings on-the-fly you'll find a couple routines showing you how.


[The date for the "USB Device - HID - HID Bootloader - C18 - PIC18F4450.hex" is 19.6.2008. I opened the zip-file maybe 1,5-2 month ago.
You probably have the latest version then.


I don't grasp this. How can I see or found WDTPS in a .HEX file?
Easy. Open the loader .HEX file with your programmers software. Change it, save the .HEX file, and re-program the loader into your PIC.


My program has DEFINE OSC 48, so that can not be the reason for this sleep-error.
OK. It wasn't shown in the example you posted. Note this won't have anything to do with the WDT or sleep error. Only timing for PBP commands.

If your programmers software doesn't allow you to read a .HEX file and make changes to config settings, then you can re-compile the loader firmware with the free version of C18.

Or I can send you a modified version .HEX file if you need it.

What USB board are you using?

keymuu
- 3rd December 2008, 21:17
Some 18F parts allow you to change config settings at run-time, but I wouldn't advise trying this until you're a bit more familiar with how to set them & what they all do. If you're using a boot-loader, you'll need to know every setting the loader expects, or you may end up doing a lot of loader firmware re-programming. If you search here for modifying config settings on-the-fly you'll find a couple routines showing you how.

You probably have the latest version then.

Easy. Open the loader .HEX file with your programmers software. Change it, save the .HEX file, and re-program the loader into your PIC.

OK. It wasn't shown in the example you posted. Note this won't have anything to do with the WDT or sleep error. Only timing for PBP commands.

If your programmers software doesn't allow you to read a .HEX file and make changes to config settings, then you can re-compile the loader firmware with the free version of C18.

Or I can send you a modified version .HEX file if you need it.


YESSSSS! :D

With meProg of course you can change such things, of course. I have just used for "so long time" now only the bootloader so I forgot the meProg. ;)

Sleep works and NAP works also even that the manual does not confirm that.


What USB board are you using?
What is USB board (melabs U2?) or what ? or 4550? or... :confused:

Bruce
- 3rd December 2008, 21:21
What is USB board (melabs U2?) or what ? or 4550? or...
Just curious if you were using a USB development board.

Did you get it working?

keymuu
- 3rd December 2008, 22:19
Just curious if you were using a USB development board.

Did you get it working?

Yep ... it works THANK YOU Bruce :)

Both SLEEP and NAP work on 18F4550 !!!! at least on my PIC18F4550 !

The fault was in the bootloader: WDTPS was too big, 128 is ok

Thank you all for your help !

Would a USB development board be good for... what? :confused:

Bruce
- 3rd December 2008, 23:50
Glad you got it working. It probably was before, but you had to wait a heck of a long time before it would wake up..;o}


Would a USB development board be good for... what?
Development boards, like say the Microchip USB dev board & meLabs USB dev boards are handy for testing things quickly without having to build your own boards or hand-wire stuff.

Definitely not necessary, but handy if you're lazy like me..;o}

Now that you have the sleep/nap/WDT stuff down pat, here's a fix for your interrupts.

This isn't what you want;
DEFINE INTERRUPT_ORG 1008h ' For Microchip USB Bootloader

It looks like you're using the newer version USB loader, so your interrupts are re-mapped to locations 0x1008 for high priority, and 0x1018 for low priority.

What you need to do with PBP to make sure it points to these re-mapped vectors goes like this;

DEFINE RESET_ORG 1000h ' For Microchip USB Bootloader
DEFINE INTHAND high_isr ' high-pri int vector
DEFINE INTLHAND low_isr ' low-pri int vector

Note that you don't need the DEFINE LOADER_USED 1.

DEFINE RESET_ORG 1000h tells PBP to start compiling all code at 1000h.

DEFINE INTHAND high_isr. When PBP see this it inserts a ORG RESET_ORG + 8 followed by a goto INTHAND, which is your high_isr routine.

DEFINE INTLHAND low_isr. Now PBP inserts a ORG RESET_ORG + 18h followed by a goto INTLHAND, which is your low_isr routine.

So you don't need to specifiy the address.

Note that your interrupt routines will need to be assembler. Something like this;


asm
high_isr
; do stuff here
RETFIE FAST

low_isr
; do stuff here
RETFIE
endasm
Now your PBP .asm interrupts can work directly with the Microchip USB loader re-mapped int vectors.

keymuu
- 4th December 2008, 18:30
Glad you got it working. It probably was before, but you had to wait a heck of a long time before it would wake up..;o}


Development boards, like say the Microchip USB dev board & meLabs USB dev boards are handy for testing things quickly without having to build your own boards or hand-wire stuff.

Definitely not necessary, but handy if you're lazy like me..;o}

Now that you have the sleep/nap/WDT stuff down pat, here's a fix for your interrupts.

This isn't what you want;
DEFINE INTERRUPT_ORG 1008h ' For Microchip USB Bootloader

It looks like you're using the newer version USB loader, so your interrupts are re-mapped to locations 0x1008 for high priority, and 0x1018 for low priority.

What you need to do with PBP to make sure it points to these re-mapped vectors goes like this;

DEFINE RESET_ORG 1000h ' For Microchip USB Bootloader
DEFINE INTHAND high_isr ' high-pri int vector
DEFINE INTLHAND low_isr ' low-pri int vector

Note that you don't need the DEFINE LOADER_USED 1.

DEFINE RESET_ORG 1000h tells PBP to start compiling all code at 1000h.

DEFINE INTHAND high_isr. When PBP see this it inserts a ORG RESET_ORG + 8 followed by a goto INTHAND, which is your high_isr routine.

DEFINE INTLHAND low_isr. Now PBP inserts a ORG RESET_ORG + 18h followed by a goto INTLHAND, which is your low_isr routine.

So you don't need to specifiy the address.

Note that your interrupt routines will need to be assembler. Something like this;


asm
high_isr
; do stuff here
RETFIE FAST

low_isr
; do stuff here
RETFIE
endasm
Now your PBP .asm interrupts can work directly with the Microchip USB loader re-mapped int vectors.

Thank you Bruce, thank you indeed :) ... you are really a kind soul !

By the way,
can one find somewhere a list of DEFINEs that can be overwritten (changed afterwards whenever you want, when your program runs) and/or those that can not be changed afterwards, like the WTDPS and ... ?

You "mail" was really educational and clear ... THANK YOU :p

Bruce
- 4th December 2008, 18:45
DEFINEs cannot be changed at run-time. These are used by the compiler at compile time
only.

WDTPS options are set in the PIC configuration register. These you can change at run-time
on some 18F parts.

This thread shows two examples;
http://www.picbasic.co.uk/forum/showthread.php?t=4363&highlight=config

Just bear in mind that changing some configuration settings at run-time may result in major
problems - so be careful.