Lesson 3: IO Input/Output - Let the Microcontroller Talk to the World
đ Learning Objectivesâ
- Understand what IO ports are
- Learn to configure IO as input or output
- Master digital output instructions
- Master digital input instructions
- Build a button-controlled LED project
đ¯ What is IO?â
IO is short for Input/Output.
- Input: Microcontroller reads information from the outside world (buttons, sensors, etc.)
- Output: Microcontroller sends information to the outside world (LEDs, buzzers, etc.)
đ§ Configuring IO Portsâ
Use register R25 to configure each IO port:
| R25 Value | Description |
|---|---|
0000 | All set to output |
00FF | All set to input |
00F0 | Lower 4 bits output, upper 4 bits input |
000F | Lower 4 bits input, upper 4 bits output |
đĄ Digital Output (0x01)â
Control output devices like LEDs and buzzers.
Instruction Formatâ
| Instruction | OP00 | OP01 | OP10 | OP11 |
|---|---|---|---|---|
01 | 00 | Output pin (00~07) or FF (bitwise) | 00 | Output value |
蝴æīŧ
- Single pin output: OP01 = 00
07 (specify LED0LED7), OP11 = 00 (ON) or 01 (OFF) - Bitwise output: OP01 = FF, OP11 = 00~FF (each bit corresponds to an LED state)
Example: Control 8 LEDsâ
- đ With Comments
- đ Clean Code
000 10 0019 0000 ; R25 = 0x0000 â All set to output
001 01 00FF 0001 ; 1st LED on (bitwise)
002 02 03E8 0000 ; delay
003 01 00FF 0002 ; 2nd LED on (bitwise)
004 02 03E8 0000 ; delay
005 01 00FF 0004 ; 3rd LED on (bitwise)
006 02 03E8 0000 ; delay
007 01 00FF 0008 ; 4th LED on (bitwise)
008 02 03E8 0000 ; delay
009 0E 0001 0000 ; loop back to 001
000 10 0019 0000
001 01 00FF 0001
002 02 03E8 0000
003 01 00FF 0002
004 02 03E8 0000
005 01 00FF 0004
006 02 03E8 0000
007 01 00FF 0008
008 02 03E8 0000
009 0E 0001 0000
đ Digital Input Jump (0x0B)â
Read input devices like buttons and switches, and jump based on state.
Instruction Formatâ
| Instruction | OP00 | OP01 | OP10 | OP11 |
|---|---|---|---|---|
0B | Condition | Jump address | 00 | 00 |
đŦ Classroom Demo Project: Button Controlled LEDâ
Project Goalâ
LED on when button pressed, LED off when button released.
Hardware Connectionsâ
- Button: Connected to IO0 (input)
- LED: Connected to IO4 (output)
Reference Programâ
- đ With Comments
- đ Clean Code
; Button-controlled LED project
000 10 0019 0001 ; R25 = 0x0001 â IO0 input, others output
001 0B 0001 0004 ; If IO0 is 1 (button released), jump to 004
002 01 0010 0000 ; LED on
003 0E 0001 0000 ; jump back
004 01 0000 0000 ; LED off
005 0E 0001 0000 ; jump back
000 10 0019 0001
001 0B 0001 0004
002 01 0010 0000
003 0E 0001 0000
004 01 0000 0000
005 0E 0001 0000
Teaching Suggestionsâ
- Add delays to make the LED blink when pressed
- Use multiple buttons to control multiple LEDs
- Try toggling state with a single press instead of holding
đĄ Understanding IO Output Dataâ
Each bit of output data corresponds to one IO port:
| Data Bit | Corresponding IO |
|---|---|
| Bit 0 | IO0 |
| Bit 1 | IO1 |
| Bit 2 | IO2 |
| ... | ... |
| Bit 7 | IO7 |
Examples:
0001â IO0 outputs high0003â IO0 and IO1 output high00FFâ All IO ports output high
đ Summaryâ
What we learned today:
- â IO = Input/Output
- â Configure IO direction with R25
- â Instruction 0x01 outputs data
- â Instruction 0x0B jumps based on input
- â Completed button-controlled LED project
Next lesson, we'll learn about PWM to make LEDs breathe!