Pulsout and Speed control

ernest

Member
Hi all, yes! There is a question at the end of this.
My home made board has a 20x2 and Two L293D motor driver chips.
I use it with 12Vdc motors to drive my Meccano models.
I am controlling 4 motors forwards and reverse and all have PWM Softstart/Softstop
To do this I use Pulsout in subroutines, this give me the option of SoftStart /SoftStop on all outputs.
To find the values required, I observed the rise and fall of the needle on 12V anologe volt meter and also output shaft of the motor. These PWM SoftStart/SoftStop subroutines really do work well on a 12Voltgeared motor.

At last the questions. (I have tried).
Can I control the SPEED of a 12V motor using Pulsout in a similar subroutine method ?
A poke in the right direction ,or better still a bit of code would be much appreciated.
Thanks Brian



Code:
SoftStart:    'UP from zero volts to 12 Volts
                   pause 100
                   low 4           'Low 4 as 5 is going high.
                   w0=100          'set the primary pulse time at 100ms 
                   w4=50           'set the primary pause time at 50ms
                   w1=0            'set the Do count to 0
                   Do              'start of Do loop
                   inc W1          'adds 1 to W1 to count the number of pulses          
                   dec W4          'decrease W4 by 1 to decrease the pause time by 1ms          
                   W0=W0 +100      'increases the primary pulse time by 100ms
                   pulsout 5,w0    'pulsout the value of W0 on  output 5    
                   pause w4        'pause decreasing between pulses          
                   Loop until w1=50'sets the number of pulses to be counted
                   w0=100
                   w1=0
                   high 5 
                   END
     
         SoftStop:'DOWN from 12volts to Zero Volts
                    pause 100
                    low 4
                    w0=100 
                    w4=50
                    W1=0 
                    Do
                    inc W1           
                    dec W4           
                    W0=W0 +100        
                    pulsout 5,w0     
                    pause w4          
                    Loop until w1=50 
                    w0=100
                    w1=0
                    low 5
                    END
 

Bill.b

Senior Member
Hi Ernest

This may be of some help.
Here i am using PWM to control two motors of my rover bot. In this sample the speed is controlled by a IR remote, in the routine to increase or
decrease the speed, the IR command can be replaced for a FOR - NEXT loop and executed when required. The PWM output can also be controlled by a
pot to set max or min speed. I have other projects where I do this if you are interested.

The schematic is a part of the rover bot that drives the L298N motor driver block. I use the Enable inputs to control the speed.

Code:
#picaxe 40X2
Setfreq M8
#No_Data
#slot 1
#No_Table
DisableBod 
pause 1000

symbol infracnt 		= b6
symbol M1FWD		= D.7
symbol M1REV		= D.6
Symbol M2FWD		= D.5
Symbol M2REV		= D.4
Symbol M1PWM		= C.2
Symbol M2PWM		= C.1
symbol OLED			= C.0 
symbol SpeedCount		= w21	

symbol Baud			= N2400
Symbol KEY_ALMON        = 56		'IR Alarm ON Buttom 
Symbol KEY_1            = 0		'IR Key 1
Symbol KEY_2            = 1		'IR key 2
Symbol KEY_3            = 2		'IR key 3
Symbol KEY_4            = 3		'IR key 4
Symbol KEY_5            = 4		'IR key 5
Symbol KEY_6            = 5		'IR key 6
Symbol KEY_7            = 6		'IR key 7
Symbol KEY_8            = 7		'IR key 8
Symbol KEY_9            = 8		'IR key 9
Symbol KEY_0            = 9		'IR key 0
Symbol KEY_UP           = 77		'IR Up Arrow
Symbol KEY_DOWN         = 79		'IR Down Arrow
Symbol KEY_LEFT         = 76		'IR Left Arrow   -  24 hour format
Symbol KEY_RIGHT        = 78		'IR Right Arrow  -  12 hour format
Symbol KEY_MENU         = 96		'IR Menu Button  -  Time, Date and Alarm Menus
Symbol KEY_OK       	= 101		'IR OK Button    -  Accept and save settings	
symbol KEY_ALMOFF		= 43		'IR Alarm OFF Button
Symbol KEY_MINUS        = 18
Symbol KEY_PLUS         = 19
	Pwmout pwmdiv16,M1PWM,249,600
	Pwmout pwmdiv16,M1PWM,249,600
	
	
IRcontrol:
	serout OLED,Baud,(254,128)
	pause 10
 	serout OLED,Baud,("   IR Control   " )	
	irin [500],a.0,infracnt		'wait until button is pushed on IR remote
	if infracnt = KEY_DOWN then gosub backfast
	if infracnt = KEY_UP then gosub fwdfast
	if infracnt = KEY_LEFT then gosub turnleft
	if infracnt = KEY_OK then gosub stopm
	if infracnt = KEY_RIGHT then gosub turnright
	if infracnt = KEY_MINUS then gosub turnaround
	if infracnt = KEY_PLUS then gosub turnaround2
	if infracnt = KEY_2 then
		speedcount = speedcount+10
		pause 50
	
		PWMduty c.2,speedcount
		PWmduty c.1,speedcount
	endif

	if infracnt = KEY_3 then 
		speedcount = speedcount-10
		pause 50
	
		PWMduty c.2,speedcount
		PWmduty c.1,speedcount
	endif
		if speedcount >=600 then
			speedcount = 600
		endif
			if speedcount < 250 then
			speedcount = 250
		endif	
	goto IRcontrol
	
TurnLeft:

	serout OLED,Baud,(254,192)
 	serout OLED,Baud,(" Turning Left  " )
	high M1FWD
	low M1Rev
	Low M2FWD
	Low M2REV
	return
	
TurnRight:

	serout OLED,Baud,(254,192)
	 serout OLED,Baud,(" Turning Right " )
	Low M1FWD
	low M1Rev
	High M2FWD
	Low M2REV

	Return
	
FwdFast:
	
	serout OLED,Baud,(254,192	
	serout OLED,Baud,("   Forward    " )
	high M1FWD
	low M1Rev
	High M2FWD
	Low M2REV
	Return
	
BackFast:

	serout OLED,Baud,(254,192)	 pause 10
	serout OLED,Baud,("  Reversing   " )	
	high M1REV
	low M1FWD
	High M2REV
	Low M2FWD
	Return
	
StopM:

	serout OLED,Baud,(254,192)
	 pause 10
 	 serout OLED,Baud,("    Stopped     " )
	Low M1PWM
	Low M2PWM
	Low M1REV
	low M1FWD
	Low M2REV
	Low M2FWD
	Return
L298NPWM.jpg

This code is for a dew heater on a teloscope, it used 3 pots to control the PWM output to the heaters. The same proceedure can be used
to control motors.

Code:
'Telescope Dew Heater Controller
'Picaxe 14m2
'Bill Brown 12/06/2012

symbol Scope 		= b.2		'Scope heater output
symbol SiScope 		= c.0		'Sight Scope heater output
symbol Lens 		= b.4		'Lens heater output
symbol ScopeSet		= W1		'Scope heater Variable
symbol SiScopeSet		= w2		'Sight Scope heater Variable
symbol LensSet		= w3		'lense heater Variable
symbol ScopeSetTemp	= w4		'Scope PWM Variable
symbol SiScopeSetTemp	= w5		'Sight Scope PWM Variable
symbol LensSetTemp	= W6		'Lense PWM Variable

setfreq M16					'Set picaxe frequency to 16meg
dirsB=%00110101
dirsC=%00000001

FirstPowerUp: 

Pwmout pwmDiv4, Scope, 100, 50 	'Set initial PWM output for Scope Heater (Minimum Value)
Pwmout pwmDiv4, SiScope, 100, 50 	'Set initial PWM output for Sight Scope Heater
Pwmout pwmDiv4, Lens, 100, 50 	'Set initial PWM output for Lens Heater





ReadTempSet:			'Read pot to set max Teperature (set mark/space ratio of PWM)

	Readadc10 b.1, ScopeSet 	'Read the Teperature value and put it into word varible "W1"
	Readadc10 c.4, SiScopeSet 	'Read the Teperature value and put it into word varible "W2"
	Readadc10 b.3, LensSet		'Read the Teperature value and put it into word varible "W3"
	
	ScopeSetTemp = ScopeSet/2     'scale the Teperature  range 0 - 511
	
	If ScopeSetTemp > 511 then 
		ScopeSetTemp = 511
	 endif 
   	If ScopeSetTemp < 25 then 
     	 	ScopeSetTemp = 25		'Zero out the Teperature value if it's already a very low value. 
	endif					'Hold the value to 511 if it goes over 511. 
	SiScopeSetTemp = SiScopeSet/2    'scale the Teperature  range 0 - 511
	
	If SiScopeSetTemp > 511 then 
	    SiScopeSetTemp = 511	 
            endif    				'Hold the value to 511 if it goes over 511 
	if SiScopeSetTemp < 25 then 
		SiScopeSetTemp = 25
	endif
	LensSetTemp = LensSet/2    	'scale the Teperature  range 0 - 511
	
	If LensSetTemp > 511 then 
	    LensSetTemp = 511
	 endif
	 
	
	if LensSetTemp < 25 then 
		LensSetTemp = 25
	endif
	Pwmduty Scope, ScopeSetTemp
	Pwmduty SiScope, SiScopeSetTemp
	Pwmduty Lens, LensSetTemp
	goto ReadTempSet
Bill
 
Last edited:

westaust55

Moderator
An advantage of PWM is that the PWM is performed in the background and your program does not have to keep generating the pulses to keep the motor running.

HPWM offers 4 outputs on the 20X2 which if single mode is used can separately control each of the 4 motors.
The. You can ramp the speed up or down by using the HPWMDUTY command and leave a motor at any desired speed.
 

ernest

Member
Hello Bill Thanks for your reply, bit over my head, I will have good look and at your program, thanks again Brian.
 

ernest

Member
Thank you for you reply, "which if single mode is used".Could you explain please, what you mean by single mode .Thanks Brian
 

nick12ab

Senior Member
Thank you for you reply, "which if single mode is used".Could you explain please, what you mean by single mode .Thanks Brian
Single Mode
Supported: 20X2, 28X1, 28X2, 28X2-3V, 40X2, 40X2-3V
Not Supported: 14M, 14M2, 20M2, 28X2-5V, 40X1, 40X2-5V

In single mode each pin works independently. It is therefore equivalent to a pwmout command. However more than one pin can be enabled at a time. Therefore this mode has two main uses:

1) To allow the equivalent of a 'pwmout' command on different outputs (than the pwmout command)

2) To allow pwmout on more than one pin (up to 4) at the same time. The pwm applied to each output is identical. This is often used to provide a brightness control on multiple LEDs or to control multiple motors.

To enable a single output simply set its corresponding bit to '1' (D-C-B-A) within the settings byte of the command e.g. to enable all 4 pins use %1111

(from hpwm)
 

westaust55

Moderator
Thank you for you reply, "which if single mode is used".Could you explain please, what you mean by single mode .Thanks Brian
@ernest (brian),

if you become of a PICAXE command that is new to you, why not read the manual first to try and ascertain what is meant?

However nick12ab has also provided a verbatim answer which could have been determined by reading the relevant webpage for BASIC commands:
http://www.picaxe.com/BASIC-Commands/Advanced-IO-Interfacing/hpwm/
or reading the manual 2 page 86 (on revision 7.9)
I had already checked against the 20X2 you mentioned at post 1 before suggesting the optiion to you.

Sorry if the above seems a bit blunt but obviously you had not read the relevant web/manual page.
We are here to help but, it is often possible to help ones self by reading the manuals.
 

ernest

Member
Thank you for pointing me in the direction of hpwp, I'll do do as westaust55 kindly suggested and read the manual.Thanks again.
 

ernest

Member
Thanks to your and nick12ab replies, I will read hpwm in the manual again. I suppose I was looking for an easy solution with the use of Pulsout command and subroutines as I have with my Pulsout SoftStart SoftStop code. I do appreciate hpwm runs in the back-ground and that hpwm is what I need to look at. Thanks again Brian
 
Top