單片機(jī)C語言編程模板(基礎(chǔ)模板)
[程序開始處的程序說明]
/********************************************************************************************* 程序名: 編寫人: 編寫時(shí)間: 年 月 日 硬件支持: 接口說明: /********************************************************************************************* 說明:
/*********************************************************************************************/
[單片機(jī)SFR定義的頭文件]
#include <REG51.h> //通用89C51頭文件 #include <REG52.h> //通用89C52頭文件 #include <STC11Fxx.H> //STC11Fxx或STC11Lxx系列單片機(jī)頭文件 #include <STC12C2052AD.H> //STC12Cx052或STC12Cx052AD系列單片機(jī)頭文件 #include <STC12C5A60S2.H> //STC12C5A60S2系列單片機(jī)頭文件
[更多庫函數(shù)頭定義]
#include <assert.h> //設(shè)定插入點(diǎn) #include <ctype.h> //字符處理 #include <errno.h> //定義錯(cuò)誤碼 #include <float.h> //浮點(diǎn)數(shù)處理 #include <fstream.h> //文件輸入/輸出 #include <iomanip.h> //參數(shù)化輸入/輸出 #include <iostream.h> //數(shù)據(jù)流輸入/輸出 #include <limits.h> //定義各種數(shù)據(jù)類型最值常量 #include <locale.h> //定義本地化函數(shù) #include <math.h> //定義數(shù)學(xué)函數(shù) #include <stdio.h> //定義輸入/輸出函數(shù) #include <stdlib.h> //定義雜項(xiàng)函數(shù)及內(nèi)存分配函數(shù) #include <string.h> //字符串處理 #include <strstrea.h> //基于數(shù)組的輸入/輸出 #include <time.h> //定義關(guān)于時(shí)間的函數(shù) #include <wchar.h> //寬字符處理及輸入/輸出 #include <wctype.h> //寬字符分類 #include <intrins.h> //51基本運(yùn)算(包括_nop_空函數(shù))
[常用定義聲明]
sfr [自定義名] = [SFR地址] ; //按字節(jié)定義SFR中的存儲(chǔ)器名。例:sfr P1 = 0x90; sbit [自定義名] = [系統(tǒng)位名] ; //按位定義SFR中的存儲(chǔ)器名。例:sbit Add_Key = P3 ^ 1; bit [自定義名] ; //定義一個(gè)位(位的值只能是0或1)例:bit LED; #define [代替名] [原名] //用代替名代替原名。例:#define LED P1 / #define TA 0x25
unsigned char [自定義名] ; //定義一個(gè)0~255的整數(shù)變量。例:unsigned char a; unsigned int [自定義名] ; //定義一個(gè)0~65535的整數(shù)變量。例:unsigned int a;
[定義常量和變量的存放位置的關(guān)鍵字]
data 字節(jié)尋址片內(nèi)RAM,片內(nèi)RAM的128字節(jié)(例:data unsigned char a;) bdata 可位尋址片內(nèi)RAM,16字節(jié),從0x20到0x2F(例:bdata unsigned char a;) idata 所有片內(nèi)RAM,256字節(jié),從0x00到0xFF(例:idata unsigned char a;) pdata 片外RAM,256字節(jié),從0x00到0xFF(例:pdata unsigned char a;) xdata 片外RAM,64K字節(jié),從0x00到0xFFFF(例:xdata unsigned char a;) code ROM存儲(chǔ)器,64K字節(jié),從0x00到0xFFFF(例:code unsigned char a;)
[選擇、循環(huán)語句]
if(1){
//為真時(shí)語句
}else{
//否則時(shí)語句
}
--------------------------
while(1){
//為真時(shí)內(nèi)容
}
--------------------------
do{
//先執(zhí)行內(nèi)容
}while(1);
--------------------------
switch (a){ case 0x01: //為真時(shí)語句 break; case 0x02: //為真時(shí)語句 break; default: //冗余語句 break; }
--------------------------
for(;;){
//循環(huán)語句
}
--------------------------
[主函數(shù)模板]
/********************************************************************************************* 函數(shù)名:主函數(shù) 調(diào) 用:無 參 數(shù):無 返回值:無 結(jié) 果:程序開始處,無限循環(huán) 備 注: /**********************************************************************************************/ void main (void){
//初始程序
while(1){
//無限循環(huán)程序
} } /**********************************************************************************************/
[中斷處理函數(shù)模板] /********************************************************************************************* 函數(shù)名:中斷處理函數(shù) 調(diào) 用:無 參 數(shù):無 返回值:無 結(jié) 果: 備 注: /**********************************************************************************************/ void name (void) interrupt 1 using 1{
//處理內(nèi)容 } /**********************************************************************************************/
[中斷入口說明]
interrupt 0 外部中斷0(ROM入口地址:0x03) interrupt 1 定時(shí)/計(jì)數(shù)器中斷0(ROM入口地址:0x0B) interrupt 2 外部中斷1(ROM入口地址:0x13) interrupt 3 定時(shí)/計(jì)數(shù)器中斷1(ROM入口地址:0x1B) interrupt 4 UART串口中斷(ROM入口地址:0x23) (更多的中斷依單片機(jī)型號而定,ROM中斷入口均相差8個(gè)字節(jié))
using 0 使用寄存器組0 using 1 使用寄存器組1 using 2 使用寄存器組2 using 3 使用寄存器組3
[普通函數(shù)框架]
/********************************************************************************************* 函數(shù)名: 調(diào) 用: 參 數(shù):無 返回值:無 結(jié) 果: 備 注: /**********************************************************************************************/ void name (void){
//函數(shù)內(nèi)容
} /**********************************************************************************************/
/********************************************************************************************* 函數(shù)名: 調(diào) 用: 參 數(shù):0~65535 / 0~255 返回值:0~65535 / 0~255 結(jié) 果: 備 注: /**********************************************************************************************/ unsigned int name (unsigned char a,unsigned int b){
//函數(shù)內(nèi)容
return a; //返回值 } /**********************************************************************************************/
|