本程序实现UART中断方式发送和接收数据功能,将接收到的数据立即发出,形成回环。
由测试程序,相关头文件<uart_test.h>,UART数据处理程序组成。
/* ************************************************
* File name: main.c
* Function: AVR的UART数据收发测试程序
* Description: 实现将RXD端口收到的数据转发至TXD端口
* 收发工作由UART中断自动完成
* Author & Date: Joshua Chan, 2012/03/28
* ************************************************/
#include <ioavr.h>
#include
#include
#include
#include
#include "uart_test.h"
/* 测试程序: 将串口接收的数据原样送出 */
void main(void)
{
unsigned char data;
fifo_pos_init(); /* 重置位置记录 */
uart_init(B9600); /* 初始化UART */
_SEI();
while (1) {
if ((data = rx_byte()) != NONE_DATA)
tx_byte(data);
_WDR(); /* 喂狗 */
}
}</io
/* ************************************************
* File name: uart_test.h
* Function: AVR的UART数据收发测试程序
* Description: 实现将RXD端口收到的数据转发至TXD端口
* 收发工作由UART中断自动完成
* Author & Date: Joshua Chan, 2012/03/28
* ************************************************/
#ifndef _UART_TEST_H
#define _UART_TEST_H
/* 定义FOSC=16.0MHz时的UBRR值 */
__flash enum baudrate {
B2400 = 416,
B4800 = 207,
B9600 = 103,
B14400 = 68,
B19200 = 51,
B28800 = 34,
B38400 = 25,
B57600 = 16,
B76800 = 12,
B115200 = 8,
B230400 = 3,
};
#define NONE_DATA '\4' /* 无新数据标志 */
#define RX_BUF_SIZE 32 /* 接收缓冲区大小 */
#define TX_BUF_SIZE 32 /* 发送缓冲区大小 */
/* 重置位置记录 */
extern void fifo_pos_init(void);
/* UART初始化 */
extern void uart_init(unsigned int baud);
/* 从接收缓冲区中读1字节 */
extern unsigned char rx_byte(void);
/* 将1字节数据送入发送缓冲区, 同时使能数据寄存器空中断 */
extern void tx_byte(unsigned char data);
#endif
/* ************************************************ * File name: uart_test.c * Function: AVR的UART数据收发测试程序 * Description: 实现将RXD端口收到的数据转发至TXD端口 * 收发工作由UART中断自动完成 * Author & Date: Joshua Chan, 2012/03/28 * ************************************************/#include #include #include #include #include #include "uart_test.h"unsigned char rx_fifo[RX_BUF_SIZE]; /* 接收缓冲区 */unsigned char tx_fifo[TX_BUF_SIZE]; /* 发送缓冲区 */unsigned char rx_pos; /* 缓冲区中最后接收数据的位置 */unsigned char read_pos; /* 缓冲区中最后读出数据的位置 */unsigned char tx_pos; /* 缓冲区中最后发送数据的位置 */unsigned char write_pos; /* 缓冲区中最后写入数据的位置 *//* UART初始化 */void uart_init(unsigned int baud){ /* 先禁用USART功能 */ UCSR0B = 0x00; /* 设置baudrate */ UBRR0H = (unsigned char)(baud>>8); UBRR0L = (unsigned char)baud; /* 设置帧格式: 8个数据位, 1个停止位 */ UCSR0C = 3 << UCSZ00; /* 使能: 接收结束中断, 接收, 发送 */ UCSR0B = (1<<rxcie0) |="" (1<<rxen0)="" (1<<txen0);="" }="" *="" 重置位置记录="" void="" fifo_pos_init(void)="" {="" rx_pos="0;" read_pos="0;" tx_pos="0;" write_pos="0;" 接收结束中断处理函数="" #pragma="" vector="USART0_RXC_vect" __interrupt void uart_rxc_isr(void){ unsigned char udr0; /* 读取数据寄存器 */ udr0 = UDR0; /* 缓冲区满则从头开始写 */ if (++rx_pos == RX_BUF_SIZE) rx_pos = 0; /* 将读取的数据写入缓冲区 */ rx_fifo[rx_pos] = udr0;}/* 从接收缓冲区中读1字节 */unsigned char rx_byte(void){ /* 判断是否有数据可读 */ if (read_pos != rx_pos) { /* 缓冲区满则从头开始读 */ if (++read_pos == RX_BUF_SIZE) read_pos = 0; return rx_fifo[read_pos]; } else { /* 无数据可读返回一个标志 */ return NONE_DATA; }}/* 数据寄存器空中断处理函数 */#pragma vector = USART0_UDRE_vect__interrupt void uart_udre_isr(void){ /* 判断是否有数据可发 */ if (tx_pos != write_pos) { if (++tx_pos == TX_BUF_SIZE) tx_pos = 0; /* 将缓冲区数据写入数据寄存器 */ UDR0 = tx_fifo[tx_pos]; } else { /* 无数据可发则禁止数据寄存器空中断, 否则此中断会一直发生 */ UCSR0B &= ~(1 << UDRIE0); }}/* 将1字节数据送入发送缓冲区, 同时使能数据寄存器空中断 */void tx_byte(unsigned char data){ if (++write_pos == TX_BUF_SIZE) write_pos = 0; /* 数据送入缓冲区 */ tx_fifo[write_pos] = data; /* 使能中断, 以便自动发送 */ UCSR0B |= (1 << UDRIE0);}
关键字:AVR UART 数据收发 |