06. 8051 Architecture · SFR · reg52.h
On the foundation of lessons 1–5 we finally step onto a real chip — the classic 8051 (AT89C52). Its peripherals (ports·timers·serial·interrupts) are all controlled through SFRs (Special Function Registers). Keil C51's <reg52.h> pre-declares the SFRs by name, so we touch hardware as if assigning to a variable: P1 = 0x0F;. We learn the two ways to write a port (whole port / sbit single bit).
What you'll learn
- 1Outline the 8051 structure (CPU·ROM/RAM·ports·timer·serial·interrupts)
- 2Explain what an SFR is and where it lives (0x80–0xFF)
- 3Understand how <reg52.h> exposes SFRs by name
- 4Use whole-port writes (P1 = ...) and bit writes (sbit)
- 5Know the C51-only keywords sfr·sbit·bit
Introduction
The 8051 is simple with few registers, making it the best chip to learn "how C touches hardware". This track's 8051 examples are verified in μVision's built-in simulator (Peripherals → I/O-Ports); no real board needed.
Key concepts
1) 8051 at a glance
| Resource | Detail |
|---|---|
| CPU | 8-bit, accumulator (ACC) centric |
| Memory | code ROM (8KB), 256B internal RAM + SFR |
| I/O | four 8-bit ports P0–P3 |
| Timer/Serial | Timer0/1(+T2), one UART |
On 8051, int is 16-bit (lesson 2). Use unsigned char (uint8_t) for 8-bit values to save RAM and code size.
2) SFR — Special Function Register
SFRs control peripherals and live in internal RAM 0x80–0xFF. SFRs whose address is a multiple of 8 (0x80·0x88·0x90…) — P0–P3·TCON·SCON·IE — are bit-addressable via sbit.
| SFR | Addr | Role |
|---|---|---|
| P1 | 0x90 | port 1 |
| TMOD | 0x89 | timer mode |
| SCON | 0x98 | serial control |
| IE | 0xA8 | interrupt enable |
3) <reg52.h> and two ways to write a port
#include <reg52.h> /* pre-declares sfr P1 = 0x90; etc */
P1 = 0x0F; /* (1) whole-port write: low 4 bits = 1 */
sbit LED0 = P1 ^ 0; /* (2) sbit: name bit 0 of P1 (file scope) */
LED0 = 1; /* only P1.0 set, others unchanged */The ^ in `P1 ^ 0` isn't XOR — it's C51 notation for "bit 0 of P1". sfr·sbit·bit are Keil C51 extensions not in standard C, so this main.c won't compile with PC gcc (verify logic in pc_test.c).
Core example
Verify the final value after a whole-port write plus single-bit edits on PC (mimicking the SFR with a plain variable).
#include <stdio.h>
int main(void) {
unsigned char P1 = 0x0F; /* whole-port write */
P1 = (unsigned char)(P1 & ~(1u << 0)); /* LED0 = 0 */
P1 = (unsigned char)(P1 | (1u << 7)); /* LED7 = 1 */
printf("P1 = 0x%02X\n", P1); /* 0x8E */
return 0;
}Simulator: in Peripherals → I/O-Ports → Port 1, confirm P1 = 0x8E (1000 1110).
Common mistakes
Q. gcc says it can't find reg52.h.
A. <reg52.h>·sfr·sbit are Keil C51-only. The device main.c builds only in μVision. On PC, verify logic with pc_test.c (standard C).
Q. sbit LED = P1 ^ 0; inside a function errors out.
A. sfr/sbit declarations must be at file scope (global). Declare them above main.
Q. A large counter with int misbehaves.
A. int is 16-bit on 8051. Use unsigned char for 8-bit port values/counters and name unsigned int(16)/unsigned long(32) for larger numbers.
Summary
- The 8051 is a simple 8-bit MCU controlling peripherals via SFRs (0x80–0xFF)
- <reg52.h> provides SFRs like P1·TMOD·IE by name
- Write a port whole (P1 = 0x0F) or by bit (sbit)
- sfr·sbit·bit are C51-only — not for PC gcc (verify logic in pc_test.c)
- Use unsigned char for 8-bit values to save resources
Exercises
- Predict the result of writing 0xAA to P1 then flipping P1.0 and P1.7 via sbit, and verify in pc_test
- Set only the low 4 bits of P2 while preserving the high 4 bits, using a mask
- Create an AT89C52 empty project in μVision and watch P1 in the Port 1 window
All lecture materials and example code (with homework and answers) are openly available on GitHub.
View on GitHub ↗