基于ZigBee的光强度采集模块李凡

上传人:卷*** 文档编号:143357717 上传时间:2022-08-26 格式:DOC 页数:43 大小:51.25MB
收藏 版权申诉 举报 下载
基于ZigBee的光强度采集模块李凡_第1页
第1页 / 共43页
基于ZigBee的光强度采集模块李凡_第2页
第2页 / 共43页
基于ZigBee的光强度采集模块李凡_第3页
第3页 / 共43页
资源描述:

《基于ZigBee的光强度采集模块李凡》由会员分享,可在线阅读,更多相关《基于ZigBee的光强度采集模块李凡(43页珍藏版)》请在装配图网上搜索。

1、基于ZigBee旳光强度采集系统李凡22/* Filename: SampleApp.c Revised: $Date: -03-18 15:56:27 -0700 (Wed, 18 Mar ) $ Revision: $Revision: 19453 $ Description: Sample Application (no Profile). Copyright Texas Instruments Incorporated. All rights reserved. IMPORTANT: Your use of this Software is limited to those speci

2、fic rights granted under the terms of a software license agreement between the user who downloaded the software, his/her employer (which must be your employer) and Texas Instruments Incorporated (the License). You may not use this Software unless you agree to abide by the terms of the License. The L

3、icense limits your use, and you acknowledge, that the Software may not be modified, copied or distributed unless embedded on a Texas Instruments microcontroller or used solely and exclusively in conjunction with a Texas Instruments radio frequency transceiver, which is integrated into your product.

4、Other than for the foregoing purpose, you may not use, reproduce, copy, prepare derivative works of, modify, distribute, perform, display or sell this Software and/or its documentation for any purpose. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE PROVIDED 揂S IS?WITHOUT W

5、ARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTR

6、IBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY

7、CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. Should you have any questions regarding your right to use this Software, contact Texas Instruments Incorporated at www.TI.com.*/* This application isnt intended to do anything useful, it is intended t

8、o be a simple example of an applications structure. This application sends its messages either as broadcast or broadcast filtered group messages. The other (more normal) message addressing is unicast. Most of the other sample applications are written to support the unicast message model. Key control

9、: SW1: Sends a flash command to all devices in Group 1. SW2: Adds/Removes (toggles) this device in and out of Group 1. This will enable and disable the reception of the flash command.*/* * INCLUDES */#include OSAL.h#include ZGlobals.h#include AF.h#include aps_groups.h#include ZDApp.h#include SampleA

10、pp.h#include SampleAppHw.h#include OnBoard.h#include MT_UART.h /此处用于串口/* HAL */#include hal_lcd.h#include hal_led.h#include hal_key.h#include ioCC2530.h #include string.h/* * 新程序 */char Light4;char BUF1;char ge,shi,bai,qian,wan; /显示变量unsigned char buffer;#define uint unsigned int#define uchar unsign

11、ed chartypedef unsigned char BYTE;typedef unsigned short WORD;#define SlaveAddress 0x46 #define IIC_WRITE 0 / WRITE direction bit#define IIC_READ 1 / READ direction bit#define TRUE 1#define FALSE 0/*我旳管脚定义是 SDA定义为P1.5 SCL定义为P1.4 */#define SCL P1_4/ #define SDA P1_5#define SCL P1_2#define SDA P1_3#de

12、fine LED P1_0 /LED定义为P0_1口输出/* * GLOBAL VARIABLES 新程序 */void lightinit(void);void conversion(uint temp_data);void Delay_1u(unsigned int times);void Delay_1ms(unsigned int times);void Delay_1s(unsigned int times);void Delay5us(void);void WriteSDA1(void);void WriteSDA0(void);void WriteSCL1(void);void

13、WriteSCL0(void);void ReadSDA(void);void BH1750_Start(void);void BH1750_Stop(void);void BH1750_SendACK(uchar ack);uchar BH1750_RecvACK(void); void SEND_0_1(void) ; void SEND_1_1(void) ;void BH1750_SendByte(uchar dat);uchar BH1750_RecvByte(void);void Single_Write_BH1750(uchar REG_Address);unsigned cha

14、r Single_Read_BH1750(void) ;/*LED口初始化*/void IO_Init(void) P1DIR |= 0x01; /P0_1口定义为输出/*闪烁进程*/void LED_Blink(void) Delay_1ms(10); LED = 0; /点亮LED Delay_1s(1); LED=1; /熄灭LED,完毕闪烁/*逻辑*/void logic(uchar num) if(num 0x7F) LED_Blink(); / This list should be filled with Application specific Cluster IDs.cons

15、t cId_t SampleApp_ClusterListSAMPLEAPP_MAX_CLUSTERS = SAMPLEAPP_PERIODIC_CLUSTERID, SAMPLEAPP_FLASH_CLUSTERID;const SimpleDescriptionFormat_t SampleApp_SimpleDesc = SAMPLEAPP_ENDPOINT, / int Endpoint; SAMPLEAPP_PROFID, / uint16 AppProfId2; SAMPLEAPP_DEVICEID, / uint16 AppDeviceId2; SAMPLEAPP_DEVICE_

16、VERSION, / int AppDevVer:4; SAMPLEAPP_FLAGS, / int AppFlags:4; SAMPLEAPP_MAX_CLUSTERS, / uint8 AppNumInClusters; (cId_t *)SampleApp_ClusterList, / uint8 *pAppInClusterList; SAMPLEAPP_MAX_CLUSTERS, / uint8 AppNumInClusters; (cId_t *)SampleApp_ClusterList / uint8 *pAppInClusterList;/ This is the Endpo

17、int/Interface description. It is defined here, but/ filled-in in SampleApp_Init(). Another way to go would be to fill/ in the structure here and make it a const (in code space). The/ way its defined in this sample app it is define in RAM.endPointDesc_t SampleApp_epDesc;/* * EXTERNAL VARIABLES */* *

18、EXTERNAL FUNCTIONS */* * LOCAL VARIABLES */uint8 SampleApp_TaskID; / Task ID for internal task/event processing / This variable will be received when / SampleApp_Init() is called.devStates_t SampleApp_NwkState;uint8 SampleApp_TransID; / This is the unique message ID (counter)afAddrType_t SampleApp_P

19、eriodic_DstAddr;afAddrType_t SampleApp_Flash_DstAddr;afAddrType_t Point_To_Point_DstAddr;/网蜂点对点通信定义aps_Group_t SampleApp_Group;uint8 SampleAppPeriodicCounter = 0;uint8 SampleAppFlashCounter = 0;/* * LOCAL FUNCTIONS */void SampleApp_HandleKeys( uint8 shift, uint8 keys );void SampleApp_MessageMSGCB( a

20、fIncomingMSGPacket_t *pckt );void SampleApp_SendPeriodicMessage( void );void SampleApp_SendFlashMessage( uint16 flashTime );void SampleApp_SendPointToPointMessage(uint8); /网蜂点对点通讯发送函数定义./* * NETWORK LAYER CALLBACKS */* * PUBLIC FUNCTIONS */* * fn SampleApp_Init * * brief Initialization function for

21、the Generic App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton . ). * * param task_id - the ID assigned by OSAL. This ID should be * used to send messages an

22、d set timers. * * return none */void SampleApp_Init( uint8 task_id ) /*LED初始化*/ IO_Init(); SampleApp_TaskID = task_id; SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; /*串口初始化*/ MT_UartInit();/初始化 MT_UartRegisterTaskID(task_id); /登记任务号 HalUARTWrite(0,Hello Worldn,12);/串口 0,字符,字符个数 /P0SEL&=0xDF;

23、 /设置P0.5口为一般IO口 /端口0功能选择 P0SEL&=0xDe; /端口0输入模式 P0INP&=0xDe; /端口0方向寄存器 P0DIR|=0x21; /设置端口为输入 /*PERCFG = 0x00; /外设控制寄存器 USART 0旳IO位置:0为P0口位置1 P0SEL = 0x0c; /P0_2,P0_3用作串口(外设功能) P2DIR &= 0XC0; /P0优先作为UART0 U0CSR |= 0x80; /设置为UART方式 U0GCR |= 11; /U0BAUD |= 216; /波特率设为115200 UTX0IF = 0; /UART0 TX中断标志初始置位

24、0 */ lightinit(); / Device hardware initialization can be added here or in main() (Zmain.c). / If the hardware is application specific - add it here. / If the hardware is other parts of the device add it in main(). #if defined ( BUILD_ALL_DEVICES ) / The Demo target is setup to have BUILD_ALL_DEVICE

25、S and HOLD_AUTO_START / We are looking at a jumper (defined in SampleAppHw.c) to be jumpered / together - if they are - we will start up a coordinator. Otherwise, / the device will start as a router. if ( readCoordinatorJumper() ) zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR; else zgDeviceLogical

26、Type = ZG_DEVICETYPE_ROUTER;#endif / BUILD_ALL_DEVICES#if defined ( HOLD_AUTO_START ) / HOLD_AUTO_START is a compile option that will surpress ZDApp / from starting the device and wait for the application to / start the device. ZDOInitDevice(0);#endif / Setup for the periodic messages destination ad

27、dress / Broadcast to everyone SampleApp_Periodic_DstAddr.addrMode =(afAddrMode_t)AddrBroadcast; SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; /SampleApp_Periodic_DstAddr.panID=0xFF; / Setup for the flash commands destination address - G

28、roup 1 SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; / 网蜂点对点通讯定义 Point_To_Point_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;/点播 Point_To_Point_DstAddr.endPoint = SAMP

29、LEAPP_ENDPOINT; Point_To_Point_DstAddr.addr.shortAddr = 0x0000; /发给协调器 / Fill out the endpoint description. SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_epDesc.task_id = &SampleApp_TaskID; SampleApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc; SampleApp_epDesc

30、.latencyReq = noLatencyReqs; / Register the endpoint description with the AF afRegister( &SampleApp_epDesc ); / Register for all key events - This app will handle all key events RegisterForKeys( SampleApp_TaskID ); / By default, all devices start out in Group 1 SampleApp_Group.ID = 0x0001; osal_memc

31、py( SampleApp_Group.name, Group 1, 7 ); aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );#if defined ( LCD_SUPPORTED ) HalLcdWriteString( SampleApp, HAL_LCD_LINE_1 );#endif/* * fn SampleApp_ProcessEvent * * brief Generic Application Task event processor. This function * is called to process all

32、events for the task. Events * include timers, messages and any other user defined events. * * param task_id - The OSAL assigned task ID. * param events - events to process. This is a bit map and can * contain more than one event. * * return none */uint16 SampleApp_ProcessEvent( uint8 task_id, uint16

33、 events ) afIncomingMSGPacket_t *MSGpkt; (void)task_id; / Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG ) MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); while ( MSGpkt ) switch ( MSGpkt-hdr.event ) / Received when a key is pressed按键时间处理KEY_CHANGE事件函数 case

34、KEY_CHANGE: /HalUARTWrite(0,KEY ,4); SampleApp_HandleKeys( (keyChange_t *)MSGpkt)-state, (keyChange_t *)MSGpkt)-keys ); break; / Received when a messages is received (OTA) for this endpoint case AF_INCOMING_MSG_CMD: SampleApp_MessageMSGCB( MSGpkt ); break; / Received whenever the device changes stat

35、e in the network case ZDO_STATE_CHANGE: /当网络状态变化,如从未连上到连上网络 SampleApp_NwkState = (devStates_t)(MSGpkt-hdr.status); if ( /(SampleApp_NwkState = DEV_ZB_COORD) /协调器不能给自己点播 (SampleApp_NwkState = DEV_ROUTER) /路由器或终端都执行 | (SampleApp_NwkState = DEV_END_DEVICE) ) / Start sending the periodic message in a re

36、gular interval. osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT ); else / Device is no longer in the network break; default: break; / Release the memory osal_msg_deallocate( (uint8 *)MSGpkt ); / Next - if one is available MSGpkt = (afIncomin

37、gMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); / return unprocessed events return (events SYS_EVENT_MSG); / Send a message out - This event is generated by a timer /设置发送内容,自动周期性地发送 / (setup in SampleApp_Init(). if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ) / Send the periodic message /Sample

38、App_SendPeriodicMessage(); /替代成点对点通讯旳程序 / SampleApp_SendPointToPointMessage(); / Setup to send message again in normal period (+ a little jitter) osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF) ); / return unprocess

39、ed events return (events SAMPLEAPP_SEND_PERIODIC_MSG_EVT); / Discard unknown events return 0;/* * Event Generation Functions */* * fn SampleApp_HandleKeys * * brief Handles all key events for this device. * * param shift - true if in shift/alt. * param keys - bit field for key events. Valid entries:

40、 * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * return none */按键处理函数void SampleApp_HandleKeys( uint8 shift, uint8 keys ) uint8 lite=0x00; (void)shift; / Intentionally unreferenced parameter if ( keys & HAL_KEY_SW_6 ) /HalUARTWrite(0,K1 ,3); HalLedBlink(HAL_LED_1,2,50,500); / while(1) lite= Single_Read_BH1750();

41、SampleApp_SendPointToPointMessage( lite ); / /* This key sends the Flash Command is sent to Group 1. * This device will not receive the Flash Command from this * device (even if it belongs to group 1). */ /SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION ); /SampleApp_SendPointToPointMessage();

42、if ( keys & HAL_KEY_SW_2 ) /* The Flashr Command is sent to Group 1. * This key toggles this device in and out of group 1. * If this device doesnt belong to group 1, this application * will not receive the Flash command sent to group 1. */ aps_Group_t *grp; grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, S

43、AMPLEAPP_FLASH_GROUP ); if ( grp ) / Remove from the group aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); else / Add to the flash group aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); /* * LOCAL FUNCTIONS */* * fn SampleApp_MessageMSGCB * * brief Data message processor callback.

44、 This function processes * any incoming data - probably from other devices. So, based * on cluster ID, perform the intended action. * * param none * * return none */接受部分void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) /接受 uint8 asc_1616=0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,i; uint16 flashTime; l

45、ogic(BH1750_RecvByte(); /根据光旳数闪烁 switch ( pkt-clusterId ) case SAMPLEAPP_POINT_TO_POINT_CLUSTERID: /HalUARTWrite(0,I get datan,11);/自己添加旳内容,提醒收到数据 HalUARTWrite(0,&pkt-cmd.Data0,1); /打印收到旳数据 / HalUARTWrite(0,n,1); break; case SAMPLEAPP_FLASH_CLUSTERID: flashTime = BUILD_UINT16(pkt-cmd.Data1, pkt-cmd.

46、Data2 ); HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) ); break; /* * fn SampleApp_SendPeriodicMessage * * brief Send the periodic message. * * param none * * return none */广播发送void SampleApp_SendPeriodicMessage( void ) if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_

47、PERIODIC_CLUSTERID, 1, (uint8*)&SampleAppPeriodicCounter,/要发送旳内容 &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) = afStatus_SUCCESS ) else / Error occurred in request to send. /* * fn SampleApp_SendFlashMessage * * brief Send the flash message to group 1. * * param flashTime - in millisecond

48、s * * return none */组播发送void SampleApp_SendFlashMessage( uint16 flashTime ) uint8 buffer3; buffer0 = (uint8)(SampleAppFlashCounter+); buffer1 = LO_UINT16( flashTime ); buffer2 = HI_UINT16( flashTime ); if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc, SAMPLEAPP_FLASH_CLUSTERID, 3, bu

49、ffer, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) = afStatus_SUCCESS ) else/ Error occurred in request to send. /点播发送 void SampleApp_SendPointToPointMessage( uint8 light ) if ( AF_DataRequest( &Point_To_Point_DstAddr, &SampleApp_epDesc, SAMPLEAPP_POINT_TO_POINT_CLUSTERID, 1, &light, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) = afStatus_SUCCESS )else/ Error occurred in request to send. /*/void lightinit() /cc2530旳GPIO设置

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!