?? serial.c
字號(hào):
/*
*********************************************************************************************************
* uC/OS-II
* 實(shí)時(shí)內(nèi)核
*
* (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
* 版權(quán)所有
*
* MCU-51 專用代碼
* KEIL C51大模式編譯
*
* 文件名 : SERIAL.C
* 作者 : Jean J. Labrosse
* 改編 : 楊屹 gdtyy@ri.gdt.com.cn 巨龍公司系統(tǒng)集成開發(fā)部 2002.09.27
*********************************************************************************************************
*/
//**********************************************************************************
//楊屹 2002/08/20 第一版
//基于中斷的串口驅(qū)動(dòng)及顯示程序
//聯(lián)系方法:gdtyy@ri.gdt.com.cn(2003/07/31以前有效)
//**********************************************************************************
//程序特點(diǎn):
// 1.基于中斷,可并發(fā)執(zhí)行
// 2.參數(shù)可配置(收發(fā)緩沖區(qū)大小,最大字符串長(zhǎng)度,TAB鍵移動(dòng)距離)
//**********************************************************************************
//使用方法:(此范例自包含,獨(dú)立于其他程序。)
// 先配制收發(fā)緩沖區(qū)大小等可變參數(shù)(在serial.h中的宏定義)
// 1.開頭加入#include <reg51.h>語句,一定要有。
// 2.初始化串口 InitSerial();//本例中為20MHz晶體,300波特率,模式2初始化
// 3.初始化串口緩沖區(qū) InitSerialBuffer();
// 4.使用顯示字節(jié),字,長(zhǎng)字,字符,字符串,清屏函數(shù)。
//自包含
//**********************************************************************************
//波特率計(jì)算公式:
// 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);
//**********************************************************************************
//書寫風(fēng)格:
// 1.帶yy前綴標(biāo)志的函數(shù)為楊屹改寫的等效C庫函數(shù)。
// 2.單個(gè)單詞用小寫,yy定義為前綴,不算一個(gè)單詞。
// 3.多個(gè)單詞(2個(gè)及以上),每個(gè)單詞首字母大寫。(有時(shí)變量名第一個(gè)單詞首字母小寫)
// 4.采用內(nèi)縮風(fēng)格,每次縮進(jìn)8個(gè)空格。
//**********************************************************************************
//應(yīng)用舉例:(可在KEIL仿真環(huán)境下運(yùn)行)
//源程序文件:serial.h/serial.c/main.c
//main.c內(nèi)容:
//#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(' ');//千萬別寫成雙引號(hào),否則打印0(亂字符)。
// PrintStr("\nHello!\nI'm YangYi.\n");
// PrintStr("Press any key to continue...");
// while(!yygetch(&ch));
// }
//}
//**********************************************************************************
//建議:
// 你完全可以把該子程序當(dāng)作函數(shù)庫使用,以便減少重復(fù)勞動(dòng),提高代碼質(zhì)量。
//**********************************************************************************
#ifndef OS_MASTER_FILE
#include "includes.h"
#endif
//#include <includes.h>
#include <serial.h>
//#include <reg51.h>
unsigned char xdata TxBuf[LenTxBuf],RxBuf[LenRxBuf];//收發(fā)緩沖區(qū)實(shí)體
unsigned char xdata *inTxBuf,*outTxBuf,*inRxBuf,*outRxBuf;//收發(fā)緩沖區(qū)讀寫指針
bit TIflag=1;//Note:It must be 1.
void InitSerial() reentrant//串口初始化
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = UART1_PAGE;
SCON1 = 0x50; // SCON1: mode 0, 8-bit UART, enable RX
SFRPAGE = TIMER01_PAGE;
TMOD &= ~0xF0;
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x01; // T1M = 0; SCA1:0 = 01
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x02; // T1M = 0; SCA1:0 = 10
}
TL1 = TH1; // initialize Timer1
TR1 = 1; // start Timer1
SFRPAGE = UART1_PAGE;
TI1 = 1; // Indicate TX1 ready
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
void InitSerialBuffer(void) reentrant//串口緩沖區(qū)初始化
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = UART1_PAGE;
inTxBuf=TxBuf;outTxBuf=TxBuf;
inRxBuf=RxBuf;outRxBuf=RxBuf;
EIE2 |= 0x40; //使能urat2中斷
//EA=1;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
void serial(void) reentrant
{ //中斷在匯編中實(shí)現(xiàn),去掉interrupt 4{//串口中斷服務(wù)子程序
unsigned char *t;
unsigned char temp;
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = UART1_PAGE;
if(TI1){
TI1=0;
if( inTxBuf == outTxBuf)
{TIflag = 1;
return;}//TxBuf Empty
temp = *outTxBuf ;
SBUF1 = temp;
outTxBuf++;
if(outTxBuf==TxBuf+LenTxBuf) outTxBuf=TxBuf;
}
if(RI1){
RI1=0;
t=inRxBuf;t++;
if(t==RxBuf+LenRxBuf) t=RxBuf;
if(t==outRxBuf) return; //RxBuf Full
*inRxBuf = SBUF1;
inRxBuf=t;
}
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
bit yygetch(unsigned char *ch) reentrant //從串口緩沖區(qū)讀1字節(jié)數(shù)據(jù)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = UART1_PAGE;
//ES=0;
if(inRxBuf==outRxBuf) { EIE2 |= 0x40; return 0;} //RxBuf Empty
*ch=*outRxBuf; outRxBuf++;
if(outRxBuf==RxBuf+LenRxBuf) outRxBuf=RxBuf;
//ES=1;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
return 1;
}
void PrintChar(unsigned char ch) reentrant//顯示字符
{
unsigned char *t;
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = UART1_PAGE;
//ES=0;
if(TIflag){
TIflag=0;
TI1=1;
}
t=inTxBuf;t++;
if(t==TxBuf+LenTxBuf) t=TxBuf;
if(t==outTxBuf) {/*ES=1;*/return;} //TxBuf Full
*inTxBuf=ch;
inTxBuf=t;
//ES=1;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
void PrintCh(unsigned char ch) reentrant//內(nèi)部使用,不建議用戶看到。
{
if(ch>=0&&ch<=9) ch=ch+'0';
else ch=ch+'A'-10;
PrintChar(ch);
}
void insidePrintByte(unsigned char Byte) //內(nèi)部使用,以十六進(jìn)制格式顯示1個(gè)字節(jié)數(shù)據(jù)
{
unsigned char c;
c=Byte;
c=c>>4;
PrintCh(c);
c=Byte;c=c&0x0F;PrintCh(c);
}
void PrintByte(unsigned char Byte) reentrant//以十六進(jìn)制格式顯示1個(gè)字節(jié)數(shù)據(jù)
{
EA=0;
insidePrintByte(Byte);
EA=1;
}
void insidePrintWord(unsigned int Word) reentrant//內(nèi)部使用,以十六進(jìn)制格式顯示1個(gè)字?jǐn)?shù)據(jù)
{
insidePrintByte(Word>>8);
insidePrintByte(Word&0xFF);
}
void PrintWord(unsigned int Word) reentrant//以十六進(jìn)制格式顯示1個(gè)字?jǐn)?shù)據(jù)
{
EA=0;
insidePrintWord(Word);
EA=1;
}
void PrintLong(unsigned long LongWord) reentrant//以十六進(jìn)制格式顯示1個(gè)長(zhǎng)字?jǐn)?shù)據(jù)
{
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個(gè)回車換行清屏幕。
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -