Lesson 2: Understanding Registers - The Microcontroller's "Lockers"
đ Learning Objectivesâ
- Understand what registers are
- Learn to read and write registers
- Understand the concept of register addresses
- 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:
| Register | Address | Purpose |
|---|---|---|
| R0 | 0x00 | General purpose |
| R1 | 0x01 | General purpose |
| R25 | 0x19 | IO 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:
| Instruction | OP00 | OP01 | OP10 | OP11 |
|---|---|---|---|---|
10 | Register address | Data | 00 | 00 |
Example: Write 0x00FF to register R0
- đ With Comments
- đ Clean Code
000 10 0000 00FF ; R0 = 0x00FF
000 10 0000 00FF
2. Read from Register (0x14)â
Read IO data into a register:
| Instruction | OP00 | OP01 | OP10 | OP11 |
|---|---|---|---|---|
14 | Target register | 00 | 00 | 00 |
đ§Š Common Registersâ
1. IO Configuration Register (R25, 0x19)â
Used to set whether IO ports are input or output:
| R25 Value | Description |
|---|---|
0000 | All set to output |
00FF | All set to input |
000F | Lower 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â
- đ With Comments
- đ Clean 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
000 10 0000 00AA
001 11 0000 0001
002 11 0001 0002
003 12 0002 0001
004 01 00FF 0000
005 02 03E8 0000
006 0E 0003 0000
đ Quick Exercisesâ
- Write an instruction to store
0x1234in R5 - Write an instruction to copy R5's value to R6
- 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!