本帖最后由 zydlyq 于 2011-7-19 19:39 编辑
写了一个串口收发的程序,就是把收到的值再发回去,但是总是收到和发送的值不一样,要么就是收不到。还请高人指点!!谢谢!!!!
#include <avr/io.h>
#include <avr/interrupt.h>
#define uchar unsigned char
#define uint unsigned int
#define FOSC 16000000
#define BUAD 9600
#define MYBUBRR FOSC/16/BUAD-1
//#define data 11010
void USART_Init (uint ubrr)
{
/*Set buad rate*/
UBRR0H=(uchar)(ubrr>>8);
UBRR0L=(uchar)ubrr;
/*Enable receive and transmitter*/
UCSR0B=(1<<RXEN)|(1<<TXEN);
/* Set frame format :8data,2stop bit*/
UCSR0C=(1<<USBS)|(3<<UCSZ0);
DDRE=0x02;
}
void USART_Transmit(uchar c)
{
/*wait for empty transmit buffer*/
while(!(UCSR0A&(1<<UDRE0)));
/*Put data into buffer,sends the data*/
UDR0=c;
}
uchar USART_Receive(void)
{
/*Waite fro data to be received*/
while(!(UCSR0A&(1<<RXC0)));
/*Get and return received data from buffer*/
return UDR0;
}
int main(void)
{
while(1)
{
USART_Init(MYBUBRR);
USART_Transmit(USART_Receive());
}
} |