求高手讲解zigbee中的initNV( void )理解
发布网友
发布时间:2022-04-25 20:39
我来回答
共1个回答
热心网友
时间:2022-06-17 05:47
CC2530用的是TI公司的zstack协议栈。学习zigbee关键在于多看官方给出的例程,基本的功能和用
法都有。对于zigbee组网,初学者不需要具体了解协议栈的内部结构,而且zstack是半开源的,你
也没机会了解核心代码,我们要做的就是了解应用程序的接口函数,然后应用它。如果你要实现温
度值的传输就要了解SimpleSensor.c和SimpleCollector.c,在ZStack-1.4.3-1.2.1的Projects
\zstack\Samples\SimpleApp下。
3.Zigbee超低功耗遥控器的设计
做zigbee设计首先要有zigbee协议栈,我用的是ZStack-1.4.3-1.2.1,芯片为CC2430。zigbee编程
主要在协议栈中修改。按你的要求应该用SimpleApp范例,在ZStack-1.4.3-1.2.1的Projects
\zstack\Samples\SimpleApp下。主要看SimpleSwitch.c和SimpleController.c两个文件。例如要
遥控LED灯的亮灭,由终端向协调器发送控制命令:
//SimpleSwitch.c
void zb_HandleKeys( uint8 shift, uint8 keys )
{
uint8 startOptions;
uint8 logicalType;
// Shift is used to make each button/switch al purpose.
if ( shift )
{
if ( keys & HAL_KEY_SW_1 )
{
}
if ( keys & HAL_KEY_SW_2 )
{
}
if ( keys & HAL_KEY_SW_3 )
{
}
if ( keys & HAL_KEY_SW_4 )
{
}
}
else
{
if ( keys & HAL_KEY_SW_1 )
{
if ( myAppState == APP_INIT )
{
// In the init state, keys are used to indicate the logical mode.
// The Switch device is always an end-device
logicalType = ZG_DEVICETYPE_ENDDEVICE;
zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);
// Do more configuration if necessary and then restart device with auto-start bit set
zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
startOptions = ZCD_STARTOPT_AUTO_START;
zb_WriteConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
zb_SystemReset();
}
else
{
// Initiate a binding with null destination
zb_BindDevice(TRUE, TOGGLE_LIGHT_CMD_ID, NULL);
}
}
if ( keys & HAL_KEY_SW_2 )
{
if ( myAppState == APP_INIT )
{
// In the init state, keys are used to indicate the logical mode.
// The Switch device is always an end-device
logicalType = ZG_DEVICETYPE_ENDDEVICE;
zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);
zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
startOptions = ZCD_STARTOPT_AUTO_START;
zb_WriteConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
zb_SystemReset();
}
else
{
// Send the command to toggle light
zb_SendDataRequest( 0xFFFE, TOGGLE_LIGHT_CMD_ID, 0,
(uint8 *)NULL, myAppSeqNumber, 0, 0 );
}
}
if ( keys & HAL_KEY_SW_3 )
{
// Remove all existing bindings
zb_BindDevice(FALSE, TOGGLE_LIGHT_CMD_ID, NULL);
}
if ( keys & HAL_KEY_SW_4 )
{
}
}
}
这个函数定义了开关的用法,按键1用来设置协调器,按键2用来设置终端或发送控制命令TOGGLE_LIGHT_CMD_ID,按键3用来绑定设备。
// SimpleController.c
void zb_ReceiveDataIndication( uint16 source, uint16 command, uint16 len, uint8 *pData )
{
if (command == TOGGLE_LIGHT_CMD_ID)
{
// Received application command to toggle the LED
HalLedSet(HAL_LED_1, HAL_LED_MODE_TOGGLE);
}
}
若接收到数据,判断命令command是否为TOGGLE_LIGHT_CMD_ID,若是则点亮LED1。其实TOGGLE_LIGHT_CMD_ID值就定义为1,为了让读者明白含义所以用一串字符表示,初学者不要认为zigbee协议栈有多复杂,耐心的看下去问题都会迎刃而解。