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

📄 uart1.c

📁 MEGA128 串口驱动程序 两个串口都有,包括H文件和C文件
💻 C
字号:
/**********************************************************************
* System Name		DOOR
* Source Name		uart1.c
* Function			dip sw状态输入
* Version	Date		Editor		Modification
* 1.0		2007/05/10	周斌		做成
**********************************************************************/


#include <avr/io.h>
#include <avr/interrupt.h>
#include "global.h"
#include "uart1.h"





//uart波特率配置表,不使用倍速模式
#define UBRR_H_14M_300 		0x0B
#define UBRR_L_14M_300 		0xFF
//
#define UBRR_H_14M_600 		0x05
#define UBRR_L_14M_600 		0xFF
//
#define UBRR_H_14M_900 		0x03
#define UBRR_L_14M_900 		0xFF
//
#define UBRR_H_14M_1200 	0x02
#define UBRR_L_14M_1200 	0xFF
//
#define UBRR_H_14M_2400 	0x01
#define UBRR_L_14M_2400 	0x7F
//
#define UBRR_H_14M_4800 	0x00
#define UBRR_L_14M_4800 	0xBF
//
#define UBRR_H_14M_9600 	0x00
#define UBRR_L_14M_9600 	0x5F
//
#define UBRR_H_14M_14400 	0x00
#define UBRR_L_14M_14400 	0x3F
//
#define UBRR_H_14M_19200 	0x00
#define UBRR_L_14M_19200 	0x2F
//
#define UBRR_H_14M_28800 	0x00
#define UBRR_L_14M_28800 	0x1F
//
#define UBRR_H_14M_38400 	0x00
#define UBRR_L_14M_38400 	0x17
//
#define UBRR_H_14M_57600 	0x00
#define UBRR_L_14M_57600 	0x0F
//
#define UBRR_H_14M_76800 	0x00
#define UBRR_L_14M_76800 	0x0B
//
#define UBRR_H_14M_115200 	0x00
#define UBRR_L_14M_115200 	0x07
//
#define UBRR_H_14M_128000 	0x00
#define UBRR_L_14M_128000 	0x06
//
#define UBRR_H_14M_256000 	0x00
#define UBRR_L_14M_256000 	0x02
//


//uart 0 缓存大小配置
#define UART1_RX_BUF_SIZE 512
#define UART1_TX_BUF_SIZE 128


//uart 0 缓存区
volatile UCHAR uart1_rx_buf_data[UART1_RX_BUF_SIZE];	//接收缓存
volatile UINT uart1_rx_buf_head;	//头指针
volatile UINT uart1_rx_buf_tail;	//尾指针
volatile UINT uart1_rx_buf_len;	//长度
//
volatile UCHAR uart1_tx_buf_data[UART1_TX_BUF_SIZE];	//发送缓存
volatile UCHAR uart1_tx_buf_head;	//头指针
volatile UCHAR uart1_tx_buf_tail;	//尾指针
volatile UCHAR uart1_tx_buf_len;	//长度



/**********************************************************************
* Function Name		uart1_init
* Function Desc		串口1初始化
* Return Value		无
* Parameter         
			t_ubbr:波特率配置选择
* Version	Date		Editor		Modification
* 1.0		2007/05/09	周斌		做成
**********************************************************************/
void uart1_init(UCHAR t_baud)
{

	/* 设置波特率*/
	switch(t_baud)
	{
		case BAUD_300:
			UBRR1H = UBRR_H_14M_300;
			UBRR1L = UBRR_L_14M_300;
			break;
		case BAUD_600:
			UBRR1H = UBRR_H_14M_600;
			UBRR1L = UBRR_L_14M_600;
			break;
		case BAUD_900:
			UBRR1H = UBRR_H_14M_900;
			UBRR1L = UBRR_L_14M_900;
			break;
		case BAUD_1200:
			UBRR1H = UBRR_H_14M_1200;
			UBRR1L = UBRR_L_14M_1200;
			break;
		case BAUD_2400:
			UBRR1H = UBRR_H_14M_2400;
			UBRR1L = UBRR_L_14M_2400;
			break;
		case BAUD_4800:
			UBRR1H = UBRR_H_14M_4800;
			UBRR1L = UBRR_L_14M_4800;
			break;
		case BAUD_9600:
			UBRR1H = UBRR_H_14M_9600;
			UBRR1L = UBRR_L_14M_9600;
			break;
		case BAUD_19200:
			UBRR1H = UBRR_H_14M_19200;
			UBRR1L = UBRR_L_14M_19200;
			break;
		case BAUD_28800:
			UBRR1H = UBRR_H_14M_28800;
			UBRR1L = UBRR_L_14M_28800;
			break;
		case BAUD_38400:
			UBRR1H = UBRR_H_14M_38400;
			UBRR1L = UBRR_L_14M_38400;
			break;
		case BAUD_57600:
			UBRR1H = UBRR_H_14M_57600;
			UBRR1L = UBRR_L_14M_57600;
			break;
		case BAUD_76800:
			UBRR1H = UBRR_H_14M_76800;
			UBRR1L = UBRR_L_14M_76800;
			break;
		case BAUD_115200:
			UBRR1H = UBRR_H_14M_115200;
			UBRR1L = UBRR_L_14M_115200;
			break;
		case BAUD_128000:
			UBRR1H = UBRR_H_14M_128000;
			UBRR1L = UBRR_L_14M_128000;
			break;
		case BAUD_256000:
			UBRR1H = UBRR_H_14M_256000;
			UBRR1L = UBRR_L_14M_256000;
			break;
		default:
			UBRR1H = UBRR_H_14M_115200;
			UBRR1L = UBRR_L_14M_115200;
			break;
	}			
	/*UCSR1B设置*/
	UCSR1B |= (1<<RXEN1);	// 接收使能
	UCSR1B |= (1<<TXEN1);	// 发送使能
	UCSR1B |= (1<<RXCIE1);	// 接收中断使能
	UCSR1B |= (1<<TXCIE1);	// 发送中断使能
	/*UCSR1C设置*/
	UCSR1C |= (1<<UCSZ10);	//8 个数据位
	UCSR1C |= (1<<UCSZ11);	//8 个数据位
	UCSR1C &= ~(1<<USBS1);	//1个停止位
	UCSR1C |= (1<<UPM10);	//奇校验
	UCSR1C |= (1<<UPM11);	//奇校验
	//
	uart1_rx_buf_head = 0;
	uart1_rx_buf_tail = 0;
	uart1_rx_buf_len  = 0;
	//
	uart1_tx_buf_head = 0;
	uart1_tx_buf_head = 0;
	uart1_tx_buf_len  = 0;
}




/**********************************************************************
* Function Name		uart1_put_byte
* Function Desc		通过串口1发送一个字节
* Return Value		无
* Parameter         
			t_char	要发送的字符
* Version	Date		Editor		Modification
* 1.0		2007/05/09	周斌		做成
**********************************************************************/
void uart1_put_byte(char t_char) 
{ 
	if ( (UCSR1A & (1<<UDRE1))&&(uart1_tx_buf_len == 0) )	//如果发送缓冲为空,且发送缓存也为空
	{
		UDR1 = t_char;	//直接发送数据
	}
	else
	{
		cli();              //暂停中断,以免数据比较时出错
		uart1_tx_buf_data[uart1_tx_buf_tail] = t_char;
		uart1_tx_buf_tail++;
		/*指针到了底部回顶部*/
		if (uart1_tx_buf_tail >= UART1_TX_BUF_SIZE)
		{
			uart1_tx_buf_tail = 0;
		}
		uart1_tx_buf_len++;
		sei(); 
	}
} 



/**********************************************************************
* Function Name		uart1_put_string
* Function Desc		通过串口1发送一个字符串
* Return Value		无
* Parameter         
*				*p_str	字符串首指针
* Version	Date		Editor		Modification
* 1.0		2007/05/09	周斌		做成
**********************************************************************/
void uart1_put_string(char * p_str)
{
	for(;*p_str!=0;p_str++)   //遇到停止符0结束 
	{
		uart1_put_byte(*p_str); 
	}
}






//串口1发送结束中断,数据已经发送且,数据寄存器为空时产生
SIGNAL(SIG_USART1_TRANS)
{
	cli();
	if (uart1_tx_buf_len>0)	//如果发送缓存中有数据
	{
		UDR1 = uart1_tx_buf_data[uart1_tx_buf_head];
		uart1_tx_buf_head++;
		/*指针到了底部回顶部*/
		if (uart1_tx_buf_head >= UART1_TX_BUF_SIZE)
		{
			uart1_tx_buf_head = 0;
		}
		uart1_tx_buf_len--;	//缓存长度--
	}
	sei();
}




//串口1接收结束中断
SIGNAL(SIG_USART1_RECV)
{
	cli();
	if(uart1_rx_buf_len < UART1_RX_BUF_SIZE)
	{
		uart1_rx_buf_data[uart1_rx_buf_tail] = UDR1;
		uart1_rx_buf_tail++;
		//指针到末尾后回到开始
		if(uart1_rx_buf_tail >= UART1_RX_BUF_SIZE)
		{
			uart1_rx_buf_tail = 0;
		}
		uart1_rx_buf_len++;
	}
	else
	{
		error_code = ERROR_UART1_RX;	//串口接收错误
	}
	sei();
}


/**********************************************************************
* Function Name		uart1_get_byte
* Function Desc		从串口1的缓存中取一个字节
* Return Value		
*		SUCESS:	取字节成功
*		FAIL:	取字节失败
* Parameter         
			*t_char	取字节地址
* Version	Date		Editor		Modification
* 1.0		2007/05/10	周斌		做成
**********************************************************************/
UCHAR uart1_get_byte(char * t_char)
{
	if(uart1_rx_buf_len>0)
	{
		*t_char = uart1_rx_buf_data[uart1_rx_buf_head];
		uart1_rx_buf_head++;
		//指针到末尾后回到开始
		if(uart1_rx_buf_head>=UART1_RX_BUF_SIZE)
		{
			uart1_rx_buf_head=0;
		}
		uart1_rx_buf_len--;
		return SUCESS;
	}
	else
	{
		return FAIL;
	}
	
}


/*********************************************************************
* Fnction Name		uart1_get_bytes
* Function Desc		从串口1的缓存中取len个字节
* Return Value		
*		SUCESS:	取字节成功
*		FAIL:	取字节失败
* Parameter         
*			*t_char	取字节地址     len  数据长度
* Version	Date		Editor		Modification
* 1.0		2007/05/10   史玮		做成
* 1.1       2007/05/14	 周斌		修改
**********************************************************************/
UCHAR uart1_get_bytes(UCHAR * t_char,UINT len)
{
	UINT i;
	if(len<=uart1_rx_buf_len)
	{
		if(uart1_rx_buf_len>0)
		{
			for(i=0;i<len;i++)
			{
				cli();
				*(t_char+i) = uart1_rx_buf_data[uart1_rx_buf_head+i];
				uart1_rx_buf_head++;
				//指针到末尾后回到开始
				if(uart1_rx_buf_head>=UART1_RX_BUF_SIZE)
				{
					uart1_rx_buf_head=0;
				}
				uart1_rx_buf_len--;
				sei();
			}
			return SUCESS;
		}
		else
		{
			return FAIL;
		}
	}
	else
	{
		return FAIL;
	}
	
}



/**********************************************************************
* Function Name		uart1_put_bytes
* Function Desc		通过串口0发送len个字节
* Return Value		无
* Parameter         
			pBytes	要发送的数据的首地址   
			len     数据长度
* Version	Date		Editor		Modification
* 1.0		2007/05/09	史玮		做成
**********************************************************************/

void uart1_put_bytes(UCHAR * pBytes,UCHAR len) 
{
	UCHAR i;
	for(i=0;i<len;i++)
	{
		uart1_put_byte(*(pBytes+i));
	}
}




/**********************************************************************
* Function Name		uart1_get_buffer_len()
* Function Desc		获取缓冲区数据长度
* Return Value		缓冲区数据长度
* Parameter         
			
* Version	Date		Editor		Modification
* 1.0		2007/05/09	史玮		做成
**********************************************************************/
UINT uart1_get_buffer_len()
{
	return uart1_rx_buf_len;
}





/**********************************************************************
* Function Name		uart1_clear_buffer()
* Function Desc		清缓冲区
* Return Value		
* Parameter         
			
* Version	Date		Editor		Modification
* 1.0		2007/05/09	史玮		做成
**********************************************************************/
void uart1_clear_buffer()
{
	cli();
	uart1_rx_buf_tail = uart1_rx_buf_head	;
	uart1_rx_buf_len = 0;	//长度=0
	sei();
}

⌨️ 快捷键说明

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