环境:
主机:WIN8
开发环境:MDK5.13
mcu: stm32f103RB
阐明:
之前在java中使用调查者形式,现将此形式的思维使用在单片机程序规划中
Android编程:调查者形式规划:
http://blog.csdn.net/jdh99/article/details/41821295
调查者形式实质:
有两个模块A,B。A是方针,B是调查者。则B能调查到A的改变。
在程序完结中进程是:
1.A发生数据
2.A告诉B
3.B处理数据
单片机中完结的办法:
Java中经过接口完结此思维,单片机是C言语编程,则能够经过函数指针来完结。
源代码中dw1000通讯模块供给了两个被调查的方针:接纳数据,发送数据完结。
源代码:
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"/** 宏界说//** 接纳数据缓存最大长度/#define LEN_DW1000_BUF_RX 128/** 数据结构//** dw1000时刻结构/union _Dw1000_Time{uint8_t pt[8];uint64_t value;};/** 接纳数据结构/struct _Dw1000_Rx{//接纳数据uint8_t buf[LEN_DW1000_BUF_RX];//接纳数据的dw1000时刻union _Dw1000_Time time;};/** 调查者形式:接纳数据处理函数指针/typedef void (*T_Dw1000_Deal_Rx)(struct _Dw1000_Rx);/** 调查者形式:发送数据完结处理函数指针/typedef void (*T_Dw1000_Deal_Tx_End)(union _Dw1000_Time);/** 接纳缓存/struct _Rx_Buf_CC1100{//接纳时刻T_Time time;//源IDuint16_t src_id;//功用码uint8_t cmd;//数据uint8_t data[3];//rssiint rssi;//lqiuint8_t lqi;};/** 函数//** 接口函数:模块载入/void dw1000_load(void);/** 接口函数:模块运转/void dw1000_run(void);/** 接口函数:中止处理函数/void dw1000_irq_handler(void);/** 接口函数:判别是否能够发送*回来:0:不能够发送,1:能够发送/uint8_t cc1100_judge_tx(void);/** 接口函数:发送数据*参数:cmd:功用码* id:方针id* data:3字节数据/void cc1100_tx(uint8_t cmd,uint16_t id,uint8_t *data);/** 接口函数:得到接纳数据*回来:接纳数据/struct _Rx_Buf_CC1100 cc1100_get_rx_buf(void);/** 接口函数:设置频点*参数:freq:需求设置的频点/void cc1100_set_freq(uint8_t freq);/** 接口函数:注册调查者:接纳数据/void dw1000_register_observer_rx(T_Dw1000_Deal_Rx function);/** 接口函数:注册调查者:发送完结/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"/** 宏界说//** 调查者最大个数/#define MAX_OBSERVER 10/** 静态变量//** 接纳数据调查者列表/static T_Dw1000_Deal_Rx Observer_Rx[MAX_OBSERVER];static uint8_t Len_Observer_Rx = 0;/** 发送完结调查者列表/static T_Dw1000_Deal_Tx_End Observer_Tx_End[MAX_OBSERVER];static uint8_t Len_Observer_Tx_End = 0;/** 静态函数//** 接纳处理/static void deal_rx(void);/** 发送完毕处理/static void deal_tx_end(void);/** 函数//** 接口函数:模块载入/void dw1000_load(void){}/** 接口函数:模块运转/void dw1000_run(void){}/** 接口函数:注册调查者:接纳数据/void dw1000_register_observer_rx(T_Dw1000_Deal_Rx function){Observer_Rx[Len_Observer_Rx++] = function;}/** 接口函数:注册调查者:发送完结/void dw1000_register_observer_tx_end(T_Dw1000_Deal_Tx_End function){Observer_Tx_End[Len_Observer_Tx_End++] = function;}/** 接口函数:中止处理函数/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//发送完毕处理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;//得到接纳数据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) ; }/** 发送完毕处理/static void deal_tx_end(void){union _Dw1000_Time time;uint8_t i = 0;//取得发送时刻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模块的接纳数据:
//添加接纳数据调查者dw1000_register_observer_rx(deal_rx);
处理函数:
void deal_rx(struct _Dw1000_Rx rx){//处理...__nop();__nop();__nop();}