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

📄 serial.c

📁 一款收款机C源代码!因为是几年前的代码了
💻 C
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************
*                          serial.c
* 说明: 这里很多的函数功能还是基于中断里面不会开放其他的中断的基础上来实现的
*		在当前的串口函数为了兼容以前, 有一些参数是没有意义的, 主要是在于设置串口波特率和停止位等的地方.
*
******************************************************************************/
#include    "ecrsys.h"
#include "ftype.h"
#include "disp.h"
#include "data.h"
#include "sysdata.h"

void	Uart_Scale_Data_Store(BYTE	port, BYTE	ch);
void	Uart_Scan_Data_Store(byte port,byte ch);
void	Uart_Data_Store(BYTE	port, byte ch);
void	Uart_Send_Data(byte port);
byte Wr_Byte_Uart(byte port, byte sendByte);
byte ReadByteUART(byte port, byte *getByte);
void	Uart_Send_Null(byte port);
void	kp_rp_data_sd_chk(byte port);
void	kp_rp_xonoff_chk(byte ch);

#define		sitic(port)		s##port##tic
#define		siric(port)		s##port##ric
#define		uitb(port)		u##port##tb
#define		re(port)			re_u##port##c1
#define		te(port)			te_u##port##c1
#define		uimr(port)		u##port##mr
#define		uibrg(port)		u##port##brg

// 串口0对应着MCU的UART0
#define		PORT0_sitic		sitic(0)	
#define		PORT0_siric		siric(0)	
#define		PORT0_uitb		uitb(0)	
#define		PORT0_re			re(0)		
#define		PORT0_te			te(0)		
#define		PORT0_uimr		uimr(0)	
#define		PORT0_brg		uibrg(0)	

// 串口1对应着MCU的UART1
#define		PORT1_sitic		sitic(1)	
#define		PORT1_siric		siric(1)	
#define		PORT1_uitb		uitb(1)	
#define		PORT1_re			re(1)		
#define		PORT1_te			te(1)		
#define		PORT1_uimr		uimr(1)	
#define		PORT1_brg		uibrg(1)	

// 串口2对应着MCU的UART2
#define		PORT2_sitic		sitic(2)	
#define		PORT2_siric		siric(2)	
#define		PORT2_uitb		uitb(2)	
#define		PORT2_re			re(2)		
#define		PORT2_te			te(2)		
#define		PORT2_uimr		uimr(2)	
#define		PORT2_brg		uibrg(2)	

// 串口3对应着MCU的UART3
#define		PORT3_sitic		sitic(3)	
#define		PORT3_siric		siric(3)	
#define		PORT3_uitb		uitb(3)	
#define		PORT3_re			re(3)		
#define		PORT3_te			te(3)		
#define		PORT3_uimr		uimr(3)	
#define		PORT3_brg		uibrg(3)	

// 串口4对应着MCU的UART4
#define		PORT4_sitic		sitic(4)	
#define		PORT4_siric		siric(4)	
#define		PORT4_uitb		uitb(4)	
#define		PORT4_re			re(4)     
#define		PORT4_te			te(4)		
#define		PORT4_uimr		uimr(4)	
#define		PORT4_brg		uibrg(4)	


// only reseve Rom's first 256 byte use for future task --- OTP twice use
const byte reseverd[256] = {
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
   }; 
/*
物理串口:  PORT0   PORT1   PORT2   PORT3   PORT4
物理属性:  8根线   未知    未知    未知    未知        (目前也不是很清楚那个串口有DSR信号)
外部编号:  PORTX   PORTX   PORTX   PORTX   PORTX
默认连接:  串口prn LCM&TS  VFD     SC      PC

假定编号:  COM3    COM4    COM5    COM2    COM1    (考虑这种假定编号
*/
const byte port_rel[][2] = {	/* 5个端口,然后左边的是一个物理端口,右边的是实际上印刷出来的端口,仅在上层软件中应用 */
	{PORT0, 3-1}, 
	{PORT1, 4-1}, 
	{PORT2, 5-1}, 
	{PORT3, 2-1}, 
	{PORT4, 1-1}, 
};


// PORT0 variables
volatile word rs0_In;
volatile word rs0_Out;
volatile byte rs0_Buff[RSBUFLEN0 + 1];
volatile word wr0_In = 0;
volatile word wr0_Out = 0;
volatile byte wr0_Buff[WRBUFLEN0 + 1];
volatile byte uart0_Sending = 0;

// PORT1 variables
volatile word rs1_In;
volatile word rs1_Out;
volatile byte rs1_Buff[RSBUFLEN1 + 1];
volatile word wr1_In = 0;
volatile word wr1_Out = 0;
volatile byte wr1_Buff[WRBUFLEN1 + 1];
volatile byte uart1_Sending = 0;

// PORT2 variables
volatile word rs2_In;
volatile word rs2_Out;
volatile byte rs2_Buff[RSBUFLEN2 + 1];
volatile word wr2_In = 0;
volatile word wr2_Out = 0;
volatile byte wr2_Buff[WRBUFLEN2 + 1];
volatile byte uart2_Sending = 0;

// PORT3 variables
volatile word rs3_In;
volatile word rs3_Out;
volatile byte rs3_Buff[RSBUFLEN3 + 1];
volatile word wr3_In = 0;
volatile word wr3_Out = 0;
volatile byte wr3_Buff[WRBUFLEN3 + 1];
volatile byte uart3_Sending = 0;

// PORT4 variables
volatile word rs4_In;
volatile word rs4_Out;
volatile byte rs4_Buff[RSBUFLEN4 + 1];
volatile word wr4_In = 0;
volatile word wr4_Out = 0;
volatile byte wr4_Buff[WRBUFLEN4 + 1];
volatile byte uart4_Sending = 0;

// The temporary variables
volatile word rs_in;
volatile word rs_out;
volatile word wr_in;
volatile word wr_out;

/*
Port No.		Using For

UART0 -- 	External Printer
UART1 -- 	TS/LCM (Touch Screen/LCM display/
UART2 -- 	VFD driving
UART3 -- 	PC communication			
UART4 -- 	BarCode scanner
*/
#define	PORT0_ENABLE()		{re_u0c1 = 1;   te_u0c1 = 1;}
#define	PORT0_DISABLE()	{re_u0c1 = 0;   te_u0c1 = 0;}

#define	PORT1_ENABLE()		{re_u1c1 = 1;   te_u1c1 = 1;}
#define	PORT1_DISABLE()	{re_u1c1 = 0;   te_u1c1 = 0;}

#define	PORT2_ENABLE()		{re_u2c1 = 1;   te_u2c1 = 1;}
#define	PORT2_DISABLE()	{re_u2c1 = 0;   te_u2c1 = 0;}

#define	PORT3_ENABLE()		{re_u3c1 = 1;   te_u3c1 = 1;}
#define	PORT3_DISABLE()	{re_u3c1 = 0;   te_u3c1 = 0;}

#define	PORT4_ENABLE()		{re_u4c1 = 1;   te_u4c1 = 1;}
#define	PORT4_DISABLE()	{re_u4c1 = 0;   te_u4c1 = 0;}

void Uart_DisInt(byte port)
{
	disint();
	return;
	#if	0
	switch (port)
	{
	case PORT0:	   PORT3_sitic = 0;   PORT3_siric = 0;		break;
	case PORT1:	   PORT1_sitic = 0;   PORT1_siric = 0;		break;
	}
	#endif
}

void Uart_EnInt(byte port)
{
	enint();
	return;
	#if	0
	switch (port)
	{
	case PORT0:	   PORT3_sitic = 0x05;   PORT3_siric = 0x05;		break;
	case PORT1:	   PORT1_sitic = 0x05;   PORT1_siric = 0x05;		break;
	}
	#endif
}

/*
* 后来串口中断讨论成每次去取他的缓冲区的指针的时候, 首先读取到一个临时单元, 然后再来使用
*/
#define	Uart_DisInt2(port)	disint()
#define	Uart_EnInt2(port)		enint()
#define	NOP_COUNT		3600

/******************************************************************************
* Describe:   check send buffer whether empty
* Input :  port
* Output:  void
* return:  0 is empty, else is no empty
******************************************************************************/
byte	CheckSdBufEmpty(byte port)
{
   byte temp = 0;
	word nop;
   
   if (port == PORT0)
   {
   	Uart_DisInt2(PORT0);
		wr_in = wr0_In;
		wr_out = wr0_Out;
   	Uart_EnInt2(PORT0);
      temp = (wr_in == wr_out) ? 0 : 1;
   }
   else if (port == PORT1)
   {
   	Uart_DisInt2(PORT1);
		wr_in = wr1_In;
		wr_out = wr1_Out;
   	Uart_EnInt2(PORT1);
      temp = (wr_in == wr_out) ? 0 : 1;
   }
   else if (port == PORT2)
   {
   	Uart_DisInt2(PORT2);
		wr_in = wr2_In;
		wr_out = wr2_Out;
   	Uart_EnInt2(PORT2);
      temp = (wr_in == wr_out) ? 0 : 1;
   }
   else if (port == PORT3)
   {
   	Uart_DisInt2(PORT3);
		wr_in = wr3_In;
		wr_out = wr3_Out;
   	Uart_EnInt2(PORT3);
      temp = (wr_in == wr_out) ? 0 : 1;
   }
   else if (port == PORT4)
   {
   	Uart_DisInt2(PORT4);
		wr_in = wr4_In;
		wr_out = wr4_Out;
   	Uart_EnInt2(PORT4);
      temp = (wr_in == wr_out) ? 0 : 1;
   }

	// 目前的函数中, 当检查到不为空的时候则进行等待, 为了减少开关中断的次数,这里进行了计数延时
	if (!temp) {
		nop = NOP_COUNT; while (nop--);
	}

   return temp;
}

/******************************************************************************
* Describe:   check send buffer whether full
* Input :  port
* Output:  void
* return:  TRUE -- full / FALSE -- not full
******************************************************************************/
byte	CheckSdBufFull(byte port)
{
   bool full = FALSE;
	word nop;

   if (port == PORT0)
   {
   	Uart_DisInt2(PORT0);
		wr_in = wr0_In;
		wr_out = wr0_Out;
   	Uart_EnInt2(PORT0);
      if (((wr_in+1) & WRBUFLEN0) == wr_out)
      	full = TRUE;
   }
   else if (port == PORT1)
   {
   	Uart_DisInt2(PORT1);
		wr_in = wr1_In;
		wr_out = wr1_Out;
   	Uart_EnInt2(PORT1);
      if (((wr_in+1) & WRBUFLEN1) == wr_out)
      	full = TRUE;
   }
   else if (port == PORT2)
   {
   	Uart_DisInt2(PORT2);
		wr_in = wr2_In;
		wr_out = wr2_Out;
   	Uart_EnInt2(PORT2);
      if (((wr_in+1) & WRBUFLEN2) == wr_out)
      	full = TRUE;
   }
   else if (port == PORT3)
   {
   	Uart_DisInt2(PORT3);
		wr_in = wr3_In;
		wr_out = wr3_Out;
   	Uart_EnInt2(PORT3);
      if (((wr_in+1) & WRBUFLEN3) == wr_out)
      	full = TRUE;
   }
   else if (port == PORT4)
   {
   	Uart_DisInt2(PORT4);
		wr_in = wr4_In;
		wr_out = wr4_Out;
   	Uart_EnInt2(PORT4);
      if (((wr_in+1) & WRBUFLEN4) == wr_out)
      	full = TRUE;
   }

	// 目前的函数中, 当检查到不为空的时候则进行等待, 为了减少开关中断的次数,这里进行了计数延时
	if (full) {
		nop = NOP_COUNT; while (nop--);
	}

   return full;
}


/******************************************************************************
* Describe:   check recevie buffer whether empty
* Input :  port
* Output:  void
* return:  0 is empty, else is no empty
******************************************************************************/
byte CheckRsBufEmpty(byte port)
{
   byte temp = 0;
	word nop;

   if (port == PORT0)
   {
   	Uart_DisInt2(PORT0);
		rs_in = rs0_In;
		rs_out = rs0_Out;
   	Uart_EnInt2(PORT0);
      temp = (rs_in == rs_out) ? 0 : 1;
   }
   else if (port == PORT1)
   {
   	Uart_DisInt2(PORT1);
		rs_in = rs1_In;
		rs_out = rs1_Out;
   	Uart_EnInt2(PORT1);
      temp = (rs_in == rs_out) ? 0 : 1;
   }
   else if (port == PORT2)
   {
   	Uart_DisInt2(PORT2);
		rs_in = rs2_In;
		rs_out = rs2_Out;
   	Uart_EnInt2(PORT2);
      temp = (rs_in == rs_out) ? 0 : 1;
   }
   else if (port == PORT3)
   {
   	Uart_DisInt2(PORT3);
		rs_in = rs3_In;
		rs_out = rs3_Out;
   	Uart_EnInt2(PORT3);
      temp = (rs_in == rs_out) ? 0 : 1;
   }
   else if (port == PORT4)
   {
   	Uart_DisInt2(PORT4);
		rs_in = rs4_In;
		rs_out = rs4_Out;
   	Uart_EnInt2(PORT4);
      temp = (rs_in == rs_out) ? 0 : 1;
   }

	if (!temp) {
		nop = NOP_COUNT; while (nop--);
	}

   return temp;
}

/******************************************************************************
* Describe:   clear recevie buffer
* Input :  port
* Output:  void
* return:  void
******************************************************************************/
void ClrRsBuf(byte port)
{
	word nop;

   if (port == PORT0)
   {
		Uart_DisInt2(port);
      rs0_In = rs0_Out = 0;
		Uart_EnInt2(port);
   }
   else if (port == PORT1)
   {
		Uart_DisInt2(port);
      rs1_In = rs1_Out = 0;
		Uart_EnInt2(port);
   }  
   else if (port == PORT2)
   {
		Uart_DisInt2(port);
      rs2_In = rs2_Out = 0;
		Uart_EnInt2(port);
   }  
   else if (port == PORT3)
   {
		Uart_DisInt2(port);
      rs3_In = rs3_Out = 0;
		Uart_EnInt2(port);
   }  
   else if (port == PORT4)
   {
		Uart_DisInt2(port);
      rs4_In = rs4_Out = 0;
		Uart_EnInt2(port);
   }
}

/*	Clear the send buffer */
void ClrSdBuf(byte port)
{
   if (port == PORT0)
   {
		Uart_DisInt2(port);
      wr0_In = wr0_Out = 0;
		Uart_EnInt2(port);
   }
   else if (port == PORT1)
   {
		Uart_DisInt2(port);
      wr1_In = wr1_Out = 0;
		Uart_EnInt2(port);
   }  
   else if (port == PORT2)
   {
		Uart_DisInt2(port);
      wr2_In = wr2_Out = 0;
		Uart_EnInt2(port);
   }  
   else if (port == PORT3)
   {
		Uart_DisInt2(port);
      wr3_In = wr3_Out = 0;
		Uart_EnInt2(port);
   }  
   else if (port == PORT4)
   {
		Uart_DisInt2(port);
      wr4_In = wr4_Out = 0;
		Uart_EnInt2(port);
   }  
}

/******************************************************************************
* Describe:   UART string send routine
* Input :  data pointer, port & data len
* Output:  void
* return:  0 is OK
******************************************************************************/
byte Wr_Str_Uart(byte port, const byte *str, byte len)
{
   if ((port != PORT0) && (port != PORT1) && (port != PORT2) && (port != PORT3) && (port != PORT4))
   {
      return (NOTOK);
   }

   for (; len > 0; len--)
   {
      if (Wr_Byte_Uart(port, *str) != OK)
      {
         return (NOTOK);
      }
      str++;
   }

   return (OK);
}

/******************************************************************************
* Describe:   UART string receive routine
* Input :  port, data len
* Output:  data pointer
* return:  0 is OK
******************************************************************************/
byte ReadStringUART(byte port, byte *str, byte len)

⌨️ 快捷键说明

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