#i nclude
#i nclude "uart.h"
/* USART Buffer Defines */
#define USART_RX_BUFFER_SIZE 16 /* 1,2,4,8,16,32,64,128 or 256
bytes */
#define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
#define USART_TX_BUFFER_SIZE 16 /* 1,2,4,8,16,32,64,128 or 256
bytes */
#define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
#if ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif
/* Static Variables */
static unsigned char USART_RxBuf[USART_RX_BUFFER_SIZE];
static volatile unsigned char USART_RxHead;
static volatile unsigned char USART_RxTail;
static unsigned char USART_TxBuf[USART_TX_BUFFER_SIZE];
static volatile unsigned char USART_TxHead;
static volatile unsigned char USART_TxTail;
/* 串口初始化 */
void InitUSART(INT8U baudrate)
{
INT8U x;
UBRR0L = baudrate;
UCSR0B = ( (1<
Interrupt Enable, Receiver Enable and Transmitter Enable*/
x = 0; /* flush receive buffer */
USART_RxTail = x;
USART_RxHead = x;
USART_TxTail = x;
USART_TxHead = x;
}
/* 接收中断 */
void USART_RX_interrupt( void )
{
unsigned char data;
unsigned char tmphead;
data = UDR0; /* read the received data */
tmphead = ( USART_RxHead + 1 ) & USART_RX_BUFFER_MASK;/*
calculate
buffer index */
USART_RxHead = tmphead; /* store new index */
if ( tmphead == USART_RxTail )
{
return;/* ERROR! Receive buffer overflow, you can use
flux cortorl */
}
USART_RxBuf[tmphead] = data; /* store received data in buffer
*/
}
/* 发送中断 */
void USART_TX_interrupt( void )
{
unsigned char tmptail;
if ( USART_TxHead != USART_TxTail )/* check if all data is
transmitted */
{
tmptail = ( USART_TxTail + 1 ) & USART_TX_BUFFER_MASK;/*
calculate buffer index */
USART_TxTail = tmptail; /* store new index */
UDR0 = USART_TxBuf[tmptail]; /* start transmition */
}
else
{
UCSR0B &= ~(1<
}
/* 读取数据函数 */
unsigned char ReceiveByte( void )
{
unsigned char tmptail;
while ( USART_RxHead == USART_RxTail ) /* wait for incomming
data */
;
tmptail = ( USART_RxTail + 1 ) & USART_RX_BUFFER_MASK;/*
calculate
buffer index */
USART_RxTail = tmptail; /* store new index */
return USART_RxBuf[tmptail]; /* return data */
}
/* 发送数据函数 */
void TransmitByte( unsigned char data )
{
unsigned char tmphead;
tmphead = ( USART_TxHead + 1 ) & USART_TX_BUFFER_MASK; /*
calculate
buffer index */
while ( tmphead == USART_TxTail )/* wait for free space in
buffer */
;
USART_TxBuf[tmphead] = data; /* store data in buffer */
USART_TxHead = tmphead; /* store new index */
UCSR0B |= (1<
/* 查询是否有数据 */
unsigned char DataInReceiveBuffer( void )
{
return ( USART_RxHead != USART_RxTail ); /* return 0 (FALSE)
if the
receive buffer is empty */
}
/* 中断中只做很少的事情,通常我是在接收中断中post一个信号量,触发别的任务读数据
*/
上一篇:Atmega16 单片机串口通信与外部中断介绍
下一篇:AVR定时器工作原理
推荐阅读最新更新时间:2024-03-16 15:00
设计资源 培训 开发板 精华推荐
- 柔灵科技陈涵:将小型、柔性的脑机接口睡眠设备,做到千家万户
- 微灵医疗李骁健:脑机接口技术正在开启意识与AI融合的新纪元
- USB Type-C® 和 USB Power Delivery:专为扩展功率范围和电池供电型系统而设计
- 景昱医疗耿东:脑机接口DBS治疗技术已实现国产替代
- 首都医科大学王长明:针对癫痫的数字疗法已进入使用阶段
- 非常见问题解答第223期:如何在没有软启动方程的情况下测量和确定软启动时序?
- 兆易创新GD25/55全系列车规级SPI NOR Flash荣获ISO 26262 ASIL D功能安全认证证书
- 新型IsoVu™ 隔离电流探头:为电流测量带来全新维度
- 英飞凌推出简化电机控制开发的ModusToolbox™电机套件
- 意法半导体IO-Link执行器电路板为工业监控和设备厂商带来一站式参考设计