0

atmega328p用のUSART組み込みc。特定の文字が受信された後にユーザーが入力したものの10文字の配列を保存しようとしています(私の場合はchar $)。これは私のためにコンパイルされますが、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 に答える