当前位置:首页 > 单片机 > 单片机
[导读]#include #include #include #include "INC\\UART.H" unsigned char xdata BaudRate = 0; unsigned char xdata Uart0_Counter = 0; unsigned char xdata Uart1_Counter = 0; unsigned char xdata Uart0_Buff[

#include

#include
#include
#include "INCUART.H"


unsigned char xdata BaudRate = 0;
unsigned char xdata Uart0_Counter = 0;
unsigned char xdata Uart1_Counter = 0;
unsigned char xdata Uart0_Buff[U0BUF_SIZE];
unsigned char xdata Uart1_Buff[U1BUF_SIZE];

/**********************************************************************/
/*名称:Uart_Init()
/*说明:串行口初始化程序
/*输入: BaudRate 串行口波特率
/*输出:无
/**********************************************************************/
void Uart_Init(unsigned char Baud)
{
unsigned int BaudR;
unsigned long RCAP,BRGR;

switch(Baud)
{
case 0x00 :BaudR = 300;break;
case 0x01 :BaudR = 1200;break;
case 0x02 :BaudR = 2400;break;
case 0x03 :BaudR = 4800;break;
case 0x04 :BaudR = 9600;break;
case 0x05 :BaudR = 19200;break;
default:BaudR = 19200;break;
}
RCAP = 0xFFFF - ((F_OSC/BaudR)/16);
RCAP2H= RCAP/0x100;
RCAP2L= RCAP%0x100;
T2MOD = 0x00;
T2CON = 0x34;
S0CON = 0x50;
ES0 = 1;

BRGR = F_OSC/BaudR - 16;
BRGR1 = BRGR/0x100;
BRGR0 = BRGR%0x100;
BRGCON = 0x01;
S1CON = 0x50;
ES1 = 1;

PCON = 0x80;
EA = 1;
}
/**********************************************************************/
/*名称:Uart0_ByteRcv()
/*说明:串行口0数据读取中断程序
/*输入:无
/*输出:无
/**********************************************************************/
static void Uart0_ByteRcv(void) interrupt 4 using 3
{
RI_0 = 0;
Uart0_Buff[Uart0_Counter] = S0BUF;
Uart0_Counter++;
if(Uart0_Counter >= U0BUF_SIZE)
{
Uart0_Counter = 0;
memset(Uart0_Buff,0x00,U0BUF_SIZE);
}
}

/**********************************************************************/
/*名称:Uart0_StrNSend()
/*说明:串行口0指定长度字符串输出程序
/*输入:*str 字符串指针
/*length字符串长度
/*输出:无
/**********************************************************************/
void Uart0_StrNSend(unsigned char *str ,unsigned char length)
{
unsigned char i;
ES0 = 0;
for(i=0;i{
S0BUF = *(str+i);
while(!TI_0);
TI_0 = 0;
}
ES0 = 1;

}

/**********************************************************************/
/*名称:Uart1_ByteRcv()
/*说明:串行口1数据读取中断程序
/*输入:无
/*输出:无
/**********************************************************************/
static void Uart1_ByteRcv (void) interrupt 10 using 3
{
RI_1 = 0;
Uart1_Buff[Uart1_Counter] = S1BUF;
Uart1_Counter++;
if(Uart1_Counter >= U1BUF_SIZE)
{
Uart1_Counter = 0;
memset(Uart1_Buff,0x00,U1BUF_SIZE);
}
}

/**********************************************************************/
/*名称:Uart1_ByteSend()
/*说明:串行口1单字符输出程序
/*输入: byte要输出的字符
/*输出:无
/**********************************************************************/
void Uart1_ByteSend(char byte)
{
ES1 = 0;
S1BUF = byte;
while(!TI_1);
TI_1 = 0;
ES1 = 1;
}

/**********************************************************************/
/*名称:Uart1_StrNSend()
/*说明:串行口1指定长度字符串输出程序
/*输入:*str 字符串指针
/*length字符串长度
/*输出:无
/**********************************************************************/
void Uart1_StrNSend(unsigned char *str ,unsigned char length)
{
unsigned char i;
ES1 = 0;
for(i=0;i{
S1BUF = *(str+i);
while(!TI_1);
TI_1 = 0;
}
ES1 = 1;
}
/**********************************************************************/
/*名称:Uart1_StrSend()
/*说明:串行口1字符串输出程序
/*输入:*str 字符串指针
/*输出:无
/**********************************************************************/
void Uart1_StrSend(unsigned char *str)
{
unsigned char i;
ES1 = 0;
for(i=0;i{
S1BUF = *(str+i);
while(!TI_1);
TI_1 = 0;
}
ES1 = 1;
}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭