Help needed for newbie to USB Audio 2.0 Reference Kit

Sub forums for various specialist XMOS applications. e.g. USB audio, motor control and robotics.
User avatar
mikechw
New User
Posts: 3
Joined: Fri Nov 25, 2011 3:03 am
Location: HK

Help needed for newbie to USB Audio 2.0 Reference Kit

Postby mikechw » Fri Nov 25, 2011 5:34 am

Hi all,

I have received the USB audio 2.0 reference kit earlier and have started to do some experiment on it.
I have read through the XC language tutorials, etc. Now looking at the reference USB audio 2.0 software.

The first thing I would like to try is to program the 2 hardware buttons to do something like turning on/off leds that were on the reference board.

I have search through the USB audio 2.0 reference software and couldn't see anything that access the 2 buttons.

Can anyone give some sample codes or point me to a direction on how to achieve this ... Thanks...

Michael :D
User avatar
Ross
XCore Expert
Posts: 768
Joined: Thu Dec 10, 2009 9:20 pm

Postby Ross » Fri Nov 25, 2011 2:43 pm

Have a look at the "port map" in the hardware manual (https://www.xmos.com/download/public/US ... 1.0%29.pdf, page 13). This will show you what port the buttons (and LEDs) are connected to.

Perhaps have a look at some of the simpler tutorials on xmos.com? Tutorials for the XC-1A or XK-1 board should be useful for lighting LEDs etc.
User avatar
mikechw
New User
Posts: 3
Joined: Fri Nov 25, 2011 3:03 am
Location: HK

Postby mikechw » Mon Dec 05, 2011 11:07 am

Thanks Ross,

I managed to look up the documents and wrote some code to print a character on the console.

Here is the sample:

Code: Select all

on stdcore[0] : in buffered port:1 switchA = XS1_PORT_1J;
void buttons() {
	int sA;
	switchA :> sA;
	while( 1 ){
		select {
			case switchA when pinsneq( sA ) :> sA:
			{
				if( sA == 0 )
					printstr( "A " );
				else
					printstr( "B " );
				break;
			}
		}
	}
}
When I press the switch on the board, the console should print out "A B A B A B" ......
But what I found is it sometimes printed out two As in a row.
I think I might need to debounce the key pressed.
Could you show me how to properly do debouncing with the ports in XC ?
Thanks very much.

Michael
User avatar
phalt
Respected Member
Posts: 298
Joined: Thu May 12, 2011 11:14 am

Postby phalt » Mon Dec 05, 2011 11:20 am

Please use the code tags :P

It's makes it easy to distinguish code from text!
User avatar
mikechw
New User
Posts: 3
Joined: Fri Nov 25, 2011 3:03 am
Location: HK

Postby mikechw » Tue Dec 06, 2011 3:33 am

Thanks for adding the code tag for me.

Here is an updated version of my code:

Code: Select all

on stdcore[0] : in buffered port:1 switchA = XS1_PORT_1J;
void buttons()
{
	int sA;
	int pressed;
	switchA :> sA;
	pressed = -1;
	while( 1 ) {
		select {
			case switchA when pinsneq( sA ) :> sA:
				if( sA == 0 && pressed != 1 )
				{
					printstr( "A " );
					pressed = 1;
				}
				else if( pressed != 0 )
				{
					printstr( "B " );
					pressed = 0;
				}
				break;
		}
	}
}
This seems to fix the problem of getting 2 As in a row if repeatly press the switch.
Can someone suggest a better ways to do this ?
User avatar
Ross
XCore Expert
Posts: 768
Joined: Thu Dec 10, 2009 9:20 pm

Postby Ross » Wed Dec 07, 2011 2:02 pm

What about using a timer to ignore glitches? Something like the following pseudo code:

Code: Select all

if(buttonDown)
{
   // Get time from timer
   t :> time;

   if(!buttonDown)
   {
      // Button state changed, ignore as glitch
   }
   else if(currentTime is greater than time + antiglitch)
   {
      // Register button press
   }
}
p.s. perhaps someone can split off this button stuff to a new thread "button de-bounce" or similar?

Who is online

Users browsing this forum: No registered users and 0 guests