Sleeping after PWM?

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
I have a Pic18f27J53 were I am using PWM to drive some analog meters.

The project is normally powered by a wallwart. I am detecting a power failure. When I detect a power failure I set the duty cycle of the PWM to zero and put the pic to sleep to wait for when normal power returns.

All works fine except that sometimes the output of the PWM pin remains high. No PWM out just at a high level.

Besides setting duty cycle to zero, what else should I be doing so the battery is not powering the meter while the pic is sleeping? Should I be setting the PWM outputs as inputs? What is the best way to shutdown output pins before sleep?
 

wayneh

Joined Sep 9, 2010
18,088
Well something is clearly wrong if a zero duty cycle becomes a 100% duty cycle. I wonder if there’s a glitch that somehow stops the PWM altogether, in whatever state it’s in at that instant, instead of actually taking the duty to zero. Could it be as simple as setting the duty to 0.001%? Wild ass guessing on my part.
 

KeithWalker

Joined Jul 10, 2017
3,603
I have a Pic18f27J53 were I am using PWM to drive some analog meters.

The project is normally powered by a wallwart. I am detecting a power failure. When I detect a power failure I set the duty cycle of the PWM to zero and put the pic to sleep to wait for when normal power returns.

All works fine except that sometimes the output of the PWM pin remains high. No PWM out just at a high level.

Besides setting duty cycle to zero, what else should I be doing so the battery is not powering the meter while the pic is sleeping? Should I be setting the PWM outputs as inputs? What is the best way to shutdown output pins before sleep?
After you set the duty cycle to zero%, could you not set the voltage on the output pins to low just to be sure, before the pic goes to sleep?
 

Kjeldgaard

Joined Apr 7, 2016
476
I have no experience with the various problems that can occur with sleep mode.

But I can see a possible problem if the PWM output is High, the Duty Cycle is set to zero and Sleep Mode is activated in the next instruction without the PWM block setting the output low.

If that is the problem, then it is a question whether it is just a few NOP instructions or a Delay corresponding to the PWM period time.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
I have no experience with the various problems that can occur with sleep mode.

But I can see a possible problem if the PWM output is High, the Duty Cycle is set to zero and Sleep Mode is activated in the next instruction without the PWM block setting the output low.

If that is the problem, then it is a question whether it is just a few NOP instructions or a Delay corresponding to the PWM period time.

That is what I was thinking. I wonder if a delay would work before I sleep. I will try it. Or I will try setting to inputs.
 

pmd34

Joined Feb 22, 2014
529
Hi Spinnaker, when does the PWM update to the newest duty cycle & set its outputs? I guess if your pulse is long then you could have sent the sleep command before the timer has reset? I'm not familiar with PICs but for the Atmel controllers I would also reset the count to zero, or to the point where it will immediately update its registers.
 
Besides setting duty cycle to zero, what else should I be doing so the battery is not powering the meter while the pic is sleeping? Should I be setting the PWM outputs as inputs? What is the best way to shutdown output pins before sleep?
Good question, I would like to know how it turns out for you in this case. Just thinking out loud...

At first I thought that the best solution is revert the pin back to GPIO and set it to an output and low. Alternatively, get it into high impedance - note that the pull down resistor issue has already been noted and may be part of the solution.

A couple of points that I read just now in the data sheet might be relevant.

4.8 Peripheral Module Disable
19.4.3 START-UP CONSIDERATIONS (especially the note box).
19.4.4 ENHANCED PWM AUTO-SHUTDOWN MODE (i don't think you are using enhanced mode, but maybe you could)
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Hi Spinnaker, when does the PWM update to the newest duty cycle & set its outputs? I guess if your pulse is long then you could have sent the sleep command before the timer has reset? I'm not familiar with PICs but for the Atmel controllers I would also reset the count to zero, or to the point where it will immediately update its registers.

It basically just updates continually . That is except when the RTC is busy.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
P.S. The only thing powering the meters is the PIC. So if I set the pin as an input (high impedance) wouldn't this be sufficient to keep the meter from draining the battery?


What I was hoping for here, in general terms, the best practices in dealing with outputs before you put a mcu to sleep.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Well something is clearly wrong if a zero duty cycle becomes a 100% duty cycle. I wonder if there’s a glitch that somehow stops the PWM altogether, in whatever state it’s in at that instant, instead of actually taking the duty to zero. Could it be as simple as setting the duty to 0.001%? Wild ass guessing on my part.

I think I am sleeping before the transition has a chance to go to zero.
 
I think I am sleeping before the transition has a chance to go to zero.
Maybe so as the duty cycle register is double buffered.

PWM output supports up to 10 bit resolution, cascading timer twowith a two bit prescalar. CCPs PWM implements double buffered input for all 10 bits to allow the selection of a new duty cycle value without interrupting an uncompleted PWM cycle period
 

crutschow

Joined Mar 14, 2008
38,322
I don't have one. What would cause the input pin to power the meter with no pull down?
If you have no pull-down, than can you just set the output to zero before going to sleep?
Or are the outputs not powered when in the sleep mode?
 
Last edited:

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
If you have no pull-down, than can you just set the output to zero before going to sleep?
Or are the outputs not powered when in the sleep mode?

Well I set the duty cycle to zero but maybe I need to take the extra step of setting it to zero.

But I think I will need to set a pin as an input anyway since I have a LED,, one for AM another for PM that both operate off of the same PIC pin. Sort of like a toggle. So one is always on. I might need to set it as an input to get them both off.
 

crutschow

Joined Mar 14, 2008
38,322
But I think I will need to set a pin as an input anyway since I have a LED,, one for AM another for PM that both operate off of the same PIC pin.
But isn't that a different pin from the PWM output?
So you could set the PWM output low, and the LED pin as an input (open).

But how will that LED pin being open shut off both LEDs?
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
But isn't that a different pin from the PWM output?
So you could set the PWM output low, and the LED pin as an input (open).

But how will that LED pin being open shut off both LEDs?

Yeah different pins different for PWM and the LED. I guess I should test it but why wouldn't making PIN an input turn off both LEDs. It is a high impedance pin at that point, I thought thought that was the whole idea behind Charlieplexing?

I am really worried about nothing because the project won't be without power for any realy lenght of time. Just want to make the project compelte I guess.

upload_2019-3-26_20-22-53.png
 

BobaMosfet

Joined Jul 1, 2009
2,211
I have a Pic18f27J53 were I am using PWM to drive some analog meters.

The project is normally powered by a wallwart. I am detecting a power failure. When I detect a power failure I set the duty cycle of the PWM to zero and put the pic to sleep to wait for when normal power returns.

All works fine except that sometimes the output of the PWM pin remains high. No PWM out just at a high level.

Besides setting duty cycle to zero, what else should I be doing so the battery is not powering the meter while the pic is sleeping? Should I be setting the PWM outputs as inputs? What is the best way to shutdown output pins before sleep?
I'd tri-state the output, so it wouldn't be in or out, or susceptible to float.
 
Top