?? serial.c
字號:
//串口中斷服務程序,僅需做簡單調用即可完成串口輸入輸出的處理
//出入均設有緩沖區,大小可任意設置。
//*************************************************************************
#include "c8051f020.h" // SFR declarations
#include "intrins.h"
#define BAUDRATE0 115200 // 用戶定義的UART0 波特率
#define DB_SENDMAXSIZE 0xf0
#define DB_RECMAXSIZE 0xf0
bit CommRecDataOverflowFlag,FlagRecComm,SendItComm;
extern unsigned char Count1ms;
unsigned char CommSendBufferHead, CommSendBufferTail;
unsigned char xdata CommSendBuffer[DB_SENDMAXSIZE];
unsigned char CommRecBufferHead, CommRecBufferTail;
unsigned char xdata CommRecBuffer[DB_RECMAXSIZE];
void ClearCommRecBuffer(void)
{
unsigned char i;
CommRecBufferHead=CommRecBufferTail=0;
CommSendBufferHead=CommSendBufferTail=0;
FlagRecComm=0;
for (i=0;i<DB_SENDMAXSIZE;i++)
{
CommSendBuffer[i]=0;
}
for (i=0;i<DB_RECMAXSIZE;i++)
{
CommRecBuffer[i]=0;
}
}
void OpenComm(void)
{
PCON |= 0x80; // SMOD=1 (HW_UART uses Timer 1 overflow with no divide down).
TMOD |= 0x20; // Configure Timer 1 for use by UART0
CKCON |= 0x10; // Timer 1 derived from SYSCLK
RCAP2H=(65536-(SYSCLK/BAUDRATE0/32))/256;
RCAP2L=(65536-(SYSCLK/BAUDRATE0/32))%256;
TH2=RCAP2H;TL2=RCAP2L;
CT2=0; //T2:timer mode
TR2=1;
TCLK=1;RCLK=1; //說明:52,對于SIO0,可選擇T1(TCLK=0,RCLK=0)或T2(TCLK=1,RCLK=1)作為波特率發生器
// SIO1只能用T1作為波特率發生器
//baud=OSC/(32*(65536-[RCAP2H,RCAP2L])
CommSendBufferHead=CommSendBufferTail=0; // set the head and tail to the base of the ring buffer
CommRecBufferHead=CommRecBufferTail=0;
FlagRecComm=0;
RI0=0; // Clear HW_UART receive and transmit
TI0=0; // complete indicators.
SCON0 = 0x50; // Configure UART0 for mode 1, receiver enabled.
ES0=1; // allow the serial interrupt
// PS=1;
SendItComm=1;
}
void SendCommChar(char ch)
{
CommSendBuffer[CommSendBufferTail]=ch;
CommSendBufferTail++;
if (CommSendBufferTail==DB_SENDMAXSIZE)
{
CommSendBufferTail=0;
}
if (SendItComm)
{
SendItComm=0;
SBUF0=CommSendBuffer[CommSendBufferHead];
}
while (CommSendBufferHead!=CommSendBufferTail);
return ;
}
code unsigned char hex[]="0123456789ABCDEF";
void SendCommHex(unsigned char senddata)//往串口發送hex碼 表示的一個字符 例如senddata=0x3A那么將向串口發送兩個字符'3','A'hex[]為轉換表,在前面有定義
{
unsigned char ch;
ch=senddata>>4;
SendCommChar(hex[ch]);
ch=senddata&0x0F;
SendCommChar(hex[ch]);
}
void SendCommWord(unsigned int asciiword)
//向串口發送一個int型的 hex碼表示的字符 例如:asciiword=0x124D 將向串口發送4個字符:'1','2','4','D'
{
unsigned char ascii;
ascii=asciiword>>8;
SendCommHex(ascii);
ascii=asciiword&0xff;
SendCommHex(ascii);
}
void SendCommLong(unsigned long asciilong)
{
SendCommWord(asciilong>>16);
SendCommWord(asciilong&0xffff);
}
void SendCommString(unsigned char *base)
{
unsigned char i=0;
if (base[0]==0) return;
for (;;)
{
if (base[i]==0) break;
CommSendBuffer[CommSendBufferTail]=base[i];
CommSendBufferTail++;
if (CommSendBufferTail==DB_SENDMAXSIZE)
{
CommSendBufferTail=0;
}
i++;
}
if (SendItComm)
{
SendItComm=0;
SBUF0=CommSendBuffer[CommSendBufferHead];
}
while (CommSendBufferHead!=CommSendBufferTail);
}
void CommISR(void) interrupt 4
{
if (_testbit_(TI0))
{
TI0=0;
CommSendBufferHead++;
if (CommSendBufferHead==DB_SENDMAXSIZE)
{
CommSendBufferHead=0;
}
if (CommSendBufferHead!=CommSendBufferTail)
{
SBUF0=CommSendBuffer[CommSendBufferHead]; // send the next byte
SendItComm=0;
}
else
{
SendItComm=1;
}
}
if (_testbit_(RI0))
{
RI0=0;
if (CommRecBufferTail==CommRecBufferHead)
{
CommRecDataOverflowFlag=1; //接收緩沖區溢出
}
CommRecBuffer[CommRecBufferTail]=SBUF0; //receive data
CommRecBufferTail++;
if (CommRecBufferTail==DB_RECMAXSIZE)
{
CommRecBufferTail=0;
}
FlagRecComm=1;
}
}
//從接收緩沖區讀數據 ,無數據返回0,有數據返回1
bit GetCommChar(unsigned char idata *ch)
{
if (CommRecBufferTail==CommRecBufferHead) return 0;
*ch=CommRecBuffer[CommRecBufferHead];
CommRecBufferHead++;
if (CommRecBufferHead==DB_RECMAXSIZE)
{
CommRecBufferHead=0;
}
if (CommRecBufferTail==CommRecBufferHead) FlagRecComm=0;
return 1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -