51 串口收發(fā)程序源代碼 環(huán)形緩沖區(qū)實(shí)現(xiàn)
/***** 51 串口收發(fā) 環(huán)形緩沖區(qū)實(shí)現(xiàn) *****
* 版本.........: 1.0
* 目標(biāo).........: STC12C5A32AD
* 文件名.......: uart.c
* 版本.........: 1.0
* 目標(biāo).........: STC12C5A32AD
* 文件名.......: uart.c
* 編譯器.......: Keil C51 V9.02
**************************************/
#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位數(shù)據(jù),可變波特率
AUXR|=0x40;//定時器1時鐘為Fosc,即1T
AUXR&=0xfe;//串口1選擇定時器1為波特率發(fā)生器
TMOD&=0x0f;//清除定時器1模式位
TMOD|=0x20;//設(shè)定定時器1為8位自動重裝方式
TL1=256-(unsignedchar)(F_CPU/UART_BAUD/32+0.5);//設(shè)定定時初值
TH1=256-(unsignedchar)(F_CPU/UART_BAUD/32+0.5);//設(shè)定定時器重裝值
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);// 等待數(shù)據(jù)發(fā)送,不丟失
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;
}
**************************************/
#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位數(shù)據(jù),可變波特率
AUXR|=0x40;//定時器1時鐘為Fosc,即1T
AUXR&=0xfe;//串口1選擇定時器1為波特率發(fā)生器
TMOD&=0x0f;//清除定時器1模式位
TMOD|=0x20;//設(shè)定定時器1為8位自動重裝方式
TL1=256-(unsignedchar)(F_CPU/UART_BAUD/32+0.5);//設(shè)定定時初值
TH1=256-(unsignedchar)(F_CPU/UART_BAUD/32+0.5);//設(shè)定定時器重裝值
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);// 等待數(shù)據(jù)發(fā)送,不丟失
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;
}
評論