觋-拂晓 发表于 2013-4-2 18:29:28

usart接收,数据丢失问题

我用mega16发送5个字节给mega128a1时,总是接收不到最后一个字节,两者的波特率都是9600。求指导。
mega16:

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

#ifndef BAUD
#define BAUD 9600
#endif
#include "Anyway_uart.h"
void USART_Init(void)
{
        DDRD&=0xfe;
        PORTD=0xff;
        /*设置波特率*/
        //UBRRH = 0;
        //UBRRL = 16;/*设置波特率,16M晶振,57600波特率,参考DataSheet*/               
        UBRRH = (((F_CPU/BAUD)/16)-1)/256;
        UBRRL = (((F_CPU/BAUD)/16)-1)%256;       
        UCSRB |= (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
        //使能发送,接收,接收中断       
}

void Putc(uint8_t data)
{
        /*等待发送缓冲器为空*/
        while( !(UCSRA & (1<<UDRE)));
        /*将数据放入缓冲区,发送数据*/
        UDR = data;       
        while(!(UCSRA&(1<<TXC)));//等待发送完成
}

void putBuffer(uint8_t* data,uint8_t len)
{
        int i;
        for (i=0;i<len;i++)
        {
                loop_until_bit_is_set(UCSRA, UDRE);
                UDR = data;
                while(!(UCSRA&(1<<TXC)));//等待发送完成
        }
}
void Puts(char *str)
{
        while(*str!='\0')
        {
                Putc(*str);
                str++;
        }       
}
int uart_putchar(char c, FILE *stream)
{
        if (c == '\n')
        uart_putchar('\r', stream);
        loop_until_bit_is_set(UCSRA, UDRE);
        UDR = c;
        while(!(UCSRA&(1<<TXC)));//等待发送完成
        return 0;
}
在main函数里循环发送:
while(1)
        {
                _delay_ms(10000);
                putBuffer(request,5);
        }
mega128a1:

ISR(USARTF0_RXC_vect)
{
        PORTE.OUTTGL = 0x80;
        //清除中断标志位
        //When interrupt-driven data reception is used, the receive complete interrupt routine must read
        //the received data from DATA in order to clear the RXCIF
        recv = USARTF0_DATA;
        printf("IRQn recv[%d]=%x\r\n",recv_count,recv);
        recv_count++;
        if(recv_count == 5)
        {
                flag = 1;
                recv_count = 0;
        }       
}
每次都只能接收到4个数据!{:cry:}

hugolulu 发表于 2013-4-2 23:44:57

等待发送完成后TXC写1清零试试

lunhuiliudao 发表于 2013-4-3 07:50:41

应该是晶振的问题!!!!!!!!
页: [1]
查看完整版本: usart接收,数据丢失问题