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

📄 serial.c

📁 一款收款机C源代码!因为是几年前的代码了
💻 C
📖 第 1 页 / 共 4 页
字号:
			return OK;
		}
		else if(port == PORT4)
		{
			PORT4_re = 0;
			PORT4_te = 0;
			PORT4_brg = baud;
			PORT4_re = 1;
			PORT4_te = 1;
			return OK;
		}
	}
	else		/*if the baud rate is illegal, we need set a DEFAULT baud rate*/
	{
		if(port == PORT0)
		{
			PORT0_re = 0;
			PORT0_te = 0;
			PORT0_brg = BRG_96;
			PORT0_re = 1;
			PORT0_te = 1;
		}
		else if(port == PORT1)
		{
			PORT1_re = 0;
			PORT1_te = 0;
			PORT1_brg = BRG_96;
			PORT1_re = 1;
			PORT1_te = 1;
		}
		else if(port == PORT2)
		{
			PORT2_re = 0;
			PORT2_te = 0;
			PORT2_brg = BRG_96;
			PORT2_re = 1;
			PORT2_te = 1;
		}
		else if(port == PORT3)
		{
			PORT3_re = 0;
			PORT3_te = 0;
			PORT3_brg = BRG_96;
			PORT3_re = 1;
			PORT3_te = 1;
		}
		else if(port == PORT4)
		{
			PORT4_re = 0;
			PORT4_te = 0;
			PORT4_brg = BRG_96;
			PORT4_re = 1;
			PORT4_te = 1;
		}
	}
	return NG;
}


///////////////////////////////////////////////////////////////////////////////
// 函数说明:	串口模式设置程序:
// 输入参数:	port -- 串口号, parity -- 奇偶校验, dataLen -- 数据长度, stop -- 停止位位数.
// 输出参数:	void
// 返回值:	uimr
// 注:			此函数不检查传入参数的有效性,故请使用宏定义来传递参数.
///////////////////////////////////////////////////////////////////////////////
byte Uart_PortSet(byte port, byte parity, byte dataLen, byte stop)
{
	// 注: 以下宏定义与串口寄存器有关,具体情况参见"E16C80HW.pdf",page 412.
	#define	UART_DATA_7_BIT		4					// transfer data 7, 8 or 9 bits long
	#define	UART_DATA_8_BIT		5
	#define	UART_DATA_9_BIT		6
	#define	UART_STOP_1_BIT		0					// one or two stop bit
	#define	UART_STOP_2_BIT		0x10
	#define	UART_PARITY_ODD		0					// odd or even parity 
	#define	UART_PARITY_EVEN		0x20
	#define	UART_PARITY_DIS		0					// parity disable or enable
	#define	UART_PARITY_EN			0x40

	byte uimr = 0;

	parity = ser_port[port].parity;
	dataLen = ser_port[port].data_len;
	stop = ser_port[port].stop_bit;
	
	switch (parity)
	{
	case UART_SET_PARITY_NO:	uimr |= UART_PARITY_DIS;	break;
	case UART_SET_PARITY_EVEN:	uimr |= UART_PARITY_EN;	uimr |= UART_PARITY_EVEN; break;
	case UART_SET_PARITY_ODD:	uimr |= UART_PARITY_EN;	uimr |= UART_PARITY_ODD; break;
	default:	break;
	}
	switch (dataLen)
	{
	case UART_SET_DATA_7: uimr |= UART_DATA_7_BIT;	break;
	case UART_SET_DATA_8: uimr |= UART_DATA_8_BIT;	break;
	case UART_SET_DATA_9: uimr |= UART_DATA_9_BIT;	break;
	default: break;
	}
	switch (stop)
	{
	case UART_SET_STOP_1:	uimr |= UART_STOP_1_BIT;	break;
	case UART_SET_STOP_2:	uimr |= UART_STOP_2_BIT;	break;
	default: break;
	}
	if (port == PORT0)
	{
		PORT3_re = 0;
		PORT3_te = 0;
		PORT3_uimr = uimr;
		PORT3_re = 1;
		PORT3_te = 1;
		return OK;
	}
	else if (port == PORT1)
	{
		PORT1_re = 0;
		PORT1_te = 0;
		PORT1_uimr = uimr;
		PORT1_re = 1;
		PORT1_te = 1;
		return OK;
	}
	return NG;
}

/* 设置串口的所有东西 */
void	Uart_SetPort(byte port, word baud, byte parity, byte datalen, byte stopbit)
{
   if ((port != PORT0) && (port != PORT1) && (port != PORT2) && (port != PORT3) && (port != PORT4))
   {
      return;
   }

	Uart_SetBaudRate(port, baud);

	Uart_PortSet(port, parity, datalen, stopbit);
}

/*---------------------------------------------
*	After user select the uart linked device,
*  then initialize the uart port's option:
*
*  for PC: 57600 bps , N, 8 , 1 is default
*	for Scanner: 9600 , N, 8 , 1 is default.
*=============================================*/
/* 注: 无论设置成哪一个用途, 都会有一个默认的波特率值. 
	PC COMM: 57600 N, 8, 1
	POS KB:  9600, N, 8, 1
	KP:		9600, N, 8, 1
	RP: 		9600, N, 8, 1
	SC:		9600, N, 8, 1
	ElecScale: 未知
	FP:		未知
	ONLINE:	未知
	CAT:     9600, N, 8, 1
	TS:		38400,N, 8, 1
	VFD:		不管
	如果是一些没有默认值的, 需要小心可能还需要进行一个波特率的还原.
*/
void	Set_Uart_Port_Dft(byte port)
{
	const byte port_dft[7][6] = 
	{
		{PT_RP,			0, UART_SET_PARITY_NO, UART_SET_DATA_8, UART_SET_STOP_1, 0}, 
		{PT_PC_COMM,	3, UART_SET_PARITY_NO, UART_SET_DATA_8, UART_SET_STOP_1, 0}, 
		{PT_SC, 		0, UART_SET_PARITY_NO, UART_SET_DATA_8, UART_SET_STOP_1, SER_PRN_POS01}, 
		{PT_PC_COMM, 	0, UART_SET_PARITY_NO, UART_SET_DATA_8, UART_SET_STOP_1, SER_PRN_POS01}, 
		{PT_SC, 		0, UART_SET_PARITY_NO, UART_SET_DATA_8, UART_SET_STOP_1, 0}, 
		{PT_CAT, 		0, UART_SET_PARITY_NO, UART_SET_DATA_8, UART_SET_STOP_1, 0}, 
		{PT_TS, 		2, UART_SET_PARITY_NO, UART_SET_DATA_8, UART_SET_STOP_1, 0}, 
	};
	byte i;

	for (i = 0; i < 7; i++) 
	{
		if (port_dft[i][0] == ser_port[port].device) 
		{
			ser_port[port].baud_idx = port_dft[i][1];
			ser_port[port].parity 	= port_dft[i][2];
			ser_port[port].data_len = port_dft[i][3];
			ser_port[port].stop_bit = port_dft[i][4];
			ser_port[port].type 	= port_dft[i][5];
			Uart_SetPort(port, NULL, NULL, NULL, NULL);
			if (port_dft[i][0] == PT_PC_COMM)
				COMM_PORT = port;
			break;
		}
	}
}

const long baud_real[] = {9600, 19200, 38400, 57600, 4800, 115200};
const word baud_set[] = {BRG_96, BRG_192, BRG_384, BRG_576, BRG_48, BRG_1152};
/*
	Get the baud rate index: 
	9600: 0
	19200: 1
	38400: 2
	57600: 3
	4800: 4
	115200: 5
	BOOL flag, 0: The real baud, such as: 9600, 19200, 38400, 57600, 4800...
				  1: The set baud, such as: BRG_96, BRG_192, BRG_384, BRG_576, BRG_48
*/
byte get_baud_idx(long baud, BOOL flag)
{
	byte idx;

	if (flag == BAUD_REAL) {
		for (idx = 0; idx < 6; idx++)
			if (baud_real[idx] == baud)
				break;
	}
	else {
		for (idx = 0; idx < 6; idx++)
			if (baud_set[idx] == baud)
				break;
	}

	if (idx >= 6)
		idx = FAIL_BYTE;
	return idx;
}

/* The the serial port baud rate */
long Uart_GetBaudRate(byte port)
{
	return baud_real[ser_port[port].baud_idx];
}

/* Get the setting baud rate, such as BRG_96, BRG_192, ... */
word GetBaud(byte idx)
{
	return baud_set[idx];
}

/* Get the lower port, such as: PORT0, PORT1, PORT2, PORT3, PORT4 */
byte get_port_low(byte port_high)
{
	byte i;
	byte low;

	low = FAIL_BYTE;
	for (i = 0; i < PORT_MAX; i++) {
		if (port_rel[i][1] == port_high) {
			low = port_rel[i][0];
			break;
		}
	}

	return low;
}

/* Get the high port from the lower port, such as: 1-1, 2-1, 3-1, ... */
byte bet_port_high(byte port_low)
{
	byte i;
	byte high;

	high = FAIL_BYTE;
	for (i = 0; i < PORT_MAX; i++) {
		if (port_rel[i][0] == port_low) {
			high = port_rel[i][1];
			break;
		}
	}

	return high;
}

#if	1
/*
 ******************************************************************************
 * 功能: 仅仅用于串口调试,没有具体的作用,正式版本不需要参与编译
 *
 ******************************************************************************
*/
void	test_uart_port(char	port)
{
	WORD	i;
	BYTE	j;
	
	for ( j = 0; j < 5; j ++ )			/*Initial for can receive and transmit data*/
		ser_port[j].device = PT_PC_COMM;

	for ( i=0; i<0xff; i++ )			/*Send a data string first to confirm 
												 the port is right*/
	{
		Wr_Byte_Uart(port,(BYTE)i);
	}


	Clr_Dsp_Data();
	
	for ( i=0; i<0xff;i ++)			/* Receive and feedback a data from host*/
	{
		if ( ReadByteUART(port,&j) != OK )
		{
			j = 0xff;
		}
		Wr_Byte_Uart(port,j);
		Insert_Byte(j,8);
	}
}
#endif

#if 0
/*
	等待串口把数据发送出来
*/
void Uart_Wait_Stop(void)
{
	byte flag = 0;

	while (flag != 0x0f) {
		disint();
		if (wr0_In == wr0_Out)
			flag |= 0x01;
		if (wr1_In == wr1_Out)
			flag |= 0x02;
		if (wr3_In == wr3_Out)
			flag |= 0x04;
		if (wr4_In == wr4_Out)
			flag |= 0x08;
		enint();
		flag++;
		flag -= 2;
		flag++;
	}
}
#endif

/******************************************************************************
*	Check the KP/RP busy condition when the buffer is full.
* 	If still in XOFF mode, then waiting until the the timer out or xon comes.
*****************************************************************************/
CHR	kp_rp_busy_chk(void)
{
	CHR	i,j;
	CHR	port = 0;
	CHR	ch = OK;
	WORD  tmp = ERR_KP_TM_OUT;
	CHR	error_str[MAX_LCD_NUM];
	
	if ( fl_kp_tm_out == 1 )
	{
		fl_kp_tm_out = 0;
		error_str[0] = 'E';
		for(i = 3; i > 0; i --)
		{
			j = tmp%10;
			tmp /= 10;
			error_str[i] = j + '0';
		}
		error_str[4] = '\0';
   	    VFDDisplay(error_str, CLEARD, LEFT);		/* Display the error information */
   	    bellcnt = 0xFE;
   	
		while(GetKey_Ex() != KD_CLEAR);		/*here wait the Clear key */
		uc_kp_wait_timer = KP_WAIT_TM;
		Clr_Dsp_Data();							/*clear the screen*/
		RightDisp(0L, sysflag->sysdots);		/*display 0.00*/
		if ( ++ kp_clr_cntr == MAX_KP_WAIT_CNTR )
        {	/*total 3 times can be used for clear*/
			kp_clr_cntr = 0;
			uc_kp_wait_timer = 0;
			if ((port_sel1 == PT_KP) || (port_sel1 == PT_RP ))
				port = PORT0;
			else if ((port_sel2 == PT_KP) || (port_sel2 == PT_RP ))
				port = PORT1;				/*only one port can be linked to uart*/

			ClrWrBuf(port);				/*Clear the send buffer*/
			ch = KD_KP_RP_BUSY;
		}
	}
	return(ch);
}

/*==================================================
*	Send data to uart send register according to the 
*  KP/RP status
*	If KP/RP during the busy status, then stop send
*  Else  Send to send buffer
*	2004-10-15 18:12
*=================================================*/
void	kp_rp_data_sd_chk(byte port)
{
	if (!fl_kp_rp_busy) {
		Uart_Send_Data(port);
	}
	else {
		if (port == PORT0)
			uart0_Sending = 0;	/* Clear the sending flag */
		else if (port == PORT1)
			uart1_Sending = 0;
		else if (port == PORT2)
			uart2_Sending = 0;
		else if (port == PORT3)
			uart3_Sending = 0;
		else if (port == PORT4)
			uart4_Sending = 0;
	}
}

/*=====================================================
*	According to the KP/RP send data, check the ON/OFF
*  status of it.
*	打印机端口,不存储任何数据于缓冲区。
*====================================================*/
void	kp_rp_xonoff_chk(byte ch)
{
	switch(ch)
	{
		case	XON:
			fl_kp_rp_busy = 0;						 /*printer not busy */
			uc_kp_wait_timer = 0;	/* no need spy the kp wait time */
			kp_clr_cntr = 0;			  					  /*total 3 times*/
			break;
		case	XOFF:
			fl_kp_rp_busy = 1;								/*kp is busy*/
			uc_kp_wait_timer = KP_WAIT_TM;
			break;
		default:
			break;
	}
}



void	Port1_Set_9600(void)
{
	PORT0_brg = BRG_96;
	PORT1_brg = BRG_576;
	PORT2_brg = BRG_96;
	PORT3_brg = BRG_96;
	PORT4_brg = BRG_96;
}


void	Port0_BRG_57600_Set(void)
{
	PORT1_re = 0;
	PORT1_te = 0;
	PORT1_brg = BRG_576;
	PORT1_re = 1;
	PORT1_te = 1;
}


void	Test_PORT0(void)
{
	while(1)
	{
		Wr_Byte_Uart(PORT1,0x55);
		Wr_Byte_Uart(PORT1,0xaa);
	}
}


/*--------------------------------------------------------------------
* Set the Uart's baud rate to 9600/19200/38400/57600 bps
*-------------------------------------------------------------------*/
/*
void	UartPortBaudRateSet(char port, char	baud)
{
	if ((baud == BRG_576) || (baud == BRG_384) || (baud == BRG_192) || 
		(baud == BRG_96) || (baud == BRG_1152) || (baud == BRG_48))
	{
		if(port == PORT0)
		{
			PORT0_re = 0;
			PORT0_te = 0;
			PORT0_brg = baud;
			PORT0_re = 1;
			PORT0_te = 1;
			return OK;
		}
		else if(port == PORT1)
		{
			PORT1_re = 0;
			PORT1_te = 0;
			PORT1_brg = baud;
			PORT1_re = 1;
			PORT1_te = 1;
			return OK;
		}
		else if(port == PORT2)
		{
			PORT2_re = 0;
			PORT2_te = 0;
			PORT2_brg = baud;
			PORT2_re = 1;
			PORT2_te = 1;
			return OK;
		}
		else if(port == PORT3)
		{
			PORT3_re = 0;
			PORT3_te = 0;
			PORT3_brg = baud;
			PORT3_re = 1;
			PORT3_te = 1;
			return OK;
		}
		else if(port == PORT4)
		{
			PORT4_re = 0;
			PORT4_te = 0;
			PORT4_brg = baud;
			PORT4_re = 1;
			PORT4_te = 1;
			return OK;
		}
	*/

⌨️ 快捷键说明

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