XMOS笔记2--key输入

 


2、port的输入

/*

XS1_PORT_4B


KEY1--X1D04--4B0
KEY2--X1D05--4B1
KEY3--X1D06--4B2
SW----X1D07--4B3
*/

on tile[1]: in port key = XS1_PORT_4B;
void task1(in port key1)
{
	int key_status;
	while(1)
	{
		key1 :> key_status;
		select
		{
			case key1 when pinsneq( 0x1) :> int y:
                y = key_status;
			
			    if( key_status == 0xe)
			    {
				    printf("The key1 is pressed. key_status: %x y:%x \n",key_status,y);
			    }
                else if(key_status == 0xd)
			    {
				    printf("The key2 is pressed. key_status: %x y:%x \n",key_status,y);
			    }
			    else if(key_status == 0xb)
			    {
				    printf("The key3 is pressed. key_status: %x y:%x \n",key_status,y);
			    }
                else if(key_status == 0x7)
			    {
				    printf("The SW is pressed. key_status: %x y:%x \n",key_status,y);
			    }
			break;
		}
	
	}
}

int main(void)
{

	par
	{
		on tile[1]: task1(key);	
	}
	
    return 0;
}

 


注意::事实证明这个例程也只能说是让大家知道怎么用输入,因为我在select里添加条件时发现,“case key1 when pinsneq( 0x1) :> int y:”这个case检测频率太高了,导致占用了线程的高资源,以至于加上其他case时,没办法相应其他case,即使是触发了其他case的条件,也没资源去执行其他case了。另外,这种方式的按键事件是没有按键消抖的。也就是说按一下按键会有多次触发。

void task1(in port key1)
{
	int key_status;	
	int is_stable = 1;
	timer tmr;
	unsigned period=1000000;//10 0000 timer ticks = 1ms, 10000 0000  => 1s
	unsigned time;
	tmr :> time;
	while(1)
	{
				
		select
		{
			case !is_stable => tmr when timerafter(time) :> void:	
				time += period;				
				is_stable=1;
				printf("time : 10ms\n");
			break;
            
            case is_stable => key1 when pinsneq(0x1) :> key_status:
				is_stable=0;
				
				if( key_status == 0xe)
				{
					printf("The key1 is pressed. key_status: %x \n",key_status);
				}
                else if(key_status == 0xd)
				{
					printf("The key2 is pressed. key_status: %x \n",key_status);
				}
				else if(key_status == 0xb)
				{
					printf("The key3 is pressed. key_status: %x \n",key_status);
				}
                else if(key_status == 0x7)
				{
					printf("The SW is pressed. key_status: %x \n",key_status);
				}
            break;
		}	
	
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值