cpubbs论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

LabVIEW+单片机学习套件全套教程资料下载[免费]LabVIEW论坛精华列表贴USB0816数据采集卡《LabVIEW宝典》
LabWindows/CVI论坛精华贴NET0816以太网数据采集卡RC0210远程设备授权系统 关闭关停锁定打开设备 户外分布式数据采集
NET1624低速高精度以太网数据采集卡WIFI0824SD无线WIFI网络数据采集卡脱机运行 SD存储 小尺寸微型 串口采集远程采集 安卓 手持移动采集 纪录仪
查看: 1390|回复: 0

一个非常好的串口程序![转载]

[复制链接]
发表于 2004-11-1 18:18:30 | 显示全部楼层 |阅读模式
#include <iom128v.h>
#include <macros.h>
#include "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<<RXCIE0) | (1<<RXEN0) | (1<<TXEN0) );/*RX Complete
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<<UDRIE0); /* disable UDRE interrupt */
}
}

/* 读取数据函数 */
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<<UDRIE0); /* enable UDRE interrupt */
}

/* 查询是否有数据 */
unsigned char DataInReceiveBuffer( void )
{
return ( USART_RxHead != USART_RxTail ); /* return 0 (FALSE) if the
receive buffer is empty */
}

/* 中断中只做很少的事情,通常我是在接收中断中post一个信号量,触发别的任务读数据
*/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|cpubbs论坛. ( 粤ICP备09171248号 )

GMT+8, 2025-4-4 02:35 , Processed in 0.806021 second(s), 7 queries , Gzip On, File On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表