我用的是ATMEga64A的芯片,想用串口做一个通信程序,但是不能接受和发送,这是程序,求各位大神帮忙指点一下
#include "math.h"
#include "iom64v.h"
#include "main.h"
#include "AVRdef.h"
#include "uart.h"
#include <macros.h>
//#pragma interrupt_handler UART0_RX_interrupt:10 UART0_TX_interrupt:11
#pragma interrupt_handler UART0_RX_interrupt:iv_USART0_RXC
#pragma interrupt_handler UART0_TX_interrupt:iv_USART0_TXC
/* UART Buffer Defines */
#define UART0_RX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */
#define UART0_RX_BUFFER_MASK ( UART0_RX_BUFFER_SIZE - 1 )
#define UART0_TX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */
#define UART0_TX_BUFFER_MASK ( UART0_TX_BUFFER_SIZE - 1 )
#if ( UART0_RX_BUFFER_SIZE & UART0_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif
/* Static Variables */
static unsigned char UART0_RxBuf[UART0_RX_BUFFER_SIZE];
static volatile unsigned char UART0_RxHead;
static volatile unsigned char UART0_RxTail;
static unsigned char UART0_TxBuf[UART0_TX_BUFFER_SIZE];
static volatile unsigned char UART0_TxHead;
static volatile unsigned char UART0_TxTail;
void Delay(int t)
{
while(t--);
}
void delay_ms(unsigned int time)
{
unsigned int t=0;
for( ;t<=1000*time;t++);
}
void IO_Configuration(void)
{
// PORTE=(0<<PE0);
// DDRE=(0<<DDE0);
// PORTE=(0<<PE1);
// DDRE=(1<<DDE1);
// PORTD=(0<<PD3)|(0<<PD2);
// DDRD=(1<<DDD3)|(0<<DDD2);
}
/* initialize UART */
void InitUART0( unsigned char baudrate )
{
unsigned char x;
UBRR0H=(unsigned char)(baudrate>>8);
UBRR0L=(unsigned char)baudrate; /* set the baud rate */
/* enable UART receiver and transmitter, and
receive interrupt */
UCSR0B=( (1<<RXCIE0) | (1<<RXEN0) | (1<<TXEN0) );
UCSR0C=(1<<USBS0)|(3<<UCSZ00);
x = 0; /* flush receive buffer */
UART0_RxTail = x;
UART0_RxHead = x;
UART0_TxTail = x;
UART0_TxHead = x;
}
/* interrupt handlers */
void UART0_RX_interrupt( void )
{
unsigned char data;
unsigned char tmphead;
data = UDR0; /* read the received data */
/* calculate buffer index */
tmphead = ( UART0_RxHead + 1 ) & UART0_RX_BUFFER_MASK;
UART0_RxHead = tmphead; /* store new index */
if ( tmphead == UART0_RxTail )
{
/* ERROR! Receive buffer overflow */
}
UART0_RxBuf[tmphead] = data; /* store received data in buffer */
}
void UART0_TX_interrupt( void )
{
unsigned char tmptail;
/* check if all data is transmitted */
if ( UART0_TxHead != UART0_TxTail )
{
/* calculate buffer index */
tmptail = ( UART0_TxTail + 1 ) & UART0_TX_BUFFER_MASK;
UART0_TxTail = tmptail; /* store new index */
UDR0 = UART0_TxBuf[tmptail]; /* start transmition */
}
else
{
UCSR0B &= ~(1<<UDRIE0); /* disable UDRE interrupt */
}
}
/* Read and write functions */
unsigned char ReceiveByte0( void )
{
unsigned char tmptail;
while ( UART0_RxHead == UART0_RxTail ) /* wait for incomming data */
;
tmptail = ( UART0_RxTail + 1 ) & UART0_RX_BUFFER_MASK;/* calculate buffer index */
UART0_RxTail = tmptail; /* store new index */
return UART0_RxBuf[tmptail]; /* return data */
}
void TransmitByte0( unsigned char data )
{
unsigned char tmphead;
/* calculate buffer index */
tmphead = ( UART0_TxHead + 1 ) & UART0_TX_BUFFER_MASK;
/* wait for free space in buffer */
while ( tmphead == UART0_TxTail )
;
UART0_TxBuf[tmphead] = data; /* store data in buffer */
UART0_TxHead = tmphead; /* store new index */
UCSR0B |= (1<<UDRIE0); /* enable UDRE interrupt */
}
unsigned char DataInReceiveBuffer0( void )
{
return ( UART0_RxHead != UART0_RxTail );
/* return 0 (FALSE) if the receive buffer is empty */
}
void main(void)
{
IO_Configuration();//
InitUART0( 6 );
_SEI();
while(1)
{
TransmitByte0( ReceiveByte0() ); /* echo the received character */
}
}
|