Contents
Chapter 1 Introduction to Computer and Microcontroller 1
1.1 Objectives 1
1.2 A Brief History of the Computer 1
1.2.1 Mainframe Computer 3
1.2.2 Minicomputer 4
1.2.3 Microcomputers 5
1.2.4 Supercomputers 6
1.3 Computer Hardware Organization 8
1.4 The Processor 9
1.4.1 The Arithmetic Logic Unit 9
1.4.2 Registers 11
1.4.3 The Control Unit 11
1.4.4 The Language Issue 12
1.5 The Microprocessor 13
1.6 Microcontroller 14
1.7 Embedded System 15
1.7.1 Characteristics of Embedded Systems 16
1.7.2 User Interfaces 16
1.8 Wireless Sensor Network (WSN) 17
1.9 Internet of Things (IoT) 18
1.10 Memory 19
1.10.1 Magnetic Memory 19
1.10.2 Optical Memory 20
1.10.3 Semiconductor Memory 20
1.11 Memory System Operation 24
1.11.1 Read Operation 25
1.11.2 Write Operation 26
1.12 Program Execution 26
1.12.1 The Program Counter Circuit 27
1.12.2 Starting Program Execution 28
1.12.3 Instruction Execution Process 29
1.13 Overview of the 8051 Microcontroller 29
1.14 Summary 31
1.15 Exercises 33
Chapter 2 C Language Programming 34
2.1 Objectives 34
2.2 Introduction to C 34
2.3 Types, Operators, and Expressions 36
2.3.1 Data Types 37
2.3.2 Variable Declaration 37
2.3.3 Constants 37
2.3.4 Arithmetic Operators 38
2.3.5 Bitwise Operators 40
2.3.6 Relational and Logical Operators 41
2.3.7 Precedence of Operators 42
2.4 Control Flow 43
2.4.1 If Statement 43
2.4.2 If-Else Statement 43
2.4.3 Multiway Conditional Statement 44
2.4.4 Switch Statement 45
2.4.5 For-Loop Statement 46
2.4.6 While Statement 46
2.4.7 Do-While Statement 47
2.4.8 Goto Statement 47
2.5 Input and Output 48
2.6 Functions and Program Structure 50
2.6.1 Function Prototype 52
2.6.2 Writing a C Program with Multiple Functions 53
2.7 Pointers, Arrays, Structures, Unions, and Type Definition 54
2.7.1 Pointers and Addresses 54
2.7.2 Arrays 56
2.7.3 Pointers and Arrays 57
2.7.4 Passing Arrays to a Function 58
2.7.5 Initializing Arrays 59
2.7.6 Structures 60
2.7.7 Unions 62
2.7.8 The typedef Statement 63
2.7.9 Enumerated Data Types 65
2.8 Miscellaneous Items 66
2.8.1 Automatic/External/Static/Volatile 66
2.8.2 Scope Rules 67
2.8.3 Type Casting 68
2.8.4 Pointer to Functions 70
2.9 The C Preprocessor 71
2.9.1 The #define Statement 71
2.9.2 The ## Operator 74
2.9.3 The #include Statement 75
2.9.4 Conditional Compilation 76
2.10 Programming Style 77
2.10.1 General Guidelines to Comments 77
2.10.2 Program Documentation 78
2.10.3 Function Documentation 78
2.10.4 Code Appearance 79
2.10.5 Naming of Variables, Constants, and Functions 81
2.11 C8051 Memory Map 81
2.11.1 C8051 Program Memory 82
2.11.2 C8051 Data Memory 82
2.12 Accessing C8051 Peripheral Registers 83
2.12.1 Using Symbolic Names to Access Peripheral Registers 83
2.12.2 Using Symbolic Names to Access Bits 85
2.13 Hardware and Software Development Tools 88
2.13.1 Hardware Development Tools 88
2.13.2 Software Development Tools 90
2.14 Summary 90
2.15 Exercises 92
2.16 Lab Assignments 93
Chapter 3 Parallel Port 94
3.1 Objectives 94
3.2 Introduction to I/O Port 94
3.3 STC8A8K64S4A12 I/O Ports 95
3.3.1 I/O Pin Operation Modes 95
3.3.2 Number of Ports 98
3.3.3 Port Pin Configuration 100
3.4 Electrical Characteristics Compatibility Consideration for I/O Interfacing 102
3.4.1 Voltage-Level Compatibility 102
3.4.2 Current Drive Capability 104
3.4.3 Timing Compatibility 106
3.5 Simple I/O Devices 107
3.5.1 Time Delay Issue 107
3.5.2 Driving the Light-Emitting Diodes (LEDs) 108
3.5.3 Driving Seven-Segment Displays 110
3.5.4 Generating a Digital Waveform Using an I/O Pin 113
3.5.5 Making a Sound Using an I/O Pin 115
3.5.6 Interfacing with DIP Switches 116
3.6 Interfacing a Parallel Port to a Keypad 117
3.6.1 Keypad Scanning 118
3.6.2 Keyboard Debouncing 119
3.6.3 ASCII Code Lookup 121
3.7 Liquid Crystal Displays (LCDs) 122
3.7.1 The Structure of the HD44780 LCD Controller 123
3.7.2 LCD Instructions 125
3.7.3 LCD Instruction Descriptions 127
3.7.4 Interfacing the HD44780 to the STC8A8K64SA12 129
3.7.5 LCD Startup Sequence 132
3.8 Interfacing with a Digital-to-Analog Converter (DAC) 135
3.8.1 The AD7302 DAC 136
3.8.2 Interfacing the AD7302 with the STC8A8K64S4A12 137
3.9 Summary 140
3.10 Exercises 141
3.11 Lab Assignments 144
Chapter 4 Clock Generation, Interrupts, and Resets 146
4.1 Objectives 146
4.2 Introduction to Clock Signal 146
4.2.1 What Is a Clock Signal? 146
4.2.2 Waveform of a Clock Signal 147
4.2.3 Circuits for Generating Clock Signals 147
4.2.4 Gating Clock Signals 147
4.2.5 Multiplying and Scaling Clock Frequency 147
4.3 The STC8A8K64S4A12’s Clock System 148
4.3.1 Internal 24-MHz RC Oscillator 148
4.3.2 External Crystal Oscillator 149
4.3.3 Internal 32-KHz RC Oscillator 149
4.4 Generating the System Clock 150
4.5 Basic Concepts of Interrupts 152
4.6 Benefits of Interrupts 153
4.7 Enabling and Disabling Interrupts 153
4.8 Prioritizing Multiple Pending Interrupts 154
4.9 Servicing Interrupts 154
4.10 Interrupt Vector 155
4.11 Interrupt Nesting 155
4.12 Resets 156
4.13 STC8A8K64S4A12 Interrupt Model 156
4.13.1 The Interrupt Sources of STC8A8K64S4A12 157
4.13.2 The STC8A8K64S4A12 Interrupt Enabling 157
4.13.3 The STC8A8K64S4A12 Interrupt Priority 160
4.13.4 Interrupt Flags 162
4.13.5 External I/O Pin Interrupts 163
4.14 Writing Interrupt Service Routine 164
4.15 The 8051 Reset 168
4.15.1 The RST Pin Reset 168
4.15.2 Power-On Reset 168
4.15.3 Power-Failure Reset 168
4.15.4 Watchdog Timer Reset 169
4.15.5 Software Reset 171
4.16 Summary 171
4.17 Exercises 173
4.18 Lab Assignments 174
Chapter 5 Timer Functions 176
5.1 Objectives 176
5.2 Introduction to Microcontroller Timer System 176
5.2.1 Using Timer Function to Create Time Delays 177
5.2.2 Input Capture 177
5.2.3 Output Compare 179
5.2.4 Pulse Width Modulation (PWM) 179
5.3 Introduction to STC8A8K64S4A12 Timer Functions 180
5.4 Timer 0 and Timer 1 182
5.5 Timer 2 184
5.6 Timer 3/4 185
5.7 Timer Applications 186
5.7.1 Time Delay Creation 186
5.7.2 Pulse Width Measurement 190
5.7.3 Frequency Measurement 192
5.7.4 Waveform Generation 193
5.7.5 Siren Generation 194
5.7.6 Playing a Song 196
5.8 Programmable Counter Array (PCA) 201
5.8.1 PCA Module Registers 202
5.8.2 PCA Operation Mode 204
5.8.3 Capture Mode 204
5.8.4 16-bit Software Timer and High-Speed Output Modes 207
5.8.5 Pulse Width Modulator Mode (PWM) 213
5.9 DC Motor Control 219
5.9.1 DC Motor Driver ICs 220
5.9.2 Driving a DC Motor Using the L293D 221
5.10 Summary 223
5.11 Exercises 224
5.12 Lab Assignments 225
Chapter 6 Universal Asynchronous Receiver Transmitter (UART) 228
6.1 Objectives 228
6.2 Concepts of Serial I/O 229
6.3 The RS-232 Standard 230
6.3.1 TIA-232 Electrical Specification 231
6.3.2 TIA-232 Functional Specification 232
6.3.3 TIA-232 Mechanical Specification 235
6.3.4 TIA-232 Procedural Specification 236
6.3.5 Data Format 239
6.3.6 UART Detectable Errors 241
6.3.7 Null Modem Connection 241
6.4 The STC8A8K64S4A12 UART Modules 241
6.5 UART1 242
6.5.1 Operation Mode 242
6.5.2 UART1 Mode 0 243
6.5.3 UART1 Mode 1 244
6.5.4 UART1 Mode 2 246
6.5.5 UART1 Mode 3 247
6.5.6 Automatic Address recognition 248
6.6 UART2/UART3/UART4 248
6.6.1 UARTn Mode 0 (n = 2, 3, 4) 251
6.6.2 UARTn Mode 1 (n = 2, 3, 4) 251
6.7 Applications of the UART1 Mode 0 252
6.8 Data Exchange with UART Asynchronous Mode via the TIA-232 Interface 257
6.9 Number Conversion 266
6.10 Multiprocessor Applications of UART 268
6.11 Terminal and Terminal Emulation 270
6.12 Summary 279
6.13 Exercises 280
6.14 Lab Assignments 282
Chapter 7 Serial Peripheral Interface (SPI) 284
7.1 Objectives 284
7.2 Introduction to SPI 284
7.2.1 Device Types 285
7.2.2 SPI Signals 285
7.2.3 SPI Data Transmission 285
7.2.4 Clock Polarity and Phase 286
7.2.5 SPI Bus Connection Methods 287
7.3 The STC8A8K64S4A12 SPI Module 288
7.4 The STC8A8K64S4A12 SPI Operation Modes 290
7.4.1 Single Master and Single Slave Mode 290
7.4.2 Dual Devices Configuration Mode 290
7.4.3 Single Master and Multiple Slaves Mode 291
7.4.4 Configuring SPI for Data Transfer 291
7.5 Writing SPI Functions for Data Transfer 293
7.6 Use I/O Pins to Simulate SPI Data Transmission 294
7.7 Examples of Peripheral Chips with SPI Interface 297
7.7.1 The 74HC595 Shift Register 297
7.7.2 The TC72 Digital Temperature Sensor 301
7.7.3 The 25LC256—An EEPROM with SPI Interface 307
7.7.4 The MCP4922 Digital-to-Analog Converter 312
7.8 Summary 317
7.9 Exercises 318
7.10 Lab Assignments 320
Chapter 8 Inter-Integrated Circuit ( I²C ) 321
8.1 Objectives 321
8.2 Introduction to the I²C Interface 321
8.2.1 Data Transmission in I²C Bus 323
8.2.2 I²C Signal Components 323
8.2.3 I²C Data Transfer Format 326
8.2.4 Issues in I²C Data Transmission 330
8.2.5 Pull-up Resistor (RP) Calculation 332
8.3 The Features of the STC8A8K64S4A12 I²C 335
8.3.1 Registers Related to Master Mode 335
8.3.2 Registers Related to Slave Mode 336
8.4 I2C Master Clock Timing 338
8.5 Data Transmission & Reception in Master Mode 340
8.6 Data Transmission & Reception in Slave Mode 343
8.7 Interfacing with AT24LC64B Serial EEPROM Using the I2C 344
8.7.1 Pin Assignment of the AT24LC64B 345
8.7.2 Device Addressing of AT24LC64B 345
8.7.3 Write Operations 346
8.7.4 The AT24LC64B Acknowledge Polling 347
8.7.5 The AT24LC64B Read Operation 347
8.8 Interfacing with I2C Real-Time Clock MCP7940N 351
8.8.1 Signal Functions 352
8.8.2 Registers MAP 352
8.8.3 Crystal Oscillator 352
8.8.4 Timekeeping 352
8.8.5 Alarms 356
8.8.6 MFP Output Configuration 356
8.8.7 Battery Backup 358
8.8.8 I2C Transmission and Reception with MCP7940N 359
8.9 Using the Digital Thermostat DS1631A 363
8.9.1 Pin Assignment 364
8.9.2 Functional Description 364
8.9.3 DS1631A Registers 365
8.9.4 The DS1631A Operation 365
8.9.5 DS1631A Command Set 367
8.9.6 Interfacing the DS1631A with the STC8A8K64S4A12 368
8.10 Summary 374
8.11 Exercises 376
8.12 Lab Assignments 378
Chapter 9 Analog to Digital Converter 379
9.1 Objectives 379
9.2 Basics of A/D Conversion 379
9.2.1 A Data Acquisition System 380
9.2.2 Analog Voltage and Digital Code Characteristic 381
9.2.3 A/D Conversion Algorithm 382
9.2.4 Interpreting A/D Conversion Result 384
9.2.5 Voltage Amplifying Circuit 385
9.2.6 Voltage Translation Circuit 386
9.3 The STC8A8K64S4A12 A/D Conversion 387
9.3.1 ADC Channels 387
9.3.2 Starting the A/D Conversion 388
9.3.3 ADC Registers 388
9.3.4 Reference Voltage 389
9.3.5 ADC Interrupt 389
9.4 Applications of ADC 390
9.4.1 Construct a Digital Voltmeter 390
9.4.2 Using the Temperature Sensor TC1047A 392
9.4.3 Measuring Barometric Pressure 396
9.4.4 Measuring Humidity 402
9.5 Summary 406
9.6 Exercises 406
9.7 Lab Assignments 408
Appendix A The STC8A8K64S4A12 Header File (stc8.h) 409
Appendix B Music Note Frequencies 425
Appendix C Tutorial on Using the Keil μVision IDE 429
References 454
中文简明目录
第1章 计算机和微控制器介绍 1
1.1 学习目标 1
1.2 计算机简史 1
1.3 计算机硬件组成 8
1.4 处理器 9
1.5 微处理器 13
1.6 微控制器 14
1.7 嵌入式系统 15
1.8 无线传感网络(WSN) 17
1.9 物联网(IoT) 18
1.10 存储器 19
1.11 存储系统工作原理 24
1.12 程序执行 26
1.13 微控制器概述 29
1.14 小结 31
1.15 习题 33
第2章 C语言编程 34
2.1 学习目标 34
2.2 C语言介绍 34
2.3 数据类型、操作数和表达式 36
2.4 控制流 43
2.5 输入和输出 48
2.6 函数和程序结构 50
2.7 指针、数组、结构体、共用体和类型定义 54
2.8 杂项 66
2.9 C语言预处理器 71
2.10 编程风格 77
2.11 C8051存储器映射 81
2.12 访问C8051外设寄存器 83
2.13 软硬件开发工具 88
2.14 小结 90
2.15 习题 92
2.16 实验 93
第3章 并行端口 94
3.1 学习目标 94
3.2 I/O端口介绍 94
3.3 STC8A8K64S4A12单片机 I/O端口 95
3.4 I/O接口电气特性的兼容性 102
3.5 简单I/O端口器件 107
3.6 并行端口与键盘的接口 117
3.7 液晶屏显示器(LCDs) 122
3.8 数模转换器接口(DAC) 135
3.9 小结 140
3.10 习题 141
3.11 实验 144
第4章 时钟生成、中断及复位 146
4.1 学习目标 146
4.2 时钟信号介绍 146
4.3 STC8A8K64S4A12单片机时钟系统 148
4.4 系统时钟生成 150
4.5 中断的基本概念 152
4.6 中断的作用 153
4.7 使能和禁用中断 153
4.8 多个待处理中断的优先级 154
4.9 中断服务 154
4.10 中断向量 155
4.11 中断嵌套 155
4.12 复位 156
4.13 STC8A8K64S4A12单片机的中断类型 156
4.14 编写中断服务程序 164
4.15 8051单片机的复位 168
4.16 小结 171
4.17 习题 173
4.18 实验 174
第5章 定时器功能 176
5.1 学习目标 176
5.2 微控制器定时器系统介绍 176
5.3 STC8A8K64S4A12单片机定时器功能介绍 180
5.4 定时器0和定时器1 182
5.5 定时器2 184
5.6 定时器3/4 185
5.7 定时器应用 186
5.8 可编程计数器阵列(PCA) 201
5.9 直流马达控制 219
5.10 小结 223
5.11 习题 224
5.12 实验 225
第6章 通用异步收发传输器(UART) 228
6.1 学习目标 228
6.2 串行I/O端口的概念 229
6.3 RS-232标准 230
6.4 STC8A8K64S4A12单片机UART模块 241
6.5 UART1 242
6.6 UART2/UART3/UART4 248
6.7 UART1模块0的应用 252
6.8 通过TIA-232接口使用UART异步模式进行数据交换 257
6.9 数值转换 266
6.10 UART的多处理器应用 268
6.11 终端和终端仿真 270
6.12 小结 279
6.13 习题 280
6.14 实验 282
第7章 串行外设接口(SPI) 284
7.1 学习目标 284
7.2 SPI介绍 284
7.3 STC8A8K64S4A12单片机SPI模块 288
7.4 STC8A8K64S4A12单片机SPI工作模式 290
7.5 编写SPI函数进行数据传输 293
7.6 使用I/O引脚实现SPI数据传输 294
7.7 使用SPI连接外设芯片的实例 297
7.8 小结 317
7.9 习题 318
7.10 实验 320
第8章 I2C总线 321
8.1 学习目标 321
8.2 I2C总线介绍 321
8.3 STC8A8K64S4A12单片机I2C总线的特点 335
8.4 I2C总线主机时钟定时 338
8.5 主机模式下的数据传输和接收 340
8.6 从机模式下的数据传输和接收 343
8.7 使用I2C总线和串行EEPROM AT24LC64B接口 344
8.8 使用I2C总线和实时时钟MCP7940N接口 351
8.9 使用I2C总线和数字恒温器DS1631A接口 363
8.10 小结 374
8.11 习题 376
8.12 实验 378
第9章 模数转换器 379
9.1 学习目标 379
9.2 模数转换基础 379
9.3 STC8A8K64S4A12单片机模数转换器 387
9.4 模数转换器的应用 390
9.5 小结 406
9.6 习题 406
9.7 实验 408
附录A STC8A8K64S4A12单片机头文件 409
附录B 音符频率对照表 425
附录C Keil mVision IDE使用教程 429
参考文献 454
发表评论