0

USART 嵌入式 c 用于 atmega328p。尝试在接收到某个字符(在我的情况下为 char $)后存储任何用户输入的 10 个字符的数组。这为我编译,但仅在我使用 hercules 实用程序阅读器输入一串字符时输出美元符号。任何帮助表示赞赏

以下是我正在使用的代码的副本

#define FOSC 16000000 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1

#include <avr/io.h>
//#include <stdio.h>

char trig='$';
char arr[10];

//This function is used to initialize the USART
//at a given UBRR value
void USARTInit(unsigned int ubrr)
{
    //Set Baud rate
    UBRR0H = (ubrr>>8);
    UBRR0L = ubrr;

    //Enable The receiver and transmitter
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    // Set fram format: 8data 2stopBit
    UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

//This function is used to read the available data
//from USART. This function will wait untill data is
//available.
unsigned char USARTReadChar( void )
{
    //Wait untill a data is available

    while(!(UCSR0A & (1<<RXC0)))
    {
        //Do nothing
    } 

    //Now USART has got data from host
    //and is available is buffer

    return UDR0;
}

//This function writes the given "data" to
//the USART which then transmit it via TX line
void USARTWriteChar(unsigned char data)
{
    //Wait untill the transmitter is ready

    while(!(UCSR0A & (1<<UDRE0)))
    {
        //Do nothing
        PORTD ^= 1 << PINB2;
    }

    //Now write the data to USART buffer

    UDR0 = data;
}

int main(void)
{
    DDRB |= 1 << PINB2;

    //Varriable Declaration
    char data;

    USARTInit(MYUBRR);   

    //Loop forever

    while(1)
    {
        //Read data
        data = USARTReadChar();
        int i =0;

        //if incoming data is a dollar sign(trig),
        if(data==trig)
        {
            //start a loop to collect data from buffer
            for(i=0;i<10;i++)
            {
                //array has 10 elements, will fill up the ith element as   per for loop
                arr[i]=data;
                // printf("arrayoutput %c\n",arr[i]);
                USARTWriteChar(data);
            }
        }
    }
}

我按照oleg的建议编辑了while循环,但仍然无法让它返回数组。整个代码如下:

#define FOSC 16000000 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
#include <avr/io.h>
#include <stdio.h>

char trig='$';
char arr[10];
//This function is used to initialize the USART
//at a given UBRR value
void USARTInit(unsigned int ubrr)
{
   //Set Baud rate

    UBRR0H = (ubrr>>8);
    UBRR0L = ubrr;

   //Enable The receiver and transmitter
  UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  // Set fram format: 8data 2stopBit
  UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

//This function is used to read the available data
//from USART. This function will wait untill data is
//available.
unsigned char USARTReadChar( void )
{
   //Wait untill a data is available

   while(!(UCSR0A & (1<<RXC0)))
   {
      //Do nothing
   }

   //Now USART has got data from host
   //and is available is buffer

   return UDR0;
}


//This function writes the given "data" to
//the USART which then transmit it via TX line
void USARTWriteChar(unsigned char data)
{
   //Wait untill the transmitter is ready

   while(!(UCSR0A & (1<<UDRE0)))
   {
      //Do nothing
          PORTD ^= 1 << PINB2;

   }

   //Now write the data to USART buffer

   UDR0 = data;
}

int main(void)
{
DDRB |= 1 << PINB2;

   //Varriable Declaration
   char data;

   USARTInit(MYUBRR);   

   //Loop forever

      //Read data

      char input[10];
      while(1){
          data = USARTReadChar();
          if(data == trig){
              for(int i = 0; i < 10; i++){
                  //here we're saving 10 characters to input array
                  input[i] = USARTReadChar();
                   USARTWriteChar(input[i]);//tested without this also
              }
          }
      }
}
4

1 回答 1

1

尝试在 for() 循环中读取字符:

char input[10];
while(1){
    data = USARTReadChar();
    if(data == trig){
        for(int i = 0; i < 10; i++){
            //here we're saving 10 characters to input array
            input[i] = USARTReadChar();
        }
        /* UPD: write stored array to console */
        for(int i =0; i < 10; i++){
            USARTWriteChar(input[i]);
        }
        /* those symbols are needed to emulate Enter button */
        USARTWriteChar('\r');
        USARTWriteChar('\n');
     }
 }

UPD:此代码完全符合您的要求。它在内存中存储 10 个字符。要将它们返回到控制台(实用程序阅读器),您必须使用 USARTWriteChar()。

于 2013-10-30T14:26:18.587 回答