我可以修改_delay_ms();检查按键是否按下?如果是这样,怎么样? [英] Can I modify _delay_ms(); for checking if key is pressed? If so, how?

查看:122
本文介绍了我可以修改_delay_ms();检查按键是否按下?如果是这样,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题_delay_ms();妨碍我。它基本上等待它完成然后运行另一个代码。那么有什么方法可以强制在_delay_ms()中读取键盘;?我在AtmelStudio工作。





这是我的代码:



 int EndOfGame()
{
char key;
int result = 1;
key = KBD_GetKey();
//我最初试过这个:
// display_msg = DSIP_END_SCORE;
// if(key == BTN_OK)返回结果;
// _ delay_ms(2000);

int i,倒计时;
for(i = 0; i< 1000&& key == 0; i ++){
countdown = 0;
display_msg = DISP_REFRESH_ALL; //刷新屏幕
display_msg = DSIP_END_SCORE; //显示结束分数

while(倒计时++< = 1000)
if(key == BTN_OK)返回结果; _delay_ms(1);
if(key == 0)_delay_ms(9000);
// _ delay_ms(10);
// display_msg = DSIP_END_SCORE;
// if(key == BTN_OK)返回结果;
//返回结果;
}
返回结果;
}





我的尝试:



我试过:



 display_msg = DSIP_END_SCORE; 
_delay_ms(10000);
if(key!= 0){
switch(key)
{
case BTN_OK:
return result;
休息;
}
}

解决方案

更好的想法是编写自己的_delay_ms_and_read_keyboard()函数。伪代码看起来像这样:



1. start_time = current_time_in_ms()

2. while(current_time_in_ms() - start_time <延迟)

2.1如果(按键已准备好),读取并返回(按键)

2.2结束时

3.返回(没有发现按键)



您需要研究系统所需的三个功能的定义(获取当前时间,检查按键,读取按键)。 BLOCKQUOTE>

I got a problem where _delay_ms(); gets in my way. It basically waits until it's complete and then runs another code. So is there any way that i force reading a keyboard in _delay_ms();? I work in AtmelStudio.


Here is my code:

int EndOfGame()
{
	char key; 
	int result = 1; 
	key = KBD_GetKey();
	//I initially  tried this:
        //display_msg = DSIP_END_SCORE;
	//if (key == BTN_OK) return result;
	//_delay_ms(2000);
	
	int i, countdown;
	for(i = 0; i<1000 && key==0; i++){
		countdown = 0;
		display_msg = DISP_REFRESH_ALL; //refresh screen
		display_msg = DSIP_END_SCORE; //displays end score
       
		while(countdown++ <= 1000)
			if (key == BTN_OK) return result; _delay_ms(1);
			if (key == 0) _delay_ms(9000);
		//_delay_ms(10);
		//display_msg=DSIP_END_SCORE;
		//if (key == BTN_OK) return result;
		//return result;
	}
	return result;
}



What I have tried:

I tried:

display_msg = DSIP_END_SCORE;
        _delay_ms(10000);
	if (key != 0){
		switch (key)
		{
			case BTN_OK:
			return result;
			break;
		}
	} 

解决方案

A better idea is to write your own _delay_ms_and_read_keyboard() function. The pseudo-code would look something like this:

1. start_time = current_time_in_ms()
2. while (current_time_in_ms() - start_time < delay)
2.1 if (a keypress is ready), read and return (keypress)
2.2 end while
3. return (no keypress found)

You will need to research the definitions of the three functions required (get current time, check for keypress, read keypress) on your system.


这篇关于我可以修改_delay_ms();检查按键是否按下?如果是这样,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆