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;
}
}
}