uart.c
#include <Atmel/AT89X51.h>
#include <uart.h>
/*定义接收&发送区缓存*/
vuchar UART_RxBuf[UART_RXBUF_SIZE];
vuchar UART_TxBuf[UART_TXBUF_SIZE];
/*定义串口缓存*/
static UART UART_Buf;
/* 初始化串口对应缓冲区*/
void UART_Init()
{
UART_Buf.Rx_ptr = UART_RxBuf;
UART_Buf.Uart_WR = UART_RxBuf;
UART_Buf.User_RD = UART_RxBuf;
UART_Buf.UART_RxBuf_IsFull = 0;
UART_Buf.Tx_ptr = UART_TxBuf;
UART_Buf.Uart_RD = UART_TxBuf;
UART_Buf.User_WR = UART_TxBuf;
UART_Buf.UART_TxBuf_IsFull = 0;
}
/*串口初始化*/
void RS232_Init()
{
SCON=0x50; //UART mod=1 REN=1
TMOD=0x20; //Timer1 Mod=2
PCON|=0x00; //SMOD1=0
TH1=0xfd;
TL1=0xfd; //9600bps at 11.0592
ES=1; //Com interrupt enable
TR1=1;
EA=1;
UART_Init();
}
void Uart_F()
{
TI=1;
}
/*串口写数据到缓存区*/
// 接收数据
uchar UART_TxByte(uchar byte)
{
uchar *ptr;
if (UART_Buf.UART_RxBuf_IsFull==1)
{
return 1;
}
ptr = UART_Buf.Uart_WR;
ptr++;
if (ptr == UART_Buf.Rx_ptr+UART_RXBUF_SIZE)
ptr=UART_Buf.Rx_ptr;
if (ptr == UART_Buf.User_RD)
{
UART_Buf.UART_RxBuf_IsFull=1;
return 1;
}
*UART_Buf.Uart_WR = byte;
UART_Buf.Uart_WR = ptr;
return 0;
}
/*串口从缓存区读数据*/
uchar UART_RxByte(uchar *dat)
{
if (UART_Buf.Uart_RD == UART_Buf.User_WR) /** 发送缓冲区无数据*/
{
return 1;
}
*dat = *(UART_Buf.Uart_RD++);
if (UART_Buf.Uart_RD == UART_Buf.Tx_ptr+UART_TXBUF_SIZE)
UART_Buf.Uart_RD = UART_Buf.Tx_ptr;
if (UART_Buf.UART_TxBuf_IsFull) //处理标志位
{
UART_Buf.UART_TxBuf_IsFull=0;
}
return 0;
}
/*用户写数据到缓存区*/
// 发送数据
uchar User_WRByte(uchar byte)
{
uchar *ptr;
if (UART_Buf.UART_TxBuf_IsFull == 1)
{
return 1;
}
ptr = UART_Buf.User_WR;
ptr++;
if (ptr == UART_Buf.Tx_ptr+UART_TXBUF_SIZE)
ptr = UART_Buf.Tx_ptr;
if(ptr == UART_Buf.Uart_RD)
{
UART_Buf.UART_TxBuf_IsFull = 1;
Uart_F();
//return 1;
}
*UART_Buf.User_WR=byte;
UART_Buf.User_WR=ptr;
return 0;
}
/*用户从缓存区读数据*/
uchar User_RDByte(uchar *dat)
{
if(UART_Buf.User_RD==UART_Buf.Uart_WR) /** 接收缓冲区无数据*/
{
return 1;
}
*dat=*(UART_Buf.User_RD++);
if(UART_Buf.User_RD==UART_Buf.Rx_ptr+UART_RXBUF_SIZE)
UART_Buf.User_RD=UART_Buf.Rx_ptr;
if (UART_Buf.UART_RxBuf_IsFull) //处理标志位
{
UART_Buf.UART_RxBuf_IsFull=0;
}
return 0;
}
/*串口中断*/
void serial_IT(void) interrupt 4
{
uchar dat;
EA=0;
if(TI)
{
if (UART_RxByte(&dat) == 0) {
SBUF = dat;
}
while(TI==0);
TI=0;
}
if(RI)
{
RI=0;
dat = SBUF;
UART_TxByte(dat);
}
EA=1;
}
main.c
#include <Atmel/AT89X51.h>
#include <uart.h>
/*写字符串函数*/
void UART_PutString(char *str)
{
while (*str)
{
if( User_WRByte(*str++)==1 ){
// Uart_F();
str--;
}
}
}
/*主函数*/
void main(void)
{
uchar dat;
RS232_Init();
UART_PutString("12345678");
/* UART_PutString("********** 51 Uart Test Program **********\r\n");
UART_PutString(" Version.........: 1.0 \r\n");
UART_PutString(" Writer..........: zzf \r\n");
UART_PutString(" Target..........: AT89c51 \r\n");
UART_PutString(" Compiler........: Keil C51 V9.02 \r\n");
UART_PutString(" Creation time...: 2011.05.26 \r\n");
UART_PutString(" Last Modify.....: 2011.05.26 \r\n");
UART_PutString("******************************************\r\n");
*/
EA=1;
while(1)
{
if (User_RDByte(&dat) == 0) {
User_WRByte(dat);
// Uart_F();
}
}
}
uart.h
#ifndef _uart_h_
#define _uart_h_
#define vuchar volatile unsigned char
#define uchar unsigned char
#define UART_TXBUF_SIZE 5
#define UART_RXBUF_SIZE 5
void RS232_Init();
void UART_Init();
void Uart_F();
uchar UART_TxByte(uchar byte);
uchar UART_RxByte(uchar *dat);
uchar User_WRByte(uchar byte);
uchar User_RDByte(uchar *dat);
typedef struct UART
{
uchar *Rx_ptr;
uchar *Uart_WR;
uchar *User_RD;
uchar UART_RxBuf_IsFull;
uchar *Tx_ptr;
uchar *Uart_RD;
uchar *User_WR;
uchar UART_TxBuf_IsFull;
}UART;
#endif
UART_PutString("12345678");发送时只能显示 45678 ,123丢失
我单步调试时 看到中断时123 也送到 发送SBUF了,求高手指点!!!不甚感谢! |