Shop OBEX P1 Docs P2 Docs Learn Events
QuickStart Board PASM Toggle with Buttons — Parallax Forums

QuickStart Board PASM Toggle with Buttons

doggiedocdoggiedoc Posts: 2,245
edited 2012-01-09 08:54 in Propeller 1
A while back I was working through the manual and the assembly language example. Tonight I modified the original AssemblyToggle.spin to work with the QS board and it built in buttons. The new code toggles the LED at P16 slowly ramping down and up the toggle rate. Pressing any of the buttons on the QS Board changes the active LED to the corresponding LED.

Forgive me if someone else has already done something like this.
{{ QS_Buttons_ AssemblyToggle.spin

   alternate version of AsseblyToggle using QS board's buttons to select a pin to toggle

}}

CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

VAR
  long thePin, theCog

PUB Main  | sameVal

  thePin := 16
  theCog := cognew (@Toggle, @thePin)                   'start with LED on P16 

  repeat
    sameVal := thePin                                   'set var for checking if thePin value has changed
    GetButton                                           'check buttons
    if sameVal <> thePin                                'if thePin value has changed b/c a button press
      cogstop (theCog)                                  'then stop the current cog and start again with new pin
      waitcnt(clkfreq / 10 + cnt)                      
      theCog := cognew (@Toggle, @thePin)
                         

  
      
PUB GetButton | buttonPressed

  dira [7..0]~
  buttonPressed := ina [7..0]
  case buttonPressed
    %10000000:
      thePin := 23
    %01000000:
      thePin := 22
    %00100000:
      thePin := 21
    %00010000:
      thePin := 20
    %00001000:
      thePin := 19
    %00000100:
      thePin := 18
    %00000010:
      thePin := 17
    %00000001:
      thePin := 16
                             

DAT
        {Toggle LED on selected pin}

              org 0                             'Begin at Cog RAM addr 0
Toggle        rdlong  ScratchMem, par
              shl     Pin, ScratchMem
              mov dira, Pin                     'Set Pin to output                              
              mov ScratchMem, cnt               'Calculate delay time
              add ScratchMem, #9                'Set minimum delay here
             
              
Increment    waitcnt ScratchMem, Delay          'Wait
              add Delay, IncValue               'Increment Delay value
              max Delay, MaxDelay wc            'Set Maximum Delay 
              xor outa, Pin                     'Toggle Pin
        if_nc jmp #Decrement                    'jump to Decrement loop
              jmp #Increment                       

Decrement    waitcnt ScratchMem, Delay          'Wait
              sub Delay, IncValue               'Increment Delay value
              min Delay, IncValue wc            'Set minimum Delay 
              xor outa, Pin                     'Toggle Pin
        if_c  jmp #Increment                    'jump to increment loop
              jmp #Decrement

                             
Pin           long      1                       'Pin variable
Delay         long       1_000_000              'Clock cycles to delay
IncValue      long          50_000              'increment value
MaxDelay      long      20_000_000              'maximum delay
ScratchMem    res       1                       'System Counter Workspace             

Comments

Sign In or Register to comment.