-2
#include <reg51.h>
#include "_LCD_R8C.c"
#define INPUT_LENGTH 11

char input[INPUT_LENGTH];  /* The input from the serial port */
int  input_pos = 0;       /* Current position to write in the input buffer */

int main()
{
  int i;

  lcd_init();
  lcd_clear();
  SCON = 0x50;
  TMOD = 0x20;                /* timer 1, mode 2, 8-bit reload */
  TH1 = 0xFD;                /* reload value for 2400 baud */
  TR1 = 1;
  TI = 1;
  RI = 1;
  while (1 == 1)
  {
    /* read the next character from the serial port */
    input[input_pos++] = getCharacter ();
    /* send it back to the original sender */
    for (i = 0; i <= input_pos; i++)
    {
      lcd_print_b(input[i]);
    }
  }
}

char getCharacter(void)
{
  char chr[INPUT_LENGTH];           /* variable to hold the new character */

  while (RI != 1) {;}
  chr[input_pos++] = SBUF;
  RI = 0;
  return (chr);
}

rfreader によって読み取られる rs232 から受け取った no を表示してみました。しかし、ディスプレイに間違った値、つまり 0016221826 ではなく 002100 が表示されます。

4

1 に答える 1

0

まず第一に、あなたは本当に正気のインデントスタイルを採用する必要があります、このコードは非常に読みにくいです。コードの問題は、ユーザー入力の配列をローカル配列 "chr"に読み込み、その配列のアドレスを文字ではなくmainに返すことです。main()はアドレスを期待せず、文字を期待します。それにもかかわらず、関数を終了すると、配列「chr」は無効になります。

forループの印刷も正しくなく、意味がありません。新しい文字を受け取るたびに、すべての文字を何度も印刷し続けます。

ハードウェアまたはMCUに他の問題がある可能性があります。私は、最も明白なソフトウェアのバグを修正しました。

#include <reg51.h>
#include "_LCD_R8C.c"

#define INPUT_LENGTH 11


int main()
{
  char input[INPUT_LENGTH];  /* The input from the serial port */
  int  input_pos  = 0;       /* Current position to write in the input buffer */

  lcd_init();
  lcd_clear();

  SCON = 0x50;
  TMOD = 0x20;                /* timer 1, mode 2, 8-bit reload */
  TH1  = 0xFD;                /* reload value for 2400 baud */
  TR1  = 1;
  TI   = 1;
  RI   = 1;

  while(1)
  {
    /* read the next character from the serial port */
    if(input_pos < INPUT_LENGTH)    /* check for buffer overflow */        
    {
      input[input_pos] = getCharacter();
      lcd_print_b(input[input_post]);  /* only makes sense to print each character once */
      input_pos++;
    }
 }


 char getCharacter (void)
 {
   char chr          /* variable to hold the new character */

   while (RI != 1) 
     ;

   chr = SBUF;
   RI = 0;

   return(chr);
 }
于 2012-09-06T06:48:46.223 に答える