首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何读取uart中的数据并同时执行循环?

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

Stack Overflow用户
提问于 2020-07-11 15:58:00
回答 1查看 235关注 0票数 0

我的程序是赛车游戏。问题是我想让敌人的车移动,即使我没有按任何键让UART接收。

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

即使我没有在uart中写任何字母,我如何执行该函数呢?

代码语言:javascript
复制
#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;

}

//我考虑的一个选项是在"read“中使用UART_FR_TXFE

代码语言:javascript
复制
//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);
    }

  }

 
}
EN

回答 1

Stack Overflow用户

发布于 2020-07-11 16:08:54

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

代码语言:javascript
复制
return (UART0_FR_R & 0x10 == 0x00) ? (uint8_t)UART0_DR_R : 0xff;

我选择0xff作为"no char read“的标记(假设没有vlid key的代码为0xff)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62846625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档