实验四 CC2530平台上UART组件的TinyOS编程

实验 CC2530平台上UART组件的TinyOS编程

实验目的:

  1. 加深和巩固学生对于TinyOS编程方法的理解和掌握
  2. 让学生初步掌握CC2530UART、及其TinyOS编程方法
  3. 学生通过本实验能够初步的了解和掌握TinyOS编程的整个过程
  4. 高学生的上机和编程过程中处理具体问题的能力

实验要求:

  1. 实验要求自己独立的完成
  2. 编写和调试过程中出现的问题记录,并事后总结到报告中
  3. 实验程序调试完成后, 用给定的平台进行测试,由老师检查测试结果,并给予相应的成绩
  4. 实验完成后,要上交实验报告

实验内容:

  1. 开发一个新的应用,通过串口通信实现从PC机通过串口发送指令,控制LED灯的亮灭。以下述顺序完成这个新应用的开发。
    1. 首先实现CC2530通过串口组件与PC机实现通信的功能
    2. 然后在上述基础上加入LED控制功能。
  2. 在实验报告中分别给出上述两个阶段的源码

实验代码:

(1)配置文件TestSerialC.nc

configuration TestSerialC

{

}

implementation

{

components TestSerialM as App;

components MainC;

App.Boot -> MainC.Boot;

//LED组件

components LedsC;

App.Leds ->LedsC.Leds;

//串口组件

components HplCC2530UartC;

 HplCC2530UartC.CC2530UartControl[0]<- App.UartControl;

App.UartStream -> HplCC2530UartC.UartStream[0];

}

(2)模块文件TestSerialM.nc

module TestSerialM

{

uses {

 interface Boot;

 interface Leds;

 interface UartStream;

 interface CC2530UartControl as UartControl;

 }

}

implementation

{

uint8_t m_strRecv;

uint8_t m_sendBuf[120];

void ShowMenu()

{

strcpy(m_sendBuf,"串口测试程序,请选择输入: " );

strcat(m_sendBuf,"[1] Toggle The No.1 Led. ");

strcat(m_sendBuf,"[2] Toggle The No.2 Led. ");

strcat(m_sendBuf,"[3] Toggle The No.3 Led. ");

call UartStream.send(m_sendBuf,strlen(m_sendBuf));

}

void SendMsg(char *str)

{

call UartStream.send(str,strlen(str));

}

task void TaskLightLed()

{

switch(m_strRecv)

{

case '1':

call Leds.led0Toggle();

SendMsg("You Toggle No.1 Led!! ");

break;

case '2':

call Leds.led1Toggle();

SendMsg("You Toggle No.2 Led!! ");

break;

case '3':

call Leds.led2Toggle();

SendMsg("You Toggle No.3 Led!! ");

break;

default:

call Leds.led0Toggle();

SendMsg(" Error Key,Toggle No.4 Led ");

ShowMenu();

break;

}

}

event void Boot.booted()

{

call UartControl.InitUart(UART_BAUDRATE); //初始化串口

call UartControl.setRxInterrupt(0x01); //使能发送中断

call UartControl.setTxInterrupt(0x01); //使能接收中断

ShowMenu();

}

async event void UartStream.receivedByte( uint8_t byte )

{

m_strRecv=byte;

post TaskLightLed();

}

async event void UartStream.receiveDone ( uint8_t* buf, uint16_t len, error_t error )

{ }

async event void UartStream.sendDone ( uint8_t* buf, uint16_t len, error_t error )

{ }

}

3makefile 编译文件

COMPONENT=TestSerialC

include $(MAKERULES)

 

实验环境:

  1. 上位机操作系统为WindowsXP,在Cygwin环境下编译

2. 集成开发环境为Notepad++ 

 

实验报告人:木舟 报告时间2018.11.16

 

原文地址:https://www.cnblogs.com/LeonNchu/p/10695779.html