PWM output on 14M syntax?

gengis

New Member
I'm having trouble getting a simple pwm output on a 14M version.

My program is back to basics turning a pulse train on and off on pin 5, or that's what I wanted . . .

What happens is that it works as expected exactly 8 times on the eighth time the pin goes high and switches the pwm on and off as always. On the scope it looks like zero volt baseline, with a half second of square wave, going to +4 volts. On the eighth time the base line shifts to +4 and the pulses go to zero.

The led turns on and off eight times, then gets brighter and dimmer eight times, then back to on and off eight times.

I figure there's something I'm not understanding about using inputs as outputs on the 14M. I passes the syntax check and I did try adding the "portc" instruction and it still does the baseline shift.

My program:
Code:
'Led output
'pickaxe 14M

start:
	pause 100
	pwmout 2,100,200	'generate 10khz pulse train on pin5 (output2)
	pause 500 		'wait half a second
	pwmout 2,0,0	'turn off pulse train on pin 5
	pause 500		'wait half a second
	goto start		'repeat forever
 

BCJKiwi

Senior Member
There's nothing wrong with the syntax but have you tried;
pwmout 2,off
instead of 0,0 as that is the manual recommendation - never seen any difference myself but you never know until you try it!

Other than that I'd be double checking the LED circuit for something strange.

You could try a resistor - say the venerable 10K - in place of the LED and monitor that with the scope to see if it behaves differently.
 
Last edited:

gengis

New Member
I just tried pwmout 2,off (minus the comma - that failed the syntax test).

That changes the symptoms but doesn't fix it. Now it flashes three times normally then eight from a high baseline and eight from a zero baseline, and repeats.

I'm currently using a NPN to drive the LED with a 3K ohm from pin 5 to the base. The symptoms are the same whether driven directly or with a transistor.

I didn't have the unused inputs grounded and tried grounding them, tried a large filter cap on the supply, no joy. Same deal from 4.5 or 3 volt supplies. Using the editor version 5.1.6 (syntax dll 131072) with Widows ME, enhanced compiler and 14M checked.

Any other ideas?
 

Mycroft2152

Senior Member
the program looks fine and simulates ok.

Since you didn't mention it, do you have a current limiting resistor on the LED itself, between the +V and the LED?

Somewhere between 330 and 1K ohms?

Myc
 

gengis

New Member
There is a limiting resistor on the led (1K) and a 3.3 K driving the base of the transistor. All unused inputs are grounded via 2.2K resistors.

Beginning to think I got a defective chip or one with defective programming.

I'll try downloading the new editor and go at it again.

What should be negligible program changes affect the symptoms, and the symptoms are repeatable - which suggests the problem is software or firmware to me.

Has anyone actually used the PWM out on 2 ?
 

BCJKiwi

Senior Member
Yes, I've used PWMOUT 2 on a 14M without problem.

The fact that it works correctly for a number of cycles then starts misbehaving does not indicate a defective program, firmware or chip.

Hence the suggestion of driving a single resistor and monitoring that - i.e. troubleshoot with the simplest possible configuration.

The nature of the problem you describe indicates a overheating effect or a buildup of some sort which stops the circuit functioning as expected.

Initially you did not mention the NPN for example. If that is not being switched properly, or there is no way for the gate signal to dissipate rapidly then you will get issues.

Please just try driving a resistor to ground as the only load on the PWM and advise.
 
Last edited:

goom

Senior Member
Yes, I have used pwmout on a -14M to drive an LM18200 H-bridge for motor speed control. It is for an RC boat setup, and works perfectly. I did find some oddities however.
With a -08M or -18X, I could simply stop the pulse train be issuing a low command. However, with the -14M, low portc 5 (or high portc 5) would work fine on Input 2 (leg 5) until a pwmout 2 command was given, after which the low/high ceased to work. I then used pwmout 2,255,0 to stop the pulse train, which worked fine. I suggest you try pwmout 2,100,0 instead of pwmout 2,0,0 to stop the pulses.
You mention the output is on pin5. I assume that you really mean physical LEG 5.
I can post my code if you think it will help.

Kevin
 

gengis

New Member
BCJKiwi:
I first tried a LED from pin 5 to ground with a 1K limiting resistor, when that didn't work as expected, I went to a transistor and 3.3 K base drive resistor, pulling the LED and 1K current limiting resistor to ground. I looked at the pin with a 2.2 K to ground load with my scope and saw the output going high (staying high through 8 half second cycles) with pulses going to zero.

My power source is two or three fresh alkaline batteries with a SPDT switch and center position off (3V-0V-4.5V). <6" of wire from battery connectors to breadboard, bypassed on the board with a 1,000 uf cap and .1 uf cap.

No heating perceptible.

goom:
Please post your program I'd like to try it (and tell me what with and how pin five is loaded - pull up or down +value)

Logical output # 2 on physical pin 5. It is confusing, but I am connected to leg #5 as you put it, and I measured all the other pins with a scope, only five has a signal present.

Pin 5 = PWM 2, HPWM A, OUT C5, IN C 5, INPUT 2
 

goom

Senior Member
Here is my code. It relies on pulsin inputs from an RC receiver, and must have In1 (leg 6) tied high to avoid an interrupt. The pulse train goes directly to the LMD18200 which is high impedance and draws only a few micro Amps.
Code:
'Reversing motor speed control using LMD18200
'Kevin Goom, 15 February 2008
'PICAXE-14M connections are:
' Leg 1                  +5V
' Leg 2 (Serial in)  Tie to ground through 10K resistor
' Leg 3  (In4/ADC4)   Pulse input from radio receiver
' Leg 4  (In3)        Tie to ground
' Leg 5  (In2)        PWM (LMD18200 Pin9)
' Leg 6 (In1)    Thermal flag (LMD18200 Pin9) Low when T>145°C
' Leg 7 (In0/ADC0)   Current Sense (LMD18200 Pin8)V x 1.28 = A (2.0K resistor)
' Leg 8 (Out5)    N/C
' Leg 9 (Out4)    N/C
' Leg 10 (Out3)    Direction (LMD18200 Pin3)
' Leg 11 (Out2)    N/C    
' Leg 12 (Out1)    Brake (LMD18200 Pin4) Motor shorted when high and PWM high, coast when PWM low
' Leg 13 (Out0/Serout)N/C
' Leg 14      Ground
symbol PW_null=w2  'Name the null position input pulse width
symbol PWin=w1   'Name the input pulse width
symbol Duty=w0   'Name the duty cycle
symbol Hysteresis=b6 'Name the amount of hysteresis
symbol Dir=b7   'Name the direction flag (0 = forward, 1=reverse)
Hysteresis=3   'Set hysteresis
Dir=0    'Set direction flag to forward initially
low 3    'Set direction to forward initially
pwmout 2,255,0   'Motor off
high 1    'Motor in coast
setint %00000000,%00000010   'Trap for thermal flag low      
initialise:
   Pause 500      'Wait 1/2 second initially
   pulsin 4,1,PW_Null    'Measure the input pulse width initially (null position)
   if PW_Null<135 or PW_Null>165 then initialise'Go back if no valid null input detected
restart:
   for b8=0 to 4     'Check for 5 times for valid pulse
   pulsin 4,1,PWin     'Measure the input pulse width
   if PWin>75 and PWin<225 then recheck 'Exit loop & continue if valid pulse detected
   next b8
   pwmout 2,255,0     'Stop PWM signal to motor
   goto restart
recheck:
   pulsin 4,1,PWin     'Measure the input pulse width
   if PWIn<75 or PWin>225 then restart 'Go back if no valid input detected
   low 1       'Release brake
   if PWin > PW_Null then forward_  'Check for forward rotation
reverse_:
   Duty=PW_Null-PWin
   if Duty<hysteresis then stop_  'Stop motor if input within deadband 
 if Dir=1 then ContR
  pwmout 2,255,1023   'Stop PWM signal to motor
  high 1     'Brake motor
  Pause 500     'Pause for 1/2 second on changing forward to reverse
  low 1     'Release brake
     high 3       'Set reverse for LMD1800
     Dir=1     'Set the direction flag to reverse
   ContR:
   Duty=Duty*26 max 1023    'Set duty cycle from 0 to 1023
   pwmout 2,255,Duty    'Send PWM signal to motor
   goto recheck     'Return to measure input pulse
forward_:
   Duty=PWin-PW_Null
   if Duty<hysteresis then stop_    'Stop motor if input within deadband
  if Dir=0 then ContF  
  pwmout 2,255,1023   'Stop PWM signal to motor
  high 1     'Brake motor
  Pause 500     'Pause for 1/2 second on changing forward to reverse
  low 1     'Release brake
     low 3       'Set forward for LMD1800
  Dir=0     'Set the direction flag to forward
   ContF:
   Duty=Duty*26 max 1023    'Set duty cycle from 0 to 1023
   pwmout 2,255,Duty    'Send PWM signal to motor
   goto recheck     'Return to measure input pulse
stop_:
   pwmout 2,255,0     'Stop PWM signal to motor
   goto recheck     'Return to measure input pulse

interrupt:
 CheckT:
 pwmout 2,255,0     'Stop PWM signal to motor
 pause 5000     'Pause 5 seconds
 if pin1=0 then CheckT   'Check if thermal flag cleared
 setint %00000000,%00000010  'Reset trap for thermal flag low
return
I hope this helps
 

gengis

New Member
Joy at last!

Thanks goom.

Using pwmout 2,100,0 to stop the pulse train works.

Code:
Start:
	pwmout 2,100,200	'Start PWM signal
	pause 500
	pwmout 2,100,0    'Stop PWM signal
	pause 500
	goto start
 

BCJKiwi

Senior Member
pwmout 2,100,0
pwmout 2 off

These do quite different things,
pwmout 2,100,0 does not stop the pwmout background operation, but does change the duty so there is effectively no output.

pwmout 2 off shuts down the pwmout background task completely and the port returns to being input2.

Either should work for you and if they don't then you need to look to the circuit for the answer.
 

gengis

New Member
How it is supposed to work and how it is working are not the same. I don't profess to understand why, just that it doesn't

pwmout 2,100,0
pwmout 2 off

These do quite different things,
pwmout 2,100,0 does not stop the pwmout background operation, but does change the duty so there is effectively no output.

pwmout 2 off shuts down the pwmout background task completely and the port returns to being input2.

Either should work for you and if they don't then you need to look to the circuit for the answer.
For the heck of it I put the [pwmout 2] off in there after already shutting it down with the [pwmout 2,100,0 ]

Then I went back and tried the bare [pwmout 2 off] command again, and it still didn't work. BUT, by putting [pwmout 2 off] followed by a line with [low portc 5] does work.

So then I tried just using [low portc 5] and that alone doesn't work - that keeps the output oscillating continuously.

I verified the behavior several times to make sure and I'm just using a "remark" to disable the lines, so the lines of code couldn't have changed.

I seriously doubt this is a circuit problem - the circuit is too simple and I've checked it several times for a mistake. This almost has to be a chip, software or firmware problem, and if I had to bet I'd go with firmware.
 

BCJKiwi

Senior Member
I was intrigued by this situation so pulled the 14m I have (FW v 9.B) and loaded up your original program.

Got the same results as you did except there were only 4 cycles at power on, then the 8 or so.

With pwmout 2 off, the problem started right way after program download but after re-power it took about 12 cycles to for the problem to appear.

With PwmOut 2,100,0 it works OK.

also checked it with pullups / downs on all unused ports, same result.

None of this is as it should be!

I guess we need to wait for Technical to comment.

Please advise the FW version for them to check.
 
Last edited:

gengis

New Member
How does one check the firmware version? I search on "firm" in the manual and fw and nothing. The chip itself has what looks like a date on it. Don't see anything in the editor that looks like it might - unless the terminal can be used.
 

premelec

Senior Member
It should show up on every download in the successful box - and also can be checked from the options menu on the editor - the chip must be connected :)
 

gengis

New Member
Thanks premelec

The firmware version is 9.C

I may have another anomoly. Awhile back I saw a timing change in the sleep command when I slowed the to clock to 1 MHZ. I was told the clock shouldn't change because sleep is derived from the 32 KHZ divider. The thing is my sleep command got shorter by a factor of four when I expected it to get 4X longer.

Anyhow, I left it in there, just extended my sleep time by four, and went about checking batteries, screwing around with the PWM problem on another breadboard, and other things, and just let it run for several days. Some time around repetition 400 or so, the time speeded up again by a factor of four.

I can't get to it now, but I'll see if I can't duplicate that problem and report it when I have time.
 

Technical

Technical Support
Staff member
pwmout 2,off is simply shorthand for pwmout 2,0,0

This will do two things, switch off the timer that generates the pwmout string and then switch off the pwm module. After the pwm module has been switched off the state of the pin is unknown and so you should re-issue a 'let dirsc' command to make sure the pin is set to the input / output configuration that you require.

An alternate way of 'forcing' pwm off is to poke 0 to the CCP1CON register
e.g. poke $15,0
 

BCJKiwi

Senior Member
Could the Manual be updated please as the recommendation there leaves the pin in an unknown state.

The manual refers to;
"PWMOUT pin OFF"
and,
"To stop pwmout on a pin it is necessary to issue a pwmout pin stop command."
But there is no PWMout Stop command.

Also there is no reference to PWMout pin,0,0

So a note to cover the need to reset the port after the pin,0,0, or pin off would be helpful, as would the inclusion of;
pwmduty 2,150,0 &#8216; set pwm duty to no output
in the code snippet in the manual.

Thanks
 
Top