ATMEGA128 UART的使用
扫描二维码
随时随地手机看文章
#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<
UCSR0C = (3<
//开总中断
sei();
}