第6篇 对blink的进一步修改 (教程lesson 1)

有没有想过对blink进一步修改?

 

blink改成一个计数器:

代码如下:

Myblink.nc

configuration MyBlink {

}

implementation {

  components Main, MyBlinkM, TimerC, LedsC;

  Main.StdControl -> TimerC.StdControl;

  Main.StdControl -> MyBlinkM.StdControl;

  MyBlinkM.Timer -> TimerC.Timer[unique("Timer")];

  MyBlinkM.Leds -> LedsC.Leds;

}

 

MyblinkM.nc

module MyBlinkM {

  provides {

    interface StdControl;

  }

  uses {

    interface Timer;

    interface Leds;

  }

}

implementation {

                   int i;              //注意到nesc中,定义一个整形变量的方式和c一致

  command result_t StdControl.init() {

    call Leds.init();

    i = 0 ;

    return SUCCESS;

  }

 

  command result_t StdControl.start() {

    // Start a repeating timer that fires every 1000ms

    return call Timer.start(TIMER_REPEAT, 10000);

  }

 

  command result_t StdControl.stop() {

    return call Timer.stop();

  }

 

task void processing()                                 //新知识!task esson3中学习

{              //mod运算,mod1,红灯亮,mod2,绿灯亮,mod4,黄灯亮。注意学习的使用

    if (i & 1) call Leds.redOn();

    else call Leds.redOff();

    if (i & 2) call Leds.greenOn();

    else call Leds.greenOff();

    if (i & 4) call Leds.yellowOn();

    else call Leds.yellowOff();

}

 

  event result_t Timer.fired()

  {

       i++;                              //计数器值上升

    post processing();                  //调用一个task的方式是post

    return SUCCESS;

  }

}

 

不愿意使用task的同学看这里:

module MyBlinkM {

  provides {

    interface StdControl;

  }

  uses {

    interface Timer;

    interface Leds;

  }

}

implementation {

                   int i;

  command result_t StdControl.init() {

    call Leds.init();

    i = 0 ;

    return SUCCESS;

  }

 

  command result_t StdControl.start() {

    // Start a repeating timer that fires every 1000ms

    return call Timer.start(TIMER_REPEAT, 10000);

  }

 

  command result_t StdControl.stop() {

    return call Timer.stop();

  }

 

  event result_t Timer.fired()

  {

       i++;

    if (i & 1) call Leds.redOn();

    else call Leds.redOff();

    if (i & 2) call Leds.greenOn();

    else call Leds.greenOff();

    if (i & 4) call Leds.yellowOn();

    else call Leds.yellowOff();

    return SUCCESS;

  }

}

 

这两个程序基本相同,但后者没使用task,在event当中直接判断。程序好坏,大家自己去编译一下跑跑看吧。

 

讲到这里,blink已经给挖掘的差不多了,大家刻意编译blink程序 并且尝试改名之后再次编译, blink中的red改为green,看看绿灯是不是每秒亮一次。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
包含代码和文档 实验实验内容 编写程序,演示多进程并发执行和进程软中断、管道通信。 父进程使用系统调用pipe( )建立一个管道,然后使用系统调用fork()创建两个子进程,子进程1和子进程2; 子进程1每隔1秒通过管道向子进程2发送数据: I send you x times. (x初值为1,每次发送后做加一操作) 子进程2从管道读出信息,并显示在屏幕上。 父进程用系统调用signal()捕捉来自键盘的中断信号(即按Ctrl+C键);当捕捉到中断信号后,父进程用系统调用Kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止: Child Process l is Killed by Parent! Child Process 2 is Killed by Parent! 父进程等待两个子进程终止后,释放管道并输出如下的信息后终止 Parent Process is Killed! 实验实验内容 过Linux多线程与信号灯机制,设计并实现计算机线程与I/O线程共享缓冲区的同步与通信。 程序要求:两个线程,共享公共变量a 线程1负责计算(1到100的累加,每次加一个数) 线程2负责打印(输出累加的中间结果) 实验实验内容 利用多个共享内存(有限空间)构成的环形缓冲,将源文件复制到目标文件,实现两个进程的誊抄。 实验实验内容 1、(1)Blink程序的编译和下载 (2)给Blink程序加入printf,在每次定时器事件触发点亮LED的同时通过串口显示信息 (3)修改BLink程序,只使用一个Timer,三个LED灯作为3位的二进制数表示(亮灯为1,不亮为0),按照0-7的顺序循环显示,同时将数值显示在终端上。
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值