当前位置:首页 > 单片机 > 单片机
[导读]#include "myuart.h"#include "avr/io.h"#include#define UDRE 5#define RXEN 4#define TXEN 3#define UCSZ0 1#define RXC 7#define RXCIE 7ISR(USART0_RX_vect)//接收中断{ unsigned char dummy; while ( UCSR0A &

#include "myuart.h"
#include "avr/io.h"
#include

#define UDRE 5
#define RXEN 4
#define TXEN 3
#define UCSZ0 1
#define RXC 7
#define RXCIE 7

ISR(USART0_RX_vect)//接收中断
{
unsigned char dummy;
while ( UCSR0A & (1< {
dummy = UDR0;
}
USART_Transmit(dummy);
}

void USART_Transmit(unsigned char data)//发送字节
{
/* 等待发送缓冲器为空 */
while ( !( UCSR0A & (1< ;
/* 将数据放入缓冲器,发送数据 */
UDR0 = data;
}


void myuart_init(void)
{
//关总中断
cli();
//4MHZ晶振,设置波特率
UCSR0A &= 0x02; //不倍频
unsigned char baud;
baud = 25; //9600
UBRR0H = (unsigned char)(baud>>8);
UBRR0L = (unsigned char)baud;
// 接收器与发送器使能,接收中断允许
UCSR0B = (1< // 设置帧格式: 8 个数据位, 1个停止位
UCSR0C = (3< //关于中断
//开总中断
sei();
}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭