STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
STC12C5620AD系列单片机器件手册
---1个时钟/机器周期8051
---�������������
超强加����������
密���������
,有全球唯一ID号
---高速,高可靠
---低功耗,超低价
---强抗静电,强抗干扰
STC12C5604AD,
STC12C5608AD,
STC12C5612AD,
STC12C5616AD,
STC12C5620AD,
STC12C5624AD,
STC12C5628AD,
STC12C5630AD,
C
T
S
U
C
M
.
d
e
t
i
m
STC12C5604
STC12C5608
STC12C5612
STC12C5616
STC12C5620
STC12C5624
STC12C5628
STC12C5630
Li
全部中国本土独立自主知识产权,请全体中国人民支持,您的
支持是中国本土����������
力量前进的�����
有力保证.
STC-ISP:最方便的在线升级软件
技术支持���
网站: www.STCMCU.com
Update date: 2011/10/30
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
目录
第1章 STC12C5620AD系列单片机总体介绍................... 8
1.1
1.2
1.3
1.4
1.5
1.6
STC12C5620AD系列单片机简介................................8
STC12C5620AD系列单片机的内部结构.........................10
STC12C5620AD系列单片机管脚图............................ 11
STC12C5620AD系列单片机选型一览表........................ 13
STC12C5620AD系列单片机最小应用系统...................... 14
STC12C5620AD系列在系统可编程(ISP)典型应用线路图.......... 15
1.6.1 STC12C5620AD系列单片机28脚典型应用电路..............................................15
1.6.2 STC12C5620AD系列单片机20脚典型应用电路..............................................17
1.6.3 STC12C5620AD系列单片机32脚典型应用电路..............................................19
1.7 STC12C5620AD系列管脚说明................................
1.8 STC12C5620AD系列单片机封装尺寸图........................
1.9 STC12C5620AD系列单片机命名规则..........................
1.10 每个单片机具有全球唯一身份证号码(ID号)...................
20
22
28
29
第2章 时钟,省电模式及复位.............................. 32
2.1 STC12C5620AD系列单片机的时钟............................ 32
2.1.1
2.1.2
2.1.3
2.1.4
STC12C5620AD系列单片机内部/外部工作时钟可选.....................................32
时钟分频及分频寄存器..................................................................................33
如何知道单片机内部R/C振荡频率(内部时钟频率)........................................34
可编程时钟输出.............................................................................................37
2.2.1
2.2.2
2.2.3
2.2.4
2.2.5
2.2.6
2.2.7
2.2.8
2.2.9
低速模式........................................................................................................43
空闲模式........................................................................................................44
掉电模式/停机模式........................................................................................44
由外部中断0唤醒掉电模式的测试程序(C程序和汇编程序)...........................45
由外部中断1唤醒掉电模式的测试程序(C程序和汇编程序)...........................47
由定时器0唤醒掉电模式的测试程序(C程序和汇编程序)...............................49
由定时器1唤醒掉电模式的测试程序(C程序和汇编程序)...............................51
由串行口RxD唤醒掉电模式的测试程序(C程序和汇编程序)..........................53
由PCA唤醒掉电模式的测试程序(C程序和汇编程序). ...................................55
2.2 STC12C5620AD系列单片机的省电模式........................ 41
2.3 复位...................................................... 59
2.3.1
2.3.2
2.3.3
2.3.4
2.3.5
2.3.6
2.3.7
外部RST引脚复位..........................................................................................59
软件复位........................................................................................................59
上电复位/掉电复位........................................................................................60
内部低压检测复位. ........................................................................................60
MAX810专用复位电路...................................................................................63
看门狗(WDT)复位..........................................................................................63
冷启动复位和热启动复位...............................................................................67
第3章 片内存储器和特殊功能寄存器(SFRs)..................................... 68
3.1 程序存储器................................................ 68
3.2 数据存储器(SRAM)................................................................................... 69
3.2.1 内部RAM.......................................................................................................69
3.2.2 内部扩展512字节RAM(物理上在内部,逻辑上在外部)................................72
3.3 特殊功能寄存器(SFRs).............................................................................. 76
第4章 STC12C5620AD系列单片机的I/O口结构............... 82
4.1 I/O口各种不同的工作模式及配置介绍.......................... 82
4.2 I/O口各种不同的工作模式结构框图............................ 86
4.2.1
4.2.2
4.2.3
4.2.4
准双向口输出配置...................................................................................................86
强推挽输出配置.......................................................................................................87
仅为输入(高阻)配置..................................................................................87
开漏输出配置(若外加上拉电阻,也可读).....................................................87
4.3 一种典型三极管控制电路....................................
4.4 典型发光二极管控制电路....................................
4.5 混合电压供电系统3V/5V器件I/O口互连........................
4.6 如何让I/O口上电复位时为低电平..............................
4.7 PWM输出时I/O口的状态.....................................
4.9 I/O口直接驱动LED数码管应用线路图..........................
4.10 I/O口直接驱动LCD应用线路图...............................
4.11 A/D做按键扫描应用线路图..................................
89
89
89
90
91
92
93
94
第5章 指令系统.......................................... 95
5.1 寻址方式.................................................. 95
5.1.1 立即寻址........................................................................................................95
5.1.2 直接寻址........................................................................................................95
5.1.3
5.1.4
5.1.5
5.1.6
5.1.7
间接寻址........................................................................................................95
寄存器寻址. ...................................................................................................96
相对寻址........................................................................................................96
变址寻址........................................................................................................96
位寻址............................................................................................................96
5.2 指令系统分类总结.......................................... 97
5.3 传统8051单片机指令定义详解(中文&English).................. 102
5.3.1 传统8051单片机指令定义详解. ....................................................................102
5.3.2 Instruction Definitions of Traditional 8051 MCU......................................142
第6章 中断系统......................................... 179
6.1
6.2
6.3
6.4
6.5
6.6
中断结构.................................................
中断寄存器...............................................
中断优先级...............................................
中断处理.................................................
外部中断.................................................
中断测试程序.............................................
6.6.1
6.6.2
6.6.3
6.6.4
6.6.5
6.6.7
181
184
192
193
194
195
外部中断0(INT0)的测试程序........................................................................195
外部中断1(INT1)的测试程序........................................................................199
定时器0中断(下降沿中断,可用于唤醒掉电模式)的测试程序....................203
定时器1中断(下降沿中断,可用于唤醒掉电模式)的测试程序....................205
RxD中断(RxD/P3.0下降沿中断,可用于唤醒掉电模式)的测试程序............207
PCA模块中断(可用于唤醒掉电模式)的测试程序. .......................................209
第7章 定时器/计数器.................................... 213
7.1 定时器/计数器的相关寄存器................................ 213
7.2 定时器/计数器0工作模式................................... 218
7.2.1
7.2.2
7.2.3
7.2.4
模式0(13位定时器/计数器)..........................................................................218
模式1(16位定时器/计数器)..........................................................................219
模式2(8位自动重装模式)..............................................................................223
模式3(两个8位计数器). ................................................................................226
7.3 定时器/计数器1工作模式................................... 227
7.3.1 模式0(13位定时器/计数器)..........................................................................227
7.3.2 模式1(16位定时器/计数器)..........................................................................228
7.3.3 模式2(8位自动重装模式)..............................................................................232
7.4 可编程时钟输出及测试程序(C程序和汇编程序)................ 235
7.4.1 定时器0的可编程时钟输出的测试程序.........................................................237
7.4.2 定时器1的可编程时钟输出的测试程序.........................................................239
7.5 古老Intel 8051单片机定时器0/1的应用举例.................... 241
第8章 串行口通信....................................... 248
8.1 串行口的相关寄存器....................................... 248
8.2 串行口工作模式........................................... 254
串行口工作模式0:同步移位寄存器............................................................254
串行口工作模式1:8位UART,波特率可变...................................................256
串行口工作模式2:9位UART,波特率固定...................................................258
串行口工作模式3:9位UART,波特率可变...................................................260
8.2.1
8.2.2
8.2.3
8.2.4
8.3
8.4
8.5
8.6
串行通信中波特率的设置...................................
串行口的测试程序.........................................
双机通信.............................................
多机通信.............................................
262
265
271
282
第9章 STC12C5620AD系列单片机的A/D转换器.............. 288
9.1
9.2
9.3
9.4
9.5
9.6
A/D转换器的结构..........................................
与A/D转换相关的寄存器....................................
A/D转换典型应用线路......................................
A/D做按键扫描应用线路图..................................
A/D转换模块的参考电压源..................................
A/D转换测试程序(C程序和汇编程序)........................
288
290
295
296
297
298
9.6.1 A/D转换测试程序(ADC中断方式)......................................................................298
9.6.2 A/D转换测试程序(ADC查询方式)...............................................................304
第10章 STC12C5620AD系列单片机PCA/PWM应用........... 310
10.1 与PCA/PWM应用有关的特殊功能寄存器..................... 310
10.2 PCA/PWM模块的结构..................................... 314
10.3 PCA模块的工作模式...................................... 316
10.3.1
10.3.2
10.3.3
10.3.4
捕获模式...............................................................................................................316
16位软件定时器模式...........................................................................................317
高速输出模式.......................................................................................................318
脉宽调节模式(PWM)...........................................................................................319
10.4 用PCA功能扩展外部中断的示例程序(C程序和汇编程序)....... 321
10.5
10.6
10.7
10.8
用PCA功能实现定时器的示例程序(C程序和汇编程序).........
PCA输出高速脉冲的示例程序(C程序和汇编程序).............
PCA输出PWM的示例程序(C程序和汇编程序)................
利用PWM实现D/A功能的典型应用线路图....................
325
329
333
337
第11章 同步串行外围接口(SPI接口)....................... 338
11.1 与SPI功能模块相关的特殊功能寄存器....................... 338
11.2 SPI接口的结构........................................... 340
11.3 SPI接口的数据通信....................................... 341
11.3.1
11.3.2
11.3.3
11.3.4
11.3.5
11.3.6
SPI接口的数据通信方式.............................................................................342
对SPI进行配置. ..........................................................................................344
作为主机/从机时的额外注意事项..............................................................345
通过SS改变模式.........................................................................................346
写冲突........................................................................................................346
数据模式. ...................................................................................................347
11.4 适用单主单从系统的SPI功能测试程序....................... 349
11.4.1 中断方式. ...................................................................................................349
11.4.2 查询方式. ...................................................................................................355
11.5 适用互为主从系统的SPI功能测试程序....................... 361
11.5.1 中断方式. ...................................................................................................361
11.5.2 查询方式. ...................................................................................................367
第12章 STC12C5620AD系列EEPROM的应用............... 373
12.1
12.2
12.3
12.4
IAP及EEPROM新增特殊功能寄存器介绍.....................
STC12C5620AD系列单片机EEPROM空间大小及地址..........
IAP及EEPROM汇编简介...................................
EEPROM测试程序........................................
373
376
377
381
第13章 STC12系列单片机开发/编程工具说明................ 389
13.1 在系统可编程(ISP)原理,官方演示工具使用说明.............. 389
13.1.1
13.1.2
13.1.3
13.1.4
13.1.5
在系统可编程(ISP)原理使用说明. ..............................................................389
STC12C5620AD系列在系统可编程(ISP)典型应用线路图. ..........................390
电脑端的ISP控制软件界面使用说明...........................................................392
STC-ISP(最方便的在线升级软件)下载编程工具硬件使用说明..................394
若无RS-232转换器,如何用STC的ISP下载板做RS-232通信转换...............395
13.2 编译器/汇编器,编程器,仿真器........................... 396
13.3 自定义下载演示程序(实现不停电下载)...................... 398
附录A:汇编语言编程.................................... 402
附录B:C语言编程....................................... 424
附录C:STC12C5620AD系列单片机电气特性................ 434
附录D:用串口扩展I/O接口............................... 436
附录E:利用STC单片机普通I/O驱动LCD显示............... 439
附录F:一个I/O口驱动发光二极管并扫描按键............... 446
附录G:如何利用Keil C软件减少代码长度.................. 447
附录H:STC12C5620AD系列单片机应用注意事项............ 448
附录I:每日更新内容的备忘录............................. 449
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
第1章 STC12C5620AD系列单片机总体介绍
1.1 STC12C5620AD系列单片机简介
STC12C5620AD系列单片机是STC生产的单时钟/机器周期(1T)的单片机,是高速/低功耗
/超强抗干扰的新一代8051单片机,指令代码完全兼容传统8051,但速度快8-12倍。内部集成
MAX810专用复位电路,4路PWM,8路高速10位A/D转换,针对电机控制,强干扰场合。
1. 增强型 8051 CPU,1T,单时钟/机器周期,指令代码完全兼容传统8051
2. 工作电压:
STC12C5620AD 系列工作电压: 5.5V - 3.5V(5V单片机)
STC12LE5620AD 系列工作电压: 3.6V - 2.2V(3V单片机)
3. 工作频率范围:0~35MHz,相当于普通8051的 0~420MHz
4. 用户应用程序空间 30K /28K / 24K / 20K / 16K / 12K / 8K / 4K 字节......
5. 片上集成768字节 RAM
6. 通用I/O口(27/23/15个),复位后为:准双向口/弱上拉(普通8051传统I/O口)
可设置成四种模式:准双向口/弱上拉,������������������
强�����������������
推挽/强上拉,仅为输入/高阻,开漏
每个I/O口驱动能力均可达到20mA,但整个芯片最大不要超过55mA
7. ISP(在系统可编程)/ IAP(在应用可编程),无需专用编程器,无需专用仿真器
可通过串口(P3.0/P3.1)直接下载用户程序,数秒即可完成一片
8. 有EEPROM功能
9. 看门狗
10.内部集成MAX810专用复位电路(外部晶体20M以下时,���������
可省外部复位电路�
)
11. 时钟源:外部高精度晶体/时钟,内部R/C振荡器
用户在下载用户程序时,可选择是使用内部R/C振荡器还是外部晶体/ 时钟
常温下内部R/C振荡器频率为:5.2MHz ~ 6.8MHz
精度要求不高时,可选择使用内部时钟,但因为有制造误差和温漂,以实际测试为准
12. 共6个16位定时器
两个与传统8051兼容的定时器/计数器,16位定时器T0 和T1,没有定时器2,
PCA模块可再实现4个16位定时器
13. 2个时钟输出口,可由T0的溢出在P1.0输出时钟,可由T1的溢出在P1������
.1����
输出时钟
14. 外部中断9路,下降沿中断或低电平触发中断,PCA模式可分别或同时支持上升沿中断/下
降沿中断,Power Down模式可由外部中断唤醒�
,INT0/P3.2, INT1/P3.3, T0/P3.4, T1/P3.5,
RxD/P3.0, PCA0/P3.7, PCA1/P3.5, PCA2/P2.0, PCA3/P2.4
15. PWM(4路)/PCA(可编程计数器阵列,4路)
--- 也可用来当4路D/A使用
--- 也可用来再实现4个定时器
--- 也可用来再实现4个外部中断(上升沿中断/下降沿中断均可分别或同时支持)
C
T
S
南通国芯微电子有限公司
U
C
M
.
d
e
t
i
m
Li
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
16. A/D转换, 10位精度ADC,共8路
17. 通用全双工异步串行口(UART),由于STC12系列是高速的8051������������
,也����������
可再用定时器软件实现
多串口
18. SPI�������������
同步通信口,主模式/从模式
19. 工作温度范围:-40 ~ +85℃(工业级) / 0 ~ 75℃(商业级)
21. 封装:LQFP-32, SOP-32/28/20, SKDIP-28, PDIP-20, TSSOP-20 (超小封装6.4mm×6.4mm),
LQFP32/SOP32有27个I/O口,SOP28/SKDIP28有23个I/O口,SOP20/TSSOP20/PDIP20有15
个I/O口,I/O口不够时,可用2到3根普通I/O口线外接74HC595/164/165(均可级联)来扩
展I/O口����
,���
还可用A/D做按键扫描来节省I/O口,或用双CPU,三线通信,还多了串口。
C
T
S
南通国芯微电子有限公司
U
C
M
总机:0513-5501 2928 / 2929 / 2966
.
d
e
t
i
m
Li
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
1.2 STC12C5620AD系列单片机的内部结构
STC12C5620AD系列单片机的内部结构框图如下图所示。STC12C5620AD单片机中包含中
央处理器(CPU)、程序存储器(Flash)、数据存储器(SRAM)、定时/计数器、UART串口、I/O
接口、高速A/D转换、SPI接口、PCA、看门狗及片内R/C振荡器和外部晶体振荡电路等模块。
STC12C5620AD系列单片机几乎包含了数据采集和控制中所需的所有单元模块,可称得上一个
片上系统。
RAM 地址
寄存器
AUX-RAM
512字节
RAM
256字节
.
d
e
t
i
m
程序存储器(Flash)
B寄存器
ACC
堆栈指针
TMP2
LVD/LVR
C
T
S
U
C
M
ISP/IAP
定时器 0/1
地址生成器
串口
ALU
PSW
程序计数器(���
PC)
WDT
PCA(4路)
SPI接口
Control
Unit
RESET
Li
TMP1
Port 0,2,3
锁存器
Port1 锁存器
ADC
XTAL1
XTAL2
Port 1 驱动器
8
P1.0 ~ P1.7
Port 0,2,3
驱动器
P1.0 ~ P1.7
P0,P2,P3
STC12C5620AD系列内部结构框图
10
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
1.3 STC12C5620AD系列������
单片机���
管脚图
所有封装形式均满足欧盟RoHS要求,LQFP-32采用Green标准生产
强烈推荐选择SOP-32/28/20及LQFP-32贴片封装,尽量不选落后的插件DIP封装
32
VCC
P2.2
1
32
VCC
2
31
P2.1
P2.3
2
31
P2.1
RST
3
30
P2.0/PCA2/PWM2
RST
3
30
P2.0/PCA2/PWM2
RxD/P3.0
4
29
P1.7/SCLK/ADC7
RxD/P3.0
4
29
P1.7/SCLK
TxD/P3.1
5
28
P1.6/MISO/ADC6
TxD/P3.1
5
28
P1.6/MISO
P0.0
6
27
P1.5/MOSI/ADC5
P0.0
6
27
P1.5/MOSI
XTAL2
7
26
P0.3
XTAL2
7
26
P0.3
XTAL1
8
25
P1.4/SS/ADC4
XTAL1
8
25
P1.4/SS
INT0/P3.2
9
24
P1.3/ADC3
INT0/P3.2
9
24
P1.3
P0.1
10
23
P0.2
INT1/P3.3
11
22
P1.2/ADC2
ECI/T0/P3.4
12
21
PWM1/PCA1/T1/P3.5
13
PWM3/PCA3/P2.4
SOP-32
1
P2.3
SOP-32
P2.2
.
d
e
t
i
m
P0.1
10
23
P0.2
INT1/P3.3
11
22
P1.2
P1.1/ADC1/CLKOUT1
ECI/T0/P3.4
12
21
P1.1/CLKOUT1
20
P1.0/ADC0/CLKOUT0
PWM1/PCA1/T1/P3.5
13
20
P1.0/CLKOUT0
14
19
P3.7/PCA0/PWM0
PWM3/PCA3/P2.4
14
19
P3.7/PCA0/PWM0
P2.5
15
18
P2.7
P2.5
15
18
P2.7
Gnd
16
17
P2.6
Gnd
16
17
P2.6
U
C
M
有27个I/O口
STC12C5620AD系列(有A/D转换),SOP-32
Li
STC12C5620AD系列(无A/D转换),SOP-32
LQFP-32封装,长×宽 = 9mm×9mm,高 < 1.6mm
P1.6/MISO/ADC6
P1.5/MOSI/ADC5
P0.3
P1.4/SS
P1.3
P0.2
P1.2
P1.1/CLKOUT1
ADC7/SCLK/P1.7
PWM2/PCA2/P2.0
P2.1
Vcc
P2.2
P2.3
RST
RxD/P3.0
TxD/P3.1
P0.0
XTAL2
XTAL1
INT0/P3.2
P0.1
INT1/P3.3
ECI/T0/P3.4
有27个I/O口
STC12C5620AD系列(有A/D转换),LQFP-32
南通国芯微电子有限公司
25
26
27
28
29
30
31
32
24
23
22
21
20
19
18
17
P1.0/ADC0/CLKOUT0
P3.7/PCA0/PWM0
P2.7
P2.6
Gnd
P2.5
P2.4/PCA3/PWM3
P3.5/T1/PCA1/PWM1
LQFP-32
16
15
14
13
12
11
10
9
P1.0/CLKOUT0
P3.7/PCA0/PWM0
P2.7
P2.6
Gnd
P2.5
P2.4/PCA3/PWM3
P3.5/T1/PCA1/PWM1
1
2
3
4
5
6
7
8
LQFP-32
16
15
14
13
12
11
10
9
总机:0513-5501 2928 / 2929 / 2966
TxD/P3.1
P0.0
XTAL2
XTAL1
INT0/P3.2
P0.1
INT1/P3.3
ECI/T0/P3.4
25
26
27
28
29
30
31
32
1
2
3
4
5
6
7
8
ADC7/SCLK/P1.7
PWM2/PCA2/P2.0
P2.1
Vcc
P2.2
P2.3
RST
RxD/P3.0
24
23
22
21
20
19
18
17
P1.6/MISO/ADC6
P1.5/MOSI/ADC5
P0.3
P1.4/SS/ADC4
P1.3/ADC3
P0.2
P1.2/ADC2
P1.1/ADC1/CLKOUT1
C
T
S
STC12C5620AD系列(无A/D转换),LQFP-32
传真:0513-5501 2969 / 2956 / 2947
11
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
28
VCC
P2.2
1
28
VCC
2
27
P2.1
P2.3
2
27
P2.1
RST
3
26
P2.0/PCA2/PWM2
RST
3
26
P2.0/PCA2/PWM2
RxD/P3.0
4
25
P1.7/SCLK/ADC7
RxD/P3.0
4
25
P1.7/SCLK
TxD/P3.1
5
24
P1.6/MISO/ADC6
TxD/P3.1
5
24
P1.6/MISO
XTAL2
6
23
P1.5/MOSI/ADC5
XTAL2
6
23
P1.5/MOSI
XTAL1
7
22
P1.4/SS/ADC4
XTAL1
7
22
P1.4/SS
INT0/P3.2
8
21
P1.3/ADC3
INT0/P3.2
8
21
P1.3
INT1/P3.3
9
20
P1.2/ADC2
INT1/P3.3
9
20
P1.2
19
P1.1/ADC1/CLKOUT1
ECI/T0/P3.4
10
19
P1.1/CLKOUT1
18
P1.0/ADC0/CLKOUT0
PWM1/PCA1/T1/P3.5
11
18
P1.0/CLKOUT0
17
P3.7/PCA0/PWM0
PWM3/PCA3/P2.4
12
17
P3.7/PCA0/PWM0
16
P2.7
P2.5
13
16
P2.7
15
P2.6
Gnd
14
15
P2.6
ECI/T0/P3.4
10
PWM1/PCA1/T1/P3.5
11
PWM3/PCA3/P2.4
12
P2.5
13
Gnd
14
.
d
e
t
i
m
有23个I/O口
STC12C5620AD系列(有A/D转换),28-Pin
20
VCC
RxD/P3.0
2
19
P1.7/SCLK/ADC7
TxD/P3.1
3
18
P1.6/MISO/ADC6
XTAL2
4
XTAL1
5
INT0/P3.2
6
INT1/P3.3
7
ECI/T0/P3.4
8
PWM1/PCA1/T1/P3.5
9
Gnd
10
C
T
S
STC12C5620AD系列(无A/D转换),28-Pin
Li
CU
M
RST
1
20
VCC
RxD/P3.0
2
19
P1.7/SCLK
TxD/P3.1
3
18
P1.6/MISO
XTAL2
4
17
P1.5/MOSI
XTAL1
5
16
P1.4/SS
INT0/P3.2
6
15
P1.3
INT1/P3.3
7
14
P1.2
13
P1.1/CLKOUT1
12
P1.0/CLKOUT0
11
P3.7/PCA0/PWM0
17
P1.5/MOSI/ADC5
16
P1.4/SS/ADC4
15
P1.3/ADC3
14
P1.2/ADC2
13
P1.1/ADC1/CLKOUT1
ECI/T0/P3.4
8
12
P1.0/ADC0/CLKOUT0
PWM/PCA1/T1/P3.5
9
11
P3.7/PCA0/PWM0
Gnd
有15个I/O口
STC12C5620AD系列(有A/D转换),20-Pin
SOP-20 / DIP-20
TSSOP-20
1
SOP-20 / DIP-20
TSSOP-20
RST
SOP-28/SKDIP-28(窄体)
1
P2.3
SOP-28/SKDIP-28(窄体)
P2.2
10
STC12C5620AD系列(无A/D转换),20-Pin
超小封装TSSOP-20, 6.4mm×6.4mm
12
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
1.4 STC12C5620AD系列单片机选型一览表
型号
Flash
定
时
工作 程序
时 PCA
SRAM
钟
电压 存储
器 定时
字节
输
(V) 器字
T0 器
出
节
T1
U
A
PCA
R EEP 16位 A/D
T ROM PWM 8路
8位
串
口
I/O
内
S
看
置
P
门
复
I
狗
位
封装
32-Pin
封装28-Pin
封装20-Pin
STC12C5620AD系列单片机选型一览
STC12C5604
5.5-3.5 4K
768
有
4
有 有
有
4路
STC12C5604AD
5.5-3.5 4K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12C5608
5.5-3.5 8K
768
有
4
有 有
有
4路
STC12C5608AD
5.5-3.5 8K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12C5612
5.5-3.5 12K
768
有
4
有 有
有
4路
STC12C5612AD
5.5-3.5 12K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12C5616
5.5-3.5 16K
768
有
4
有 有
有
4路
STC12C5616AD
5.5-3.5 16K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12C5620
5.5-3.5 20K
768
有
4
有 有
有
4路
STC12C5620AD
5.5-3.5 20K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12C5624
5.5-3.5 24K
768
有
4
有 有
有
4路
STC12C5624AD
5.5-3.5 24K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12C5628
5.5-3.5 28K
768
有
4
有 有
有
4路
STC12C5628AD
5.5-3.5 28K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12C5630
5.5-3.5 30K
768
有
4
有 有
有
4路
STC12C5630AD
5.5-3.5 30K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12LE5604
3.6-2.2 4K
768
有
4
有 有
有
4路
STC12LE5604AD 3.6-2.2 4K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12LE5608
3.6-2.2 8K
768
有
4
有 有
有
4路
STC12LE5608AD 3.6-2.2 8K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12LE5612
3.6-2.2 12K
768
有
4
有 有
有
4路
STC12LE5612AD 3.6-2.2 12K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12LE5616
C
T
S
U
C
M
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
.
d
e
t
i
m
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
Li
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12LE5620AD系列单片机选型一览
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
3.6-2.2 16K
768
有
4
有 有
有
4路
STC12LE5616AD 3.6-2.2 16K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12LE5620
3.6-2.2 20K
768
有
4
有 有
有
4路
STC12LE5620AD 3.6-2.2 20K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12LE5624
3.6-2.2 24K
768
有
4
有 有
有
4路
STC12LE5624AD 3.6-2.2 24K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12LE5628
3.6-2.2 28K
768
有
4
有 有
有
4路
STC12LE5628AD 3.6-2.2 28K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
STC12LE5630
3.6-2.2 30K
768
有
4
有 有
有
4路
STC12LE5630AD 3.6-2.2 30K
768
有
4
有 有
有
4路 10位 27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
南通国芯微电子有限公司
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
27/23/15 有 有 有 SOP/LQFP SOP/SKDIP SOP/TSSOP/DIP
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
13
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
1.5 STC12C5620AD系列���������
单片机最小应用系统
晶振���
频率�
在20MHz以下时:
可以不用C1,R1接1K电阻到地
不过建议保留R/C复位电路
10μF +
C1
10K
1 P2.2
VCC 28
2 P2.3
P2.1 27
3 RST
PWM2/PCA2/P2.0 26
R14 P3.0/RxD
ADC7/SCLK/P1.7 25
5 P3.1/TxD
ADC6/MIOS/P1.6 24
6 XTAL2
ADC5/MOSI/P1.5 23
7 XTAL1
ADC4/SS/P1.4 22
C2
<
>=
equals
not equals
less than
less than or equal to
greater than
greater than or equal to
Note that for each operator, two forms are acceptable (e.g., "EQ" or "="). In the following examples, all relational
tests are "true":
MOV A, #5 = 5
MOV A,#5 NE 4
MOV A,# 'X' LT 'Z'
MOV A,# 'X' >= 'X'
MOV A,#$ > 0
MOV A,#100 GE 50
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
409
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
So, the assembled instructions are equal to
MOV
A, #0FFH
Even though expressions evaluate to 16-bit results (i.e., 0FFFFH), in the examples above only the low-order eight
bits are used, since the instruction is a move byte operation. The result is not considered too big in this case, because as signed numbers the 16-bit value FFFFH and the 8-bit value FFH are the same (-1).
Expression Examples
The following are examples of expressions and the values that result:
Expression
Result
'B' - 'A'
0001H
8/3
0002H
155 MOD 2
0001H
4 * 4
0010H
8 AND 7
0000H
NOT 1
FFFEH
'A' SHL 8
4100H
LOW 65535
00FFH
(8 + 1) * 2
0012H
5 EQ 4
0000H
'A' LT 'B'
FFFFH
3 < >=
NOT
AND
OR XOR
When operators of the same precedence are used, they are evaluated left to right.
Examples:
Expression
Value
HIGH ( 'A' SHL 8)
0041H
HIGH 'A' SHL 8
0000H
NOT 'A' - 1
FFBFH
'A' OR 'A' SHL 8
4141H
410
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
ASSEMBLER DIRECTIVES
Assembler directives are instructions to the assembler program. They are not assembly language instructions executable by the target microprocessor. However, they are placed in the mnemonic field of the program. With the
exception of DB and DW, they have no direct effect on the contents of memory.
ASM51 provides several catagories of directives:
Assembler state control (ORG, END, USING)
Symbol definition (SEGMENT, EQU, SET, DATA, IDATA, XDATA, BIT, CODE)
Storage initialization/reservation (DS, DBIT, DB, DW)
Program linkage (PUBLIC, EXTRN,NAME)
Segment selection (RSEG, CSEG, DSEG, ISEG, ESEG, XSEG)
Each assembler directive is presented below, ordered by catagory.
Assembler State Control
.
d
e
t
i
m
ORG (Set Origin)
The format for the ORG (set origin) directive is
ORG
expression
The ORG directive alters the location counter to set a new program origin for statements that follow. A label is
not permitted. Two examples follow.
Li
ORG 100H
;SET LOCATION COUNTER TO 100H
ORG
($ + 1000H) AND 0F00H ;SET TO NEXT 4K BOUNDARY
U
C
M
The ORG directive can be used in any segment type. If the current segment is absolute, the value will be an absolute address in the current segment. If a relocatable segment is active, the value of the ORG expression is treated
as an offset from the base address of the current instance of the segment.
End
C
T
S
The format of the END directive is
END
END should be the last statement in the source file. No label is permitted and nothing beyond the END statement
is processed by the assembler.
Using
The format of the END directive is
USING expression
This directive informs ASM51 of the currently active register bank. Subsequent uses of the predefined symbolic
register addresses AR0 to AR7 will convert to the appropriate direct address for the active register bank. Consider
the following sequence:
USING 3
PUSH AR7
USING 1
PUSH AR7
The first push above assembles to PUSH 1FH (R7 in bank 3), whereas the second push assembles to PUSH 0FH
(R7 in bank 1).
Note that USING does not actually switch register banks; it only informs ASM51 of the active bank. Executing 8051 instructions is the only way to switch register banks. This is illustrated by modifying the example above
as follows:
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
411
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
MOV PSW, #00011000B
USING 3
PUSH AR7
MOV PSW, #00001000B
USING 1
PUSH AR7
临时技术支持:13922829991
研发顾问:13922809991
;SELECT REGISTER BANK 3
;ASSEMBLE TO PUSH 1FH
;SELECT REGISTER BANK 1
;ASSEMBLE TO PUSH 0FH
Symbol Definition
The symbol definition directives create symbols that represent segment, registers, numbers, and addresses. None
of these directives may be preceded by a label. Symbols defined by these directives may not have been previously
defined and may not be redefined by any means. The SET directive is the only exception. Symbol definiton directives are described below.
Segment
The format for the SEGMENT directive is shown below.
symbol
SEGMENT
.
d
e
t
i
m
segment_type
The symbol is the name of a relocatable segment. In the use of segments, ASM51 is more complex than conventional assemblers, which generally support only "code" and "data" segment types. However, ASM51 defines additional segment types to accommodate the diverse memory spaces in the 8051. The following are the defined 8051
segment types (memory spaces):
U
C
M
Li
CODE (the code segment)
XDATA (the external data space)
DATA (the internal data space accessible by direct addressing, 00H–07H)
IDATA (the entire internal data space accessible by indirect addressing, 00H–07H)
BIT (the bit space; overlapping byte locations 20H–2FH of the internal data space)
C
T
S
For example, the statement
EPROM
SEGMENT
CODE
declares the symbol EPROM to be a SEGMENT of type CODE. Note that this statement simply declares what
EPROM is. To actually begin using this segment, the RSEG directive is used (see below).
EQU (Equate)
Symbol
The format for the EQU directive is
EQU
expression
The EQU directive assigns a numeric value to a specified symbol name. The symbol must be a valid symbol
name, and the expression must conform to the rules described earlier.
The following are examples of the EQU directive:
N27
EQU
27
HERE
EQU
$
CR
EQU
0DH
MESSAGE:
DB 'This is a message'
LENGTH
EQU
$ - MESSAGE
;SET N27 TO THE VALUE 27
;SET "HERE" TO THE VALUE OF
;THE LOCATION COUNTER
;SET CR (CARRIAGE RETURN) TO 0DH
;"LENGTH" EQUALS LENGTH OF "MESSAGE"
Other Symbol Definition Directives
The SET directive is similar to the EQU directive except the
symbol may be redefined later, using another SET directive.
412
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
The DATA, IDATA, XDATA, BIT, and CODE directives assign addresses of the corresponding segment type
to a symbol. These directives are not essential. A similar effect can be achieved using the EQU directive; if used,
however, they evoke powerful type-checking by ASM51. Consider the following two directives and four instructions:
FLAG1
FLAG2
EQU
BIT
SETB
SETB
MOV
MOV
05H
05H
FLAG1
FLAG2
FLAG1, #0
FLAG2, #0
The use of FLAG2 in the last instruction in this sequence will generate a "data segment address expected" error
message from ASM51. Since FLAG2 is defined as a bit address (using the BIT directive), it can be used in a set
bit instruction, but it cannot be used in a move byte instruction. Hence, the error. Even though FLAG1 represents
the same value (05H), it was defined using EQU and does not have an associated address space. This is not an
advantage of EQU, but rather, a disadvantage. By properly defining address symbols for use in a specific memory
space (using the directives BIT, DATA, XDATA,ect.), the programmer takes advantage of ASM51's powerful
type-checking and avoids bugs from the misuse of symbols.
Storage Initialization/Reservation
U
C
M
.
d
e
t
i
m
Li
The storage initialization and reservation directives initialize and reserve space in either word, byte, or bit units.
The space reserved starts at the location indicated by the current value of the location counter in the currently active segment. These directives may be preceded by a label. The storage initialization/reservation directives are
described below.
C
T
S
DS (Define Storage)
[label:]
The format for the DS (define storage) directive is
DS
expression
The DS directive reserves space in byte units. It can be used in any segment type except BIT. The expression
must be a valid assemble-time expression with no forward references and no relocatable or external references.
When a DS statement is encountered in a program, the location counter of the current segment is incremented by
the value of the expression. The sum of the location counter and the specified expression should not exceed the
limitations of the current address space.
The following statement create a 40-byte buffer in the internal data segment:
LENGTH
BUFFER:
DSEG AT
30H
EQU
40
DS
LENGRH
;PUT IN DATA SEGMENT (ABSOLUTE, INTERNAL)
;40 BYTES RESERVED
The label BUFFER represents the address of the first location of reserved memory. For this example, the buffer
begins at address 30H because "AT 30H" is specified with DSEG. The buffer could be cleared using the following
instruction sequence:
LOOP:
MOV R7,
MOV R0,
MOV @R0,
DJNZ R7,
(continue)
南通国芯微电子有限公司
#LENGTH
#BUFFER
#0
LOOP
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
413
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
To create a 1000-byte buffer in external RAM starting at 4000H, the following directives could be used:
XSTART
XLENGTH
XBUFFER:
EQU
4000H
EQU 1000
XSEG
AT
DS XLENGTH
XSTART
This buffer could be cleared with the following instruction sequence:
LOOP:
MOV
DPTR, #XBUFFER
CLR
A
MOVX @DPTR, A
INC
DPTR
MOV
A,
DPL
CJNE
A,
#LOW (XBUFFER + XLENGTH + 1), LOOP
MOV
A,
DPH
CJNE
A,
#HIGH (XBUFFER + XLENGTH + 1), LOOP
(continue)
.
d
e
t
i
m
This is an excellent example of a powerful use of ASM51's operators and assemble-time expressions. Since an
instruction does not exist to compare the data pointer with an immediate value, the operation must be fabricated
from available instructions. Two compares are required, one each for the high- and low-bytes of the DPTR. Furthermore, the compare-and-jump-if-not-equal instruction works only with the accumulator or a register, so the
data pointer bytes must be moved into the accumulator before the CJNE instruction. The loop terminates only
when the data pointer has reached XBUFFER + LENGTH + 1. (The "+1" is needed because the data pointer is
incremented after the last MOVX instruction.)
DBIT
C
T
S
U
C
M
Li
The format for the DBIT (define bit) directive is,
[label:]
DBIT
expression
The DBIT directive reserves space in bit units. It can be used only in a BIT segment. The expression must be a
valid assemble-time expression with no forward references. When the DBIT statement is encountered in a program, the location counter of the current (BIT) segment is incremented by the value of the expression. Note that
in a BIT segment, the basic unit of the location counter is bits rather than bytes. The following directives creat
three flags in a absolute bit segment:
KEFLAG:
PRFLAG:
DKFLAG:
BSEG
DBIT 1
DBIT 1
DBIT 1
;BIT SEGMENT (ABSOLUTE)
;KEYBOARD STATUS
;PRINTER STATUS
;DISK STATUS
Since an address is not specified with BSEG in the example above, the address of the flags defined by DBIT
could be determined (if one wishes to to so) by examining the symbol table in the .LST or .M51 files. If the definitions above were the first use of BSEG, then KBFLAG would be at bit address 00H (bit 0 of byte address 20H).
If other bits were defined previously using BSEG, then the definitions above would follow the last bit defined.
DB (Define Byte)
[label:]
The format for the DB (define byte) directive is,
DB
expression [, expression] […]
The DB directive initializes code memory with byte values. Since it is used to actually place data constants in
code memory, a CODE segment must be active. The expression list is a series of one or more byte values (each of
which may be an expression) separated by commas.
414
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
The DB directive permits character strings (enclosed in single quotes) longer than two characters as long as they
are not part of an expression. Each character in the string is converted to the corresponding ASCII code. If a label
is used, it is assigned the address of th first byte. For example, the following statements
SQUARES:
MESSAGE:
CSEG AT
0100H
DB
0, 1, 4, 9, 16, 25
DB
'Login:', 0
;SQUARES OF NUMBERS 0-5
;NULL-TERMINATED CHARACTER STRING
When assembled, result in the following hexadecimal memory assignments for external code memory:
Address
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
010A
010B
010C
Contents
00
01
04
09
10
19
4C
6F
67
69
6E
3A
00
DW (Define Word)
[label:]
U
C
M
.
d
e
t
i
m
Li
The format for the DW (define word) directive is
C
T
S
DW
expression
[, expression] […]
The DW directive is the same as the DB directive except two memory locations (16 bits) are assigned for each
data item. For example, the statements
CSEG
DW
AT
200H
$, 'A', 1234H, 2, 'BC'
result in the following hexadecimal memory assignments:
Address
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
Contents
02
00
00
41
12
34
00
02
42
43
Program Linkage
Program linkage directives allow the separately assembled modules (files) to communicate by permitting intermodule references and the naming of modules. In the following discussion, a "module" can be considered a "file."
(In fact, a module may encompass more than one file.)
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
415
STC12C5620AD系列单片机指南
Public
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
The format for the PUBLIC (public symbol) directive is
PUBLIC
symbol
[, symbol] […]
The PUBLIC directive allows the list of specified symbols to known and used outside the currently assembled
module. A symbol declared PUBLIC must be defined in the current module. Declaring it PUBLIC allows it to be
referenced in another module. For example,
PUBLIC
Extrn
INCHAR, OUTCHR, INLINE, OUTSTR
The format for the EXTRN (external symbol) directive is
EXTRN
segment_type (symbol [, symbol] […], …)
The EXTRN directive lists symbols to be referenced in the current module that are defined in other modules. The
list of external symbols must have a segment type associated with each symbol in the list. (The segment types are
CODE, XDATA, DATA, IDATA, BIT, and NUMBER. NUMBER is a type-less symbol defined by EQU.) The
segment type indicates the way a symbol may be used. The information is important at link-time to ensure symbols are used properly in different modules.
The PUBLIC and EXTRN directives work together. Consider the two files, MAIN.SRC and MESSAGES.
SRC. The subroutines HELLO and GOOD_BYE are defined in the module MESSAGES but are made available
to other modules using the PUBLIC directive. The subroutines are called in the module MAIN even though they
are not defined there. The EXTRN directive declares that these symbols are defined in another module.
MAIN.SRC:
MESSAGES.SRC:
HELLO:
GOOD_BYE:
EXTRN
…
CALL
…
CALL
…
END
C
T
S
U
C
M
.
d
e
t
i
m
Li
CODE (HELLO, GOOD_BYE)
HELLO
GOOD_BYE
PUBLIC
HELLO, GOOD_BYE
…
(begin subroutine)
…
RET
(begin subroutine)
…
RET
…
END
Neither MAIN.SRC nor MESSAGES.SRC is a complete program; they must be assembled separately and
linked together to form an executable program. During linking, the external references are resolved with correct
addresses inserted as the destination for the CALL instructions.
Name
416
The format for the NAME directive is
NAME module_name
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
All the usual rules for symbol names apply to module names. If a name is not provided, the module takes on
the file name (without a drive or subdirectory specifier and without an extension). In the absence of any use of
the NAME directive, a program will contain one module for each file. The concept of "modules," therefore, is
somewhat cumbersome, at least for relatively small programming problems. Even programs of moderate size (encompassing, for example, several files complete with relocatable segments) needn't use the NAME directive and
needn't pay any special attention to the concept of "modules." For this reason, it was mentioned in the definition
that a module may be considered a "file," to simplify learning ASM51. However, for very large programs (several
thousand lines of code, or more), it makes sense to partition the problem into modules, where, for example, each
module may encompass several files containing routines having a common purpose.
Segment Selection Directives
When the assembler encounters a segment selection directive, it diverts the following code or data into the selected segment until another segment is selected by a segment selection directive. The directive may select may select
a previously defined relocatable segment or optionally create and select absolute segments.
RSEG (Relocatable Segment)
RSEG
.
d
e
t
i
m
The format for the RSEG (relocatable segment) directive is
segment_name
Li
Where "segment_name" is the name of a relocatable segment previously defined with the SEGMENT directive.
RSEG is a "segment selection" directive that diverts subsequent code or data into the named segment until another
segment selection directive is encountered.
Selecting Absolute Segments
hand, is selected using one of the directives:
CSEG
DSEG
ISEG
BSEG
XSEG
(AT
(AT
(AT
(AT
(AT
C
T
S
address)
address)
address)
address)
address)
U
C
M
RSEG selects a relocatable segment. An "absolute" segment, on the other
These directives select an absolute segment within the code, internal data, indirect internal data, bit, or external
data address spaces, respectively. If an absolute address is provided (by indicating "AT address"), the assembler
terminates the last absolute address segment, if any, of the specified segment type and creates a new absolute segment starting at that address. If an absolute address is not specified, the last absolute segment of the specified type
is continuted. If no absolute segment of this type was previously selected and the absolute address is omitted, a
new segment is created starting at location 0. Forward references are not allowed and start addresses must be absolute.
Each segment has its own location counter, which is always set to 0 initially. The default segment is an absolute code segment; therefore, the initial state of the assembler is location 0000H in the absolute code segment.
When another segment is chosen for the first time, the location counter of the former segment retains the last
active value. When that former segment is reselected, the location counter picks up at the last active value. The
ORG directive may be used to change the location counter within the currently selected segment.
ASSEMBLER CONTROLS
Assembler controls establish the format of the listing and object files by regulating the actions of ASM51. For the
most part, assembler controls affect the look of the listing file, without having any affect on the program itself.
They can be entered on the invocation line when a program is assembled, or they can be placed in the source file.
Assembler controls appearing in the source file must be preceded with a dollor sign and must begin in column 1.
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
417
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
There are two categories of assembler controls: primary and general. Primary controls can be placed in the
invocation line or at the beginnig of the source program. Only other primary controls may precede a primary control. General controls may be placed anywhere in the source program.
LINKER OPERATION
In developing large application programs, it is common to divide tasks into subprograms or modules containing
sections of code (usually subroutines) that can be written separately from the overall program. The term "modular programming" refers to this programming strategy. Generally, modules are relocatable, meaning they are not
intended for a specific address in the code or data space. A linking and locating program is needed to combine the
modules into one absolute object module that can be executed.
Intel's RL51 is a typical linker/locator. It processes a series of relocatable object modules as input and creates
an executable machine language program (PROGRAM, perhaps) and a listing file containing a memory map and
symbol table (PROGRAM.M51). This is illustrated in following figure.
.
d
e
t
i
m
PROGRAM.ABS
FILE3.OBJ
FILE2.OBJ
RL51
FILE1.OBJ
Legend
Utility program
User file
C
T
S
CU
M
Li
PROGRAM.MAP
Linker operation
As relocatable modules are combined, all values for external symbols are resolved with values inserted into
the output file. The linker is invoked from the system prompt by
RL51
input_list
[T0 output_file]
[location_controls]
The input_list is a list of relocatable object modules (files) separated by commas. The output_list is the name
of the output absolute object module. If none is supplied, it defaults to the name of the first input file without any
suffix. The location_controls set start addresses for the named segments.
For example, suppose three modules or files (MAIN.OBJ, MESSAGES.OBJ, and SUBROUTINES.OBJ) are
to be combined into an executable program (EXAMPLE), and that these modules each contain two relocatable
segments, one called EPROM of type CODE, and the other called ONCHIP of type DATA. Suppose further that
the code segment is to be executable at address 4000H and the data segment is to reside starting at address 30H (in
internal RAM). The following linker invocation could be used:
RS51
MAIN.OBJ, MESSAGES.OBJ, SUBROUTINES.OBJ TO EXAMPLE & CODE
(EPROM (4000H) DATA (ONCHIP (30H))
Note that the ampersand character "&" is used as the line continuaton character.
If the program begins at the label START, and this is the first instruction in the MAIN module, then execution begins at address 4000H. If the MAIN module was not linked first, or if the label START is not at the beginning of MAIN, then the program's entry point can be determined by examining the symbol table in the listing file
EXAMPLE.M51 created by RL51. By default, EXAMPLE.M51 will contain only the link map. If a symbol table
is desired, then each source program must have used the SDEBUG control. The following table shows the assembler controls supported by ASM51.
418
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
Assembler controls supported by ASM51
NAME
DATE (date)
DEBUG
EJECT
ERRORPRINT
(file)
NOERRORPRINT
PRIMARY/
GENERAL
GEN
G
GENONLY
INCLUED(file)
LIST
NOLIST
MACRO
(men_precent)
NOMACRO
MOD51
G
G
G
G
P
NOMOD51
P
OBJECT(file)
NOOBJECT
PAGING
P
P
P
NOPAGING
PAGELENGTH
(N)
PAGE WIDTH (N)
P
P
PRINT(file)
NOPRINT
SAVE
RESTORE
REGISTERBANK
(rb,...)
NOREGISTERBANK
SYMBOLS
NOSYMBOLS
TITLE(string)
P
P
G
G
P
MEANING
Place string in header (9 char. max.)
Outputs debug symbol information to object file
Continue listing on next page
Designates a file to receive error messages in addition to the
listing file (defauts to console)
NOERRORPRINT
NOEP Designates that error messages will be printed in listing file
only
GENONLY
GO
List only the fully expanded source as if all lines generated
by a macro call were already in the source file
GENONLY
NOGE List only the original source text in the listing file
not applicable
IC
Designates a file to be included as part of the program
LIST
LI
Print subsequent lines of source code in listing file
LIST
NOLI Do not print subsequent lines of source code in lisitng file
MACRO(50)
MR
Evaluate and expand all macro calls. Allocate percentage of
free memory for macro processing
MACRO(50)
NOMR Do not evalutate macro calls
MOD51
MO
Recognize the 8051-specific predefined special function
registers
MOD51
NOMO Do not recognize the 8051-specific predefined special
function registers
OBJECT(source.OBJ)
OJ
Designates file to receive object code
OBJECT(source.OBJ) NOOJ Designates that no object file will be created
PAGING
PI
Designates that listing file be broken into pages and each
will have a header
PAGING
NOPI Designates that listing file will contain no page breaks
PAGELENGT(60)
PL
Sets maximun number of lines in each page of listing file
(range=10 to 65536)
PAGEWIDTH(120)
PW
Set maximum number of characters in each line of listing
file (range = 72 to 132)
PRINT(source.LST)
PR
Designates file to receive source listing
PRINT(source.LST)
NOPR Designates that no listing file will be created
not applicable
SA
Stores current control settings from SAVE stack
not applicable
RS
Restores control settings from SAVE stack
REGISTERBANK(0)
RB
Indicates one or more banks used in program module
P
REGISTERBANK(0)
NORB
Indicates that no register banks are used
P
P
G
SYMBOLS
SYMBOLS
TITLE( )
SB
NOSB
TT
WORKFILES
(path)
XREF
P
same as source
WF
Creates a formatted table of all symbols used in program
Designates that no symbol table is created
Places a string in all subsequent page headers (max.60
characters)
Designates alternate path for temporay workfiles
P
NOXREF
XR
NOXREF
P
NOXREF
NOXR
P
P
G
P
P
P
P
P
南通国芯微电子有限公司
DEFAULT
DATE( )
NODEBUG
not applicable
NOERRORPRINT
C
T
S
ABBREV.
DA
DB
EJ
EP
U
C
M
.
d
e
t
i
m
Li
Creates a cross reference listing of all symbols used in
program
Designates that no cross reference list is created
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
419
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
MACROS
The macro processing facility (MPL) of ASM51 is a "string replacement" facility. Macros allow frequently used
sections of code be defined once using a simple mnemonic and used anywhere in the program by inserting the
mnemonic. Programming using macros is a powerful extension of the techniques described thus far. Macros can
be defined anywhere in a source program and subsequently used like any other instruction. The syntax for macro
definition is
%*DEFINE
(call_pattern)
(macro_body)
Once defined, the call pattern is like a mnemonic; it may be used like any assembly language instruction by
placing it in the mnemonic field of a program. Macros are made distinct from "real" instructions by preceding
them with a percent sign, "%". When the source program is assembled, everything within the macro-body, on
a character-by-character basis, is substituted for the call-pattern. The mystique of macros is largely unfounded.
They provide a simple means for replacing cumbersome instruction patterns with primitive, easy-to-remember
mnemonics. The substitution, we reiterate, is on a character-by-character basis—nothing more, nothing less.
For example, if the following macro definition appears at the beginning of a source file,
%*DEFINE
(PUSH_DPTR)
(PUSH DPH
PUSH DPL
)
then the statement
%PUSH_DPTR
will appear in the .LST file as
PUSH
PUSH
C
T
S
DPH
DPL
U
C
M
.
d
e
t
i
m
Li
The example above is a typical macro. Since the 8051 stack instructions operate only on direct addresses,
pushing the data pointer requires two PUSH instructions. A similar macro can be created to POP the data pointer.
There are several distinct advantages in using macros:
A source program using macros is more readable, since the macro mnemonic is generally more indicative
of the intended operation than the equivalent assembler instructions.
The source program is shorter and requires less typing.
Using macros reduces bugs
Using macros frees the programmer from dealing with low-level details.
The last two points above are related. Once a macro is written and debugged, it is used freely without the worry
of bugs. In the PUSH_DPTR example above, if PUSH and POP instructions are used rather than push and pop
macros, the programmer may inadvertently reverse the order of the pushes or pops. (Was it the high-byte or lowbyte that was pushed first?) This would create a bug. Using macros, however, the details are worked out once—
when the macro is written—and the macro is used freely thereafter, without the worry of bugs.
Since the replacement is on a character-by-character basis, the macro definition should be carefully constructed with carriage returns, tabs, ect., to ensure proper alignment of the macro statements with the rest of the
assembly language program. Some trial and error is required.
There are advanced features of ASM51's macro-processing facility that allow for parameter passing, local
labels, repeat operations, assembly flow control, and so on. These are discussed below.
420
南通国芯微电子有限公司
总机:0513-5501 2928 / 2929 / 2966
传真:0513-5501 2969 / 2956 / 2947
STC12C5620AD系列单片机指南
技术支持网站:www.STCMCU.com
临时技术支持:13922829991
研发顾问:13922809991
Parameter Passing
A macro with parameters passed from the main program has the following modified format:
%*DEFINE
(macro_name (parameter_list))
(macro_body)
For example, if the following macro is defined,
%*DEFINE
(CJNE
)
(CMPA# (VALUE))
A, #%VALUE, $ + 3
then the macro call
%CMPA# (20H)
will expand to the following instruction in the .LST file:
CJNE
A, #20H, $ + 3
.
d
e
t
i
m
Although the 8051 does not have a "compare accumulator" instruction, one is easily created using the CJNE
instruction with "$+3" (the next instruction) as the destination for the conditional jump. The CMPA# mnemonic
may be easier to remember for many programmers. Besides, use of the macro unburdens the programmer from
remembering notational details, such as "$+3."
Let's develop another example. It would be nice if the 8051 had instructions such as
JUMP
JUMP
JUMP
JUMP
IF
IF
IF
IF
ACCUMULATOR
ACCUMULATOR
ACCUMULATOR
ACCUMULATOR
Li
GREATER THAN X
GREATER THAN OR EQUAL TO X
LESS THAN X
LESS THAN OR EQUAL TO X
C
T
S
U
C
M
but it does not. These operations can be created using CJNE followed by JC or JNC, but the details are tricky.
Suppose, for example, it is desired to jump to the label GREATER_THAN if the accumulator contains an ASCII
code greater than "Z" (5AH). The following instruction sequence would work:
CJNE
JNC
A, #5BH, $÷3
GREATER_THAN
The CJNE instruction subtracts 5BH (i.e., "Z" + 1) from the content of A and sets or clears the carry flag accordingly. CJNE leaves C=1 for accumulator values 00H up to and including 5AH. (Note: 5AH-5BH