51单片机及AVR单片机的串口通信及Proteus仿真-3
 
- UID
- 852722
|

51单片机及AVR单片机的串口通信及Proteus仿真-3
AVR Atmega64的程序:
两个文件,一个是将函数模块化,别一个是主函数,调用(- -!最近习惯将程序模块化。。。)
//------------------Uart.C---------------------
//----这里将通信函数模块化------------
#Include <Iom64v.H>
Void Uart0_init(Void)
{
UCSR0B = 0x00; //Disable While Setting Baud Rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x33; //Set Baud Rate Lo
UBRR0H = 0x00; //Set Baud Rate Hi
UCSR0B = 0x18;
}
Void Uart0_Transmit( Unsigned Char Data )
{
/* Wait For Empty Transmit Buffer */
While ( !( UCSR0A & (1<<UDRE0)) )
;
/* Copy Ninth Bit To TXB8 */
UCSR0B &= ~(1<<TXB80);
//If ( Data & 0x0100 )
//UCSR0B |= (1<<TXB80);
/* Put Data Into Buffer, Sends The Data */
UDR0 = Data;
}
Unsigned Char Uart0_Receive( Void )
{
/* 等待接收数据*/
While ( !(UCSR0A & (1<<RXC0)) )
;
/* 从缓冲器中获取并返回数据*/
Return UDR0;
}
//--------------Main.C-----------
//--------------------------------
#Include <Iom64v.H>
#Include "Spi.H"
#Define Commun_symbol 0x31
//-----------Send A Commun_symbol-----
//-----------Receive A Commun_symbol--
// <--No,Continue Receive||||||Yes-->Receive The Data And Send
Void Main()
{
Unsigned Char Bybuff;
DDRB=0xff;
PORTB=0xff;
Uart0_init();
{
Do
{
Bybuff=Uart0_Receive();
}
While (Bybuff!=Commun_symbol);//Commun_symbol);
While(1)
{
Uart0_Transmit(Bybuff);
Bybuff=Uart0_Receive();
PORTB=(0xff|Bybuff);
}
}
}
Ok,任务完成了,改天实验一下! |
|
|
|
|
|