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

📄 uart.c

📁 51读写u盘 uart.c main.c rwudiskdemo.c
💻 C
字号:
#include <reg52.h>
#include "config.h"

//参数配置
#define MCLK		11059200L	//定义CPU主频(Hz)
//#define BAUD_RATE	9600L		//设置波特率




void ReceOneChar(char ReceChar);

void UartSendChar(char c);



uint8 RS323BaudNumber(uint32 bps)
{
	uint8  RS323Baud;
	switch(bps)
	{
	/*	case 115200:
			RS323Baud = 7;
			break;
	*/	case 57600:		//6
			RS323Baud = 0xFF;
			break;
	/*	case 56000:
			RS323Baud = 5;	   
			break;	   */
		case 38400:		  //4
			RS323Baud = 0xfe;
			break;		 
		case 19200:	   //3
			RS323Baud = 0xfd;
			break;		 
		case 14400:	   //2
			RS323Baud = 0xfc;
			break;
		case 9600:	  //1
			RS323Baud = 0xfa;
			break;
		case 4800:
			RS323Baud = 0xf4;
			break;
		default:
			RS323Baud = 0xfa;
			break;	
	}
	return 	 RS323Baud;
}

//********************************************************************************************************
// 函数名称:SendResponse
// 输入参数:buff,字符串指针,类型:unsigned char
//           len,字符长度,类型:unsigned short
// 输出参数:无
// 功能描述:发送帧函数为底层字符发送处理函数,ZLG/MODBUS RTU协议栈中使用该函数发送帧数据
//********************************************************************************************************
void SendResponse(uint8 *buff,uint16 len)
{
	uint16	i;
	for(i=0;i<len;i++)
	{
		UartSendChar(*(buff+i));
	}
}

	  




//定义发送中断辅助标志(仅用于发送)
static volatile bit _TI_;


//串行口初始化
void UART0Init(uint32 bps)
{
	char t;
	EA = 0;
	RXD = 1;
	TXD = 1;
	_TI_ = 1;
	SCON = 0x50;	//串口方式1
	PCON |= 0x80;	//波特率加倍
	t = TMOD;
	t &= 0x0F;
	t |= 0x20;
	TMOD = t;
//	TH1 = 256 - ( MCLK / 12 ) / ( 16 * 9600L );
	TH1 = RS323BaudNumber(bps) ;
	TL1 = TH1;
	TR1 = 1;
	ES = 1;			//允许串行口中断
	EA = 1;			//允许中断
}


//串行口中断服务程序
static void UartINTSVC() interrupt 4
{
	char c;
	if ( RI )	//接收中断
	{
		c = SBUF;
		RI = 0;
		ReceOneChar(c);
	}
	if ( TI )	//发送中断
	{
		TI = 0;
		_TI_ = 1;
	}
}


/*
功能:
	通过串行口发送单个字节
参数:
	c:被发送的字节数据,取值0x00~0xFF
*/
void UartSendChar(uint8 c)
{
	while ( !_TI_ );
	_TI_ = 0;
	SBUF = c;
}


/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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