Skip to main content

Special Code Examples


Button-Controlled LED Chaser with Direction Reversal​

Objective: 8 LEDs chase from right to left. Press and hold a button to pause; release to reverse direction. Each press toggles direction.

Register Map:

RegisterPurpose
R01LED bitmask (initial 0x0001)
R02Direction flag (0x0000 = left shift / right-to-left, 0x0001 = right shift / left-to-right)
R05Delay value in seconds
R3EButton state register (0x0000 = not pressed, 0x0001 = pressed)

Mnemonic Form​

;=== Initialization ===
000 FZ 0001 0001 ; R01 = 1 (rightmost LED on)
001 FZ 0002 0000 ; R02 = 0 (direction: left shift = right-to-left)
002 FZ 0005 0001 ; R05 = 1 (1-second delay)

;=== Main Loop (line 003) ===
003 SS 0001 0000 ; Output R01 to LEDs
004 YS 0005 0001 ; Delay 1 second
005 JFZ 0000 003E ; Copy R3E (button) to R00
006 TJZ 0800 0010 ; If R00 ≠ 0 (pressed), jump to pause 010
007 JFZ 0000 0002 ; Copy R02 (direction) to R00
008 TJZ 0700 000C ; If R00 == 0 (left shift), jump to 00C

; Right shift branch (direction = 1, left-to-right)
009 XHYW 0101 0001 ; R01 circular right shift by 1
00A ZY 0000 0003 ; Jump back to main loop 003

; Left shift branch (direction = 0, right-to-left)
00C XHYW 1101 0001 ; R01 circular left shift by 1
00D ZY 0000 0003 ; Jump back to main loop 003

;=== Pause (line 010) ===
010 SS 0001 0000 ; Output R01 (hold LED state)
011 YS 0005 0001 ; Delay
012 JFZ 0000 003E ; Copy R3E to R00
013 TJZ 0700 0015 ; If R00 == 0 (released), jump to reverse 015
014 ZY 0000 0010 ; Otherwise keep waiting 010

;=== Reverse Direction (line 015) ===
015 JFZ 0000 0002 ; Copy R02 to R00
016 TJZ 0700 0019 ; If R00 == 0 (was left shift), jump to 019
; Was right shift → change to left shift
017 FZ 0002 0000 ; R02 = 0 (left shift)
018 ZY 0000 0003 ; Jump back to main loop 003
; Was left shift → change to right shift (line 019)
019 FZ 0002 0001 ; R02 = 1 (right shift)
01A ZY 0000 0003 ; Jump back to main loop 003

Instruction Form​

;=== Initialization ===
000 10 0001 0001 ; R01 = 1 (rightmost LED on)
001 10 0002 0000 ; R02 = 0 (direction: left shift = right-to-left)
002 10 0005 0001 ; R05 = 1 (1-second delay)

;=== Main Loop (line 003) ===
003 13 0001 0000 ; Output R01 to LEDs
004 02 0005 0001 ; Delay 1 second
005 11 0000 003E ; Copy R3E (button) to R00
006 0A 0800 0010 ; If R00 ≠ 0 (pressed), jump to pause 010
007 11 0000 0002 ; Copy R02 (direction) to R00
008 0A 0700 000C ; If R00 == 0 (left shift), jump to 00C

; Right shift branch (direction = 1, left-to-right)
009 15 0101 0001 ; R01 circular right shift by 1
00A 0E 0000 0003 ; Jump back to main loop 003

; Left shift branch (direction = 0, right-to-left)
00C 15 1101 0001 ; R01 circular left shift by 1
00D 0E 0000 0003 ; Jump back to main loop 003

;=== Pause (line 010) ===
010 13 0001 0000 ; Output R01 (hold LED state)
011 02 0005 0001 ; Delay
012 11 0000 003E ; Copy R3E to R00
013 0A 0700 0015 ; If R00 == 0 (released), jump to reverse 015
014 0E 0000 0010 ; Otherwise keep waiting 010

;=== Reverse Direction (line 015) ===
015 11 0000 0002 ; Copy R02 to R00
016 0A 0700 0019 ; If R00 == 0 (was left shift), jump to 019
; Was right shift → change to left shift
017 10 0002 0000 ; R02 = 0 (left shift)
018 0E 0000 0003 ; Jump back to main loop 003
; Was left shift → change to right shift (line 019)
019 10 0002 0001 ; R02 = 1 (right shift)
01A 0E 0000 0003 ; Jump back to main loop 003

Behavior Summary​

ActionEffect
Normal run8 LEDs chase from right to left (circular left shift)
Press and hold buttonChaser pauses immediately, only current LED stays on
Release buttonDirection reverses — right-to-left becomes left-to-right, and vice versa
Press againDirection reverses again, toggling repeatedly