?? serial.c
字號:
/*
*********************************************************************************************************
* uC/OS-II
* 實時內核
*
* (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
* 版權所有
*
* MCU-51 專用代碼
* KEIL C51大模式編譯
*
* 文件名 : SERIAL.C
* 作者 : Jean J. Labrosse
*********************************************************************************************************
*/
//**********************************************************************************
//基于中斷的串口驅動及顯示程序
//**********************************************************************************
//程序特點:
// 1.基于中斷,可并發執行
// 2.參數可配置(收發緩沖區大小,最大字符串長度,TAB鍵移動距離)
//**********************************************************************************
//使用方法:(此范例自包含,獨立于其他程序。)
// 先配制收發緩沖區大小等可變參數(在serial.h中的宏定義)
// 1.開頭加入#include <reg51.h>語句,一定要有。
// 2.初始化串口 InitSerial();//本例中為20MHz晶體,300波特率,模式2初始化
// 3.初始化串口緩沖區 InitSerialBuffer();
// 4.使用顯示字節,字,長字,字符,字符串,清屏函數。
//自包含
//**********************************************************************************
//波特率計算公式:
// TH1=256-(2^SMOD/32*Fosc/12*1/Bound)
// 其中:SMOD=0,1;Fosc=晶體或晶振頻率;Bound=波特率
// 本例中,SMOD=0;Fosc=20*10E6;Bound=300,所以TH1=0x52。
//Baud rate(20Mhz)
//300(52);1200(D5);2400(EA);4800(F5);9600(FB);19200(FD);38400(FF);
//**********************************************************************************
//書寫風格:
// 1.帶modify_前綴標志的函數改寫過的等效C庫函數。
// 2.單個單詞用小寫,modify_定義為前綴,不算一個單詞。
// 3.多個單詞(2個及以上),每個單詞首字母大寫。(有時變量名第一個單詞首字母小寫)
// 4.采用內縮風格,每次縮進8個空格。
//**********************************************************************************
//應用舉例:(可在KEIL仿真環境下運行)
//源程序文件:serial.h/serial.c/main.c
//main.c內容:
//#include <reg51.h>//Note:It must be added.必須在serial.c之前
//#include <serial.h>
//main()
//{
// unsigned char ch;
//
// InitSerial();
// InitSerialBuffer();
// while(1){
// PrintStr("\n");
// PrintByte(90);PrintStr(" ");
// PrintWord(90);PrintStr(" ");
// PrintLong(90);PrintStr(" ");
// PrintChar('y');PrintChar(' ');//千萬別寫成雙引號,否則打印0(亂字符)。
// PrintStr("\nHello!\nI'm YangYi.\n");
// PrintStr("Press any key to continue...");
// while(!modify_getch(&ch));
// }
//}
//**********************************************************************************
//建議:
// 你完全可以把該子程序當作函數庫使用,以便減少重復勞動,提高代碼質量。
//**********************************************************************************
#ifndef OS_MASTER_FILE
#include "includes.h"
#endif
//#include <includes.h>
//#include <serial.h>
//#include <reg51.h>
unsigned char TxBuf[LenTxBuf],RxBuf[LenRxBuf]; //收發緩沖區實體
unsigned char *inTxBuf,*outTxBuf,*inRxBuf,*outRxBuf; //收發緩沖區讀寫指針
bit TIflag=1;//Note:It must be 1.
void InitSerial() reentrant //串口初始化
{
TMOD=TMOD&0x0F;
TMOD=TMOD|0x20;
TL1=0xFD,TH1=0xFD; //19200 , 22.1184MHz
SCON=0x50;PCON=0x00;
TR1=1;
}
void InitSerialBuffer(void) reentrant //串口緩沖區初始化
{
inTxBuf=TxBuf;outTxBuf=TxBuf;
inRxBuf=RxBuf;outRxBuf=RxBuf;
ES=1;
//EA=1;
}
void serial(void) reentrant
{ //中斷在匯編中實現,去掉interrupt 4{//串口中斷服務子程序
unsigned char *t;
if(TI){
TI=0;
if(inTxBuf==outTxBuf) {TIflag=1;return;} //TxBuf Empty
SBUF=*outTxBuf; outTxBuf++;
if(outTxBuf==TxBuf+LenTxBuf) outTxBuf=TxBuf;
}
if(RI){
RI=0;
t=inRxBuf;t++;
if(t==RxBuf+LenRxBuf) t=RxBuf;
if(t==outRxBuf) return; //RxBuf Full
*inRxBuf=SBUF;
inRxBuf=t;
}
}
bit modify_getch(unsigned char *ch) reentrant //從串口緩沖區讀1字節數據
{
//ES=0;
if(inRxBuf==outRxBuf) {ES=1;return 0;} //RxBuf Empty
*ch=*outRxBuf; outRxBuf++;
if(outRxBuf==RxBuf+LenRxBuf) outRxBuf=RxBuf;
//ES=1;
return 1;
}
void PrintChar(unsigned char ch) reentrant //顯示字符
{
unsigned char *t;
//ES=0;
if(TIflag){
TIflag=0;
TI=1;
}
t=inTxBuf;t++;
if(t==TxBuf+LenTxBuf) t=TxBuf;
if(t==outTxBuf) {/*ES=1;*/return;} //TxBuf Full
*inTxBuf=ch;
inTxBuf=t;
//ES=1;
}
void PrintCh(unsigned char ch) reentrant //內部使用,不建議用戶看到。
{
if(ch>=0&&ch<=9) ch=ch+'0';
else ch=ch+'A'-10;
PrintChar(ch);
}
void insidePrintByte(unsigned char Byte) reentrant //內部使用,以十六進制格式顯示1個字節數據
{
unsigned char c;
c=Byte;
c=c>>4;
PrintCh(c);
c=Byte;c=c&0x0F;PrintCh(c);
}
void PrintByte(unsigned char Byte) reentrant //以十六進制格式顯示1個字節數據
{
EA=0;
insidePrintByte(Byte);
EA=1;
}
void insidePrintWord(unsigned int Word) reentrant //內部使用,以十六進制格式顯示1個字數據
{
insidePrintByte(Word>>8);
insidePrintByte(Word&0xFF);
}
void PrintWord(unsigned int Word) reentrant //以十六進制格式顯示1個字數據
{
EA=0;
insidePrintWord(Word);
EA=1;
}
void PrintLong(unsigned long LongWord) reentrant //以十六進制格式顯示1個長字數據
{
EA=0;
insidePrintWord(LongWord>>16);
insidePrintWord(LongWord&0xFFFF);
EA=1;
}
void PrintStr(unsigned char *str) reentrant //顯示字符串
{
int i;
unsigned char j;
unsigned char ch;
EA=0;
for(i=0;i<MaxLenStr;i++){
ch=*(str+i);
if(ch=='\0') break;
else if(ch=='\n'){PrintChar(10);PrintChar(13);}
else if(ch=='\t'){
for(j=0;j<TABNum;j++)
PrintChar(' ');
}
else PrintChar(ch);
}
EA=1;
}
void clrscr() reentrant //清屏
{
PrintStr("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
//25個回車換行清屏幕。
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -