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

📄 uart0.c

📁 MEGA128 串口驱动程序 两个串口都有,包括H文件和C文件
💻 C
字号:
/**********************************************************************
* System Name		DOOR
* Source Name		uart0.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 "uart0.h"
#include "timer0.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 UART0_RX_BUF_SIZE 128
#define UART0_TX_BUF_SIZE 250


//uart 0 缓存区
volatile UCHAR uart0_rx_buf_data[UART0_RX_BUF_SIZE];	//接收缓存
volatile UCHAR uart0_rx_buf_head;	//头指针
volatile UCHAR uart0_rx_buf_tail;	//尾指针
volatile UCHAR uart0_rx_buf_len;	//长度
//
volatile UCHAR uart0_tx_buf_data[UART0_TX_BUF_SIZE];	//发送缓存
volatile UCHAR uart0_tx_buf_head;	//头指针
volatile UCHAR uart0_tx_buf_tail;	//尾指针
volatile UCHAR uart0_tx_buf_len;	//长度



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

	/* 设置波特率*/
	switch(t_baud)
	{
		case BAUD_300:
			UBRR0H = UBRR_H_14M_300;
			UBRR0L = UBRR_L_14M_300;
			break;
		case BAUD_600:
			UBRR0H = UBRR_H_14M_600;
			UBRR0L = UBRR_L_14M_600;
			break;
		case BAUD_900:
			UBRR0H = UBRR_H_14M_900;
			UBRR0L = UBRR_L_14M_900;
			break;
		case BAUD_1200:
			UBRR0H = UBRR_H_14M_1200;
			UBRR0L = UBRR_L_14M_1200;
			break;
		case BAUD_2400:
			UBRR0H = UBRR_H_14M_2400;
			UBRR0L = UBRR_L_14M_2400;
			break;
		case BAUD_4800:
			UBRR0H = UBRR_H_14M_4800;
			UBRR0L = UBRR_L_14M_4800;
			break;
		case BAUD_9600:
			UBRR0H = UBRR_H_14M_9600;
			UBRR0L = UBRR_L_14M_9600;
			break;
		case BAUD_19200:
			UBRR0H = UBRR_H_14M_19200;
			UBRR0L = UBRR_L_14M_19200;
			break;
		case BAUD_28800:
			UBRR0H = UBRR_H_14M_28800;
			UBRR0L = UBRR_L_14M_28800;
			break;
		case BAUD_38400:
			UBRR0H = UBRR_H_14M_38400;
			UBRR0L = UBRR_L_14M_38400;
			break;
		case BAUD_57600:
			UBRR0H = UBRR_H_14M_57600;
			UBRR0L = UBRR_L_14M_57600;
			break;
		case BAUD_76800:
			UBRR0H = UBRR_H_14M_76800;
			UBRR0L = UBRR_L_14M_76800;
			break;
		case BAUD_115200:
			UBRR0H = UBRR_H_14M_115200;
			UBRR0L = UBRR_L_14M_115200;
			break;
		case BAUD_128000:
			UBRR0H = UBRR_H_14M_128000;
			UBRR0L = UBRR_L_14M_128000;
			break;
		case BAUD_256000:
			UBRR0H = UBRR_H_14M_256000;
			UBRR0L = UBRR_L_14M_256000;
			break;
		default:
			UBRR0H = UBRR_H_14M_115200;
			UBRR0L = UBRR_L_14M_115200;
			break;
	}			
	/*UCSR0B设置*/
	UCSR0B |= (1<<RXEN0);	// 接收使能
	UCSR0B |= (1<<TXEN0);	// 发送使能
	UCSR0B |= (1<<RXCIE0);	// 接收中断使能
	UCSR0B |= (1<<TXCIE0);	// 发送中断使能


	/*UCSR0C设置*/
	UCSR0C |= (1<<UCSZ01);	//8 个数据位
	UCSR0C |= (1<<UCSZ00);	//8 个数据位
	UCSR0C &= ~(1<<USBS0);	//1个停止位
	UCSR0C |= (1<<UPM01);	//偶校验
	UCSR0C &= ~(0<<UPM00);	//偶校验
	//
	uart0_rx_buf_head = 0;
	uart0_rx_buf_tail = 0;
	uart0_rx_buf_len  = 0;
	//
	uart0_tx_buf_head = 0;
	uart0_tx_buf_head = 0;
	uart0_tx_buf_len  = 0;
}




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



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






//串口0发送结束中断,数据已经发送且,数据寄存器为空时产生
SIGNAL(SIG_USART0_TRANS)
{
	cli();
	if (uart0_tx_buf_len>0)	//如果发送缓存中有数据
	{
		UDR0 = uart0_tx_buf_data[uart0_tx_buf_head];
		uart0_tx_buf_head++;
		/*指针到了底部回顶部*/
		if (uart0_tx_buf_head >= UART0_TX_BUF_SIZE)
		{
			uart0_tx_buf_head = 0;
		}
		uart0_tx_buf_len--;	//缓存长度--
	}
	timer0_task_uart0pack_stat = TIMER0_TASK_UART0PACK_NEW;
	sei();
}




//串口0接收结束中断
SIGNAL(SIG_USART0_RECV)
{
	cli();
	if(uart0_rx_buf_len < UART0_RX_BUF_SIZE)
	{
		uart0_rx_buf_data[uart0_rx_buf_tail] = UDR0;
		uart0_rx_buf_tail++;
		//指针到末尾后回到开始
		if(uart0_rx_buf_tail >= UART0_RX_BUF_SIZE)
		{
			uart0_rx_buf_tail = 0;
		}
		uart0_rx_buf_len++;
	}
	else
	{
		error_code = ERROR_UART0_RX;	//串口接收错误
	}
	sei();
}


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




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



/**********************************************************************
* Function Name		uart0_put_bytes
* Function Desc		通过串口0发送len个字节
* Return Value		无
* Parameter         
			pBytes	要发送的数据的首地址   
			len     数据长度
* Version	Date		Editor		Modification
* 1.0		2007/05/09	史玮		做成
**********************************************************************/
void uart0_put_bytes(UCHAR * pBytes,UCHAR len) 
{
	UCHAR i;
	for(i=0;i<len;i++)
	{
		uart0_put_byte(*(pBytes+i));
	}
}

⌨️ 快捷键说明

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