strange behaviour of if after select

Technical questions regarding the XTC tools and programming with XMOS.
schmiedl
Junior Member
Posts: 7
Joined: Wed Jul 13, 2011 3:38 pm

strange behaviour of if after select

Post by schmiedl »

Hey guys,
I'm writing a simple button event listener for the XK-1A board. I want to fire a single up/down event, when the button is pressed/released.

Without the timerafter between the select-case and the if, it sometimes happens, that the code in the if-block is executed on button-release

Code: Select all

void button_listener(in port b0, in port b1, chanend led_chan) {
	unsigned b0status, b1status, time;
	timer t;
	b0 :> b0status;
	b1 :> b1status;
	led_chan <: servo_state;

	while (1) {
		select {
			case b0 when pinsneq(b0status) :> b0status:
				t :> time;
				t  when timerafter(time + 500000) :> void; // sleep 5ms because the if fails on button release, if not
				if (b0status == 0 ) {
					next_servo_state(); // static function to change static var servo_state
					led_chan <: servo_state;
				} 
				break;
		}
	}
}
It seems to me that b0status is not updated quickly enough. Could that be a result of pipelining and code optimization? I have no other idea.

Thanks for your help!

Greetings,
Chris


richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

When you press / release the button the value on the pins may bounce between one and zero before settling down the the correct value. I think this explains the behaviour you are seeing. I suggest you try searching the web for "debouncing". The following post also gives some references that may be useful https://www.xcore.com/forum/viewtopic.php?p=9584#p9584
schmiedl
Junior Member
Posts: 7
Joined: Wed Jul 13, 2011 3:38 pm

Post by schmiedl »

thanks a lot!