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

📄 uart.c

📁 用LPC936实现通过SPI扩展多个串口,在这里实现了对多个通道的管理,C代码,
💻 C
字号:
#include <intrins.h>
#include "uart.h"

#define _XTAL_120000_
#define _SCON_VALUE_			0x50
#define _SSTAT_VALUE_			0x60

#if defined (_XTAL_110592_)
unsigned char code baud_para[][2]=	//11.0596M
					{{0,80},{0,176},{1,16},
			 		 {1,112},{2,48},
			 		 {2,240},{4,112},{8,240}
					 {17,240},{35,240}};
#elif defined (_XTAL_120000_)
unsigned char code baud_para[][2]=	//12M
					{{0,88},{0,192},
			 		 {1,41},{1,144},{2,97},
			 		 {3,49},{4,210},{9,180},
					 {19,120},{39,0}};
#else
unsigned char code baud_para[][2]=
					{{0,48},{0,112},{0,176},{0,240},
					 {1,112},{1,240},{2,240},{5,240},
					 {11,240},{23,240}};
#endif

Queue idata tx_queue;
unsigned char xdata tx_buffer[TX_BUFFER_SIZE];

Queue idata rx_queue;
unsigned char xdata rx_buffer[RX_BUFFER_SIZE];

Queue idata mtx_queue;
unsigned char xdata mtx_buffer[MTX_BUFFER_SIZE];

Queue idata mrx_queue;
unsigned char xdata mrx_buffer[MRX_BUFFER_SIZE];

bit tx_running;
bit term_ctr_enable;		//dtr dsr enable
bit flow_ctr_enable;		//cts rts enable
bit msg_flag_on=0;
bit serial_opened=0;

void init_uart()
{
	queue_init(tx_queue,tx_buffer,TX_BUFFER_SIZE);
	queue_init(rx_queue,rx_buffer,RX_BUFFER_SIZE);
	queue_init(mtx_queue,mtx_buffer,MTX_BUFFER_SIZE);
	queue_init(mrx_queue,mrx_buffer,MRX_BUFFER_SIZE);
	tx_running = term_ctr_enable = flow_ctr_enable = 0;
	msg_flag_on=0;
	serial_opened = 0;
	RTS=1;
	DTR=1;
	SSTAT=_SSTAT_VALUE_;
	SCON=_SCON_VALUE_;	
}

bit open_uart(char baud)
{
	if(baud <= 10){
		BRGCON = 2;
		tx_running = term_ctr_enable = flow_ctr_enable = 0;
		queue_clear(tx_queue);
		queue_clear(rx_queue);
		queue_clear(mrx_queue);
		queue_clear(mtx_queue);
		BRGR0 = baud_para[baud][1];
		BRGR1 = baud_para[baud][0];
		BRGCON = 3;
		ESR = 1;
		EST = 1;
		DTR = 0;
		RTS = 0;
		serial_opened = 1;
		return 1;
	}else return 0;
}

int get_char(void){
	int c;

	c = queue_output(&rx_queue);
	if(flow_ctr_enable && RTS){
		if(queue_low(rx_queue)) RTS = 0;
	}

	return c;
}

bit send_char(char c)						//75
{
	bit b;
	int p;
	
	EST=0;
	b = queue_input(&tx_queue,c);
	EST=1;

	if(serial_opened && (!tx_running)){
		if(term_ctr_enable){
			if(DSR) return b;
		}

		if(flow_ctr_enable){
			if(CTS) return b;
		}
		
		EST=0;
		p = queue_output(&mtx_queue);
		if(p == -1)
			p = queue_output(&tx_queue);
		if(p != -1){
			SBUF = p & 0xff;
			tx_running = 1;
		}
		EST=1;
	}
	
	return b;
}

int get_msg(void)
{
	return queue_output(&mrx_queue);
}

bit send_msg(char c)
{
	int p;
	bit b;

	EST=0;
	b = queue_input(&mtx_queue,c);
	EST=1;

	if(serial_opened && (!tx_running)){
		if(term_ctr_enable){
			if(DSR) return b;
		}

		if(flow_ctr_enable){
			if(CTS) return b;
		}
		
		EST=0;
		p = queue_output(&mtx_queue);
		if(p == -1)
			p = queue_output(&tx_queue);
		if(p != -1){
			SBUF = p & 0xff;
			tx_running = 1;
		}
		EST=1;
	}

	return b;
}

void uart_scan(void)
{
	int p;
	if(serial_opened && (!tx_running)){
		if(term_ctr_enable){
			if(DSR) return ;
		}

		if(flow_ctr_enable){
			if(CTS) return ;
		}
		
		EST=0;
		p = queue_output(&mtx_queue);
		if(p == -1)
			p = queue_output(&tx_queue);
		if(p != -1){
			SBUF = p & 0xff;
			tx_running = 1;
		}
		EST=1;
	}
}

void TX_INT(void) interrupt 13		//45
{
	int c;
	
	TI=0;

	if(term_ctr_enable){
		if(DSR){
			tx_running = 0;
			return;
		}
	}

	if(flow_ctr_enable){
		if(CTS){
			tx_running = 0;
			return;
		}
	}
	
	c = queue_output(&mtx_queue);
	if(c == -1)
		c = queue_output(&tx_queue);
	if(c != -1){
		SBUF=c&0xff;
	}	
	else tx_running = 0;

}

void RX_INT(void) interrupt 4		//55
{
	unsigned char c;

	if(_testbit_(RI)){
		c = SBUF;
		if(msg_flag_on){
			if(c == MSG_EOF_FLAG){
				queue_input(&mrx_queue,c);
				msg_flag_on = 0;
			}else if(c == DATA_EOF_FLAG){
				queue_input(&rx_queue,c);
				msg_flag_on = 0;
			}else{
				queue_input(&mrx_queue,c);
			}
		}else{
			if(c == MSG_EOF_FLAG){
				msg_flag_on = 1;
			}else{
				queue_input(&rx_queue,c);
			}
		}
	
		if(flow_ctr_enable && (!RTS)){
			if(queue_high(rx_queue)) RTS = 1;
		}
	}
}

⌨️ 快捷键说明

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