Lesson 1: Understanding Binary - From 0 and 1 to the Computer World
๐ Learning Objectivesโ
- Understand what binary is
- Master conversion between decimal and binary
- Understand the role of binary in microcontrollers
- Experience practical applications of binary through experiments
๐ฏ What is Binary?โ
Review of Decimalโ
We use decimal in daily life, which has 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Rule: Carry over when reaching 10
15
+ 8
----
23
Introduction to Binaryโ
Computers use binary, which has only 2 digits: 0 and 1.
Rule: Carry over when reaching 2
101
+ 1
----
110
๐ Decimal โ Binary Conversionโ
Decimal โ Binary: Divide by 2, Collect Remaindersโ
Example: Convert decimal 13 to binary
13 รท 2 = 6 remainder 1 โ least significant bit
6 รท 2 = 3 remainder 0
3 รท 2 = 1 remainder 1
1 รท 2 = 0 remainder 1 โ most significant bit
Result: 1101
Binary โ Decimal: Weighted Sumโ
Example: Convert binary 1011 to decimal
1 0 1 1
โ โ โ โ
2ยณ 2ยฒ 2ยน 2โฐ
8 4 2 1
8 + 0 + 2 + 1 = 11
๐ก The Meaning of Binary in Microcontrollersโ
1. Representing Switch Statesโ
0= OFF1= ON
2. Representing Logicโ
0= False1= True
3. Register Storageโ
Microcontroller registers store data in binary!
๐ฌ Classroom Demo: Binary LED Arrayโ
Experiment Goalโ
Use 4 LEDs to represent a 4-bit binary number, counting from 0 to 15.
Hardware Connectionsโ
| LED | Connection | Binary Bit |
|---|---|---|
| LED1 | Output 0 | 2โฐ (1) |
| LED2 | Output 1 | 2ยน (2) |
| LED3 | Output 2 | 2ยฒ (4) |
| LED4 | Output 3 | 2ยณ (8) |
Example Codeโ
; Count from 0 to 15, display binary with LEDs
000 01 0000 0000 ; 0 (0000)
001 02 03E8 0000 ; delay
002 01 0001 0000 ; 1 (0001)
003 02 03E8 0000 ; delay
004 01 0002 0000 ; 2 (0010)
005 02 03E8 0000 ; delay
006 01 0003 0000 ; 3 (0011)
007 02 03E8 0000 ; delay
008 0E 0000 0000 ; loop
Teaching Suggestionsโ
Ask students to guess what the next number will be, or let them design their own counting program.
๐ Common Decimal-Binary Conversion Tableโ
| Decimal | Binary | Hex |
|---|---|---|
| 0 | 0000 | 0x00 |
| 1 | 0001 | 0x01 |
| 2 | 0010 | 0x02 |
| 3 | 0011 | 0x03 |
| 4 | 0100 | 0x04 |
| 5 | 0101 | 0x05 |
| 8 | 1000 | 0x08 |
| 10 | 1010 | 0x0A |
| 15 | 1111 | 0x0F |
| 16 | 0001 0000 | 0x10 |
| 255 | 1111 1111 | 0xFF |
โ Quick Quizโ
- What is decimal 7 in binary?
- What is binary 1101 in decimal?
- What's the maximum value of an 8-bit binary number? (Answer: 255)
๐ Summaryโ
What we learned today:
- โ Binary only has 0 and 1
- โ Can convert using divide-by-2 and weighted sum methods
- โ Binary represents switch states and logic values
- โ LED arrays can visually demonstrate binary
Next lesson, we'll learn about registers and see how binary is stored in microcontrollers!