如何在uart中读取数据并同时执行循环?

时间:2020-07-11 07:58:00

标签: c uart

我的程序是赛车游戏。问题是即使我没有按任何键来接收UART,我也希望敌方汽车行驶。

当到达按键读取指令(Serial.read)时,它不会执行任何其他指令,例如敌人速度功能,该功能允许移动敌方坦克。

即使我没有在uart中写任何字母,如何执行该功能?

#include "tm4c123gh6pm.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include "tivaduino.h"
#include "Nokia5110.h"

//library

uint8_t HardwareSerial0::read(void){
    uint8_t datoRx;
    
    while(UART0_FR_R & 0x10==0x10); 
    

//UART0_FR_R is in 1 if transmission buffer is full (no data is sent). if not, data can be sent to the buffer and it sends to TIVA

    datoRx = (uint8_t)UART0_DR_R;
    return datoRx;

}

///我想到的一个选项是在“读取”中使用UART_FR_TXFE

//main program
 while (1) {


//is responsible for moving enemy cars to the far left and then disappear

Nokia5110_enemy_speed (positionk1, positionk2, positionk3, positionk4, positionk5,
             positionh1, positionh2, positionh3, positionh4, positionh5); 


//read a key on the uart (defined to be w, a, s, d)
char_key = Serial.read ();

sprintf (key, "% c", char_key); `//convert char_key into string`


if ((h <24) && (h> = 0)) {// as long as x remains on the map

   if ((strcmp (key, "w") == 0) && (h> 0)) {

Nokia5110_clean_cart (k, h);
h = h-8;
        Nokia5110_drawing_cart (k, h);
    }
     if ((strcmp (key, "s") == 0) && (h <16)) {
Nokia5110_clean_cart (k, h);
       h = h + 8;
   
      Nokia5110_drawing_cart (k, h);
    }
if ((strcmp (key, "d") == 0) && (k <78)) {
    Nokia5110_clean_cart (k, h);
       k = k + 6;
   
      Nokia5110_drawing_cart (k, h);
    }
if ((strcmp (key, "a") == 0) && (k> 0)) {
     Nokia5110_clean_cart (k, h);
       k = k-6;
    
     Nokia5110_drawing_cart (k, h);
    }

  }

 
}

1 个答案:

答案 0 :(得分:0)

您正在积极等待UART的下一个字节。相反,请尝试仅检查是否收到了字节。如果是这样,则返回它,否则返回一些“空”代码。例如:

return (UART0_FR_R & 0x10 == 0x00) ? (uint8_t)UART0_DR_R : 0xff;

我选择0xff作为“无字符读取”的标记(假设没有vlid键的代码为0xff)。