Skip to main content

Code Sharing


๐Ÿ“š Lesson 1 - Getting Startedโ€‹

Three-color LED - Sequential Execution to Endโ€‹

000  41  0000  0010
001 41 0000 0011
002 41 0000 0012
003 0F 0000 0000

Three-color LED - Loop Execution - Adjust Color at Runtimeโ€‹

000  41  0000  0010
001 41 0000 0011
002 41 0000 0012
003 0E 0000 0000

Three-color LED - 1s Delay - Register Increment - Color Combinationโ€‹

000  10  0010  0001
001 12 0010 0001
002 02 0001 0001
003 13 0010 0001
004 0E 0000 0001

๐Ÿ“š Lesson 2 - Sensors & Controlโ€‹

PWM Control LED Brightnessโ€‹

000  10  0000  03E8
001 10 002A 0000
002 10 002B 0080
003 10 002C 0100
004 12 002A 0010
005 12 002B 0005
006 12 002C 0002
007 05 002C 0001
008 0E 0000 0004

Explore Sound-controlled Lightโ€‹

000  10  0000  1000
001 10 0025 0001
002 AD 0000 002A
003 AD 0001 002D
004 04 2A00 0010
005 0E 0000 0002
010 0B 0001 0012
011 0E 0000 0002
012 E4 007F 7F7F
013 02 0005 0001
014 E4 0000 0000
015 0E 0000 0002

Indoor Temperature Monitoringโ€‹

000  10  000C  0CBC
001 11 0000 002E
002 08 000C 0005
003 41 0000 0010
004 0E 0000 0000
005 41 0000 0012
006 0E 0000 0000

Simulate Indoor Lighting Circuitโ€‹

000  10  0002  0001
001 10 0003 0000
002 06 3F02 0004
003 0E 0000 0002
004 06 3F03 0006
005 0E 0000 0004
006 06 0102 0008
007 0E 0000 0010
008 10 0001 0000
009 E4 0000 0000
00A 0E 0000 0002
010 10 0001 0001
011 E4 0011 1111
012 0E 0000 0002

Simulated Lucky Wheel Spinโ€‹

000 0C 0000 0080
001 0C 0000 00B0
002 0C 0000 00A2
003 0E 0000 0002
080 10 0000 0000
081 10 0001 0014
082 10 0002 0028
083 10 0003 003C
084 10 0004 0050
085 10 0005 0000
086 10 0006 0001
087 10 0007 0002
088 10 0011 0000
089 10 0012 0005
08A 0D 0000 0000
090 05 1112 0092
091 0D 0000 0000
092 0C 0000 00C0
093 0C 0000 00C0
094 1F 0000 0000
095 2D 0000 028F
096 0C 0000 00D0
097 0C 0000 00B0
098 12 0011 0001
099 0D 0000 0000
0A2 06 3F06 00A4
0A3 0E 0000 00A8
0A4 06 3F05 00A6
0A5 0E 0000 00A4
0A6 0C 0000 0090
0A8 06 3F07 00AA
0A9 0D 0000 0000
0AA 10 0011 0000
0AB 0D 0000 0000
0B0 10 0028 0210
0B1 58 8080 2000
0B2 10 0028 FFFF
0B3 55 0000 0000
0B4 02 00C8 0000
0B5 0D 0000 0000
0C0 E4 00FF 0000
0C1 02 00C8 0000
0C2 E4 00FF FF00
0C3 02 00C8 0000
0C4 E4 0000 FF00
0C5 02 00C8 0000
0C6 E4 00FF 00FF
0C7 02 00C8 0000
0C8 E4 0000 00FF
0C9 02 00C8 0000
0CA 0D 0000 0000
0D0 04 0001 00D3
0D1 E4 00FF 0000
0D2 0D 0000 0000
0D3 04 0002 00D6
0D4 E4 00FF FF00
0D5 0D 0000 0000
0D6 04 0003 00D9
0D7 E4 0000 FF00
0D8 0D 0000 0000
0D9 04 0004 00DC
0DA E4 00FF 00FF
0DB 0D 0000 0000
0DC E4 0000 00FF
0DD 0D 0000 0000

Main Program (000-003)โ€‹

LineInstructionDescription
000DY 0000 0080Call register initialization subroutine starting at line 080
001DY 0000 00B0Call screen display subroutine (circle+number) starting at line 0B0
002DY 0000 00A2Call lottery judgment loop subroutine starting at line 0A2
003ZY 0000 0002Jump back to line 002, continuous lottery loop

Register Initialization (080-08A)โ€‹

LineInstructionDescription
080FZ 0000 0000R00 = 0, temporary storage for random number
081FZ 0001 0014R01 = 0x14, write value 20
082FZ 0002 0028R02 = 0x28, write value 40
083FZ 0003 003CR03 = 0x3C, write value 60
084FZ 0004 0050R04 = 0x50, write value 80
085FZ 0005 0000R05 = 0, constant for button comparison
086FZ 0006 0001R06 = 1, constant for button comparison
087FZ 0007 0002R07 = 2, constant for button comparison
088FZ 0011 0000R11 = 0, prize counter (cumulative lottery rounds)
089FZ 0012 0005R12 = 5, less-than jump threshold (for lottery round limit)
08AFH 0000 0000Initialization complete, return to main program

Lottery Loop Control (090-099)โ€‹

LineInstructionDescription
090XYZ 1112 0092If R11 < R12 (lottery count less than 5 rounds), jump to line 092
091FH 0000 0000Lottery round limit reached, return to caller
092DY 0000 00C0Call color light rotation subroutine starting at line 0C0 (visual transition)
093DY 0000 00C0Call color light rotation again (extend visual effect)
094SJS 0000 0000Generate random number, store in R00
095CHU 0000 028FR00 = R00 รท 0x028F, scale random number to target range
096DY 0000 00D0Call prize level judgment subroutine starting at line 0D0
097DY 0000 00B0Call screen display subroutine at line 0B0 (display current result)
098ZJ 0011 0001R11 = R11 + 1, increment lottery counter
099FH 0000 0000End of current subroutine, return

Conditional Judgment and State Switching (0A2-0AB)โ€‹

LineInstructionDescription
0A2DZ 3F06 00A4Compare R06 with button value, jump to 0A4 if condition met
0A3ZY 0000 00A8Unconditional jump to line 0A8 (state A processing path)
0A4DZ 3F05 00A6Compare R05 with button value, jump to 0A6 if condition met
0A5ZY 0000 00A4Jump back to line 0A4 (state hold)
0A6DY 0000 0090Call lottery loop subroutine starting at line 090
0A8DZ 3F07 00AACompare R07 with button value, jump to 0AA if condition met
0A9FH 0000 0000State judgment complete, return to main program
0AAFZ 0011 0000Reset R11 = 0, clear lottery counter
0ABFH 0000 0000Return to main program

Screen Display: Circle + Formatted Number (0B0-0B5)โ€‹

LineInstructionDescription
0B0FZ 0028 0210Set drawing foreground color R28 = 0x0210
0B1YXTC 8080 2000Draw filled circle at screen coordinates (0x80, 0x80) with radius 0x2000
0B2FZ 0028 FFFFSet drawing foreground color R28 = 0xFFFF (to clear/overwrite previous number)
0B3SC 0000 0000Formatted output of R00 register value to screen
0B4YS 00C8 0000Delay 200 ms
0B5FH 0000 0000Display subroutine complete, return

Color Light Rotation Effect (0C0-0CA)โ€‹

LineInstructionDescription
0C0CD 00FF 0000WS2812B LED color change: Red ON (R=FF, G=00, B=00)
0C1YS 00C8 0000Delay 200 ms
0C2CD 00FF FF00LED color change: Yellow ON (R=FF, G=FF, B=00)
0C3YS 00C8 0000Delay 200 ms
0C4CD 0000 FF00LED color change: Green ON (R=00, G=FF, B=00)
0C5YS 00C8 0000Delay 200 ms
0C6CD 00FF 00FFLED color change: Pink ON (R=FF, G=00, B=FF)
0C7YS 00C8 0000Delay 200 ms
0C8CD 0000 00FFLED color change: Blue ON (R=00, G=00, B=FF)
0C9YS 00C8 0000Delay 200 ms
0CAFH 0000 0000Color light rotation subroutine complete, return

Prize Level Judgment (0D0-0DD)โ€‹

LineInstructionDescription
0D0DYZ 0001 00D3If random number R00 > R01, jump to line 0D3 (not 1st prize)
0D1CD 00FF 00001st Prize: LED Red ON (highest prize)
0D2FH 0000 0000Return
0D3DYZ 0002 00D6If R00 > R02, jump to line 0D6 (not 2nd prize)
0D4CD 00FF FF002nd Prize: LED Yellow ON
0D5FH 0000 0000Return
0D6DYZ 0003 00D9If R00 > R03, jump to line 0D9 (not 3rd prize)
0D7CD 0000 FF003rd Prize: LED Green ON
0D8FH 0000 0000Return
0D9DYZ 0004 00DCIf R00 > R04, jump to line 0DC (not 4th prize)
0DACD 00FF 00FF4th Prize: LED Pink ON
0DBFH 0000 0000Return
0DCCD 0000 00FF5th Prize: LED Blue ON
0DDFH 0000 0000Return

Joystick-Controlled Servo (PCA9685)โ€‹

Connect the joystick to an AD channel and control servo position through the PCA9685 module. The analog value from the joystick is mapped to the servo PWM channel.

000  10  0001  000C
001 AD 0000 0000
002 1D 0000 0001
003 2A 0000 0066
004 10 0002 0003
005 EC 0002 0000
006 0E 0000 0000
LineInstructionDescription
000FZ 0001 000CSet scaling coefficient for joystick-to-servo angle mapping
001AD 0000 0000Read analog value from AD channel 0 (joystick X-axis) into R00
002RCHU 0000 0001Divide AD value by R01 to get reduced angle value
003JIA 0000 0066Add offset 0x66 (servo midpoint angle)
004FZ 0002 0003Set R02 = 3, specifying PCA9685 PWM channel 3
005WPWM 0002 0000Write R00 value to PCA9685 PWM channel specified by R02
006ZY 0000 0000Return to program start, continuously detect joystick position

Seven-Segment Display Decimal Counterโ€‹

Display incrementing decimal numbers from 0 on the seven-segment display through register value encoding.

000  10  0001  0001
001 30 0100 0000
002 16 0000 0000
003 13 0000 0001
004 12 0001 0001
005 02 0001 0001
006 0E 0000 0001
LineInstructionDescription
000FZ 0001 0001Initialize display value R01 = 1 (starting from number 1)
001BMQD 0100 0000Convert low 8 bits of R01 to seven-segment encoding, result in R00
002QF 0000 0000Bitwise NOT of encoding result (for common-anode display)
003SS 0000 0001Send R00 value to seven-segment display
004ZJ 0001 0001R01 = R01 + 1, prepare next number to display
005YS 0001 0001Delay to allow number observation
006ZY 0000 0001Jump back to line 001, continue displaying next number

๐Ÿ“š Small Fanโ€‹

Individual Pin Output Level Controlโ€‹

000  41  0000  0010
001 42 0000 0011
002 02 0005 0001
003 42 0000 0010
004 41 0000 0011
005 02 0001 0001
006 42 0000 0011
007 0F 0000 0000

Bitwise Pin Output Level Controlโ€‹

000  41  0000  0010
001 02 0005 0001
002 41 0000 0011
003 02 0001 0001
004 42 0000 0010
005 42 0000 0011
006 0F 0000 0000

Button-Controlled Small Fanโ€‹

000  10  0025  0080
001 42 0000 0010
002 0B 0000 0005
003 42 0000 0011
004 0E 0000 0002
005 41 0000 0011
006 0E 0000 0002

One-Button Toggle Small Fanโ€‹

000  10  0002  0001
001 10 0003 0000
002 06 3F02 0004
003 0E 0000 0002
004 06 3F03 0006
005 0E 0000 0004
006 06 0102 0008
007 0E 0000 0010
008 10 0001 0000
009 42 0000 0010
00A 0E 0000 0002
010 10 0001 0001
011 41 0000 0010
012 0E 0000 0002

๐Ÿ“š Value Mappingโ€‹

Control Fan Speed or LED Brightness with Analog Inputโ€‹

000  10  0001  0005
001 AD 0000 0000
002 00 0000 0000
003 00 0000 0000
004 1D 0000 0001
005 11 002A 0000
006 0E 0000 0000

PWM Control: Joystick-Controlled Fan Speed Using Full Rangeโ€‹

000  10  0001  0005
001 AD 0000 0000
002 2B 0000 09A0
003 2C 0000 0002
004 1D 0000 0001
005 11 002A 0000
006 0E 0000 0000

๐Ÿ“š Instruction Setโ€‹

Conditional Jump 0x09 Exampleโ€‹

000  10  0000  0000
001 10 0005 0008
002 12 0000 0001
003 04 0005 0010
004 05 0005 0020
005 06 0005 0030
006 02 03E8 0000
007 0E 0000 0002
010 41 0000 0013
011 0E 0000 0002
020 41 0000 0012
021 0E 0000 0002
030 41 0000 0011
031 0E 0000 0002

๐Ÿ“š Subroutine Callโ€‹

Indoor Noise Detection Systemโ€‹

000  10  0001  0BB0
001 10 0002 1300
002 AD 0000 0000
003 04 0002 0010
004 04 0001 0020
005 E4 0000 FF00
006 0E 0000 0002
010 0C 0000 0040
011 0C 0000 0040
012 0E 0000 0002
020 0C 0000 0030
021 0C 0000 0030
022 0E 0000 0002
030 E4 0000 FF00
031 02 01C4 0000
032 E4 00FF FF00
033 02 01C4 0000
034 0D 0000 0000
040 E4 00FF 0000
041 02 0001 0001
042 E4 00FF FF00
043 02 0001 0001
044 E4 00FF 00FF
045 02 0001 0001
046 0D 0000 0000

๐Ÿ“š Community Library Smart Access Control Systemโ€‹

A complete smart access control system for community libraries with:

  • Two-button control: Button A for entering, Button B for exiting
  • Capacity limit: Max 10 people
  • LED indicator: Green when space available, Red when full
  • Gate control: Servo opens/closes automatically
  • Digital display: Shows current person count

Register Mapโ€‹

RegisterPurpose
R02Servo channel select (constant = 1)
R03Servo 0ยฐ position (constant = 0x0066)
R04Servo 90ยฐ position (constant = 0x0133)
R05Button state: not pressed (constant = 0)
R06Button A pressed (constant = 1)
R07Button B pressed (constant = 2)
R11Current person count (0-10)
R12Max capacity (constant = 10)
R28Display color configuration

Subroutine Overviewโ€‹

AddressNameDescription
0x080InitRegister initialization
0x090EnterCountIncrement count when entering
0x098ExitCountDecrement count when exiting
0x0A2EnterButtonMonitor button A (enter request)
0x0A8ExitButtonMonitor button B (exit request)
0x0B0DisplayShow current count on screen
0x0C0LEDControlSet LED color based on capacity
0x0D0GateActionOpen and close the gate
000  0C  0000  0080
001 0C 0000 00B0
002 0C 0000 00C0
003 0C 0000 00A2
004 0E 0000 0001
080 10 0002 0001
081 10 0003 0066
082 10 0004 0133
083 10 0005 0000
084 10 0006 0001
085 10 0007 0002
086 10 0011 0000
087 10 0012 000A
088 0D 0000 0000
090 06 1112 0092
091 12 0011 0001
092 0C 0000 00D0
093 0D 0000 0000
098 12 1011 0001
099 0C 0000 00D0
09A 0D 0000 0000
0A2 06 3F06 00A4
0A3 0E 0000 00A8
0A4 06 3F05 00A6
0A5 0E 0000 00A4
0A6 0C 0000 0090
0A7 0D 0000 0000
0A8 06 3F07 00AA
0A9 0D 0000 0000
0AA 06 3F05 00AC
0AB 0E 0000 00AA
0AC 0C 0000 0098
0AD 0D 0000 0000
0B0 10 0028 0210
0B1 58 8080 2000
0B2 10 0028 FFFF
0B3 55 0000 1100
0B4 02 00C8 0000
0B5 0D 0000 0000
0C0 05 1112 00C3
0C1 E4 00FF 0000
0C2 0D 0000 0000
0C3 E4 0000 FF00
0C4 0D 0000 0000
0D0 EC 0002 0003
0D1 02 0001 0001
0D2 EC 0002 0004
0D3 0D 0000 0000

Operation Flowโ€‹

Start
โ”‚
โ–ผ
[Initialize Registers] โ†’ Set constants, count = 0
โ”‚
โ–ผ
[Main Loop]
โ”‚
โ”œโ”€โ–บ [Display Count] โ†’ Show current count on screen
โ”‚
โ”œโ”€โ–บ [LED Control] โ†’ Green (count < 10) / Red (count == 10)
โ”‚
โ”œโ”€โ–บ [Button A Check] โ†’ If pressed and released:
โ”‚ โ”œโ”€โ–บ Not full โ†’ count++ โ†’ [Gate Action] โ†’ open/close gate
โ”‚ โ””โ”€โ–บ Full โ†’ [Gate Action] โ†’ open/close gate (still allow entry?)
โ”‚
โ”œโ”€โ–บ [Button B Check] โ†’ If pressed and released:
โ”‚ โ””โ”€โ–บ count-- โ†’ [Gate Action] โ†’ open/close gate
โ”‚
โ””โ”€โ–บ Loop back to display

Key Featuresโ€‹

  1. Debounce Handling: Each button waits for release before processing, preventing multiple triggers
  2. Capacity Control: Library max capacity is 10 people (R12 = 10)
  3. Visual Feedback: LED shows green when space available, red when full
  4. Automatic Gate: Servo opens (90ยฐ) for 1 second, then closes (0ยฐ)
  5. Digital Display: Shows current count as 2-digit number

Testing Scenariosโ€‹

ActionExpected Result
Press Button A (count=0)Count becomes 1, gate opens then closes, LED green
Press Button A (count=9)Count becomes 10, gate opens then closes, LED red
Press Button A (count=10)Gate opens then closes, count remains 10, LED red
Press Button B (count=1)Count becomes 0, gate opens then closes, LED green
Press Button B (count=0)Count remains 0, gate opens then closes, LED green