November 23, 2024, 08:48:05 PM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


PWM dimming

Started by shupe1, August 19, 2011, 09:22:14 PM

Previous topic - Next topic

shupe1

Hi,

I am trying to learn more about how to use PWM dimming with LEDs.  I have created a VERY basic program that seems to be working just fine, EXCEPT, no matter what PIN I define the fade on, PIN0 is what actually runs the program.  For example, as the code (below) is currently written PIN5 should be dimming.... however, when I load the program on my Prop 1 trainer, PIN0 is dimming.

What am I missing here??

Thanks!!

' =========================================================================
'
'   File......   PWM test
'   Purpose...
'   Author....   Travis
'   E-mail....   travis.shupe@gmail.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

' -----[ Constants ]-------------------------------------------------------

SYMBOL level            = B3


' -----[ Variables ]-------------------------------------------------------



' -----[ Initialization ]--------------------------------------------------

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00111111                              ' make P0-P5 outputs


' -----[ Program Code ]----------------------------------------------------

Main:

FOR  level = 0 TO 255 STEP 2
  PWM PIN5, level, 5
  NEXT
  HIGH PIN5

  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------

JackMan

Try this.

' =========================================================================
'
'   File......   PWM test
'   Purpose...
'   Author....   Travis
'   E-mail....   travis.shupe@gmail.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

' -----[ Constants ]-------------------------------------------------------

SYMBOL IsOn     = 1
SYMBOL IsOff     = 0

' -----[ Variables ]-------------------------------------------------------

SYMBOL level            = B3


' -----[ Initialization ]--------------------------------------------------

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00111111                              ' make P0-P5 outputs


' -----[ Program Code ]----------------------------------------------------

Main:

FOR  level = 0 TO 255 STEP 2
  PWM PIN5, level, 5
  NEXT
  PIN5 = IsOn
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------

JonnyMac

August 19, 2011, 09:54:25 PM #2 Last Edit: August 19, 2011, 09:56:06 PM by JonnyMac
This is one of the traps with PBASIC1; the use of PINx in a statement.  You see, PINx is actually a bit variable that you can write to or read from.  When PINx appears of the left side of a statement (of the = sign, that is), for example

  PIN5 = 1

...then we are writing to it.  When it is on the right side of the statement, or embedded in a command, we are actually reading from it and using that value.  So when you do this:

  PWM PIN5, level, 5

...what the Prop-1 is doing is reading the input on P5 and then using that value (0 or 1) as the pin for the PWM command.  Again, this is the trickiest aspect of the PBASIC1.  If you want to use P5 as your PWM pin you could create a constant like this:

SYMBOL  LED             = 5

... and then update your PWM statement like this:

  PWM LED, level, 5

The value of LED is 5 so you will get the correct pin to do it's thing.  BTW, using named constants like this is always a good idea; it can make the code more readable and it makes our programs much easier to update and maintain.

If you want the LED to fade up and then stay on you will want to put  this:

  HIGH LED

... after the PWM loop.  The PWM command can be used for charging RC circuits (analog output) so the pin is left in input mode at the end of the PWM command.
Jon McPhalen
EFX-TEK Hollywood Office

bsnut

This what your program will look like base on what Jon is talking about.


' =========================================================================
'
'   File......   PWM test
'   Purpose...
'   Author....   Travis
'   E-mail....   travis.shupe@gmail.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------
SYMBOL  LED             = 5                          ' LED connected on P5

' -----[ Constants ]-------------------------------------------------------

SYMBOL level            = B3


' -----[ Variables ]-------------------------------------------------------



' -----[ Initialization ]--------------------------------------------------

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00111111                              ' make P0-P5 outputs


' -----[ Program Code ]----------------------------------------------------

Main:

FOR  level = 0 TO 255 STEP 2
  PWM LED, level, 5
  NEXT
  HIGH LED

  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


Remember that you can find more info on the PWM command in the Basic Stamp Editor help file as well.
William Stefan
The Basic Stamp Nut

JonnyMac

Be very careful with formatting; alignment of blocks (FOR-NEXT) with indentation of lines in between makes the code easier to read and debug.  I'm going to be hounding everyone about this as the Spin language, which is used in the HC-8+, strictly enforces indentation.  Here's my idea of a properly formatted program.  Note that I am not afraid of white space (blank lines and spaces between things) -- you shouldn't be, either.

' =========================================================================
'
'   File......   PWM_Test.BS1
'   Purpose...
'   Author....   Travis
'   E-mail....   travis.shupe@gmail.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  LED             = 5                     ' LED connected on P5


' -----[ Constants ]-------------------------------------------------------

SYMBOL level            = B2


' -----[ Variables ]-------------------------------------------------------



' -----[ Initialization ]--------------------------------------------------

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00111111                              ' make P0-P5 outputs


' -----[ Program Code ]----------------------------------------------------

Main:
  FOR level = 0 TO 255 STEP 2                   ' ramp up brighness
    PWM LED, level, 5
  NEXT
  HIGH LED                                      ' force on
  PAUSE 1000

  FOR level = 255 TO 0 STEP -2                  ' ramp down brightness
    PWM LED, level, 5
  NEXT
  LOW LED                                       ' force off
  PAUSE 1000

  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


In the Help file, under PBASIC Language Reference, there is a section called "The Elements of PBASIC Style."  I recommend that we all review and follow those guidelines.  Remember, computer languages are not just for the computer, but for other human beings to read as well.  Keeping things neat and orderly will cut down on bugs and make life easier for the other humans that read our programs.

Jon McPhalen
EFX-TEK Hollywood Office

shupe1

This is all GREAT info!!  Thanks everyone!  I was getting very confused and couldn't for the life of me figure out why I was dimming PIN0 and not PIN5.

This is exactly why I LOVE forums!

Travis

JonnyMac

I should have noted that the reason it was always P0 (and never P1) is that the ULN input circuitry, which is connected to all pins unless removed, pulls the pin down through a resistor.  Without some external signal on P5 your original code would always output the PWM to P0.
Jon McPhalen
EFX-TEK Hollywood Office