Strange PWMOUT behaviour - can anyone explain it?

PhilHornby

Senior Member
I have an old project, that I have recently been doing some work on: it's a 14M2-based 'Battery Capacity' tester. Basically, it uses various PWM settings to drive a MOSFET (via a resistor and capacitor) to achieve controlled discharge currents, with the attached cell run-time measured and displayed on a 20x4 LCD.

The program uses two tasks on the 14M2, one does the voltage monitoring and controlling, the other updates the LCD and logs via HSerout (with a software interlock between the two). A while back, the program expanded into the 2nd 14M2 slot - and although the code that runs in Slot #0 doesn't actually need it, two tasks are employed in both slots (Slot #0's 2nd task just suspends itself when it runs).

When the battery measuring run completes, the PWM is switched off and current flow from the cell ceases. Pressing a button triggers a "RESET" and it's ready for the next test. The strange behaviour with PWMOUT, is noticeable because of my latest modification...

I have added a facility to abort the test early ... and trigger that 'RESET'. It took me slightly by surprise to find that this didn't turn off the PWM - but I know that not everything gets reset ... so not the end of the world...

To counter this, I added a PWMOUT PWMPin,Off statement as the first line of Slot #0, Task #0 and ... it had no effect at all :unsure:
(Issuing this statement before the RESET statement works, so I have a fix.)

If I allowed the restarted program to proceed, it was eventually getting back into line, of its own accord (it issues a PWMOUT 'on', a PWMDUTY and another PWMOUT 'OFF'), so ...

Is this a timing issue ... does the PWM circuitry not respond immediately after a RESET?. Or is not possible to turn it OFF, until it's specifically been turned ON?

(or is my project possessed by a winged daemon of the night ... which occasionally happens) :whistle:

If necessary, I can try and distill out the essence of the problem into a much simpler program - but I wondered if anyone can spot what's going on, before I do that.
 

Flenser

Senior Member
From the manual, the RESET command is "the software equivalent of pressing the external reset switch or removing/reconnecting power".

If, after you turn the power on, your program issues a particular PWMOUT command then after you issue the RESET command your program should start from the beginning, as if the power had just been turned on, and issue the same PWMOUT command.
 

hippy

Technical Support
Staff member
I don't have a 14M2 to hand but this demonstrates PWMOUT on an 18M2 is reset when a RESET command is executed, a connected LED is turned off when RESET happens -

Code:
#Picaxe 18M2

Start0:
  Do
    Pause 1000
  Loop

Start1:
  Pause 2000
  PwmOut B.3, 255, 511
  Pause 2000
  Reset
This also works the same when RESET is in the second slot. The LED is off for four seconds when it was previously off for just two -
Code:
#Picaxe 18M2
#Slot 0

Start0:
  Do
    Pause 1000
  Loop

Start1:
  Pause 2000
  Run 1
Code:
#Picaxe 18M2
#Slot 1

Start0:
  Do
    Pause 1000
  Loop

Start1:
  Pause 2000
  PwmOut B.3, 255, 511
  Pause 2000
  Reset
 

PhilHornby

Senior Member
From the manual, the RESET command is "the software equivalent of pressing the external reset switch or removing/reconnecting power".
It's been demonstrated before, that that's not quite the full story ...
See: this thread where @hippy makes use of the fact that RESET and Power cycle are not the same thing.

It's the not the fact that PWM is still running after RESET that I find odd - it's the fact that PWMOUT 'off' isn't enough to switch it off...
 

PhilHornby

Senior Member
I don't have a 14M2 to hand but this demonstrates PWMOUT on an 18M2 is reset when a RESET command is executed, a connected LED is turned off when RESET happens -
I would fully expect the LED to turn off - but did the PWM?

EDIT - I just re-read your post. I take it you were using a LED attached to B.3 to detect the presence of the PWM?
 
Last edited:

hippy

Technical Support
Staff member
I just re-read your post. I take it you were using a LED attached to B.3 to detect the presence of the PWM?
That's correct. I had an AXE091 to hand with an 18M2 fitted so just connected B.3 to the on-board LED+R anode, cathode to 0V.

It could be duty has dropped to 0%. I would have to test that. Using PWMDUTY would probably test that; it won't have any effect if PWM is not running, untested ...
Code:
Pause 2000
PwmDuty B.3, 511
Pause 2000
PwmOut B.3, 255, 511
Pause 2000
Reset
After going through the Reset once, LED off for 4 seconds and on for 2 suggests PWM is being reset. LED off for 2 on for 4 that it's not.

The more definitive test would be to PEEKSFR the related registers.
 

PhilHornby

Senior Member
The more definitive test would be to PEEKSFR the related registers.
I had a quick glance at the PIC16(L)F1825/9 Datasheet and decided this is not a simple topic, but I'll perserve...

I had a go at distilling the essence of my program out, to see if the effect is still there and it is. I can now say that Slots and Tasks don't seem to play any part in this effect.

Code:
#picaxe 14M2
#terminal 2400
#no_data

#include "Shared Data.basinc"                    ; Pin, variable and constant definitions

START0:

    pwmout PWMPin,off                        ;does NOTHING!

    pullup %0000100000000000                ;Pullup C.3 - has a switch to earth connected, so pull it high.

    hsersetup BAUD,%10                    ;inverted o/p
    hserout 0,(cr,lf,"START PROGRAM")
    
    adcconfig %11
    
    do while SwitchPinAlt = 0 : loop            ;Ensure switch released.
    do while SwitchPinAlt = 1 : loop            ;Wait for switch PRESS
    do while SwitchPinAlt = 0 : loop            ;Ensure switch released.

    hserout 0,(cr,lf,"Smoke on - GO!")
    
    PWMoutput = 556                        ;about right for 1A ... gets trimmed later

    pwmout PWMPin, 249, PWMoutput                ;Turn PWM output back on. (249 when two tasks, gives 16KHz)
    ;pwmout PWMPin, 62, PWMoutput                ;Turn PWM output back on. (62 when single task, gives 16KHz)
do
    fvrsetup FVR1024                               ;set Reference Voltage to 1.024V
    readadc10 ResistorSensor,SensorVoltage        ;Read voltage at sensing resistor

    if SensorVoltage  < 290 then                ;about 1A discharge
        PWMoutput = PWMoutput + 1 max 1000   
    elseif SensorVoltage > 290 then
        PWMoutput = PWMoutput - 1 min 1
    endif

    pwmduty PWMPin,PWMoutput           
    
    if SwitchPinAlt = 0 then
        hserout 0,(cr,lf,#PWMoutput," STOP ")
        pwmout PWMPin,off                    ;WORKS (with caveats) when issued here
        RESET
    endif
loop   
  
START1: suspend 1
    goto start1
The program starts, and wait for a switch press. The built-in ammeter in my project registers no current (so no pwm output to the Mosfet). Pressing the switch turns on the pwm, the current shoots up to 3A and the control loop rapidly brings it down to 1A or so.

Pressing the switch again, triggers a RESET and the current stays at 1A, even though the program has clearly restarted. Again, adding a 'pwmout off' to the beginning of the program does nothing - adding the pwmout off, before the RESET has a slightly unsual effect; reducing the current to about 0.6A, rather than 0 - so, different to the effect I've seen before.

Removing Task 1 (and adjusting variables) to suit reveals that a single Task (single Slot) program still exhibits the same characteristics. Interestingly,
re-programming the Picaxe doesn't switch off the PWM either!

I will dig out a breadboard, spare 14M2 and scope - and see what I can reduce this problem down to...
 

PhilHornby

Senior Member
I built @hippy 's 18M2 test circuit ... and it worked in exactly the same way on the 14M2 (confirmed by the 'scope) :confused:

Then it dawned on me, as to what is happening and it's specific to my circuit :oops:

This is the relevent section of my hardware :-

CircuitSnippet.jpg

Point A is connected to Pin B.2 of the 14M2.

When a PWMOUT , Off is executed, it does not return the Pin to its default state. So when executed as the first statement in a program, it does nothing - the pin remains as a (high impedance) input.

However, when issued within the running program - as well as stopping the PWM output - it discharges the 0.047µF capacitor (because it leaves the pin as an output (at 0V).

It was just residual charge, that made it appear that the PWM signal was still being output. This also explains why it continued, even when the Picaxe was being programmed.

I've now got Output PWMPin as the first statement in Slot #0 and that solves the problem.
 
Last edited:

The bear

Senior Member
@ PhilHornby,
Good that you have got to the bottom of it, the update is appreciated. It is too technical for me, but I do like a good ending.
bear..
 

hippy

Technical Support
Staff member
When a PWMOUT , Off is executed, it does not return the Pin to its default state. So when executed as the first statement in a program, it does nothing - the pin remains as a (high impedance) input.
That's probably right and as expected. I guess PWMOUT OFF sets the PWM to 0% but doesn't alter the pin state, which will be input after a Power-On or RESET, but will be an output once a PWMOUT has been executed.

PWMOUT B.2, OFF is the equivalent of PWMOUT B.2, 0, 0. It is the duty being 0 which turns off the PWM and allows the pin to be used for non-PWMOUT purposes.

You could probably achieve what you wanted with PWMOUT B.2, 249, 0 but you would still have a period of 'unknown state' between RESET and that being executed.

This is probably one of those cases where it's best not to use RESET and handle everything programmatically.
 

PhilHornby

Senior Member
I stole this circuit design in good faith - and now I find there are issues with it - I might sue! :)

Actually, this has got me thinking about this whole PWM/Feedback loop business (as in, do I actually need it)...

...given how long the charge sits on the capacitor ... can't I just get away with a pulseout b.2,xx followed input b.2 - maybe repeated 'once-in-a-while'.

Going away to experiment...
 
Top