#ifndef _PC232_H_
#define _PC232_H_
#include <reg51.h>
#include "intrins.h"
#define uint8 unsigned char
#define uint16 unsigned int
#define PC232_MAX 16 //最大字符串長度
uint8 PC232_data; //當前字符
uint8 PC232_stringSBUF[PC232_MAX]={" "}; //字符串
uint8 PC232_string[PC232_MAX]={" "}; //字符串
uint8 PC232_Loc=0; //字符地址
void PC232delay( ) //10ms@22.0784MHz
{
unsigned char i, j, k;
i = 1;
j = 216;
k = 35;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void UartInit() //9600bps@22.0784MHz
{
EA=1;
ES=1;
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位數據,可變波特率
TMOD &= 0x0F; //清除定時器1模式位
TMOD |= 0x20; //設定定時器1為8位自動重裝方式
TL1 = 0xFA; //設定定時初值
TH1 = 0xFA; //設定定時器重裝值
ET1 = 0; //禁止定時器1中斷
TR1 = 1; //啟動定時器1
}
/*------------------------------- 發送一個字節---------------------------------------*/
void SendByte(uint8 dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
/*------------------- 發送一個字符串------------------------------------------*/
void SendStr(uint8 *s)
{
while(*s != '\0') //字符串是否發送完,
{
SendByte(*s);
s++;
}
}
/*------------------- 串口中斷------------------------------------------*/
void PC232serial()interrupt 4
{
if(RI && PC232_Loc<PC232_MAX){
switch(PC232_MAX){
case 1 : PC232_data=SBUF;
SendByte(PC232_data);
PC232_string[0]=SBUF;
PC232_string[1]=0x00;
break;
default: PC232_data=SBUF;
SendByte(PC232_data);
PC232_stringSBUF[PC232_Loc]=SBUF;
PC232_string[PC232_Loc]=PC232_stringSBUF[PC232_Loc];
PC232_string[++PC232_Loc]=0x00;
break;
}
}
if(PC232_Loc==PC232_MAX){
PC232_Loc=0;
PC232_stringSBUF[0]=0x00;
}
RI=0;
}
#endif
|