AVR單片機—(五)、ATMEGA16的USART與PC機串行通信—01(補充)
//------------------------------------------------------------------------------
//ATMEGA16與PC機的通信實驗1
//PC機發(fā)送一個字符給單片機,單片機收到后既在發(fā)光二極管進行顯示,同時將其回傳給PC
//單片機的發(fā)送和接收都采用查詢方式
#include"ioavr.h"
#include"intrinsics.h"
#include"delayics.h"
//------------------------------------------------------------------------------
typedef unsigned char uchar;
typedef unsigned intuint;
#define UDRE5
#define RXC 7
//------------------------------------------------------------------------------
//端口初始化函數(shù)
void port_init()
{
DDRB=0XFF;
PORTB=0XFF;
DDRD=0X02;
PORTD=0XFF;
}
//------------------------------------------------------------------------------
//USART初始化函數(shù)
void usart_init()
{
UCSRA=0X02;//置位U2X,倍速發(fā)送
UCSRC=0X06;//選擇字符長度為8位
UBRRL=0X67;//設(shè)置波特率9600,查看datesheet
UBRRH=0X00;
UCSRB=0X18;//發(fā)送使能、接收使能
}
//------------------------------------------------------------------------------
//芯片初始化函數(shù)
void device_init()
{
__disable_interrupt();
port_init();
usart_init();
}
//------------------------------------------------------------------------------
//發(fā)送5~8位數(shù)據(jù)位的幀
void usart_send_8(uchar i)
{
while(!(UCSRA&(1<
}
//------------------------------------------------------------------------------
//接收5~8位數(shù)據(jù)位的幀
uchar usart_receive_8()
{
while(!(UCSRA&(1<
}
//------------------------------------------------------------------------------
//main
void main()
{
uchar temp;
device_init();
while(1)
{
temp=usart_receive_8();
PORTB=~temp;
usart_send_8(temp);
}
}