AVR单片机—(五)、ATMEGA16的USART与PC机串行通信—01(补充)
扫描二维码
随时随地手机看文章
//------------------------------------------------------------------------------
//ATMEGA16与PC机的通信实验1
//PC机发送一个字符给单片机,单片机收到后既在发光二极管进行显示,同时将其回传给PC
//单片机的发送和接收都采用查询方式
#include"ioavr.h"
#include"intrinsics.h"
#include"delayics.h"
//------------------------------------------------------------------------------
typedef unsigned char uchar;
typedef unsigned intuint;
#define UDRE5
#define RXC 7
//------------------------------------------------------------------------------
//端口初始化函数
void port_init()
{
DDRB=0XFF;
PORTB=0XFF;
DDRD=0X02;
PORTD=0XFF;
}
//------------------------------------------------------------------------------
//USART初始化函数
void usart_init()
{
UCSRA=0X02;//置位U2X,倍速发送
UCSRC=0X06;//选择字符长度为8位
UBRRL=0X67;//设置波特率9600,查看datesheet
UBRRH=0X00;
UCSRB=0X18;//发送使能、接收使能
}
//------------------------------------------------------------------------------
//芯片初始化函数
void device_init()
{
__disable_interrupt();
port_init();
usart_init();
}
//------------------------------------------------------------------------------
//发送5~8位数据位的帧
void usart_send_8(uchar i)
{
while(!(UCSRA&(1<
}
//------------------------------------------------------------------------------
//接收5~8位数据位的帧
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);
}
}