Hello from a complete newbie

First time on the site? Say hello here!
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post 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


AJB2K3
Member++
Posts: 27
Joined: Sat Dec 14, 2013 1:03 pm

Post 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!
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post 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
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post 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).
Post Reply