⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 16C554VX1128MAX4359-MAX4456P89LPC952 驱动开发
💻 C
字号:
#include <reg952.h>
#include <string.h>
#include "uart.h"

#define TBUF_SIZE 64
#define RBUF_SIZE 64
#define SIZEMASK  0x3F
#define INT_DISABLE	 EA=0
#define INT_ENABLE	 EA=1


static idata unsigned char tbuf[TBUF_SIZE];
static idata unsigned char rbuf[RBUF_SIZE];
static  unsigned char t_in = 0;
static  unsigned char t_out = 0;
static  unsigned char t_disabled = 0;
static  unsigned char r_in = 0;
static  unsigned char r_out = 0;

/*串口中断服务*/
static void com_isr(void) interrupt 4 using 1 
{
	EA=0;
	if (RI_0 != 0)
	{//接收处理
		RI_0= 0;
		if (((r_in +1)&SIZEMASK)!=r_out)
		{
			rbuf[r_in]=S0BUF;
			r_in=(r_in+1)&SIZEMASK;
		}
	}
	if (TI_0!=0)
	{//发送处理
		TI_0=0;
		if (t_in!=t_out)
		{
			S0BUF = tbuf[t_out];
			t_out=(t_out+1)&SIZEMASK;
		}
		else
			t_disabled =1;
	}
	EA=1;
}

/*字符发送函数*/

int com_putchar(unsigned char c) reentrant
{
	INT_DISABLE;
	if ((TBUF_SIZE-com_tbuflen())<=2) //若缓冲区满,则返回出错标志
	{
		INT_ENABLE;
		return (-1);
	}
	tbuf[t_in] = c;
	t_in=(t_in+1)&SIZEMASK;
	if (t_disabled)
	{
		t_disabled=0;
		TI_0=1;
	}
	INT_ENABLE;
	return (0);
}


/*字符接受函数*/
int com_getchar(void)
{
	int c;
	if (com_rbuflen()==0)
		return -1;
	INT_DISABLE;
	c=rbuf[r_out];
	r_out=(r_out+1)&SIZEMASK;
	INT_ENABLE;
	return (c);
}

/*计算接收缓冲器长度*/
unsigned char com_rbuflen(void)
{
	return ((r_in -r_out)&SIZEMASK);
}


unsigned char com_tbuflen(void)
{
	return ((t_in-t_out)&SIZEMASK);
}

//-------------------------
//初始化串口
//-------------------------
void UART0_Init()
{
	t_in=0;		//清零发送缓冲区
	t_out=0;	
	t_disabled=1;	//禁止发送
	r_in=0;
	r_out=0;

	PCON=0x00;
	S0CON=0x50;  //使能接收选择串口模式1即8位uart
//	S0STAT=0x60;   //选择独立的Tx/Rx中断,并禁止双缓冲
	S0STAT=0x40;   //选择组合的Tx/Rx中断,并禁止双缓冲
//	BRGCON_0 =0x02;  //使能BRG,且选择波特率发生器0 用于产生UART0 模式1 的波特率               
	BRGR0_0=0xb0;   //57600baud @ 11.0592MHz	波特率=CCLK/((BRGR1_n, BRGR0_n)+16)
	BRGR1_0=0x00;
//	BRGR0_0=11059200/baudrate-16

//	BRGR0_0=0x50;   //115200baud @ 11.0592MHz	波特率=CCLK/((BRGR1_n, BRGR0_n)+16)
//	BRGR1_0=0x00;
	BRGCON_0 =0x03;
	EA=1; //使能中断
	ESR=1;  //ESR=Enable Serial Recieve
	EST=1;  //EST=Enable Serial Transmit
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -