部分代码:
* 版别………: 1.0
* 方针………: STC12C5A32AD
* 文件名…….: uart.c
**************************************/
#include “uart.h”
#include “uart_config.h”
#include “main_config.h”
#include “STC_NEW_8051.H”
volatileunsignedcharUART_TxBuf[UART_TXBUF_SIZE];
volatileunsignedcharUART_TxBuf_Index=0;
volatileunsignedcharUART_TxBuf_Length=0;
volatileunsignedcharUART_RxBuf[UART_RXBUF_SIZE];
volatileunsignedcharUART_RxBuf_Index=0;
volatileunsignedcharUART_RxBuf_Length=0;
volatilebitUART_RxBuf_IsFull=0;
voidUART_Init(void)
{
PCON&=0x7f;//波特率不倍速
SCON=0x50;//8位数据,可变波特率
AUXR|=0x40;//守时器1时钟为Fosc,即1T
AUXR&=0xfe;//串口1挑选守时器1为波特率发生器
TMOD&=0x0f;//铲除守时器1形式位
TMOD|=0x20;//设定守时器1为8位主动重装方法
TL1=256-(unsignedchar)(F_CPU/UART_BAUD/32+0.5);//设定守时初值
TH1=256-(unsignedchar)(F_CPU/UART_BAUD/32+0.5);//设定守时器重装值
ET1=0;//制止守时器1中止
TR1=1;//发动守时器1
ES=1;
TI=0;
EA=1;// 使能总中止
}
volatileunsignedcharUART_Sending=0;
voidUART_TxByte(unsignedcharbyte)
{
EA=0;
if(UART_Sending)
{
EA=1;
while(UART_TxBuf_Length>=UART_TXBUF_SIZE);// 等候数据发送,不丢掉
EA=0;
if(UART_TxBuf_Length
UART_TxBuf[UART_TxBuf_Index]=byte;
UART_TxBuf_Length++;
UART_TxBuf_Index++;
if(UART_TxBuf_Index>=UART_TXBUF_SIZE)
UART_TxBuf_Index=0;
}
}
else
{
SBUF=byte;
UART_Sending=1;
}
EA=1;
}
unsignedchartemp=0;
voidUART_ISR(void)interrupt4
{
EA=0;
if(TI)
{
TI=0;
if(UART_TxBuf_Length==0)
{
UART_Sending=0;
}
else
{
if(UART_TxBuf_Index>=UART_TxBuf_Length)
SBUF=UART_TxBuf[UART_TxBuf_Index-UART_TxBuf_Length];
else
SBUF=UART_TxBuf[UART_TXBUF_SIZE+UART_TxBuf_Index-UART_TxBuf_Length];
UART_TxBuf_Length–;
}
}
if(RI)
{
RI=0;
temp=SBUF;
if(UART_RxBuf_Length
UART_RxBuf[UART_RxBuf_Index]=temp;
UART_RxBuf_Length++;
UART_RxBuf_Index++;
if(UART_RxBuf_Index>=UART_RXBUF_SIZE)
UART_RxBuf_Index=0;
}
else
{
UART_RxBuf_IsFull=1;
}
}
EA=1;
}