《单片机C语言编程模板.pdf》由会员分享,可在线阅读,更多相关《单片机C语言编程模板.pdf(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、单片机 C语言编程模板(基础模板)程序开始处的程序说明 /*程序名:编写人:编写时间:年月日硬件支持:接口说明:/*说明:/*/单片机 SFR定义的头文件#include /通用 89C51 头文件#include /通用 89C52 头文件#include /STC11Fxx 或 STC11Lxx系列单片机头文件#include /STC12Cx052 或 STC12Cx052AD系列单片机头文件#include /STC12C5A60S2 系列单片机头文件更多库函数头定义#include /设定插入点#include /字符处理#include /定义错误码#include /浮点数处理#
2、include /文件输入输出#include /参数化输入输出#include /数据流输入输出#include /定义各种数据类型最值常量#include /定义本地化函数#include /定义数学函数#include /定义输入输出函数#include /定义杂项函数及内存分配函数#include /字符串处理#include /基于数组的输入输出#include /定义关于时间的函数#include /宽字符处理及输入输出#include /宽字符分类#include /51 基本运算(包括_nop_空函数)常用定义声明 sfr 自定义名 =SFR地址 ;/按字节定义SFR中的存储器
3、名。例:sfr P1=0 x90;sbit 自定义名 =系统位名 ;/按位定义SFR中的存储器名。例:sbitAdd_Key=P3 1;bit 自定义名 ;/定义一个位(位的值只能是0 或 1)例:bit LED;#define 代替名 原名 /用代替名代替原名。例:#define LED P1/#define TA 0 x25 unsigned char 自定义名 ;/定义一个0255 的整数变量。例:unsigned char a;unsigned int 自定义名 ;/定义一个065535 的整数变量。例:unsignedint a;定义常量和变量的存放位置的关键字 data 字节寻址片
4、内RAM,片内 RAM 的 128 字节(例:data unsigned char a;)bdata 可位寻址片内RAM,16 字节,从0 x20 到 0 x2F(例:bdata unsigned char a;)idata 所有片内RAM,256 字节,从0 x00 到 0 xFF(例:idata unsigned char a;)pdata 片外 RAM,256 字节,从0 x00 到 0 xFF(例:pdata unsigned char a;)xdata 片外 RAM,64K 字节,从0 x00 到 0 xFFFF(例:xdata unsigned char a;)code ROM 存
5、储器,64K 字节,从0 x00 到 0 xFFFF(例:code unsigned char a;)选择、循环语句 if(1)/为真时语句else/否则时语句-文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z1
6、0文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E
7、7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C
8、7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1
9、HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6
10、T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编
11、码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N
12、10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10while(1)/为真时内容-do/先执行内容while(1);-switch(a)case 0 x01:/为真时语句break;case 0 x02:/为真时语句break;default:/冗余语句break;-for(;)/循环语句-主函数模板 /*文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E
13、7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C
14、7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1
15、HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6
16、T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编
17、码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N
18、10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8
19、ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10函数名:主函数调用:无参数:无返回值:无结果:程序开始处,无限循环备注:/*/void main(void)/初始程序while(1)
20、/无限循环程序 /*/中断处理函数模板 /*函数名:中断处理函数调用:无参数:无返回值:无结果:备注:/*/void name(void)interrupt 1 using 1/处理内容/*/中断入口说明 interrupt 0 外部中断0(ROM 入口地址:0 x03)interrupt 1 定时/计数器中断0(ROM 入口地址:0 x0B)interrupt 2 外部中断1(ROM 入口地址:0 x13)文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:C
21、B10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z
22、10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9
23、E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1
24、C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1
25、 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I
26、6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档
27、编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10interrupt 3 定时/计数器中断1(ROM 入口地址:0 x1B)interrupt 4 UART 串口中断(ROM 入口地址:0 x23)(更多的中断依单
28、片机型号而定,ROM 中断入口均相差8 个字节)using 0 使用寄存器组0 using 1 使用寄存器组1 using 2 使用寄存器组2 using 3 使用寄存器组3 普通函数框架 /*函数名:调用:参数:无返回值:无结果:备注:/*/void name(void)/函数内容/*/*函数名:调用:参数:065535/0255 返回值:065535/0255 结果:备注:/*/unsignedint name(unsigned char a,unsignedint b)/函数内容return a;/返回值/*/文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N
29、10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8
30、ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R
31、7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J1
32、0T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB
33、10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z1
34、0文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10文档编码:CB10I6T6J10T1 HJ4R7S1C7R8 ZN9E7H1N10Z10