PIC32MZ standby / wakeup is ok, how change it to sleep?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Post Reply
stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times

PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by stefan.erni »

Hi Ben

To let the PIC sleep I config first an interrupt on portG, Pin G15 to wake him up if I press ON-Button on G15.

Then I let him sleep with this asm comand
asm volatile ( "wait" );
in a C code Icon.
sleep.PNG
(6.15 KiB) Downloaded 2188 times

It's working like this...
By the program start the PIC(board) needs 140mA
after the wait command the PIC(board) needs 90mA and some buttons has no fuction.
If I press the ON-Button it goes back to 140mA and the another buttons are working again.
90mA is still to much.....
In the manual I found this info
OSCCON:
bit 4 SLPEN: Sleep Mode Enable bit
1 = Device will enter Sleep mode when a WAIT instruction is executed
0 = Device will enter Idle mode when a WAIT instruction is executed
I read the value of the register and send it to the terminalprogram.

It give me this value
1 0001 0010 0010
Means bit 4 is 0 (idle mode)

In FC8, how can I set register OSSCON bit4 to 1 ?
sleep2.PNG
(7.39 KiB) Downloaded 2188 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by Benj »

Hello,

You can simply do this using a C code icon.

Code: Select all

OSCCONbits.SLPEN = 1;
Alternatively you can do it by ORing a mask.

Code: Select all

OSCCON |= 0x00000010;

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by stefan.erni »

Hi Ben

I tried both, but both does not change the OSCCON bit4 or the powerconsum.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by medelec35 »

For bit 4

Code: Select all

OSCCON |= 0x00010000;
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by Benj »

medelec35 wrote:For bit 4

Code: Select all

OSCCON |= 0x00010000;
That looks like bit 16 to me ;)

Maybe Martin meant this.

Code: Select all

OSCCON |= 0b00010000;
Not sure why the code isn't working for you. It certainly should be from the looks of it and the bit is read and write so you should be able to write to it and read it back.

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by stefan.erni »

Hi Martin


I think Ben is using hex and Martin bin..,,

I made some test with the watchdog divider
I send it to the terminalprogram and it shows me
2
then I change in the project option config
and my terminalprogram shows
10
then I use the c command
WDTCON |= 0x0000010;

and it shows still
10

FCV_REGISTER32=WDTCON;

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by Benj »

Hello,

Bits 0, 2, 3, 4, 5 and 6 of the WDTCON register are read only. Bit 1 is writeable and corresponds to the window setting.

set bit 1

WDTCONbits.WDTWINEN = 1;

or

WDTCON |= 0x02;


clear bit 1

WDTCONbits.WDTWINEN = 0;

or

WDTCON &= 0xFFFFFFFD;

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by medelec35 »

Woops :oops: , silly me not concentrating
I did mean

Code: Select all

OSCCON |= 0b00010000;
sorry.
Of course Ben was correct in the first place so my post should be ignored.
Martin

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by stefan.erni »

I tried both....I mean all the 4...
It shows always 10
sleep3.PNG
(23.69 KiB) Downloaded 2160 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by Benj »

Aha hang on a minute it's a PIC32 isn't it. These are different and you don't have direct write access to registers like on other devices....

Please try this instead.

Code: Select all

_SFR_BIT_SET(&OSCCON, 4);
Fingers crossed this will do it.

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by stefan.erni »

Hi Ben

We are on the right way, but not finished yet.

I read about the SET/CLR/INV and also about the virtual adress of register....

The command
_SFR_BIT_SET(&OSCCON, 4);
did not change the bit

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by stefan.erni »

Hi Ben
Some more we have to do....
A write to the OSCCON register requires an unlock sequence first. Refer to the device datasheet for more information on this unlock sequence. Here is an example:
http://microchipdeveloper.com/faq:2131

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by Benj »

Aha brilliant well found. Is that working better for you now then?

If not maybe try using the register write function.

Code: Select all

//Unlock Combination
_SFR_WRITE(&SYSKEY, 0xAA996655);
_SFR_WRITE(&SYSKEY, 0x556699AA);

// now change the OSCCON register
_SFR_BIT_SET(&OSCCON, 4);

// now lock again
_SFR_WRITE(&SYSKEY, 0x0);

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times

Re: PIC32MZ standby / wakeup is ok, how change it to sleep?

Post by stefan.erni »

Hi Ben

I just copy the code in FC8 and it's working. The powerconsum goes down to 50mA from the complet PIC Board (with a lot of hardware)
Some hardware I have to turn off....
And when I press the ON Button it's starting the board

the terminalprogram shows for the OSCCON register
1 0001 0011 0010 // with the code
1 0001 0010 0010 // without the code

The Code you give me is also working nice.
Thank you for the help.

/*
Enter C code below this comment
*/
SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA;
// now change the OSCCON register
OSCCONbits.SLPEN = 1;
// now lock again
SYSKEY = 0x0;
asm volatile ( "wait" );

Post Reply