May 17, 2024, 04:00:05 AM

News:

You can now use Vixen to program your Prop-1 and Prop-2 controllers!  Get started quickly and easily, without having to learn PBASIC.  Details in the Library forum.


LED Fading

Started by JonnyMac, August 02, 2007, 10:27:18 PM

Previous topic - Next topic

JonnyMac

As you know, the Prop-1 and Prop-2 have the PWM instruction which can be used to modulate an LED; the trouble is that it's only good in a loop -- there's no way to set an LED to 50% brightness and leave it there.

The Prop-SX has the ability to do "interrupts."  These can be tricky for beginners, so I've written the program for you.  Using this code you have eight regular outputs (P0 - P7), four dimmable outputs (P8 - P11), and four triggers (P12 - P15).  Note that you can connect a lamp to the OUTx dimming channels -- just make sure the lamp current doesn't exceed the ULN capabilities (225 mA per pin).

Since interrupts are used, any time-centric instruction will be affected; that's why PAUSE has been replaced with DELAY_MS.  Since the ISR runs at a know rate it can be used to manage a clock for program delays.

' =========================================================================
'
'   File...... LED_Fader.SXB
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 02 AUG 2007
'
' =========================================================================


' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------


' -------------------------------------------------------------------------
' Conditional Compilation Symbols
' -------------------------------------------------------------------------


' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSCXT2, TURBO, STACKX, OPTIONX, BOR42
FREQ            20_000_000
ID              "Fader4"


' -------------------------------------------------------------------------
' I/O Pins
' -------------------------------------------------------------------------

Outs            PIN     RB   OUTPUT

Leds            PIN     RC
Led1            PIN     RC.0 OUTPUT             ' P8/OUT8
Led2            PIN     RC.1 OUTPUT             ' P9/OUT9
Led3            PIN     RC.2 OUTPUT             ' P10/OUT10
Led4            PIN     RC.3 OUTPUT             ' P11/OUT11

TriggerA        PIN     RC.4 INPUT              ' P12
TriggerB        PIN     RC.5 INPUT              ' P13
TriggerC        PIN     RC.6 INPUT              ' P14
TriggerD        PIN     RC.7 INPUT              ' PI5


' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------


' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------

tix             VAR     Byte                    ' for ISR-based delays

idx             VAR     Byte                    ' loop controller

tmpB1           VAR     Byte                    ' for subs/funcs
tmpB2           VAR     Byte
tmpW1           VAR     Word

dimmer          VAR     Byte (16)               ' bank for dimmer vars
dimTimer        VAR     dimmer(0)
level1          VAR     dimmer(1)               ' LED levels, 0 to 255
level2          VAR     dimmer(2)
level3          VAR     dimmer(3)
level4          VAR     dimmer(4)
acc1            VAR     dimmer(5)               ' pwm accumulators
acc2            VAR     dimmer(6)
acc3            VAR     dimmer(7)
acc4            VAR     dimmer(8)


' =========================================================================
  INTERRUPT NOCODE 100_000
' =========================================================================

Check_Tix:
  ASM
    BANK  tix
    TEST  tix                                   ' check tix
    JZ    Update_Leds                           ' if zero, not running
    DEC   tix                                   '   else decrement

Update_LEDs:
    BANK  dimmer
    INC   dimTimer
    JNZ   Update_Dimmers

Reset_Accs:
    MOV   Leds, #%0000                          ' clear leds
    MOV   acc1, level1                          ' reload PWM accumlators
    MOV   acc2, level2
    MOV   acc3, level3
    MOV   acc4, level4

Update_Dimmers:
    INC   acc1                                  ' update accumulator
    SNZ                                         ' skip if no roll-over
    SETB  Led1                                  '   else LED is on
    INC   acc2
    SNZ
    SETB  Led2
    INC   acc3
    SNZ
    SETB  Led3
    INC   acc4
    SNZ
    SETB  Led4

ISR_Done:
    BANK  0
  ENDASM
  RETURNINT


' =========================================================================
  PROGRAM Start
' =========================================================================


' -------------------------------------------------------------------------
' Subroutine / Function Declarations
' -------------------------------------------------------------------------

DELAY_MS        SUB     1, 2                    ' delay in milliseconds


' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:


Main:
  FOR idx = 0 TO 255                            ' get brighter
    level1 = idx
    level2 = idx
    level3 = idx
    level4 = idx
    DELAY_MS 10
  NEXT
  FOR idx = 255 TO 0 STEP -1                    ' get dimmer
    level1 = idx
    level2 = idx
    level3 = idx
    level4 = idx
    DELAY_MS 10
  NEXT
  GOTO Main

' -------------------------------------------------------------------------
' Subroutine / Function Code
' -------------------------------------------------------------------------

' Use: DELAY_MS ms
' -- delays program in milliseconds
' -- uses ISR timer

SUB DELAY_MS
  IF __PARAMCNT = 1 THEN
    tmpW1 = __PARAM1                            ' save byte value
  ELSE
    tmpW1 = __WPARAM12                          ' save word value
  ENDIF
  DO WHILE tmpW1 > 0
    tix = 100                                   ' set for 1 ms
    DO WHILE tix > 0                            ' wait
    LOOP
    DEC tmpW1                                   ' decrement ms counter
  LOOP
  ENDSUB

' -------------------------------------------------------------------------


' -------------------------------------------------------------------------
' User Data
' -------------------------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

wilsondk

The following link is for a 4 bit led dimmer.  Just wondering if this would work with any of your controllers.  It seems like it could handle
a lot of the timing for dimming.  I was thinking of trying one.  Has anyone out there tried one on these?

http://www.nxp.com/#/pip/cb=[type=product,path=50807/41735/48878,final=PCA9532_2]|pip=[pip=PCA9533_2][0]

JonnyMac

The forums code doesn't like characters in that link -- go to

www.nxp.com

and then enter PCA9532 in the search box.

Here's a link to the data sheet: http://www.nxp.com/acrobat_download/datasheets/PCA9532_3.pdf
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

August 14, 2007, 03:23:54 PM #3 Last Edit: August 14, 2007, 03:34:47 PM by JonnyMac
You could control the PCA9532 with a Prop-2 or Prop-SX (preferred).  The SX supports true I2C instructions; in the Prop-2 they have to be synthesized but it is possible.  This article (written by yours truly) shows how to do I2C in the BS2 (Prop-2 core controller): http://www.parallax.com/dl/docs/cols/nv/vol5/col/nv115.pdf

Update: On further reading of the PC9532 spec it appears that you can only have two PWM frequencies, hence four available brightness levels (off, PWM0, PWM1, full on) for all LEDs.  The ISR-driven code I posted above will allow every LED to be at its own level (0 to 255, independent of the others) and the number of outputs is easily expanded.


Jon McPhalen
EFX-TEK Hollywood Office

Bean (Hitt Consulting)

Jon,
  Did you notice that one line of the program got screwed up by the smiley converter ?
  "acc4            VAR     dimmer("

  Pretty funny actually...
Bean.


JonnyMac

Yeah, I hat that the forum changes things to smileys, but I was asked to enable them.
Jon McPhalen
EFX-TEK Hollywood Office