* 版本.........: 1.0
* 作者.........: 陈利栋
* 目标.........: ATmega128
* 文件名.......: UART.h
* 编译器.......: IAR for AVR V5.5
* 创建时间.....: 2010.10.15
* 最后修改.....: 2010.10.15
*****************************************/
#ifndef __UART_H__
#define __UART_H__
#include
#include "../main.h" /* F_CPU */
#define UART_BAUD 9600 //波特率
#define UART_TXBUF_LENGTH 500
#define UART_RXBUF_LENGTH 500
#if UART_TXBUF_LENGTH <= 0xff
#define UART_TXBUF_HEAD_TAIL_TYPE unsigned char
#else
#define UART_TXBUF_HEAD_TAIL_TYPE unsigned int
#endif /* UART_TXBUF_LENGTH */
#if UART_RXBUF_LENGTH <= 0xff
#define UART_RXBUF_HEAD_TAIL_TYPE unsigned char
#else
#define UART_RXBUF_HEAD_TAIL_TYPE unsigned int
#endif /* UART_TXBUF_LENGTH */
typedef struct
{
unsigned char da
UART_TXBUF_HEAD_TAIL_TYPE head;
UART_TXBUF_HEAD_TAIL_TYPE tail;
UART_TXBUF_HEAD_TAIL_TYPE number;
}UART_TXBUF;
typedef struct
{
unsigned char da
UART_RXBUF_HEAD_TAIL_TYPE head;
UART_RXBUF_HEAD_TAIL_TYPE tail;
UART_RXBUF_HEAD_TAIL_TYPE number;
}UART_RXBUF;
extern volatile UART_TXBUF My_UART_TxBuf;
extern volatile UART_RXBUF My_UART_RxBuf;
extern void UART_Init(void);
extern void UART_TxByte(unsigned char _da
#endif /* __UART_H__ */
* 版本.........: 1.0
* 作者.........: 陈利栋
* 目标.........: ATmega128
* 文件名.......: UART.c
* 编译器.......: IAR for AVR V5.5
* 创建时间.....: 2010.10.15
* 最后修改.....: 2010.10.15
*****************************************/
#include "UART.h"
volatile UART_TXBUF My_UART_TxBuf;
volatile UART_RXBUF My_UART_RxBuf;
void UART_Init(void)
{
UBRR0L = (F_CPU / 16L / UART_BAUD - 1);
UCSR0B_RXEN0 = 1; // 接收使能
UCSR0B_TXEN0 = 1; // 发送使能
UCSR0B_RXCIE0 = 1; // 接收结束中断使能
UCSR0B_TXCIE0 = 1; // 发送结束中断使能
// 缓冲区初始化
My_UART_TxBuf.head = 0;
My_UART_TxBuf.tail = 0;
My_UART_TxBuf.number = 0;
My_UART_RxBuf.head = 0;
My_UART_RxBuf.tail = 0;
My_UART_RxBuf.number = 0;
__enable_interrupt(); // 使能总中断
}
void UART_TxByte(unsigned char _da
{
if (UCSR0A_UDRE0 == 1 && My_UART_TxBuf.number == 0) // 数据寄存器空 且 缓冲区没有数据
{
UDR0 = _da
}
else
{
if (My_UART_TxBuf.number < (UART_TXBUF_LENGTH - 1)) // 发送缓冲区未满
{
My_UART_TxBuf.number++;
My_UART_TxBuf.da
My_UART_TxBuf.tail++;
if (My_UART_TxBuf.tail == UART_TXBUF_LENGTH)
{
My_UART_TxBuf.tail = 0;
}
}
else // 发送缓冲区已满
{
// 发送缓冲区溢出
}
}
}
#pragma vector = USART0_TXC_vect
__interrupt void USART0_TXC(void)
{
if (UCSR0A_UDRE0 == 1 && My_UART_TxBuf.number > 0) // 数据寄存器空 且 缓冲区有数据
{
UDR0 = My_UART_TxBuf.da
My_UART_TxBuf.number--;
My_UART_TxBuf.head++;
if (My_UART_TxBuf.head == UART_TXBUF_LENGTH)
{
My_UART_TxBuf.head = 0;
}
}
}
#pragma vector = USART0_RXC_vect
__interrupt void USART0_RXC(void)
{
static unsigned char temp = 0;
temp = UDR0;
if (My_UART_RxBuf.number < (UART_RXBUF_LENGTH - 1)) // 接收缓冲区未满
{
My_UART_RxBuf.da
My_UART_RxBuf.number++;
My_UART_RxBuf.tail++;
if (My_UART_RxBuf.tail == UART_RXBUF_LENGTH)
{
My_UART_RxBuf.tail = 0;
}
}
else
{
// 接收缓冲区溢出
}
}
上一篇:STM32 arm汇编(thumb-2指令集)启动代码分析
下一篇:STM32 BIT_BAND 位段位带别名区使用入门
推荐阅读最新更新时间:2024-03-16 15:14