|
單片機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; //返回值 } /**********************************************************************************************/
|