我的程序是赛车游戏。问题是我想让敌人的车移动,即使我没有按任何键让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;
}//我考虑的一个选项是在"read“中使用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);
}
}
}发布于 2020-07-11 16:08:54
您正在积极等待来自UART的下一个字节。相反,尝试只检查是否有接收到的字节。如果是,返回它,否则返回一些“空”代码。例如:
return (UART0_FR_R & 0x10 == 0x00) ? (uint8_t)UART0_DR_R : 0xff;我选择0xff作为"no char read“的标记(假设没有vlid key的代码为0xff)。
https://stackoverflow.com/questions/62846625
复制相似问题