PWM and PWM out with Logicator how to switch off and vary?

JPB33

Senior Member
Trying to get pwm and pwm commands to work using logicator. I can switch pwmout on on sucessfully and can vary the duty cycle as seen when I download to dim an led on my T board but cannot make it switch off even by adding a stop command so I must have a really basic problem! Read the instructions but doesnt seem to cover it unless I am missing more somewhere? Is it covered elsewhere also is it possible to vary pwmout duty cycle using an analogue input?

Thanks
 

nick12ab

Senior Member
Trying to get pwm and pwm commands to work using logicator. I can switch pwmout on on sucessfully and can vary the duty cycle as seen when I download to dim an led on my T board but cannot make it switch off even by adding a stop command so I must have a really basic problem! Read the instructions but doesnt seem to cover it unless I am missing more somewhere? Is it covered elsewhere
To stop the PWM either set the duty cycle to 0 or use pwmout pwmpin,off in a BASIC cell.

also is it possible to vary pwmout duty cycle using an analogue input?
Yes. The easiest way to do that is to set the PWM frequency so that the PWMout period value is 63 (it doesn't matter what the frequency is as it's just an LED) then the duty cycle can be set to the ADC reading (of 0-255). Whenever you want to set the PWM duty cycle to the ADC reading use a BASIC cell with this code:
Code:
readadc [I]adcchannel[/I],b8
pwmduty [I]pwmpin[/I],b8
 

JPB33

Senior Member
Thanks for the reply, I can now get the PWMOUT to stop by setting the duty cycle to 0. Tried various combinations but sorry but I am unable to make it work in variable mode when downloading the instructions including the basic cell commands to my T4 board. The chip I am using is an 18X. Any further guidance possible?

Thanks
 

hippy

Technical Support
Staff member
The 18X does not support the PWMDUTY command so use a PWMOUT in the BASIC cell.

Use the variable from a READADC cell, eg if reading ADC into A ...

PWMOUT B.3, 63, varA
 

JPB33

Senior Member
I think my 18M2 is dead so used the 18X and PWMOUT. Got some life out of it so many thanks for your help. Havent put a scope on the o/p yet but the LED goes up and down using the pot on my T4 board. It is min brightness clockwise on the pot and I cannot seem to change it by swapping the analogue i/p over so what am I doing wrong there? Want to control a dc motor speed in conjunction with a logic FET

Also attached is my circuit for any comments.

Doest it go dark outside quickly when your playing with Picaxe!
 

Attachments

hippy

Technical Support
Staff member
It is min brightness clockwise on the pot and I cannot seem to change it by swapping the analogue i/p over so what am I doing wrong there?
Not sure I follow, but you could perhaps fix with an EXPRESSION cell which does -

A = 255 - A

That will take an original 0..255 input and reverse it to a 255..0 input, will turn a clockwise maximum reading into an anti-clockwise maximum.

Doest it go dark outside quickly when your playing with Picaxe!
It's when it starts getting light again you realise you've spent too long at it :)
 

JPB33

Senior Member
Yes, overall I'm pretty poor at explaining things but I normally get there, I meant reversing the direction of the potentiometer so that clockwise is max and adding an expression cell as suggested does the trick. Clockwise is always max to me by convention!

Just put it on the scope and it certainly goes full range with a small amount of ripple on the waveform , for something simple like is it ok to leave the clock speed set as default?

Thanks
 

nick12ab

Senior Member
Just put it on the scope and it certainly goes full range with a small amount of ripple on the waveform , for something simple like is it ok to leave the clock speed set as default?
The pot or the PWM?

On PWM this is expected as that's how PWM works - it turns a pin on and off really fast to simulate an analogue signal.

On the output of the potentiometer rapid but slight changes in the voltage and therefore ADC reading seem to be normal but this can be corrected by fitting a small capacitor between the PICAXE ADC pin and 0V.
 

JPB33

Senior Member
The pot/input is very clean and the ripple is about 0.1v p to p on the top and bottom of the waveform, adding various value caps to the pot doesnt make any difference but dont think it will matter.

Was hoping to switch off the pwmout using wait after a pre determined time as I need a 20sec burst but doesnt seem as simple as that. For a 20sec burst of pwm which I can vary do I need to use a PWM cell rather than PWMOUT? The 18X chip I am using doesnt support this so it looks like I will have to wait for the 18M2 to arrive before I can try that on my board.

Anyway, many thanks for getting me this far, great to see an led going bright and dimm with a turn of a pot on the T board at last!
 

hippy

Technical Support
Staff member
Was hoping to switch off the pwmout using wait after a pre determined time as I need a 20sec burst but doesnt seem as simple as that.
Not sure why not. This should do the job -

READADC pin, varA
PWMOUT B.3, 63, varA
WAIT 20
PWMOUT B.3, 63, 0

All but the second command can be achieved with standard Logicator cells. The second inside a BASIC cell.
 

JPB33

Senior Member
Thanks Yes thats what I thought I had, or similar with the the input decision as a switch, see attached

Should I be able to add the wait cell before switching the PWMOUT off? It works fine om my 18X, add the wait cell and nothing!
 

Attachments

hippy

Technical Support
Staff member
You have the order of your cells completely wrong. You are setting the PWMOUT duty before reading the ADC, turning PWMOUT off before the WAIT. The 0<A0<255 seems to be completely redundant.
 

JPB33

Senior Member
Yes it a direct consequence of not knowing what your doing! Point taken about 0<A0<255, I popped that in as I had used it previously and seemed a good idea, but cannot make sence of making it wait I am afraid,

Thanks
 

JPB33

Senior Member
Points taken and re arranged. I can producee a fixed burst but when I add a wait cell it does not work and dont understand why not. I have attached working examples, can some please explain where I am going wrong as I want to be able to vary the pwm level fed to the mosfet driver over the fixed time period?

Thanks
 

Attachments

hippy

Technical Support
Staff member
Can some please explain where I am going wrong as I want to be able to vary the pwm level fed to the mosfet driver over the fixed time period?
So you want to be able to vary the PWM during the WAIT then turn off ?

You cannot do it during the WAIT as such but you can slice the WAIT up into smaller pieces and execute a number of those to effectively create a longer WAIT, and while executing those smaller WAIT's you can add the code to read the ADC and set the PWM.

The first step is to take what you currently have working, adjust that to have the WAIT period you want but using a loop which includes shorter WAIT's. Then insert the READADC and setting PWMOUT from variable code.

Or alternatively - and simpler once you've done it - you can use the multi-tasking capabilities of the 18M2 to do what you want. One task will wait until activated, start a second task, WAIT however long, then stop the second task, stop the PWMOUT. The second task will simply READADC and set PWMOUT.
 

JPB33

Senior Member
Thanks for the suggestions I am already running three tasks so dont want to add more as I want to use PWMOUT within two of the strings so tried the shorter wait and looped it. That works fine with a pwm as selected for the required time (see attached) until I add the basic and readadc cells then it does not vary but the waiting time seems to extend as the input pot is twiddled. Have I got your instructions right?

Thanks



View attachment PWM looped 24-2-13.plf
 

hippy

Technical Support
Staff member
Have I got your instructions right?
It works for me so perhaps you are not putting the additional cells in the right place but the dual use of A for the READADC reading and loop counter will cause problems.
 

JPB33

Senior Member
At least that sounds hopeful if it works for you! Have tried A for READADC B for loop counter but no joy. I have the basic and readadc cells before the pwmout as I thought that tells the pwm what to do when the pot is twiddled---am I thinking wrong?
 

Attachments

nick12ab

Senior Member
At least that sounds hopeful if it works for you! Have tried A for READADC B for loop counter but no joy. I have the basic and readadc cells before the pwmout as I thought that tells the pwm what to do when the pot is twiddled---am I thinking wrong?
Have the arrow go up to the readadc cell, not the Inc B cell.
 

hippy

Technical Support
Staff member
Have the arrow go up to the readadc cell, not the Inc B cell.
You also need to set the PWMOUT using the BASIC cell after reading the READADC value, and you only want to set the duty to 0% when the loop completes.
 
Top