




已閱讀5頁,還剩27頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
ZigBee串口應(yīng)用,一、串口收發(fā)基礎(chǔ)實驗 二、ZigBee協(xié)議棧串口應(yīng)用擴(kuò)展實驗,實驗課題:串口數(shù)據(jù)收發(fā)基礎(chǔ)實驗 實驗?zāi)康模豪么谑瞻l(fā)數(shù)據(jù) 試驗步驟:ZigBee協(xié)議棧中串口通信的配置使用一個結(jié)構(gòu)體來實現(xiàn),該結(jié)構(gòu)體為hal_UARTCfg_t, 不必關(guān)心該結(jié)構(gòu)體的具體定義形式,只需要對其功能有個了解,該結(jié)構(gòu)體將串口初始化的參數(shù)集合在一起, 只需要初始化各個參數(shù)即可 最后使用HalUARTOpen()函數(shù)對串口進(jìn)行初始化,該函數(shù)將halUARTCfg_t類型的結(jié)構(gòu)體變量作為相關(guān)參數(shù) 回調(diào)函數(shù):通過指針(函數(shù)地址)調(diào)用的函數(shù)。如果把函數(shù)的地址作為參數(shù)傳遞給另一個函數(shù),當(dāng)通過這個指針 調(diào)用它所指向的函數(shù)時,稱為函數(shù)的回調(diào)。,一、串口收發(fā)基礎(chǔ)實驗,使用串口的基本步驟: 初始化串口,包括設(shè)置波特率、中斷等; 向發(fā)送緩沖區(qū)發(fā)送數(shù)據(jù)或者從接收緩沖區(qū)讀取數(shù)據(jù)。,串口工作原理剖析,在ZigBee協(xié)議棧中,halUARTCfg_t結(jié)構(gòu)體是如何定義的; 串口是如何初始化的; 發(fā)送給串口的數(shù)據(jù)時如何接受的; 串口是如何向PC機(jī)發(fā)送數(shù)據(jù)的。 這些問題涉及如下三個函數(shù),ZigBee協(xié)議棧中提供的與串口操作有關(guān)的三個函數(shù)為(可在zmain.c和hal_uart.c中查看相關(guān)函數(shù)) uint8 HalUARTOpen(uint8 halUARTCfg_t *config); uint16 HalUARTRead(uint8 port,uint8 *buf,uint16 len); Uint16 HalUARTWrite(uint8 prot,uint *buf,uint16 len);,Coordinator.c,#include “OSAL.h“ #include “AF.h“ #include “ZDApp.h“ #include “ZDObject.h“ #include “ZDProfile.h“ #include #include “Coordinator.h“ #include “DebugTrace.h“ #if !defined( WIN32 ) #include “OnBoard.h“ #endif #include “hal_lcd.h“ #include “hal_led.h“ #include “hal_key.h“ #include “hal_uart.h“ /包含頭文件,const cId_t GenericApp_ClusterListGENERICAPP_MAX_CLUSTERS = GENERICAPP_CLUSTERID ; const SimpleDescriptionFormat_t GenericApp_SimpleDesc = GENERICAPP_ENDPOINT, GENERICAPP_PROFID, GENERICAPP_DEVICEID, GENERICAPP_DEVICE_VERSION, GENERICAPP_FLAGS, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, 0, (cId_t *)NULL ; endPointDesc_t GenericApp_epDesc; byte GenericApp_TaskID; byte GenericApp_TransID;,/* typedef struct byte endPoint; byte *task_id; SimpleDescriptionFormat_t *simpleDesc; afNetworkLatencyReq_t latencyReq; endPointDesc_t; */ void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); void GenericApp_SendTheMessage( void ); /UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events ); static void rxCB(uint8 port,uint8 event);,/任務(wù)初始化函數(shù) void GenericApp_Init( byte task_id ) halUARTCfg_t uartConfig; GenericApp_TaskID = task_id; GenericApp_TransID = 0; GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT; GenericApp_epDesc.task_id = /串口初始化 ,/事件處理函數(shù) UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events ) return 0; /本實驗沒有進(jìn)行事件處理 ,static void rxCB(uint8 port,uint8 event) unsigned char uartbuf128=“ “; HalUARTRead(0,uartbuf,16); /調(diào)用函數(shù),從串口讀取數(shù)據(jù)并將其放在uartbuf數(shù)組中 if(osal_memcmp(uartbuf,““,16) /使用osal_memcmp()判斷接收到的數(shù)據(jù)是否是“”,返回TRUE,執(zhí)行HalUARTWrite()函數(shù) HalUARTWrite(0,uartbuf,16); /調(diào)用函數(shù)將接收到的字符輸出到串口 /ZigBee協(xié)議棧使用條件編譯,在GenericApp-Coordinator工程上右鍵單擊,options/C/C+ Compiler在Defined symbols:下拉框中添加HAL_UART=TRUE,點擊OK即可。,二、ZigBee協(xié)議棧串口應(yīng)用擴(kuò)展實驗,實驗課題:ZigBee協(xié)議棧串口應(yīng)用擴(kuò)展實驗 實驗?zāi)康模豪么谑瞻l(fā)數(shù)據(jù) 實驗原理:協(xié)調(diào)器建立ZigBee無線網(wǎng)絡(luò),終端節(jié)點自動加入該網(wǎng)絡(luò)中,然后終端節(jié)點周期性地向協(xié)調(diào)器發(fā)送字符串“EndDevice” ,協(xié)調(diào)器收到該字符串后,通過串口將其輸出到用戶PC機(jī)。,Coordinator.c,#include “OSAL.h“ #include “AF.h“ #include “ZDApp.h“ #include “ZDObject.h“ #include “ZDProfile.h“ #include #include “Coordinator.h“ #include “DebugTrace.h“ #if !defined( WIN32 ) #include “OnBoard.h“ #endif #include “hal_lcd.h“ #include “hal_led.h“ #include “hal_key.h“ #include “hal_uart.h“,const cId_t GenericApp_ClusterListGENERICAPP_MAX_CLUSTERS = GENERICAPP_CLUSTERID ; const SimpleDescriptionFormat_t GenericApp_SimpleDesc = GENERICAPP_ENDPOINT, GENERICAPP_PROFID, GENERICAPP_DEVICEID, GENERICAPP_DEVICE_VERSION, GENERICAPP_FLAGS, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, 0, (cId_t *)NULL ; endPointDesc_t GenericApp_epDesc; byte GenericApp_TaskID; byte GenericApp_TransID; /unsigned char uartbuf128; /將這一行注釋掉,/* typedef struct byte endPoint; byte *task_id; SimpleDescriptionFormat_t *simpleDesc; afNetworkLatencyReq_t latencyReq; endPointDesc_t; */ void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); void GenericApp_SendTheMessage( void ); /static void rxCB(uint8 port,uint8 event); /將這一行注釋掉,/任務(wù)初始化函數(shù) void GenericApp_Init( byte task_id ) halUARTCfg_t uartConfig; GenericApp_TaskID = task_id; GenericApp_TransID = 0; GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT; GenericApp_epDesc.task_id = /串口初始化 ,/事件處理函數(shù) UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events ) afIncomingMSGPacket_t *MSGpkt; if( events ,osal_msg_deallocate( (uint8 *)MSGpkt ); MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID); return (events SYS_EVENT_MSG ); return 0; /當(dāng)協(xié)調(diào)器收到終端節(jié)點發(fā)送來的數(shù)據(jù)后,首先使用osal_msg_receive()函數(shù),從消息隊列接收到消息,然后調(diào)用GenericApp_ /MessageMSGCB()函數(shù)中將接收到的數(shù)據(jù)通過串口發(fā)送給PC機(jī),void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) unsigned char buffer10 = “ “; switch( pkt-clusterId ) case GENERICAPP_CLUSTERID: osal_memcpy(buffer,pkt-cmd.Data,10 ); HalUARTWrite(0,buffer,10); break; /使用osal_memcpy()函數(shù),將接收到的數(shù)據(jù)拷貝到buffer數(shù)組中,然后就可以將該數(shù)據(jù)通過串口發(fā)送給PC機(jī),EndDevice.c Osal_start_timerEx()函數(shù)原型如下: 調(diào)用 uint8 osal_start_timerEx(uint8 taskID,uint16 event_id,uint16 timeout_value) 周期性發(fā)送(任務(wù),事件,時間),#include “OSAL.h“ #include “AF.h“ #include “ZDApp.h“ #include “ZDObject.h“ #include “ZDProfile.h“ #include #include “Coordinator.h“/使用該頭文件的宏定義 #include “DebugTrace.h“ #if !defined( WIN32 ) #include “OnBoard.h“ #endif,#include “hal_lcd.h“ #include “hal_led.h“ #include “hal_key.h“ #include “hal_uart.h“ #define SEND_DATA_EVENT 0x01 const cId_t GenericApp_ClusterListGENERICAPP_MAX_CLUSTERS = GENERICAPP_CLUSTERID ;,/簡單設(shè)備描述符 結(jié)構(gòu)體 常量 const SimpleDescriptionFormat_t GenericApp_SimpleDesc = GENERICAPP_ENDPOINT, GENERICAPP_PROFID, GENERICAPP_DEVICEID, GENERICAPP_DEVICE_VERSION, GENERICAPP_FLAGS, 0, (cId_t *)NULL, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList ; /初始化端口描述符 endPointDesc_t GenericApp_epDesc;/簡單節(jié)點描述符 byte GenericApp_TaskID;/任務(wù)優(yōu)先級 byte GenericApp_TransID;/數(shù)據(jù)發(fā)送序列號 devStates_t GenericApp_NwkState;/節(jié)點狀態(tài)保存變量 枚舉型 記錄設(shè)備狀態(tài),/定義結(jié)構(gòu)體 /*typedef struct byte endPoint; byte *task_id; SimpleDescriptionFormat_t *simpleDesc; afNetworkLatencyReq_t latencyReq; endPointDesc_t;*/ /聲明函數(shù) void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); void GenericApp_SendTheMessage( void ); int8 readTemp(void);,/定義任務(wù)初始化函數(shù) void GenericApp_Init( byte task_id ) GenericApp_TaskID = task_id; GenericApp_TransID = 0;/累加數(shù)據(jù)發(fā)送序列號,可用于計算丟包率 GenericApp_NwkState = DEV_INIT;/設(shè)備狀態(tài)初始化為DEV_INIT表示該節(jié)點沒有連接到ZigBee網(wǎng)絡(luò) GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT; GenericApp_epDesc.task_id = /節(jié)點描述符注冊 ,/以
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 環(huán)境工程概論課件
- 《SOFIT評估》教學(xué)課件
- 動物醫(yī)學(xué)產(chǎn)品介紹
- 勇敢牛牛創(chuàng)意美術(shù)課件
- 《QCC活動介紹》課件
- 主體工程安全管理與安全技術(shù)課件
- 2019-2025年教師資格之中學(xué)音樂學(xué)科知識與教學(xué)能力每日一練試卷A卷含答案
- 小白兔兒童畫課件
- 幼兒園教育體系概述
- 可行性研究報告批復(fù)的材料
- (二模)貴陽市2025年高三年級適應(yīng)性考試(二)物理試卷(含答案)
- 合資公司成立可行性研究報告范文
- 《康復(fù)技術(shù)》課件-踝關(guān)節(jié)扭傷康復(fù)
- 2025年中國亮白防蛀固齒牙膏市場調(diào)查研究報告
- 上甘嶺戰(zhàn)役課件
- 黑龍江省齊齊哈爾市普高聯(lián)誼校2022-2023學(xué)年高一下學(xué)期語文期末試卷(含答案)
- 名家班主任培訓(xùn):AI賦能與德育創(chuàng)新
- 首汽約車合同協(xié)議
- 工程師評審代辦合同協(xié)議
- (二模)2025年深圳市高三年級第二次調(diào)研考試物理試卷(含標(biāo)準(zhǔn)答案)
- 2025-2030中國供電行業(yè)深度發(fā)展研究與“十四五”企業(yè)投資戰(zhàn)略規(guī)劃報告
評論
0/150
提交評論