Sleep operation

13 views
Skip to first unread message

Multitrain

unread,
May 10, 2023, 5:13:10 PM5/10/23
to jallist
Hello,

To make my design easier (I have an other solution with 2 external transistors) I use the ASM Sleep instruction. it works but the current cosumption is about 0.75 mA.  I don't know what more and how to disable it to reach the 0.35µA of FIGURE 16-13: TYPICAL IPD vs. VDD (SLEEP MODE, ALL PERIPHERALS DISABLED of the data sheet for the 12F683. Pin_A3 is tied via a resistor to GND

Here my code:

; Push button perceuse  version avec sleep
; Freq PWM 4 kHz
; rapport cyclique 50%, 75% et 100%
; PIN_A2 out  PWM
; Utilisation de IOC pour sortir du mode sleep
 ; sleeep: le wake-up avec IOC se fait avec un "1",

include 12f683                    -- target PICmicro
pragma target OSC INTOSC_NOCLKOUT
-- This program assumes the internal oscillator
-- is running at a frequency of 4 MHz.
pragma target clock 4_000_000      -- oscillator frequency
-- configuration memory settings (fuses)
pragma target WDT disabled   --  watchdog
pragma target MCLR internal  -- internal
pragma target CP ENABLED
pragma target brownout ENABLED

 pin_A2_direction = output
 pin_A5_direction = input
 pin_A0_direction = output
 pin_A1_direction = output
 pin_A4_direction = output
 
 OPTION_REG = 0b_1111_1111    -- ~GPPU disabled
 WPU = 0b_0001_1111          -- pull up sur ttes les pin sauf la 5

 enable_digital_io()         -- disable analog I/O (even if none on the chip)
 include delay_pat
 include pwm_hardware

   var bit push_B
   var byte count_in = 0
   pwm_set_frequency (4000)
   var byte ratio
   var bit Bouton is pin_a5
   var bit led1 is pin_a0
   var bit led2 is pin_a1
   var bit led3 is pin_a4
   var bit mem_on = 1
   var bit mem_off = 1

    INTCON_GPIE = 1 ; enables IOC
      INTCON_GIE  = false;-- Enables all unmasked interrupts   false;: no interrupt
      INTCON_PEIE = false        -- Enables all unmasked peripheral interrupts
      IOC_IOC5 = on
   porta = 0 ; all out off
   
forever loop

; when pressing the button, I put pin_A5 high then I disable the input with mem_on = 0
; after the "debounce time of 50 ms, it set Push_B to 1

if Bouton & mem_on then mem_on = 0 mem_off = 1 delay_1ms(050) Push_B = 1 end if

; after rising edge push_B changes to 1, the counter "count_in" is incremented

if push_B Then count_in = count_in + 1 push_B = 0 end if

; various duty cycles an led are set

if count_in == 0 then pwm1_off() pin_a2 = off  end if
if count_in == 1 then pwm1_set_dutycycle_percent(050) pwm1_on() led1 = 1 end if
if count_in == 2 then pwm1_set_dutycycle_percent(075) led2 = 1 led1 = 0 end if
if count_in == 3 then pwm1_set_dutycycle_percent(100) led3 = 1 led2 = 0 end if

; At the fourth push, all is stopped and the PIC goes in sleep mode and "count_in" set to 0

if count_in == 4 then pwm1_off() led1 = 0 led2 = 0 led3 = 0 count_in = 0 pin_a2 = 0
                 asm sleep
                      end if
                     
;When the button is realised the input is again disabled for 200 ms. Push_B
; is set to 0 and mem_on is on to validate the next push.

if !Bouton & mem_off then mem_off = 0 mem_on = 1 delay_1ms(200) Push_B = 0 end if

end loop





Thanks
Patrick

Rob CJ

unread,
May 11, 2023, 2:06:10 AM5/11/23
to jal...@googlegroups.com
Hi Patrick,
It can depend on your hardware design. If you enable the weak pull-up resistors you must be sure not to pull- down that pin externally since that will introduce extra current for your circuit.

Met vriendelijke groet,
Rob Jansen

From: jal...@googlegroups.com <jal...@googlegroups.com> on behalf of Multitrain <patfr...@gmail.com>
Sent: Wednesday, May 10, 2023 11:13:10 PM
To: jallist <jal...@googlegroups.com>
Subject: [jallist] Sleep operation
 
--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallist/04313535-0ae0-43be-ba8a-37f80247ad0en%40googlegroups.com.

Patrick FROUCHT

unread,
May 11, 2023, 3:31:57 AM5/11/23
to jal...@googlegroups.com
Hi Rob

Even if the wpu register has the pin_A5 with a pull-up, the   OPTION_REG = 0b_1111_1111    disables any pull-up resistor ,

Patrick FROUCHT

unread,
May 11, 2023, 3:36:27 AM5/11/23
to jal...@googlegroups.com
I even reùmoved the two lines
; OPTION_REG = 0b_1111_1111    -- ~GPPU disabled

; WPU = 0b_0001_1111          -- pull up sur ttes les pin sauf la 5

I still get  a current of 0.75mA

Patrick

Rob CJ

unread,
May 11, 2023, 3:41:54 AM5/11/23
to jal...@googlegroups.com
Hi Patrick,
Can you send the schematic diagram? I have used sleep in the past on another pic and in sleep the Pic consumed uA.
Thanks 

Met vriendelijke groet,
Rob Jansen

From: jal...@googlegroups.com <jal...@googlegroups.com> on behalf of Patrick FROUCHT <patfr...@gmail.com>
Sent: Thursday, May 11, 2023 9:36:14 AM
To: jal...@googlegroups.com <jal...@googlegroups.com>
Subject: Re: [jallist] Sleep operation
 

Patrick FROUCHT

unread,
May 11, 2023, 4:20:16 AM5/11/23
to jal...@googlegroups.com
I'am not at home, I'll send it this afternoon.
Thx
Patrick
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallist/GVXP195MB1637C6033A2B1A53E25AF3D1E6749%40GVXP195MB1637.EURP195.PROD.OUTLOOK.COM.

Patrick FROUCHT

unread,
May 11, 2023, 9:21:42 AM5/11/23
to jal...@googlegroups.com
Rob,
find the ultra simple schematic as attached document.

Patrick

To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.
push_perceue.jpg

Rob CJ

unread,
May 11, 2023, 12:56:53 PM5/11/23
to jal...@googlegroups.com
Hi Patrick,

In general I would recommend to pull-up pins, e.g. the GP3 and for GP5 I would also attach the switch to ground and use a pull-up resistor instead of a pull-down resistor. So the switch then becomes active low.

You do not use internal pull-ups so that could not be the issue for GP5 but I am not sure about GP3/MCLR (which is active low so it would be more logical to pull it up even if you do not use it).

The data sheet says this about GP3/MCLR.

"GP3 pull-up is enabled when MCLRE is ‘1’ in the Configuration Word register." 

But you are not using it as MCLR so could not be it (but you never know).

Still I would try using pull-ups instead of pull down just to be sure or you can try disconnecting the resistors R15 and R10 and see what happens.

Since you are using a debounce in software you could remove C1, it will have a small leaking current (but not 0.75 mA unless it is a bad capacitor).

Kind regards,

Rob




Van: jal...@googlegroups.com <jal...@googlegroups.com> namens Patrick FROUCHT <patfr...@gmail.com>
Verzonden: donderdag 11 mei 2023 15:21
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: Re: [jallist] Sleep operation
 

Patrick FROUCHT

unread,
May 12, 2023, 2:40:24 AM5/12/23
to jal...@googlegroups.com
Hi Rob,
Sorry for the late answer

If I want to use an input to wake up the PIC. The only solution is to put a high level on one pin, in my case pin_A5.  If you look at the diagram FIGURE 12-7: INTERRUPT LOGIC, the only way to wake up after a sleep is to put  a high level on one pin.
.
You can see that to wake up from sleep, you need a positive level on the inputs so the switch from pin_A5 to +5V and the resistor to the ground.

Pin_A3 is used as an input pin, pulling it via a resistor to Vdd or Vss doesn't change anything.

I also changed in my program  all outputs to inputs with a resistor to Vss before the sleep instruction and again as output after the sleep instruction, no change for the current consumption in sleep, always 750 µA !

In Data sheet §15.3 DC Characteristics: PIC12F683-I (Industrial) test D020 Power-down Base Current(IPD)(2)
with note 2 :
The power-down current in Sleep mode does not depend on the oscillator type. Power-down current is
measured with the part in Sleep mode, For lowest current consumption in this mode, all I/O pins
should be either at VDD or VSS, with no external circuitry drawing current from the I/O pin.
I also added:
  T1CON_T1OSCEN = 0 to be sure that T1OSC is: disabled
  pragma target brownout RUNONLY to disable BOR during sleep

I even took another PIC and.... always 750 µA !

Surely, I am somewhere wrong but ....

Patrick

Rob CJ

unread,
May 12, 2023, 3:32:17 AM5/12/23
to jal...@googlegroups.com
Hi Patrick,
I can do a test using your hardware and software but I do not have this Pic. I can use another one for comparison.

Met vriendelijke groet,
Rob Jansen
Sent: Friday, May 12, 2023 8:40:11 AM

Patrick FROUCHT

unread,
May 12, 2023, 4:34:54 AM5/12/23
to jal...@googlegroups.com
No problem to try it on another PIC.

Here my last code
include 12f683                    -- target PICmicro
pragma target OSC INTOSC_NOCLKOUT
-- This program assumes the internal oscillator
-- is running at a frequency of 4 MHz.
pragma target clock 4_000_000      -- oscillator frequency
-- configuration memory settings (fuses)
pragma target WDT disabled   --  watchdog
pragma target MCLR internal  -- internal
pragma target CP ENABLED
pragma target brownout RUNONLY


 pin_A2_direction = output
 pin_A5_direction = input
 pin_A0_direction = output
 pin_A1_direction = output
 pin_A4_direction = output
 
; OPTION_REG = 0b_1111_1111    -- ~GPPU disabled
; WPU = 0b_0001_1111          -- pull up sur ttes les pin sauf la 5

 enable_digital_io()         -- disable analog I/O (even if none on the chip)
 include delay_pat
 include pwm_hardware

   var bit push_B
   var byte count_in = 0
   pwm_set_frequency (4000)
   var byte ratio
   var bit Bouton is pin_a5
   var bit led1 is pin_a0
   var bit led2 is pin_a1
   var bit led3 is pin_a4
   var bit mem_on = 1
   var bit mem_off = 1

    INTCON_GPIE = 1 ; enables IOC
      INTCON_GIE  = false;-- Enables all unmasked interrupts   false;: no interrupt
      INTCON_PEIE = false        -- Enables all unmasked peripheral interrupts
      IOC_IOC5 = on
      T1CON_T1OSCEN = 0

   porta = 0 ; all out off
   
forever loop

; when pressing the button, I put pin_A5 high then I disable the input with mem_on = 0
; after the "debounce time of 50 ms, it set Push_B to 1

if Bouton & mem_on then mem_on = 0 mem_off = 1 delay_1ms(050) Push_B = 1 end if

; after rising edge push_B changes to 1, the counter "count_in" is increased


if push_B Then count_in = count_in + 1 push_B = 0 end if

; various duty cycles an led are set

if count_in == 0 then pwm1_off() pin_a2 = off  end if
if count_in == 1 then pwm1_set_dutycycle_percent(050) pwm1_on() led1 = 1 end if
if count_in == 2 then pwm1_set_dutycycle_percent(075) led2 = 1 led1 = 0 end if
if count_in == 3 then pwm1_set_dutycycle_percent(100) led3 = 1 led2 = 0 end if

; At the fourth push, all is stopped and the PIC goes in sleep mode and "count_in" set to 0

if count_in == 4 then pwm1_off() led1 = 0 led2 = 0 led3 = 0 count_in = 0 pin_a2 = 0
pin_A0_direction = input
pin_A1_direction = input
pin_A2_direction = input
pin_A4_direction = input

                 asm sleep

                 
pin_A0_direction = output
pin_A1_direction = output
pin_A2_direction = output
pin_A4_direction = output

                      end if
                     
;When the button is realised the input is again disabled for 200 ms. Push_B
; is set to 0 and mem_on ison to validate the next push.

Patrick FROUCHT

unread,
May 12, 2023, 4:36:08 AM5/12/23
to jal...@googlegroups.com
I forget ..very important:

THX

Patrix

Rob CJ

unread,
May 12, 2023, 8:29:38 AM5/12/23
to jal...@googlegroups.com
Hi Patrick, 

You're welcome. I will test it with a 12f615 since that version compiles your program without problems.

Although I had to to change delay_pat to delay 🙂.

I will let you know what comes out of the test.

Kind regards,

Rob


Verzonden: vrijdag 12 mei 2023 10:35

Patrick FROUCHT

unread,
May 12, 2023, 10:08:28 AM5/12/23
to jal...@googlegroups.com
Hi Rob,


Delay_pat is the delay librairie with some additional n* 20, 50, 100µs and n*10, 20 50ms. I made them because when I upgraded my programs from JAL V1 to V2, the new delay librairie missed them.

THX

Patrick

Rob CJ

unread,
May 12, 2023, 2:33:41 PM5/12/23
to jal...@googlegroups.com
Hi Patrick,

I did a test using a PIC12F617. It compiles your code without any problems.

I connected the resistors to GP5 and GP3 to ground and the LEDs to the other pins as in your schematic diagram.

I noticed that your program is not completely correct. It never gets to the point where the sleep instruction is. So the PIC keeps on running when all LEDs are out. In that case the PIC draws around 1 mA in my case.

I did some modifications to your program:
  • Since you want to wakeup from sleep you must have GIE enabled and you need an interrupt routine to handle the IOC interrupt when it occurs. I added that.
  • As I said it never gets to the point where count == 4 so it will never go to sleep. As a workaround I added the sleep at the line where count == 0. In that case the PIC will go to sleep.
When the PIC is in sleep my uA meter show 0.0 uA which is correct since it should be about 50 nA but I cannot measure that.

I attached the updated program. Keep in mind to change it to your PIC, the code is the same but I am using a 12F617,

Kind regards,

Rob




Verzonden: vrijdag 12 mei 2023 16:08
12f617_sleep_current.jal

Patrick FROUCHT

unread,
May 12, 2023, 3:10:53 PM5/12/23
to jal...@googlegroups.com
Rob,

Thank you very much, I'll check this tomorrow.

When looking at the diagram FIGURE 12-7: INTERRUPT LOGIC, i did not think an interrupt was needed because (I don't think the diagram is wrong) the wake up from sleep goes out before the interrupt  last gate. So for me GIE doesn't need to be set! That was my understanding of the diagram.
I used the leds to know in which state the PIC was and where it was in my program.
If all leds are off, this means count_in = 4

You are  right but I don't understand what happened.

THX again

Patrick


Rob CJ

unread,
May 13, 2023, 3:43:57 AM5/13/23
to jal...@googlegroups.com
Hi Patrick,

The only way to get out of sleep is by an interrupt (or a reset), in your case an interrupt on change interrupt otherwise the PIC will not wakeup anymore.

In figure 12-7 the GIE is at the end (and it is an AND gate) which means that if GIE is FALSE, all interrupts are disabled and so your PIC will sleep forever.

Next to that you must handle the IOC interrupt because if it is triggered and not reset by the interrupt routine it will not occur again. That's why I added the interrupt routine to handle the IOC interrupt.

Good luck testing and finding out why count == 4 is never reached.

Kind regards,

Rob


Verzonden: vrijdag 12 mei 2023 21:10

Patrick FROUCHT

unread,
May 13, 2023, 5:23:27 AM5/13/23
to jal...@googlegroups.com
Hi Rob,

"finding out why count == 4 is never reached  I think it reached this program line but skipped the SLEEP instruction because there was no interrupt.in my program.
I admit that with interrupt in mind, I had to just simply apply what is written in the § 4.2.3.

The program works OK

Thanks again

Patrick



Reply all
Reply to author
Forward
0 new messages