Shop OBEX P1 Docs P2 Docs Learn Events
PASM - Dimmer Triac Driver code help — Parallax Forums

PASM - Dimmer Triac Driver code help

lanternfishlanternfish Posts: 366
edited 2011-12-05 23:18 in Propeller 1
I have been playing around with the Dimmer Triac Driver Object from the OBEX.

What I want to achieve is an inverted output i.e. the driver currently is active high output; I want to an active low output.

The object begins:
PUB Load (BrightnessAt)

'' Call this routine with address of brighness variable.
'' The brighness variable should be set from 0-255, 0=full off, 255=full on.

'' BrightnessAt is address of variable to pass to PASM code to set the brighness (0 - 255)

        ZeroPin :=  |< CrossDetect  ' bit mask for Zero Crossing Detector Pin (Active High)
        OutputPin :=  |< Output     ' bit mask for Output Pin (Active High)

        BrightVarAt:=BrightnessAt             ' Push address of brighness into PASM code

        cognew(@Dimmer,@Stack)
        

DAT
{

Enter with the ADDRESS of the LONG that contains the brighness level.
The brightness level can be any value but it is looking for 0 = full off to 255 = full on.
If the value is less than 0, it is set to 0.
If the value is greater than 255, it is set to 255

}

And then I changed the :loop0 line below:
org     0
                                                                                                
Dimmer
                mov     St1,OutputPin           ' Set all bits for input except output pin
                mov     dira, St1
:loop0          mov     outa,#0                 ' *** Turn OFF output

To:
:loop0          mov     outa,#OutputPin                 ' *** Turn ON output

And further into the object:
:loop1          '' Look for leading edge of zero crossing input
                mov     st3,ina                 ' Read input value
                and     st3,ZeroPin wz          ' Mask off all but input bit
        if_z    jmp     #:loop1                 ' Bit not high, do it again


                rdlong  st1,BrightVarAt
                maxs    st1,#255
                mins    st1,#0

                mov     st3,#255                ' Invert because delay makes light dimmer
                sub     st3,st1

                '' See if full on, if so, skip delay
                cmp     st3,#0 wz
        if_z    jmp     #:loop2
        
                '' See if full off, if so, skip delay
                cmp     st3,#255 wz
        if_z    jmp     #:loop3
        
                '' Calculate number of clock cycles to delay for 0-255 level
                '' 100% on time = 80_000_000 / 120 = total of 666_667 clock cycles  (666_667/255 = 2614 counts/bit)
                '' Our full on = 2048+512+32+16=2608 clock cycles per level (2608*255=665_040 total clock cycles)
                  
                mov     st1,st3                 ' 80Mhz / (1/256 x 1/120) = 2604 clock cycles per half cycle 
                shl     st3,#11                 ' level x 2048 (2614 - 2048 = 566)

                mov     st2,st1                 
                shl     st2,#9                  ' level x 512 (566 - 512 = 54)
                add     st3,st2

                mov     st2,st1                 
                shl     st2,#5                  ' level x 32 (54 - 32 = 22) ' error of 22/2614 = insignificant but not 100% on
                add     st3,st2

                mov     st2,st1                 
                shl     st2,#4                  ' level x 16 (22 - 16 = 6) ' error of 6/2614 = insignificant but not 100% on
                add     st3,st2                 ' so if level = 255, skip timing and turn on immediately.

                add     st3,cnt                 ' Delay until this clock value is reached

                '' Check to see if main delay value is reached                
                waitcnt   st3,#0                ' Wait until main count time is reached, time to turn on Triac
:loop2       mov     outa,OutputPin          ' *** Turn ON output pin
                

I changed :loop2 to:
mov     outa,#0          ' *** Turn OFF output pin

And then at the end of the code:
:loop3          '' Look for trailing edge of zero crossing input
                mov     st3,ina                 ' Read input value
                and     st3,ZeroPin wz          ' Mask off all but input bit
        if_nz   jmp     #:loop3                 ' Bit high, do it again
                
:loop4          '' Look for leading edge of zero crossing input to go HIGH again to start next cycle
                mov     st3,ina                 ' Read input value
                and     st3,ZeroPin wz          ' Mask off all but input bit
        if_z    jmp     #:loop4                 ' Bit not high, loop until cycle ends

                '' Done with this half cycle, do it again
                mov     outa,#0                 ' *** Turn OFF output pin
                jmp     #:loop0
                
              
ZeroPin         long    0                       ' Pin number for Zero-Crossing Pulse (Active High)
OutputPin       long    0                       ' Pin number for output to MOC3010M
BrightVarAt     long    0                       ' Address of brighness variable
     
ST1             res     1                       ' Temp 1
ST2             res     1                       ' Temp 2
ST3             res     1                       ' Temp 1

                fit     496         ' Make sure it fits within a cog's memory space

I changed:
'' Done with this half cycle, do it again
                mov     outa,#0                 ' *** Turn OFF output pin
                jmp     #:loop0

to:
'' Done with this half cycle, do it again
                mov     outa,#OutputPin                 ' *** Turn ON output pin 
                jmp     #:loop0

Using a quickstart board I have assigned port 16 to the output/triac driver pin and port 23 to for the zero crossing (active high) input.

While the original code works the modified doesn't.

Q1. What have I done wrong?

My "duh" in advance.

Cheers

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-12-05 20:28
    You'll notice that the original code uses mov outa, OutputPin (no #). That's about it. Your current use of # will cause the register location ($26) being written to outa not its content (bit mask, |< 16).
  • lanternfishlanternfish Posts: 366
    edited 2011-12-05 23:18
    Hi kuroneko

    Yes, the dreaded #.

    Once again you have helped me. Thank you.
Sign In or Register to comment.