Blink学习
首先,因为所有程序都需要模块以及配件,配件是连接自己主程序与源的连线。所以需要建两个文件模块BlinkC.nc以及配件BlinkAppC.nc。然后,需要Makefile文件。
1. BlinkC.nc
1) 功能分析:通过定时开关Leds,实现几个灯轮流闪烁。所以,我们需要定时Timer<TMilli>,灯Leds,启动程序Boot;所有接口就在这儿了;
module MyBlinkC{
uses interface Timer<TMilli> as Timer0;
uses interface Timer<TMilli> as Timer1;
uses interface Timer<TMilli> as Timer2;
uses interface Boot;
uses interface Leds;
}
对了,关于时间需要引用Timer.h头文件,所以,开始时需要有#include “Timer.h”
2) 具体实现:
a.首先boot事件需要实现,boot时需要开启定时器Timer
event void Boot.booted()
{
call Timer0.startPeriodic(250);
call Timer1.startPeriodic(500);
call Timer2.startPeriodic(1000);
}
b.接着就要实现灯闪烁效果,这时需要Leds.ledxToggle()闪烁
event void Timer0.fired()
{
call Leds.led0Toggle();
}
event void Timer1.fired()
{
call Leds.led1Toggle();
}
event void Timer2.fired()
{
call Leds.led2Toggle();
}
2.BlinkAppC.nc
configuration BlinkAppC.nc{ }
主要是连接
implementation{
components MainC,MyBlinkC,LedsC;//声明组件MainC,MyBlinkC,LedsC
components new TimerMilliC() as Timer0;
components new TimerMilliC() as Timer1;
components new TimerMilliC() as Timer2;//声明对应模块BlinkC中的三个定时器
MyBlinkC->MainC.Boot;
MyBlinkC.Timer0->Timer0;
MyBlinkC.Timer1->Timer1;
MyBlinkC.Timer2->Timer2;
MyBlinkC.Leds->LedsC; //连接所有组件
}
3. Makefile文件
COMPONENT=BlinkAppC
include $(MAKERULES)
4.实验
连接实验器材,我用的是telosb节点
sudo chmod 666 /dev/ttyUSB0
make telosb
make telosb reinstall