Skip to main content

Lesson 2: Understanding Registers - The Microcontroller's "Lockers"

📌 Learning Objectives​

  1. Understand what registers are
  2. Learn to read and write registers
  3. Understand the concept of register addresses
  4. Master the purpose of common JoyKit registers

đŸŽ¯ What is a Register?​

A Useful Analogy​

Imagine your desk has a row of drawers, where each drawer:

  • Has its own number (address)
  • Can store things (data)
  • Can be accessed to put in or take out items (read/write)

Registers are the microcontroller's "drawers" for temporarily storing data!


đŸ“Ļ Basic Properties of Registers​

1. Address​

Each register has a unique address, represented in hexadecimal:

RegisterAddressPurpose
R00x00General purpose
R10x01General purpose
R250x19IO configuration
.........

2. Bit Width​

JoyKit registers are 16-bit, with data range:

  • Minimum: 0000 (hex) = 0
  • Maximum: FFFF (hex) = 65535

🔧 Register Read/Write Instructions​

1. Write to Register (0x10)​

Write data to a specified register:

InstructionOP00OP01OP10OP11
10Register addressData0000

Example: Write 0x00FF to register R0

000 10 0000 00FF  ; R0 = 0x00FF

2. Read from Register (0x14)​

Read IO data into a register:

InstructionOP00OP01OP10OP11
14Target register000000

🧩 Common Registers​

1. IO Configuration Register (R25, 0x19)​

Used to set whether IO ports are input or output:

R25 ValueDescription
0000All set to output
00FFAll set to input
000FLower 4 bits input, upper 4 bits output

2. System Registers​

  • R0-R15: General purpose registers, freely usable
  • R25: IO configuration (just discussed)
  • R31-R33: LED color data (R31=Red, R32=Green, R33=Blue)

đŸ”Ŧ Classroom Demo: Register Value "Relay"​

Experiment Goal​

Demonstrate data transfer between registers.

Program Code​

; Register data relay experiment
000 10 0000 00AA ; R0 = 0xAA
001 11 0000 0001 ; R1 = R0 (R1 = 0xAA)
002 11 0001 0002 ; R2 = R1 (R2 = 0xAA)
003 12 0002 0001 ; R2 += 1 (R2 = 0xAB)
004 01 00FF 0000 ; Display R2 value with LEDs
005 02 03E8 0000 ; delay
006 0E 0003 0000 ; loop

📝 Quick Exercises​

  1. Write an instruction to store 0x1234 in R5
  2. Write an instruction to copy R5's value to R6
  3. Write an instruction to increment R6 by 1

🎉 Summary​

What we learned today:

  • ✅ Registers are the microcontroller's "lockers"
  • ✅ Each register has an address
  • ✅ Use instruction 0x10 to write to registers
  • ✅ R25 configures IO input/output
  • ✅ Data can be transferred between registers

Next lesson, we'll learn about IO Input/Output to control real hardware!