Stopping PWM output, 08M picaxe

lauriet

Member
Stepping through the Simulator in the Programming Editor 5.4.3 while testing code to stop a motor by setting the pwm output low, I have observed the following:
This code sample performs as expected:

'08M chip
'setfreq m4
symbol k = b0
one:
k=1 'no change
low 2 'no change
high 2 'pin 2 set high
low 2 'pin 2 set low
k=2 'no change
low 2 'no change
goto one

but this code sets the pwm output active when a seemingly unrelated process occurs:

two:
k=1 'pin 2 set high except the first time
low 2 'pin 2 set low
pwmout 2,249,500 'pin 2 set high
low 2 'pin 2 set low
k=2 'pin 2 set high
low 2 'pin 2 set low
goto two

As the actual code I am writing has a residual pwm value I want to return to without complexity, so don't want to use 'pwmout 2,0,0' can youse guyz advise if
a) this is an 'undocumented feature' of the simulator alone,
b) this behaviour will also occur live,
c) I am a dunderhead and need a gentle talking to.

Additionally, is there any way of seeing the actual current PWM values in the simulator, not just the current variable values?

Many thanks,
Laurie.
 

BeanieBots

Moderator
It's difficult to work out what you are trying to do because your comments and your code do not match.

two:
k=1 'pin 2 set high except the first time [sets b0 to the value 1, has no effect on any output pins]
low 2 'pin 2 set low [correct]
pwmout 2,249,500 'pin 2 set high [starts 50% duty PWM on 2]
low 2 'pin 2 set low [should switch off PWM]
k=2 'pin 2 set high [sets b0 to value 2]
low 2 'pin 2 set low [should switch off PWM (again)]
goto two

Please explain in more detail what you actually want to happen.
 

inglewoodpete

Senior Member
I'm not sure you understand what your program is doing. Immediately after starting the PWM, you disable it. In a real 08M, that would happen about 250uS after you start the PWM. Remember, each instruction take roughly 250uS to execute, unlike the simulator.

Also, you can't just restore the PWM to some previously configured rate unless you reconfigure it.

Try the following:
Code:
#PICAXE 08M
SetFreq m4
Symbol k = b0
Symbol Speed = w3
'
Two:k=1                'pin 2 set high except the first time
    Speed = 500
    low 2              'pin 2 set low
    pwmout 2,249,Speed 'pin 2 set high/low
    Pause 1000         'Pin 2 oscillates for 1 second
    low 2              'pin 2 set low
    k=2                'pin 2 set high
    pwmout 2,249,Speed 'pin 2 set high/low
    Pause 1000         'Pin 2 oscillates for 1 second
    low 2              'pin 2 set low
    GoTo Two
 

lauriet

Member
Thanks for the replies so far.
I'll try to be a bit clearer:
The code is not meant to be practical, I cobbled it up after seeing an effect I don't understand IN THE SIMULATOR; the comments are comments on what the simulator shows, not what it should show, and the 'k' should have no purpose. But it sets pin 2 high. In the simulator.
Surely it shouldn't have any effect?
 

hippy

Ex-Staff (retired)
the 'k' should have no purpose. But it sets pin 2 high. In the simulator.
Surely it shouldn't have any effect?
It looks like it's not the 'k' which is having any effect but it's the simulator re-lighting the 'pulse' icon on pin 2 after it's been turned off by "Low 2" because PWMOUT is still active. It seems that assigning to 'k' updates the simulation display for pin 2 whereas other commands may not. Put a PWMOUT 2, OFF before any 'k' assignment and it all behaves as expected.
 

Goeytex

Senior Member
Like others, I am not sure I understand what you are wanting the code to do. Maybe a better explanation is in order.
Tell us exactly what you want your code to do. If you have left out parts of your code, then you should include them
as well.

two:
k=1 'pin 2 set high except the first time
This will not set a pin high it only give variable b0 a value of 1

low 2 'pin 2 set low
This will set pin c.2 low if it was already high. If it was previously low, it
will do nothing. If it was previously unused it will assign C.2 as an output.


pwmout 2,249,500 'pin 2 set high
This will not set pin 2 "high". It will turn on a PWM output.

low 2 'pin 2 set low
This turns off PWM almost immediately after it was turned on
This is probably not a very good way to turn off PWM. You might
consider PWMDUTY 2, 0 as a better option.


k=2 'pin 2 set high
This will not set pin 2 high and does not reflect the state of Pin 2. Pin 2 is low now
because of the previous line.


low 2 'pin 2 set low
Pin 2 is already low ... this will do nothing ?

goto two
LOOP
=====================================

To retain PWM value, assign a variable for duty. eg Symbol DUTY = W5
Then give it a value eg Duty = 500
To enable PWM eg PWMOUT 2, 249 , 0 (Pin remains low)

To turn PWM output ON PWMDUTY 2, DUTY
To turn PWM output OFF PWMDUTY 2, 0
Back on...................... PWMDUTY 2, DUTY
 
Last edited:

hippy

Ex-Staff (retired)
Like others, I am not sure I understand what you are wanting the code to do. Maybe a better explanation is in order.
Consider it as "random code" which just shows the simulator not behaving as the author expected. Here's a simpler example ...

Code:
#Picaxe 08M
Do
  PwmOut 2, 1, 1
  Low 2
  b0 = 0
Loop
The question is really; why does the pin 2 output go bright green when the "b0 = 0" is executed ?

It may be that the question should be more; why does "Low 2" make the pin 2 output dark green if it doesn't turn PWMOUT off ?

I don't have an answer but have noted the issue.
 

lauriet

Member
Thanks, Hippy, for crystalising my observation so much better than I could.

Do you think this is just a simulator quirk, or might the 08M behave like that on pukka code?
Is there a way of seeing the current PWM values in the simulator? In reality, the motor is not following the variables and I'm trying to track down the situation in simulation.
I can work round the behaviour, but memory is tight.
Laurie.
 

hippy

Ex-Staff (retired)
Do you think this is just a simulator quirk, or might the 08M behave like that on pukka code?
Almost certainly a simulator quirk in the colouring of the output pins. However you can't turn the PWM signal on and off in the way you are trying to do it. You need PWMOUT to turn it on and a PWMOUT OFF to turn it off. You have to issue another PWMOUT to turn it back on again.

Is there a way of seeing the current PWM values in the simulator?
Unfortunately not, but if you use variables to set the PWMOUT parameters and don't alter those variables they will track that information. For example ...

b0 = 99
PwmOut C.2, b0, 200
 

Goeytex

Senior Member
An 0bservation in the simulator.

Code:
#Picaxe 08M
Do
  High 0 [COLOR=#008000] ' pins remains at 00000000[/COLOR]
  High 1 [COLOR=#008000] ' pins remains at 00000000[/COLOR]
  High 2  [COLOR=#008000]' pins remains at 00000000[/COLOR]
  High 4 [COLOR=#008000] ' pins remains at 00000000 [/COLOR]
  
  w3 = 1  [COLOR=#008000] 'Pins Changes to 00010111[/COLOR]

  Low 0   [COLOR=#008000] 'Pins remains at 0001011[/COLOR]
  Low 1    [COLOR=#008000]'Pins remains at 0001011[/COLOR]
  Low 2   [COLOR=#008000] 'Pins remains at 0001011[/COLOR]
  Low 4   [COLOR=#008000] 'Pins remains at 0001011[/COLOR]

w3 = 0    [COLOR=#008000] 'Pins Change to 00000000
[/COLOR]loop
 
Top