c - USART嵌入C.触发字符存储数组

标签 c embedded

用于 atmega328p 的 USART 嵌入式 c。尝试在收到某个字符(在我的例子中为 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
              }
          }
      }
}

最佳答案

尝试在 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()。

关于c - USART嵌入C.触发字符存储数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19684606/

相关文章:

c - 尝试使用 clock_gettime(),但从 time.h 中得到大量 "undeclared"错误

c++ - 在没有 FPU 的情况下 float 会怎样?

c - PIC16F883 LED 闪烁

c - 不同文件中的相同函数返回不同的结果

c - 重定向 VxWorks 串行输入

optimization - llvm 使用库函数进行优化

c - 在 Xcode 上编译时出现 (lldb)

c - 如何在 C 中同时运行两个子进程?

c - 在嵌入式 linux 中模拟触摸屏点击

c - 我的 C 程序的输出不是预期的