找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

一段串口代码不懂,请高手逐行解释~

[复制链接]
admin 发表于 2012-9-2 08:35:33 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
#include "uart.h"
#include "uart_config.h"
#include "main_config.h"
#include "STC_NEW_8051.H"
volatile unsigned char UART_TxBuf[UART_TXBUF_SIZE];
volatile unsigned char UART_TxBuf_Index = 0;
volatile unsigned char UART_TxBuf_Length = 0;
volatile unsigned char UART_RxBuf[UART_RXBUF_SIZE];
volatile unsigned char UART_RxBuf_Index = 0;
volatile unsigned char UART_RxBuf_Length = 0;
volatile bit UART_RxBuf_IsFull = 0;
void UART_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 - (unsigned char)(F_CPU / UART_BAUD / 32 + 0.5);//设定定时初值
TH1 = 256 - (unsigned char)(F_CPU / UART_BAUD / 32 + 0.5); //设定定时器重装值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
ES = 1;
TI = 0;
EA = 1; // 使能总中断
}
void UART_TxByte(unsigned char byte)
{
EA = 0;
if (UART_Sending)
{
EA = 1;
while (UART_TxBuf_Length >= UART_TXBUF_SIZE); // 等待数据发送,不丢失
EA = 0;
if (UART_TxBuf_Length < UART_TXBUF_SIZE)
{
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;
}
unsigned char temp = 0;
void UART_ISR(void) interrupt 4
{
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_SIZE)
{
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;
}
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-24 11:59 , Processed in 0.051067 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表