并行执行
并行执行时使用par {} 进行并行处理
点灯
#include <stdio.h>
#include<xs1.h>
#include<timer.h>
#include<platform.h>
port p = XS1_PORT_8C;
void hw(unsigned n) {
printf("Hello world from task number %u\n", n);
}
int main() {
#if 0
par {
hw(0);
hw(1);
hw(3);
}
#endif
while (1) {
p <: 0x44;
delay_milliseconds(200);
p <: 0x00;
delay_milliseconds(200);
p <: 0x4;
delay_milliseconds(200);
p <: 0x00;
delay_milliseconds(200);
p <: 0x40;
delay_milliseconds(200);
p <: 0x00;
delay_milliseconds(200);
}
return 0;
}
指定port 的接口
<: 用于接口的赋值,这里使用的是8bit的口,这里控制第3bit 和第7 bit的 gpio 交替闪亮
xC 语言特点
xC 语言跟c语言类似,不支持 goto、位域、函数之指针及 C99 风格的初始化。
在c 文件声明的函数如果要在xc中使用,需要使用extern "c" 进行声明
##foo.c 中实例的函数
int f() {
printf("This is a C function\n");
}
## bar.xc中的引用
extern "c" {
extern int f(); // This function is defined in C
}
void g() {
par {
f();
f();
}
}