📄 uart.c
字号:
#include "system.h"
volatile unsigned char UartSendBuffer[OutLEN]; //发送缓冲
volatile unsigned char UartReceiveBuffer[InLEN]; //接收数据缓冲
volatile unsigned char *outlast=UartSendBuffer; //最后由中断传输出去的字节位置
volatile unsigned char *putlast=UartSendBuffer; //最后放入发送缓冲区的字节位置
volatile unsigned char UartSendBufferemptyFlag=1; //缓冲区数据发完标志 发完=1
volatile unsigned char UartSendBufferHaveDataFlag=0; //发送缓冲区非空标志 有=1
volatile unsigned char UartReceiveCounter=0; //接收计数器
volatile unsigned char UartRxTimerStartFlag=0; //接收超时计数器启动标志
volatile unsigned char UartWaitForCounter=0; //接收超时计数器
volatile unsigned char UartDataReadyFlag=0; //接收完成标志
//=======================================================================================================
// 函数名称: UartInit
//
// 功能描述:串口初始化
//
// 输 入: void
//
// 输 出: void
//
// 全局变量: UartReceiveCounter ; UartRxTimerStartFlag
// 调用模块: 无
//
// 作 者: 卢明君
// 日 期: 2009年9月15日
// 备 注:
//-------------------------------------------------------------------------------------------------------
// 修改人:
// 日 期:
// 备 注:
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
/*
void UartInit(void)
{
SCON = 0x50;
//PCON |= 0x80;
//AUXR2|= 0x40;
//Timer1Init();
TR1=0; //停止定时器
// TCON=0x00; //定时器控制寄存器 注意:TCON只需操作一次
TMOD |= 0x20; //定时器1
TL1 = -(SYSCLK/12/32/baud);//0xfa; // -(SYSCLK/12/32/baud); //注意波特率加倍位
TH1 = TL1;
TR1=1; //启动定时器1
// ET1=1; //打开定时器1中断
UartReceiveCounter=0;
UartRxTimerStartFlag=0;
}
*/
//=======================================================================================================
// 函数名称: UartSendchar
//
// 功能描述:放入一个字节到发送缓冲区
//
// 输 入: unsigned char ucdata
//
// 输 出: void
//
// 全局变量: outlast ; putlast ; UartSendBuffer[] ; UartSendBufferHaveDataFlag ; UartSendBufferemptyFlag
// 调用模块: 无
//
// 作 者: 卢明君
// 日 期: 2009年9月15日
// 备 注:
//-------------------------------------------------------------------------------------------------------
// 修改人:
// 日 期:
// 备 注:
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
void UartSendchar(unsigned char ucdata)
{
ES=0; // 暂停串行中断,以免数据比较时出错
while((((outlast-putlast)==2)&&(outlast > putlast ))||((outlast < putlast)&&(OutLEN-(putlast-outlast)==2)))
{
ES=1;
ucdata++;
ucdata--;
ES=0;
}
*putlast=ucdata; // 放字节进入缓冲区
putlast++; // 发送缓冲区指针加1
if (putlast==UartSendBuffer+OutLEN) putlast=UartSendBuffer; // 指针到了顶部换到底部
UartSendBufferHaveDataFlag=1;
if (UartSendBufferemptyFlag) // 缓冲区无数据
{
UartSendBufferemptyFlag =0;
SBUF=*outlast; // 未发送完继续发送
outlast++; // 最后传出去的字节位置加1
if (outlast==UartSendBuffer+OutLEN)outlast=UartSendBuffer; // 地址到顶部回到底部
if (putlast==outlast)UartSendBufferHaveDataFlag=0; // 数据发送完置发送缓冲区空标志
} // 缓冲区开始为空,置为有,启动发送
ES=1;
}
//=======================================================================================================
// 函数名称: UartSendString
//
// 功能描述:发送字符串
//
// 输 入: unsigned char *str
//
// 输 出: void
//
// 全局变量:
// 调用模块: UartSendchar
//
// 作 者: 卢明君
// 日 期: 2009年9月15日
// 备 注:
//-------------------------------------------------------------------------------------------------------
// 修改人:
// 日 期:
// 备 注:
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
void UartSendString(unsigned char *str)
{
while(*str) // 遇到停止符0结束
{
UartSendchar(*str++);
//while(UartSendBufferHaveDataFlag);
}
}
//=======================================================================================================
// 函数名称: UartSendByte
//
// 功能描述:发送一串数据
//
// 输 入: unsigned char *Startaddr,unsigned char SendByte
//
// 输 出: void
//
// 全局变量:
// 调用模块: UartSendchar
//
// 作 者: 卢明君
// 日 期: 2009年9月15日
// 备 注:
//-------------------------------------------------------------------------------------------------------
// 修改人:
// 日 期:
// 备 注:
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
void UartSendByte(unsigned char *Startaddr,unsigned char SendByte)
{
while(SendByte--)
{
UartSendchar(*Startaddr++);
}
}
void Uart(void) interrupt 4 using 2
{
if(TI)
{
TI=0;
if (UartSendBufferHaveDataFlag)
{
SBUF=*outlast; // 未发送完继续发送
outlast++; // 最后传出去的字节位置加1
if (outlast==UartSendBuffer+OutLEN)outlast=UartSendBuffer; // 地址到顶部回到底部
if (putlast==outlast)UartSendBufferHaveDataFlag=0; // 数据发送完置发送缓冲区空标志
}
else UartSendBufferemptyFlag =1;
}
if(RI)
{
RI = 0;
//==========================================================
// 若有必要 有待于加入 偶校验算法 .数据位的bit7位为校验位
//==========================================================
UartReceiveBuffer[UartReceiveCounter++]=SBUF&0x7f;
UartRxTimerStartFlag=1; // 启动超时计数器
UartWaitForCounter=0; // 清超时计数器 // 10ms
if (UartReceiveCounter>=InLEN)
{
UartDataReadyFlag=1;
//UartReceiveCounter=0;
REN=0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -