从AVR的UART接收char *

时间:2014-02-04 18:16:34

标签: c avr uart

我希望UART使用ATMEGA16接收一个字符串(指向字符的指针)。我在套件上烧了这个代码然后我使用了超级终端(realterm)并进行了测试以输入一个字符串(“on”),如果收到它,那么portc(LEDS)将被设置为1但它不起作用.. 。 任何人!? :D

功能的实施

#include <avr/io.h>

#define F_CPU 8000000UL
unsigned char *x;

#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

void uartinit()
{
    UCSRB |= (1 << RXEN) | (1 << TXEN);   
                    // Turn on the transmission and reception circuitry
    UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); 
                    // Use 8-bit character sizes

    UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..
                           // into the low byte of the UBRR register
    UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
                                  // into the high byte of the UBRR register
}

void uartsend( unsigned char *data )
{
    while(*data != '\0')
    {
        /* Wait for empty transmit buffer */
        while ( !( UCSRA & (1<<UDRE)) );
        /* Put data into buffer, sends the data */
        UDR = *data;
        data++;
    }


    while ( !( UCSRA & (1<<UDRE)) );

    UDR = *data;

}

unsigned char * uartrecieve()
{
    //unsigned char i=0;
    //    unsigned char * x;

    /* Wait for data to be received */

    //    char * ptr = &UDR;

    while ( !(UCSRA & (1<<RXC)) );

    while(UDR != '\0')
    {
        *x = UDR;
        x++;
        while ( !(UCSRA & (1<<RXC)) );
    }
    *x = UDR;

     return x;
} 

这是主要功能

#include <avr/io.h>
#include "UARTInterface.h"


int main(void)
{
    DDRC=0xFF;
    uartinit();
    while(1)
    {
        unsigned char *y;
        y=uartrecieve();    
        if(strcmp(y,"on")==0)    
        {
            PORTC=0xff;

        }
        //uartsend(y);
        //TODO:: Please write your application code 
    }
} 

1 个答案:

答案 0 :(得分:3)

您的代码存在一些问题:

1。您没有为收到的字符分配任何空间。您有一个全局unsigned char *x(未初始化),您取消引用并为其赋值,然后递增 - 这只是覆盖内存中的随机位置。

您应该通过从调用函数(在本例中为main)创建一个数组来指定一些空间,并将指针传递给uartreceive以及缓冲区的大小

unsigned char y[20]; // in main
unsigned char len;
len = uartreceive(y, 20);
...

然后(注意这是未经测试的)

unsigned char uartrecieve(unsigned char *x, unsigned char size)
{
    unsigned char i = 0;

    if (size == 0) return 0;            // return 0 if no space

    while (i < size - 1) {              // check space is available (including additional null char at end)
        unsigned char c;
        while ( !(UCSRA & (1<<RXC)) );  // wait for another char - WARNING this will wait forever if nothing is received
        c = UDR;
        if (c == '\0') break;           // break on NULL character
        x[i] = c;                       // write into the supplied buffer
        i++;
    }
    x[i] = 0;                           // ensure string is null terminated

    return i + 1;                       // return number of characters written
} 

每次调用此函数时,它都会覆盖rx_buffer的先前内容,因此请确保首先使用它。如果您不确定这里发生了什么,请阅读数组,指针和字符串。

最好还是传入指向uartreceive的指针,以便调用函数可以提供内存区域

2。默认情况下,您的串行终端软件不太可能发送NULL终止字符串(即最后使用'\ 0'),通常它会发送一个新行(' \ n')角色。我相信realterm可以做到这一点,但值得检查。

3. 从UDR读取将清除RXC标志,允许AVR将另一个字符写入UDR,因此连续两次从UDR读取可能是一个坏主意