单片机C语言编程模板(基础模板)
[程序开始处的程序说明]
/********************************************************************************************* 程序名: 编写人: 编写时间: 年 月 日 硬件支持: 接口说明: /********************************************************************************************* 说明:
/*********************************************************************************************/
[单片机SFR定义的头文件]
#include <REG51.h> //通用89C51头文件 #include <REG52.h> //通用89C52头文件 #include <STC11Fxx.H> //STC11Fxx或STC11Lxx系列单片机头文件 #include <STC12C2052AD.H> //STC12Cx052或STC12Cx052AD系列单片机头文件 #include <STC12C5A60S2.H> //STC12C5A60S2系列单片机头文件
[更多库函数头定义]
#include <assert.h> //设定插入点 #include <ctype.h> //字符处理 #include <errno.h> //定义错误码 #include <float.h> //浮点数处理 #include <fstream.h> //文件输入/输出 #include <iomanip.h> //参数化输入/输出 #include <iostream.h> //数据流输入/输出 #include <limits.h> //定义各种数据类型最值常量 #include <locale.h> //定义本地化函数 #include <math.h> //定义数学函数 #include <stdio.h> //定义输入/输出函数 #include <stdlib.h> //定义杂项函数及内存分配函数 #include <string.h> //字符串处理 #include <strstrea.h> //基于数组的输入/输出 #include <time.h> //定义关于时间的函数 #include <wchar.h> //宽字符处理及输入/输出 #include <wctype.h> //宽字符分类 #include <intrins.h> //51基本运算(包括_nop_空函数)
[常用定义声明]
sfr [自定义名] = [SFR地址] ; //按字节定义SFR中的存储器名。例:sfr P1 = 0x90; sbit [自定义名] = [系统位名] ; //按位定义SFR中的存储器名。例:sbit Add_Key = P3 ^ 1; bit [自定义名] ; //定义一个位(位的值只能是0或1)例:bit LED; #define [代替名] [原名] //用代替名代替原名。例:#define LED P1 / #define TA 0x25
unsigned char [自定义名] ; //定义一个0~255的整数变量。例:unsigned char a; unsigned int [自定义名] ; //定义一个0~65535的整数变量。例:unsigned int a;
[定义常量和变量的存放位置的关键字]
data 字节寻址片内RAM,片内RAM的128字节(例:data unsigned char a;) bdata 可位寻址片内RAM,16字节,从0x20到0x2F(例:bdata unsigned char a;) idata 所有片内RAM,256字节,从0x00到0xFF(例:idata unsigned char a;) pdata 片外RAM,256字节,从0x00到0xFF(例:pdata unsigned char a;) xdata 片外RAM,64K字节,从0x00到0xFFFF(例:xdata unsigned char a;) code ROM存储器,64K字节,从0x00到0xFFFF(例:code unsigned char a;)
[选择、循环语句]
if(1){
//为真时语句
}else{
//否则时语句
}
--------------------------
while(1){
//为真时内容
}
--------------------------
do{
//先执行内容
}while(1);
--------------------------
switch (a){ case 0x01: //为真时语句 break; case 0x02: //为真时语句 break; default: //冗余语句 break; }
--------------------------
for(;;){
//循环语句
}
--------------------------
[主函数模板]
/********************************************************************************************* 函数名:主函数 调 用:无 参 数:无 返回值:无 结 果:程序开始处,无限循环 备 注: /**********************************************************************************************/ void main (void){
//初始程序
while(1){
//无限循环程序
} } /**********************************************************************************************/
[中断处理函数模板] /********************************************************************************************* 函数名:中断处理函数 调 用:无 参 数:无 返回值:无 结 果: 备 注: /**********************************************************************************************/ void name (void) interrupt 1 using 1{
//处理内容 } /**********************************************************************************************/
[中断入口说明]
interrupt 0 外部中断0(ROM入口地址:0x03) interrupt 1 定时/计数器中断0(ROM入口地址:0x0B) interrupt 2 外部中断1(ROM入口地址:0x13) interrupt 3 定时/计数器中断1(ROM入口地址:0x1B) interrupt 4 UART串口中断(ROM入口地址:0x23) (更多的中断依单片机型号而定,ROM中断入口均相差8个字节)
using 0 使用寄存器组0 using 1 使用寄存器组1 using 2 使用寄存器组2 using 3 使用寄存器组3
[普通函数框架]
/********************************************************************************************* 函数名: 调 用: 参 数:无 返回值:无 结 果: 备 注: /**********************************************************************************************/ void name (void){
//函数内容
} /**********************************************************************************************/
/********************************************************************************************* 函数名: 调 用: 参 数:0~65535 / 0~255 返回值:0~65535 / 0~255 结 果: 备 注: /**********************************************************************************************/ unsigned int name (unsigned char a,unsigned int b){
//函数内容
return a; //返回值 } /**********************************************************************************************/
|