资源描述
单击此处编辑母版标题样式,实践4 TinyOS应用开发,*,实践4 TinyOS应用开发,实践指导,知识拓展,实践指导,实践4.G.1,实现CC2530光敏信息的采集传输及向PC机传送数据,需要完成以下工作:(1)通过AD进行光敏信息的采集。(2)通过射频将数据发送至接收者。(3)接收者接收到数据之后,将接收的数据通过串口发送至PC机。,【分析】(1)本实验使用两个CC2530节点,命名为节点1和节点2。(2)在“mytinyos/apps目录下创立工程文件。其中,程序的编写分为两局部:数据的发送局部和接收局部;节点1负责信息的采集和发送,节点2负责信息的接收以及将数据通过串口传输至PC机。(3)编写程序及Makefile文件。(4)观察实验现象。,【参考解决方案】1创立工程目录由于本实验的节点1和节点2使用的程序不同,因此本实验的发送和接收分别在同一个工程文件的两个子目录下。(1)创立传感器采集传输工程目录Sensor。翻开cygwin,在“opt/mytinyos/apps目录下输入“mkdir Sensor,创立目录,具体操作如图S4-1所示。,图S4-1 创立Sensor目录,(2)创立发送和接收文件子目录。以步骤(1)创立文件为例在Sensor文件夹下分别创立发送和接收工程文件夹:ADRadioSend和ADRadioReceive,如图S4-2所示。,图S4-2 创立发送和接收子目录,创立完成之后目录如图S4-3所示。,图S4-3 创立完成后的目录,2发送程序的编写发送程序的编写需要完成以下几项工作:(1)创立并编写ADSensorSendApp.nc文件。使用EditPlus程序在ADRadioSend目录下新建ADSensorSendApp.nc文件,并输入如下代码:,#include#include BlinkToRadio.hconfiguration ADSensorSendAppCimplementationcomponents ADSensorSendC;components new AdcC()as ADSensor;,components ActiveMessageC;components LedsC;,/ADC信息采集配置ADSensorSendC.ADSensorControl-ADSensor;/ADC读取ADSensorSendC.ADSensorRead-ADSensor;components MainC;/启动接口ADSensorSendC.Boot-MainC.Boot;components new TimerMilliC()as SensorTimerC;/定时器ADSensorSendC.SensorTimer-SensorTimerC;components PlatformSerialC;/串口配置ADSensorSendC.StdControl-PlatformSerialC.StdControl;/串口传输,ADSensorSendC.UartStream-PlatformSerialC.UartStream;/LED组件ADSensorSendC.Leds-LedsC.Leds;/*射频发送组件*/ADSensorSendC.Packet-ActiveMessageC;ADSensorSendC.AMPacket-ActiveMessageC;ADSensorSendC.AMSend-ActiveMessageC.AMSenduniqueCount(ADRadioSend);ADSensorSendC.AMControl-ActiveMessageC;ADSensorSendC.PacketAcknowledgements-ActiveMessageC;,(2)创立并编写ADSensorSendC.nc文件。使用EditPlus程序在ADRadioSend目录下新建ADSensorSendC.nc文件,并输入如下代码:#include“Adc.h#include“BlinkToRadio.h#include module ADSensorSendCuses,interface Boot;interface AdcControl as ADSensorControl;interface Read as ADSensorRead;interface Timer as SensorTimer;interface Leds;interface Packet;interface AMPacket;interface AMSend;interface SplitControl as AMControl;interface PacketAcknowledgements;,interface StdControl;interface UartStream;implementationuint8_t m_len;uint8_t m_send_buf2;uint16_t counter;message_t pkt;,/*传感器采集任务*/task void sensorTask()/选择P0.7为AD采集通道,call ADSensorControl.enable(ADC_REF_AVDD,ADC_14_BIT,ADC_AIN7);/开始采集AD信息call ADSensorRead.read();/*系统启动*/event void Boot.booted()/开启定时器任务,call SensorTimer.startPeriodic(1000);/开启ad采集call StdControl.start();/开启无线传输call AMControl.start();/*当无线开启之后会触发AMControl.startDone事件*/event void AMControl.startDone(error_t err)if(err=SUCCESS),/如果开启成功,将点亮LED1call Leds.led0On();/call Timer0.startPeriodic(TIMER_PERIOD_MILLI);else/否那么重新启动无线call AMControl.start();,/定时事件event void SensorTimer.fired()/开启传感器采集任务post sensorTask();/*ADC读取完毕之后将会触发readDone事件*/event void ADSensorRead.readDone(error_t result,int16_t val)BlinkToRadioMsg*btrpkt;,/获得要发送的信息包btrpkt=(BlinkToRadioMsg*)call Packet.getPayload(,async event void UartStream.sendDone(uint8_t*buf,uint16_t len,error_t error)async event void UartStream.receivedByte(uint8_t byte);/*在接收完receive命令欲接收的长度后会调用此事件*/async event void UartStream.receiveDone(uint8_t*buf,uint16_t len,error_t error),/*如果发送成功,接收者将发送确认帧给发送者*/event void AMSend.sendDone(message_t*msg,error_t error)if(event void AMControl.stopDone(error_t err),(3)创立BlinkToRadio.h文件。使用EditPlus程序在ADRadioSend目录下新建BlinkToRadio.h文件,并输入如下代码:#ifndef _BLINKTORADIO_H#define _BLINKTORADIO_Henum/定时事件TIMER_PERIOD_MILLI=2000;/*发送数据结构体*/typedef nx_struct BlinkToRadioMsg,nx_uint16_t nodeid;nx_uint16_t counter;BlinkToRadioMsg;#endif,(4)编写Makefile文件。使用EditPlus程序在ADRadioSend目录下创立Makefile文件,并输入以下内容:COMPONENT=ADSensorSendAppCinclude$(MAKERULES)(5)编译程序。在Cygwin下,保证工作目录为ADRadioSend,输入“$make cc2530命令进行编译,编译完成后如图S4-4所示。,图S4-4 编译ADRadioSend程序,3接收程序编写接收程序的编写需要完成以下几项工作:(1)创立并编写ADSensorReceiveApp.nc文件。使用EditPlus程序在ADRadioReceive目录下新建ADSensorReceiveApp.nc文件,并输入如下代码:#include#include“BlinkToRadio.hconfiguration ADSensorReceiveAppC,implementationcomponents ADSensorReceiveC;components ActiveMessageC;components new AMReceiverC(AM_BLINKTORADIO);components LedsC;components MainC;ADSensorReceiveC.Boot-MainC.Boot;components new TimerMilliC()as SensorTimerC;ADSensorReceiveC.SensorTimer-SensorTimerC;components PlatformSerialC;ADSensorReceiveC.StdControl-PlatformSerialC.StdControl;ADSensorReceiveC.UartStream-PlatformSerialC.UartStream;ADSensorReceiveC.Leds-LedsC.Leds;ADSensorReceiveC.AMControl-ActiveMessageC;ADSensorReceiveC.Receive-ActiveMessageC.ReceiveuniqueCount(ADRadioReceive);,(2)创立并编写ADSensorReceiveC.nc文件。使用EditPlus程序在ADRadioReceive目录下新建ADSensorReceiveC.nc文件,并输入如下代码:#include“BlinkToRadio.h#include module ADSensorReceiveCuses interface Boot;,interface Timer as SensorTimer;interface Leds;interface Packet;interface AMPacket;interface SplitControl as AMControl;interface Receive;interface StdControl;interface UartStream;implementation,uint8_t m_len;uint8_t m_send_buf2;uint16_t counter;message_t pkt;event void Boot.booted()/开启定时器任务call SensorTimer.startPeriodic(1000);/开启无线传输call AMControl.start();,/*当无线开启之后会触发AMControl.startDone事件*/event void AMControl.sta
展开阅读全文