anchoret 发表于 2013-5-21 07:07:24

查询方式接收字符串问题。

本帖最后由 anchoret 于 2013-5-21 15:19 编辑

我采用查询方式接收字符串,可每次只能接收到第一个字符,帮我看看怎么回事,谢谢!
unsigned int USART0_Rec( unsigned char *buf )
{
        unsigned int i;
        unsigned long int time;
        i = 0;
        time = 0;
        /* Wait for the first data to be received*/
        while(!( UCSR0A & (1<<RXC0)))
        ;
        buf = UDR0;
        while(!( UCSR0A & (1<<UDRE0)))    //等待读空,去掉也不行
        ;
        i++;
        do
        {
                while(!( UCSR0A & (1<<RXC0)))
                {
                        buf = UDR0;
                        while(!( UCSR0A & (1<<UDRE0))) //等待读空,去掉也不行
                        ;
                        i++;
                }
                time++;
        } while (time <= 5000000);
       
        return i;
}

lkm_unication 发表于 2013-5-21 08:48:56

鉴于这一句while(!( UCSR0A & (1<<RXC0))) 可知寄存器名应该是取值的, 那么buf = UDR0; 把数值传给地址就不合理了。

gongxd 发表于 2013-5-21 08:49:46

接收字符最好用 状态机写

skynet 发表于 2013-5-21 08:57:49

骚年,不要撞了南墙不回头
这货用查询,你做玩具啊??
中断,你唯一的选择

anchoret 发表于 2013-5-21 13:23:24

lkm_unication 发表于 2013-5-21 08:48
鉴于这一句while(!( UCSR0A & (1

哦,我写错了,试试

anchoret 发表于 2013-5-21 15:17:32

是贴过来没了

anchoret 发表于 2013-5-22 07:40:30

本帖最后由 anchoret 于 2013-5-22 07:44 编辑

找到问题了,是do。。。while里那个while的问题{:sweat:},谢谢大家!
页: [1]
查看完整版本: 查询方式接收字符串问题。