新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應用 > 單片機中應用觀察者模式

單片機中應用觀察者模式

作者: 時間:2016-11-20 來源:網(wǎng)絡 收藏
環(huán)境:

主機:WIN8

本文引用地址:http://m.butianyuan.cn/article/201611/318612.htm

開發(fā)環(huán)境:MDK5.13

mcu: stm32f103RB

說明:

之前在java中應用觀察者模式,現(xiàn)將此模式的思想應用在單片機程序設(shè)計

Android編程:觀察者模式設(shè)計:

http://blog.csdn.net/jdh99/article/details/41821295

觀察者模式本質(zhì):

有兩個模塊A,B。A是目標,B是觀察者。則B能觀察到A的變化。

在程序?qū)崿F(xiàn)中過程是:

1.A產(chǎn)生數(shù)據(jù)

2.A通知B

3.B處理數(shù)據(jù)

單片機中實現(xiàn)的方法:

Java中通過接口實現(xiàn)此思想,單片機是C語言編程,則可以通過函數(shù)指針來實現(xiàn)。

源代碼中dw1000通信模塊提供了兩個被觀察的目標:接收數(shù)據(jù),發(fā)送數(shù)據(jù)完成。

源代碼:

dw1000.h

/**						 dw1000通信模塊頭文件*						(c)copyright 2015,jdh*						  All Right Reserved*新建時間:2015/1/5 by jdh*修改時間:2015/1/6 by jdh/#ifndef _DW1000_H_#define _DW1000_H_/**							頭文件/#include "world.h"/**							宏定義//**							接收數(shù)據(jù)緩存最大長度/#define LEN_DW1000_BUF_RX			128/**							數(shù)據(jù)結(jié)構(gòu)//**							dw1000時間結(jié)構(gòu)/union _Dw1000_Time{uint8_t pt[8];uint64_t value;};/**							接收數(shù)據(jù)結(jié)構(gòu)/struct _Dw1000_Rx{//接收數(shù)據(jù)uint8_t buf[LEN_DW1000_BUF_RX];//接收數(shù)據(jù)的dw1000時間union _Dw1000_Time time;};/**							觀察者模式:接收數(shù)據(jù)處理函數(shù)指針/typedef void (*T_Dw1000_Deal_Rx)(struct _Dw1000_Rx);/**							觀察者模式:發(fā)送數(shù)據(jù)完成處理函數(shù)指針/typedef void (*T_Dw1000_Deal_Tx_End)(union _Dw1000_Time);/**							接收緩存/struct _Rx_Buf_CC1100{//接收時間T_Time time;//源IDuint16_t src_id;//功能碼uint8_t cmd;//數(shù)據(jù)uint8_t data[3];//rssiint rssi;//lqiuint8_t lqi;};/**							函數(shù)//**							接口函數(shù):模塊載入/void dw1000_load(void);/**							接口函數(shù):模塊運行/void dw1000_run(void);/**							接口函數(shù):中斷處理函數(shù)/void dw1000_irq_handler(void);/**							接口函數(shù):判斷是否可以發(fā)送*返回:0:不可以發(fā)送,1:可以發(fā)送/uint8_t cc1100_judge_tx(void);/**							接口函數(shù):發(fā)送數(shù)據(jù)*參數(shù):cmd:功能碼*     id:目標id*     data:3字節(jié)數(shù)據(jù)/void cc1100_tx(uint8_t cmd,uint16_t id,uint8_t *data);/**							接口函數(shù):得到接收數(shù)據(jù)*返回:接收數(shù)據(jù)/struct _Rx_Buf_CC1100 cc1100_get_rx_buf(void);/**							接口函數(shù):設(shè)置頻點*參數(shù):freq:需要設(shè)置的頻點/void cc1100_set_freq(uint8_t freq);/**							接口函數(shù):注冊觀察者:接收數(shù)據(jù)/void dw1000_register_observer_rx(T_Dw1000_Deal_Rx function);/**							接口函數(shù):注冊觀察者:發(fā)送完成/void dw1000_register_observer_tx_end(T_Dw1000_Deal_Tx_End function);#endif

dw1000.c

/**						 dw1000通信模塊主文件*						(c)copyright 2015,jdh*						  All Right Reserved*新建時間:2015/1/5 by jdh*修改時間:2015/1/6 by jdh//**							頭文件/#include "dw1000.h"/**							宏定義//**							觀察者最大個數(shù)/#define MAX_OBSERVER				10/**							靜態(tài)變量//**							接收數(shù)據(jù)觀察者列表/static T_Dw1000_Deal_Rx Observer_Rx[MAX_OBSERVER];static uint8_t Len_Observer_Rx = 0;/**							發(fā)送完成觀察者列表/static T_Dw1000_Deal_Tx_End Observer_Tx_End[MAX_OBSERVER];static uint8_t Len_Observer_Tx_End = 0;/**							靜態(tài)函數(shù)//**							接收處理/static void deal_rx(void);/**							發(fā)送結(jié)束處理/static void deal_tx_end(void);/**							函數(shù)//**							接口函數(shù):模塊載入/void dw1000_load(void){}/**							接口函數(shù):模塊運行/void dw1000_run(void){}/**							接口函數(shù):注冊觀察者:接收數(shù)據(jù)/void dw1000_register_observer_rx(T_Dw1000_Deal_Rx function){Observer_Rx[Len_Observer_Rx++] = function;}/**							接口函數(shù):注冊觀察者:發(fā)送完成/void dw1000_register_observer_tx_end(T_Dw1000_Deal_Tx_End function){Observer_Tx_End[Len_Observer_Tx_End++] = function;}/**							接口函數(shù):中斷處理函數(shù)/void dw1000_irq_handler(void){uint32_t status = 0;uint32_t clear = 0; // will clear any events seenuint8_t resetrx;status = dwt_read32bitreg(SYS_STATUS_ID) ;            // read status register low 32bitif(status & SYS_STATUS_LDEDONE){if((status & (SYS_STATUS_LDEDONE | SYS_STATUS_RXPHD | SYS_STATUS_RXSFDD)) != (SYS_STATUS_LDEDONE | SYS_STATUS_RXPHD | SYS_STATUS_RXSFDD)){resetrx = 0xe0;//got LDE done but other flags SFD and PHR are clear - this is a bad frame - reset the transceiverdwt_forcetrxoff(); //this will clear all events//set rx resetdwt_writetodevice(PMSC_ID, 0x3, 1, &resetrx);resetrx = 0xf0; //clear RX resetdwt_writetodevice(PMSC_ID, 0x3, 1, &resetrx);//			dwt_write16bitoffsetreg(SYS_CTRL_ID,0,(uint16)SYS_CTRL_RXENAB) ;}}if((status & SYS_STATUS_RXFCG) && (status & SYS_STATUS_LDEDONE))  // Receiver FCS Good{//clear all receive status bits (as we are finished with this receive event)clear |= status & CLEAR_ALLRXGOOD_EVENTS  ;dwt_write32bitreg(SYS_STATUS_ID,clear) ;         // write status register to clear event bits we have seen//接收處理deal_rx();}else{if (status & SYS_STATUS_TXFRS)  // Transmit Frame Sent{clear |= CLEAR_ALLTX_EVENTS; //clear TX event bitsdwt_write32bitreg(SYS_STATUS_ID,clear) ;         // write status register to clear event bits we have seen//發(fā)送結(jié)束處理deal_tx_end();}else{if (status & SYS_STATUS_RXRFTO) {//接收超時clear |= status & SYS_STATUS_RXRFTO ;dwt_write32bitreg(SYS_STATUS_ID,clear) ;         // write status register to clear event bits we have seendwt_setrxtimeout(0);dwt_rxenable(0) ;}else{//異常 清除所有標識					 clear |= CLEAR_ALLRXERROR_EVENTS;dwt_write32bitreg(SYS_STATUS_ID,clear) ;         // write status register to clear event bits we have seendwt_forcetrxoff(); //this will clear all events//set rx resetdwt_writetodevice(PMSC_ID, 0x3, 1, &resetrx);resetrx = 0xf0; //clear RX resetdwt_writetodevice(PMSC_ID, 0x3, 1, &resetrx);dwt_rxenable(0) ;	 }}}}/**							接收處理/static void deal_rx(void){struct _Dw1000_Rx rx;uint16_t len;uint8_t i = 0;len = dwt_read16bitoffsetreg(RX_FINFO_ID, 0) & 0x3FF;if (len >= 127) {return;}dwt_write32bitreg(SYS_STATUS_ID,CLEAR_ALLRXGOOD_EVENTS) ; //得到接收時間dwt_readrxtimestamp(rx.time.pt);rx.time.value &= MASK_40BIT;//得到接收數(shù)據(jù)dwt_readfromdevice(RX_BUFFER_ID,0,len,rx.buf) ;//通知觀察者for (i = 0;i < Len_Observer_Rx;i++){Observer_Rx[i](rx);}//	uint8   resetrx;//	////	resetrx = 0xe0;   	//got LDE done but other flags SFD and PHR are clear - this is a bad frame - reset the transceiver////	dwt_forcetrxoff();													 //this will clear all events////	dwt_writetodevice(PMSC_ID, 0x3, 1, &resetrx);//set rx reset////	resetrx = 0xf0; 														 //clear RX reset////	dwt_writetodevice(PMSC_ID, 0x3, 1, &resetrx);dwt_write16bitoffsetreg(SYS_CTRL_ID,0,(uint16)SYS_CTRL_RXENAB) ;			}/**							發(fā)送結(jié)束處理/static void deal_tx_end(void){union _Dw1000_Time time;uint8_t i = 0;//獲得發(fā)送時間dwt_readtxtimestamp(time.pt) ;time.value &= MASK_40BIT;	//通知觀察者for (i = 0;i < Len_Observer_Tx_End;i++){Observer_Tx_End[i](time);}}

main.c中觀察dw1000模塊的接收數(shù)據(jù):

//增加接收數(shù)據(jù)觀察者dw1000_register_observer_rx(deal_rx);

處理函數(shù):

void deal_rx(struct _Dw1000_Rx rx){//處理...__nop();__nop();__nop();}


關(guān)鍵詞: 單片機觀察者模

評論


技術(shù)專區(qū)

關(guān)閉