Skip to main content

Lesson 3: IO Input/Output - Let the Microcontroller Talk to the World

📌 Learning Objectives​

  1. Understand what IO ports are
  2. Learn to configure IO as input or output
  3. Master digital output instructions
  4. Master digital input instructions
  5. 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 ValueDescription
0000All set to output
00FFAll set to input
00F0Lower 4 bits output, upper 4 bits input
000FLower 4 bits input, upper 4 bits output

💡 Digital Output (0x01)​

Control output devices like LEDs and buzzers.

Instruction Format​

InstructionOP00OP01OP10OP11
0100Output pin (00~07) or FF (bitwise)00Output value

č¯´æ˜Žīŧš

  • Single pin output: OP01 = 0007 (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​

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

🔍 Digital Input Jump (0x0B)​

Read input devices like buttons and switches, and jump based on state.

Instruction Format​

InstructionOP00OP01OP10OP11
0BConditionJump address0000

đŸ”Ŧ 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​

; 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

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 BitCorresponding IO
Bit 0IO0
Bit 1IO1
Bit 2IO2
......
Bit 7IO7

Examples:

  • 0001 → IO0 outputs high
  • 0003 → IO0 and IO1 output high
  • 00FF → 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!