Maker Pro
Maker Pro

help with picaxe

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
No need to get snarky. <-- edit: You probably weren't. I shouldn't have said that. Sorry.

I'll take a look at the docs and see what I can do to explain it to you.

(And yes, you'd be surprised how many people don't bother to do even the most basic research)
 
Last edited:

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
ok, I think I can explain it now...

Code:
If counter=0 then 
       pwmout pwmdiv4, 2, 99, 400
       Wait 2                              ' Add Wait here.
       for w1 = 0 to 400             ' pwmduty needs a (W)ord var, not a (B)yte var
       pwmduty C.2, w1               'adjust PWM Duty from 0% to 100%
       pause 2                       ' increase the on time
       next w1

would cause a flash at the beginning because this line:

Code:
pwmout pwmdiv4, 2, 99, 400

sets the duty cycle to 400/1024 of full intensity

the loop then sets the intensity to 0/1024 before steadily increasing it to 400/1024.

The last parameter sets the duty cycle, that is essentially the proportion of the time that the led is on. The valid range is 0 (off) to 1024 (on). Intermediate values (say 290) mean that the led is on for 290/1024ths of the time and off for (1024-290)/1024ths of a second.

Your eye sees this as an intensity of jest over 1/4 of full intensity. So as your loop slowly increases the value the led spends more and more time on, and less and less off, you see it as a gradually increasing brightness.

The other parameters describe the pin, and the period (1/frequency). The frequency is not critical as long as it is high enough that you don't see flashing or flickering.

I misread the last parameter as "duty,cycles", rather than what it is "duty cycle". ...and sorry for that.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
And to add to that, there is also the pwm command which does specify the number of cycles (and 0 probably means 0).

This command is probably not useful in this application.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
I'm going to try to explain this a little differently than Steve did. I'm going to make believe you're a (dumb as a box of rocks) blond. :p Just kidding, but I am going to try to boil it down to plain un-cryptic English. I can't do it right now though. I've been (physically) vegetating in front of this PC for hours. I must saddle up and work these old muscles.

Hey, the sun just broke through! I'm outa here!

Chris
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Okay Robert, here's my attempt to explain the "PwmOut" command/function. First you'll note that I called it a Command/Function. Some might argue that it's technically a Function only. Functions are comprised of the function name (PwmOut) followed by variables or constants called "Arguments". Arguments are always placed on the right side of the function call and some parts are separated by commas. Not all functions include arguments though.

When PwmOut is executed it actually calls a function or sub procedure named PwmOut. This function contains a large block of code that you don't see and is apparently not accessible by the user. I've searched for it in the Programming Editor folder with no results. Chances are these functions are hard coded in the ProgEdit.ex, which is the executable that runs when you open Picaxe.

The above minutia can be totally ignored by you. I only included it for completeness. From this point on I'm going to refer to functions as commands.

What does the PwmOut command do?:
(1) It turns the PWM (Pulse Width Modulation) function ON.
(2) Everything to the right of PwmOut sets the parameters (arguments) of the PWM waveform.
(3) The first argument is the Pin number used followed by a comma separator.
(4) The second argument is the Output Period followed by a comma separator.
(5) The third argument is the Duty Cycle which is a typo in the PDF >> "Duty Cycles".

The Period actually specifies the reciprocal of Frequency F=1/Time. Period is the Time required to complete one full cycle. Unfortunately this parameter can't be entered as time period directly, nor can the Duty Cycle. See PWM Calculator below.

Duty Cycle specifies the pin (High) time of one full cycle.

PwmDuty: This command can only be used after the PwmOut command has been executed. It is only used the change the duty cycle that was previously set in the PwmOut command. This command has only 2 arguments PIN & Duty Cycle. The PDF contains the same "Duty Cycles" typo as PwmOut..

Note: While PwmDuty cannot execute without being preceded by the PwmOut command the opposite is not true. The PwmOut command is a stand alone command. It does not require PwmDuty to be executed subsequently.

PwmOUT pin, OFF: This command is used to turn the Pwm OFF. "Pin" is replaced by the pin number you're using.

You don't need to know how these two arguments (variables in this case) pass the frequency and duty cycle to the PWM pin. Picaxe provides a PWM calculator found in the Picaxe tab on the tool bar. It automatically generates the code to copy and paste into your app. When you play with it you will find that the calculator will automatically insert the PwmDiv(n) keywords if your desired output frequency requires scaling. In time I'm sure that your curiosity will have you playing with the math that they're using.

As a side note remember the beginning of this post,.. the part I said you can ignore? Well the calculator is probably doing the inverse of what I described the hidden PwmOut function as doing. Remember, absolutely every line of code has to be converted into a "bit-wise" (BINARY) language.

I hope this helps.

Chris
 

robertgzzzt

Jun 26, 2013
98
Joined
Jun 26, 2013
Messages
98
Everything to the right of PwmOut sets the parameters (arguments) of the PWM waveform.

pwmout, period, duty cycle.

I was mistakenly thinking that the "period" is the number of on/off cycles (i.e. "pulses"), when in fact, it is the length of a SINGLE on/off cycle, and the "duty cycle" is the ON portion of a single ON/OFF cycle.

When you had me change the duty cycle from 400 to 0 to get rid of the flicker, it totally worked. The LED ramps up with no flicker whatsoever.

I just don't understand how the "mark time" (or ON time) can be zero.

The "word variable" that is used in the code that I copied is "0 to 400"

Code:
for w1 = 0 to 400 ' pwmduty needs a (W)ord var, not a (B)yte var
pwmduty C.2, w1 'adjust PWM Duty from 0% to 100%

Is this the actual line of code that "ramps up" the LED?

@Steve. Thank you very much for your effort to help me understand. Please forgive me if you thought I was being belligerent, as that was definitely not my intent.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Remember, absolutely every line of code has to be converted into a "bit-wise" (BINARY) language.

In the case of the PicAXE, this process is tokenization. The tokens are then interpreted by the firmware in the Pic (the firmware makes it a PicAxe).

The actual code for these commands in in this firmware. But this is really just detail that's not too important. If you want more details it was discussed here at some length.
 

robertgzzzt

Jun 26, 2013
98
Joined
Jun 26, 2013
Messages
98
Oh wait, I think I see now. The pwmout command begins with the duty cycle variable set to zero, then the pwmduty command increases it from 0 to 400. the reason it "flickered" was because it read the 400 from the initial command, then immediately went to zero as it carried out W1....and gradually ramped back up to 400.

Am I making any sense?
 
Last edited:

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
In the case of the PicAXE, this process is tokenization. The tokens are then interpreted by the firmware in the Pic (the firmware makes it a PicAxe).

The actual code for these commands in in this firmware. But this is really just detail that's not too important. If you want more details it was discussed here at some length.

No argument but the Picaxe Interpreter/simulator has to perform the same functions that the chip would. So whatever functions are flashed in the chip's boot loader has to also be called from the interpreter.

Oh wait, I think I see now. The pwmout command begins with the duty cycle variable set to zero, then the pwmduty command increases it from 0 to 400. the reason it "flickered" was because it read the 400 from the initial command, then immediately went to zero as it carried out W1....and gradually ramped back up to 400.

Am I making any sense?

Yes, you get it now! That's why I changed the PwmOut duty cycle to 0. It prevents the pin from turning the LED on (at all) until it ramps up in the PwmDuty loop. When the duty cycle = 0 the pin voltage is essentially also zero. I don't want to confuse you but this does not translate to Pin2 being off. It just means it's held low. If you connected a LED with limiting resistor between Pin2 and the Vdd (+5V) the LED would turn on brightly when PwmOut is executed and then ramp down when the PwmDuty loop is executed. .. Capice?

Chris
 

robertgzzzt

Jun 26, 2013
98
Joined
Jun 26, 2013
Messages
98
I don't want to confuse you but this does not translate to Pin2 being off. It just means it's held low. If you connected a LED with limiting resistor between Pin2 and the Vdd (+5V) the LED would turn on brightly when PwmOut is executed and then ramp down when the PwmDuty loop is executed. .. Capice?

Yes, sir. I do.

The thing that screwed me up was thinking "99" as being the number of on/off cycles, when in reality it's the length of a SINGLE on/off cycle, and 400 is the length of a single ON cycle @100%. It's the word variable (0 to 400) in the "for next" loop (pwmduty C.2, w1) that ramps up the brightness of the LED.

I still don't understand how the 99/400 is calculated, but I don't need to know. I know enough for now.

On thing that puzzles me, and this has nothing to do with PWM, I was writing the code for my "deflector dish control", for when I get my 14M2 chip (as I need two PWM ports for this). In writing my code, I copied this section of the code I'm using now with my 08M2 chip.

Code:
main:
       If pressed=1 then			'Test the status of the switch
		pressed = 0			'Reset the the switch status for the next press
	If counter >4 then 	                'If the counter is > 4 
		counter=0		       'reset it to start over again
		    endif				'end the if statement

In the picaxe programming editor I changed my chip to the 14M2 to run the simulator, but it flagged my code "if command without endif", but this code runs perfectly fine in the code I borrowed it from. The simulator doesn't complain about a missing endif. Wuzzup with that? Is it just a glitch, or did overlook the missing endif in the previous code?

Another thing, it flagged the interrupt command for debouncing my switch....

Code:
init:
      setint %00001000,%00001000 	' activate interrupt when pin3 only goes high
						'pin3 is the push button

the interrupt command can only be used on pins C.0 to C.2 on the 14M2. That's strange because ONLY pin C.3 is an input only according to the 14M2 pinout. C.0, 1 and 2 are inputs or outputs. If I use the interrupt command for C.0, will it automatically recognize C.0 as an input?

For the interrupt to be active high on pin C.0, would I type it as....

Code:
setint %00000001,%00000001

????
 
Last edited:

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Every "If" statement must be terminated with an 'EndIf" statement. This is where code indentation makes code easier to read. If you look at my first "If" statement (If A) and follow the column straight down you will see its terminating "EndIf" statement (EndIf A). Now look at my second 'If" statement (If B) and follow the column straight down. You will see the "EndIf" termination (EndIf B) for If "B".

Code:
[FONT="Times New Roman"][B]main:
       If pressed = 1 Then       ' If "A"
            pressed = 0	
                If counter > 4 Then      ' If "B"
                     counter = 0
                EndIf	                ' EndIf "B"	       		    
       EndIf                              ' EndIf "A"
[/B][/FONT]

I'll look into your interrupt issue and get back to you.

Edit: This is called "nesting" If statements because If "B" is nested within If "A".

Chris
 
Last edited:

robertgzzzt

Jun 26, 2013
98
Joined
Jun 26, 2013
Messages
98
Thanks, Chris. I thought you might know how the interrupt command worked off the top of your head. It's no biggie, I'm pretty sure I understood it correctly, if not, I'll know soon enough. I'm gonna order a couple of larger picaxe chips (14 and 20M2), amongst other things, when I get paid Friday. I really appreciate all you and Steve have done for me.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Sorry. but my brain is fried on oxycodone. I threw my back out again. They haven't done a damn thing to help me stand erect or ease the pain though! I need more beer to wash it down.

Happy New Year!

Chris
 

robertgzzzt

Jun 26, 2013
98
Joined
Jun 26, 2013
Messages
98
Sorry. but my brain is fried on oxycodone. I threw my back out again. They haven't done a damn thing to help me stand erect or ease the pain though! I need more beer to wash it down.

Happy New Year!

Chris

I know how you feel. I've got a lot of arthritis in my spine from three previous surgeries, including a cervical fusion, plus to add to it, with this cold weather we've been having..... even my toenails hurt. :eek:

When you get to feeling better...

Unlike the red/white common anode LEDs I'm using for the torpedo launchers, these blue/yellow bi-colored LEDs for the deflector dish control are common cathode, and those are the ones I'm going to have to drive with an N-Channel MOSFET (NTD-4906N). If I connect the anodes to the DRAIN, and the cathodes to the 5V POS rail, then write the PWM code backwards, i.e. 400 to 0 to ramp up and vice verse, will it work that way? I would just try it and see for myself, but I don't want to fry anything.

Happy New Year to you too. I hope you get to feeling better soon.
 
Last edited:

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
You may have posted it earlier but I don't feel like searching for it. What's the current level of your LEDs? How many (parallel strings) will be driven by each FET?

This information well help us suggest the best solution because common cathode bi-leds are not ideal for driving them in the drain leg of a FET. A source follower (common drain) configuration is not doable here either.

The more I think about this the more I think that the only practical alternative is to use a PNP LED driver and invert your mark-space PwmDuty code. Personally, I'd look for a common anode bi-led. That's the only way you'll be able to use your FET.


Chris
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Robert, I didn't mention P-CH FETs because they're not as common as N-CH. I figured that logic level P-CH's would be near none existent. These days things change rapidly. So, after a search I found plenty of hits for "logic level p channel mosfet".

Chris
 

robertgzzzt

Jun 26, 2013
98
Joined
Jun 26, 2013
Messages
98
What's the current level of your LEDs? How many (parallel strings) will be driven by each FET?

The forward current rating is 20mA. I only need to drive 2 bi-colored (amber/blue) LEDs.


I don't know why I didn't think about this before, but the 14M2 has 4 pwm outputs. I'll need to use 2 of them for the left and right torpedo simulations, if I could use the other two outputs simultaneously, that would be the answer. I don't think it would be a problem as long as I'm using the same frequency, but if it is a problem, I guess I'll just have to find some common anode blue/yellow bi-colored LEDs.

I'll see if I can write the code in a way the simulator will accept.
 

robertgzzzt

Jun 26, 2013
98
Joined
Jun 26, 2013
Messages
98
I don't know what I was thinking. Even if I could use two pwm outputs simultaneously it still wouldn't solve my problem. I still need to run two LEDs off of a single output, as two bi-colored LEDs is essential FOUR LEDs. I guess I'll have to find some common anode blue/yellow LEDs.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
I'll sketch up a PNP driver for you. Your LED current is minimal, so just about any PNP will drive your common cathode bi-leds. Save you're high power FETs for another project. They're overkill in this application anyway. FYI, all uC's have a maximum accumulative current rating. The Picaxe will not like 20mA per pin x 3 pins = 60mA. So, transistor drivers are a must.

Chris
 

robertgzzzt

Jun 26, 2013
98
Joined
Jun 26, 2013
Messages
98
I'll sketch up a PNP driver for you. Your LED current is minimal, so just about any PNP will drive your common cathode bi-leds. Save you're high power FETs for another project.

Thanks, I'd really appreciate that.

FYI, all uC's have a maximum accumulative current rating. The Picaxe will not like 20mA per pin x 3 pins = 60mA.

The specs say that each output can sink or source 20mA, but the max combined current is only 90mA. Why is the heck do they put so output pins on them if they can only do 90mA?

I haven't bought the blue/yellow LEDs yet, Chris, but the ONLY blue/yellow I can find in common Anode is 10MM ones, so I would rather use common cathode, as I can get those in the 5mm or 3mm variety, both (all three actually) are 20mA forward current. In fact, let it be SOP that whenever I mention the word LED, they will be 20mA LEDs.

I have some BC327 PNP transistors. Will that do the trick for my amplifier? If not, could you or Steve recommend something?

Here's what I want to do. I will need to have TWO bi-colored blue/yellow LEDs on at the same time, with the two yellows on one PWM output, and the two blues on another. Just to be clear, ONLY the two yellows or the two blues will be ON at any given time.

Then I will have the red side of TWO bi-colored red/white LEDs on the other two PWM outputs on the 14M2. Only ONE of those will be ON at any given time. The red side will ramp up, then a quick 50ms burst of white, then the red will ramp down. This will alternate between left and right at each button push. I shouldn't need an amplifier for those, as each red and each white will have it's own output. (correct me if i'm wrong)
 
Last edited:
Top