yyl8324 发表于 2014-2-19 15:20:45

咨询avr串口发送接收问题

现在的程序只能发送接收单个字符,如果通过串口发送一个字符串返回一个字符串   应该怎样修改   新手求指教谢谢
# include <iom16v.h>
# include <macros.h>
#pragma interrupt_handler isr:12
# define uchar unsigned char
# define uint unsigned int
# define fosc    8000000
uchar rdata, flag;
void uart_init(uint baud)
{
       uint a;
       UCSRC = 0x86;
       a = fosc/16/baud-1;
       UBRRL = a%256;
       UBRRH = a/256;
       UCSRB = 0x98;
       SREG |= BIT(7);
}

void isr()
{
       UCSRB &= ~BIT(7);
       rdata = UDR;
       flag = 1;
       UCSRB|= BIT(7);
}

void uart_send(uchar data)
{
       while (!(UCSRA&BIT(5)));
       UDR = data;
       while (!(UCSRA&BIT(6)));
       UCSRA |= BIT(6);
}

void main()
{
       DDRD |= 0x02;
       uart_init(9600);
       while (1)
       {
                     if (flag)
                   {
                             uart_send(rdata);
                          flag = 0;
                   }
       }
}
页: [1]
查看完整版本: 咨询avr串口发送接收问题