Page 2 of 2

Re: Hello from a complete newbie

Posted: Sat Dec 28, 2013 11:49 pm
by Folknology
It might be easier to start with a simple blinky:

Code: Select all

#include <xs1.h>

#define DELAY 10000000

out port ld1 = XS1_PORT_1A; // Declare use of 1 bit port A as output

int main(void) {
	timer t; // Declare a timer resource
	int time; // Declare something to store timer output

	t :> time; // get current timer value
	
	while(1) { // loop forever
		ld1 <: 0; // LED off
		t when timerafter(time+DELAY) :> time; // Wait 10*DELAY nSecs
		ld1 <: 1; // LED on
		t when timerafter(time+DELAY) :> time; // Wait 10*DELAY nSecs
	}
	return 0;
}
This might be simpler to understand because it uses a 1 bit port to begin with (removing complexity of wider ports and partial pin usage).

(StartKit has 2 LEDs connecetd to 1 bit ports)

P.S. Although this example works it might not be a very Xmos way of doing it, a while select loop with timerafter case would be better but introduces even more unfamiliar features.

regards
Al

Re: Hello from a complete newbie

Posted: Mon Dec 30, 2013 9:52 am
by AJB2K3
Just to point out!
out port ld1 = XS1_PORT_1A; // Declare use of 1 bit port A as output
The startkit runs happy without the "out" at the beginning of this line!

Re: Hello from a complete newbie

Posted: Mon Dec 30, 2013 10:18 am
by Folknology
'out' and 'in' prefixes set port direction, without these the port can be considered bi-directional and subsequent uses of the the port via '<:' will place it into output mode.

regards
Al

Re: Hello from a complete newbie

Posted: Fri Jan 03, 2014 2:59 pm
by richard
The 'in' and 'out' qualifiers are similar to the 'const' qualifier in C - they restrict what operations you can perform but apart from this they have no effect. Removing all the 'in' and 'out' qualifiers from a program doesn't change the behavior of that program.

The benefits of using 'in' and 'out' is that they document the intent of the code and they prevent silly mistakes (accidently outputting to a port that should only be used for input or vice versa).