📄 uart.c
字号:
//#include "ML-330.H"
//#define SYSCLK 24500000
#include <string.h>
#include "c8051F330.h"
//#include "const.h"
#include "uart.h"
//串口读写驱动程序
#define SYSCLK 22118400
#define MAX_LEN 128
#define MAX_SEND_LEN 256
volatile bit readFlag0 = 0; //Uart0中断接收处理标志
volatile bit readFlag1 = 0; //Uart1中断接收处理标志
volatile bit sendFlag0 = 0; //Uart0中断发送处理标志
volatile bit sendFlag1 = 0; //Uart1中断发送处理标志
volatile unsigned char xdata PRead0 = 0; //uart0接收缓冲读指针
volatile unsigned char xdata PWrite0 = 0; //uart0接收缓冲写指针
volatile unsigned char xdata PRead1 = 0; //uart1接收缓冲读指针
volatile unsigned char xdata PWrite1 = 0; //uart1接收缓冲写指针
volatile unsigned char xdata PSendRead0 = 0; //uart0发送缓冲读指针
volatile unsigned char xdata PSendWrite0 = 0; //uart0发送缓冲写指针
volatile unsigned char xdata PSendRead1 = 0; //uart1发送缓冲读指针
volatile unsigned char xdata PSendWrite1 = 0; //uart1发送缓冲写指针
unsigned char xdata BufData0[MAX_LEN]; //uart0接收缓冲
unsigned char xdata BufSend0[MAX_SEND_LEN]; //uart0发送缓冲
unsigned char xdata BufData1[MAX_LEN]; //uart1接收缓冲
unsigned char xdata BufSend1[MAX_SEND_LEN]; //uart1发送缓冲
unsigned char xdata SendCount0 = 0; //uart0发送字节数
unsigned char xdata SendCount1 = 0; //uart0发送字节数
void Uart0_Init(unsigned long BAUDRATE)
{
SCON0 = 0x10;//允许接收
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = 255-(SYSCLK/BAUDRATE);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x09;
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
/*TH1 = 63;
CKCON &= ~0x0B;
CKCON |= 0x08;*/
TL1 = TH1;
TMOD &= ~0xf0;
TMOD |= 0x20;
TR1 = 1;
// TI0 = 1;
}
//串口接收函数
unsigned char UART_Read(const unsigned char ComPort,
unsigned char *Buffer)
{
unsigned char xdata BufferStr[MAX_LEN];
int xdata readCounts = 0; //已经读取的字符个数,与MAX_LEN比较
unsigned char xdata temp = 0;
unsigned char xdata BufLen = 0;
if ((PRead0!=PWrite0) & (ComPort == 0))
{
temp = PWrite0;
readCounts = (int)temp - (int)PRead0;
if (readCounts > 0)
{
memcpy( BufferStr, BufData0 + PRead0 , readCounts);
PRead0 = temp;
memcpy(Buffer,BufferStr,readCounts);
BufLen = readCounts;
}
else if ( readCounts < 0)
{
memcpy( BufferStr, BufData0 + PRead0 , MAX_LEN-PRead0);
memcpy( BufferStr+MAX_LEN-PRead0, BufData0 , temp);
PRead0 = temp;
memcpy(Buffer,BufferStr,MAX_LEN+readCounts);
BufLen = MAX_LEN+readCounts;
}
readFlag0 = 1;
}
else if ((PRead1!=PWrite1) & (ComPort == 1))
{
temp = PWrite1;
readCounts = (int)temp - (int)PRead1;
memset( BufferStr, 0, MAX_LEN );
if (readCounts > 0)
{
memcpy( BufferStr, BufData1 + PRead1 , readCounts);
PRead1 = temp;
memcpy(Buffer,BufferStr,readCounts);
BufLen = readCounts;
}
else if ( readCounts < 0)
{
memcpy( BufferStr, BufData1 + PRead1 , MAX_LEN-PRead1);
memcpy( BufferStr+MAX_LEN-PRead1, BufData1 , temp);
PRead1 = temp;
memcpy(Buffer,BufferStr,MAX_LEN+readCounts);
BufLen = MAX_LEN+readCounts;
}
readFlag1 = 1;
}
return BufLen;
}
void Send_Char(const unsigned char com,unsigned char ch)
{
switch (com)
{
case 0:
//SFRPAGE = UART0_PAGE;
SBUF0 = ch;
break;
case 1:
//SFRPAGE = UART1_PAGE;
//SBUF1 = ch;
break;
default:
break;
}
}
//串口发送函数
void UART_Send(const unsigned char ComPort,
const unsigned char *Data,
const unsigned char DataSize)
{
unsigned int k = 0;
int i;
if ((DataSize > 0)&(DataSize< MAX_SEND_LEN))
{
switch (ComPort)
{
case 0:
//将需要发送的数据放入相应的发送缓冲内
for (i=0;i<DataSize;i++)
{
BufSend0[PSendWrite0] = Data[i];
PSendWrite0++;
if (PSendWrite0 == MAX_SEND_LEN)
{
PSendWrite0 = 0;
}
}
//判断中断服务程序是否完成发送(发送标志位是否为0)
if (sendFlag0 == 0)
{
sendFlag0 = 1; //置发送标志位为1
Send_Char(ComPort,*Data);//调用发送字符函数,发送第一个字节,触发发送中断
}
break;
case 1:
//将需要发送的数据放入相应的发送缓冲内
for (i=0;i<DataSize;i++)
{
BufSend1[PSendWrite1] = Data[i];
PSendWrite1++;
if (PSendWrite1 == MAX_SEND_LEN)
{
PSendWrite1 = 0;
}
}
//判断中断服务程序是否完成发送(发送标志位是否为0)
if (sendFlag1 == 0)
{
sendFlag1 = 1; //置发送标志位为1
Send_Char(ComPort,*Data);//调用发送字符函数,发送第一个字节,触发发送中断
}
break;
default:
break;
}
}
}
//UART0中断服务程序. 接收字符
void UART0_ISR(void) interrupt 4
{
unsigned char rxch;
readFlag0 = 0; //中断处理标志
//SFRPAGE = UART0_PAGE;
if(RI0) //中断标志 RI0=1 数据完整接收
{
RI0 = 0; //软件清零
rxch = SBUF0; //读缓冲
if(PWrite0==MAX_LEN)
{
PWrite0 = 0;
}
BufData0[PWrite0] = rxch;
PWrite0++;
readFlag0 =1;
}
if (TI0)
{ // handle transmit function
TI0 = 0; // 清除发送完成标志
//将缓冲尾指针加1
PSendRead0++;
if (PSendRead0 == MAX_SEND_LEN)
{
PSendRead0 = 0;
}
if (PSendWrite0!=PSendRead0) //判断缓冲内的首尾指针是否相同
{
SBUF0 = BufSend0[PSendRead0]; //如果不同,发送尾指针所在的字节
}
else
{
sendFlag0 = 0; //如果相同,置发送标志为0
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -