电子设计补充库点对点通信_第1页
电子设计补充库点对点通信_第2页
电子设计补充库点对点通信_第3页
电子设计补充库点对点通信_第4页
电子设计补充库点对点通信_第5页
已阅读5页,还剩24页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

硬件:PC机(二台PCAB66778899AA的数据/A0A112233445566778899点B端的串口工具上能接收到A点发送的数据;#ifndefMAIN_H#defineMAIN_H#defineREG_BIT_DEF#defineGLOBAL_ISR_DISABLE() (EA=0)#define (EA=#defineLEDP2#define //243 117788(117647#defineISR_PRIORITY_HIGH#defineISR_PRIORITY_LOW typedefbit typedefunsignedchar typedefunsignedintWORD; typedefunsignedlongDWORD; //32bittypedefunsignedcharBool;REG_BIT_DEFLED1= //#defineSET_LED(x)(LED##x=0)#defineCLR_LED(x)(LED##x=REG_BIT_DEFCCRESET=0x96;//P1^6;REG_BIT_DEFCCVREGEN=0x97;//P1^7;REG_BIT_DEFFIFOP=0x92;//P1^2;REG_BIT_DEFFIFO =0x94;//P1^4;REG_BIT_DEFSFD =0x90;//P1^0;REG_BIT_DEF =0x95;//#defineSFD_IS_ACTIVE() //SFDbitisset#defineFIFO_IS_ACTIVE() #define //CCAbitis#define//Command#define#define#define#define#define#define#define#define#define#define#define#define//#define#define#define#define#define#define#define#define#define#define#define#define#define#define#define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define //#define #define ////#define #define //#define #defineCC2420_RAM_RXFIFO #define #defineCC2420_RAM_PANID #define #defineCC2420_CRC_OK #defineGLOBAL_ISR_CTRL_REG()(EA)#defineNSS_LOW() #definePHY_24G_CHANNEL_MAX26//MAXchannelnumberfor2.4Gchannel#definePHY_24G_CHANNEL_MIN 11//MINchannelnumberfor2.4Gchannel#definePHY_24G_CHANNEL_PAGE0#defineMAX_CHAN_NUM PHY_24G_CHANNEL_MIN+1)#definePHY_POWER_LEV_MAX31 //MAXpowerlevel#definePHY_POWER_LEV_MIN3 //MINpowerlevel#defineSYSTEM_CLK24500000#define #defineSPI0CKR_VALUE(SYSTEM_CLK/2/SPI0_CLK-1)#defineNSS_LOW() #define 2#define2#define#define1#define#define (65536-(SYSTEM_CLK/12)*(x)#defineaMaxPHYPacketSize //ThemaximumPSDUsize(inoctets)thePHYshallbeabletoreceive.#defineaMinPHYPacketSize //TheminimalPSDUsize(inoctets)thePHYshallbeabletoreceive.typedefstructBYTEphyCurrentChannel; //TheRFchannelnumber.0~26 //b27~b31channelpagesupported,BYTE //0x00~0xbfrepresent31~-BYTE //1~BYTE //0~ //Themaximumnumberofsymbolsinaframe. //Thedurationofthesynchronizationheader(SHR) //Thenumberofsymbolsperoctetforthecurrent DEVICE_IDLE=0,typedefstruct typedefenum{BUF_EMPTY=0,typedefstructEXT_DATA*Buf;BYTEMaxSize;BYTEMaxNum;BYTEReadIndex;eBufStateStatus; tag_UART_BAUDRATE{BR_115200=0x01,typedefstructtag_MAC_TX_BUF{ tag_WORD_BYTE{WORDstructBYTEBYTEstaticBYTEEXT_DATAtBufManaMsgEXT_DATAgPhyToMacBufMana; EXT_DATAgPhyPib; //phyPIBdatastaticBYTEEXT_DATAstaticBYTEEXT_DATAstatictBufManaMsgEXT_DATAgAppToMacBufMana;statictBufManaMsgEXT_DATAgMacToAppBufMana; #ifndef#ifndef#ifndefmain.c参考程序#include<C8051F120.H>#include<intrins.h>#include"main.h"voidWTDDisable(void){ //writingto0xDEand0xADmustbefinishedin4clockWDTCN=0xDE;WDTCN=}voidSystemClkInit(void){WORDEXT_DATAtmp; =0x67; //enableextosc10-30MHzrangefor(tmp=24500;tmp!=0;tmp--) //Wait1msforinitialization{;}while((OSCXCN&0x80)=={;}OSCICN=0x83; //Enable24.5MHzinternaloscfor(tmp=24500;tmp!=0;tmp--) //Wait1msforinitialization{;}while((OSCICN&0x40)=={;}CLKSEL= //Selectinternalclock}voidSfrAutoSwitchDis(void){SFRPGCN=0x00;}voidPortInit(void){ = //SFRpage |= //enableP0MDOUT|= //enableTXasapush-pull |= //EnableP0MDOUT|= //SCK,MOSI,NSSpush-pull |= //enablesmbus(skip2 |= //enableCEX0=P1.0,CEX1= |= //enable/int0=P1MDOUT|= //RESETVRGENpush-pull |= //Enable}voidUart0Init(BOOLpri){ES0=IP|=}IP&=} SCON0= //Mode1(8bitdata),Nocheckstopbit,EnableSSTA0 //Baudratedoublerenable,T1溢出率为TXRXSFRPAGE=TIMER01_PAGE; =0x00; = //T1timermodeuses8bitCKCON=0x10; //T1timerisusedforsystemclock =0;}//功能:ThisfunctioninitializestheUARTtimervoidUart0TimeOutTimerInit(BOOLpri){PT0

|==

//16}}

= //TurnoffBYTEsaveSfr=SFRPAGE;SFRPAGE=TIMER01_PAGE;TR0=0;//stoptimercountTF0=0;//clearisrflagET0=0;//TurnoffisrTH0=0;TL0=SFRPAGE=//功能:SPIvoidSpi0Init(void){ //SFRpageSPI0CFG= //mastermode,+clock,1st = //4wireNSSSPI0CKR= //initSPISPI0CN|= //enableSFRPAGE=0x0F; //disablespiinterrupt }voidExternalISRInit(BOOLpri){ |= //lowlevel &= //clearIE0 IP }}}

IP&=voidUart0SetBaudrate(BYTEbaudrate){ =0; //TurnoffT1TH1=baudrate;TL1= = //Turnon}//功能:Thisfunctionsetsthefirstlengthbytesinbufferto bufferpointto value:bytevalueto bufferlengthtovoidXdataMemset(voidEXT_DATA*p,BYTEvalue,WORDEXT_DATAlen)reentrant{BYTEEXT_DATA*byteP;if(len==}*byteP++=}}//功能:InitializeUart0trxdatacontrolvoidUartTRxManaMsgInit(void){gUartTRxManaMsg.TxDataFirstAddr=NULL;gUartTRxManaMsg.RxDataFirstAddr=NULL;}//功能:ThisfunctioninitializethebufferinformationbetweenAppandvoidBufBetweenAppAndMacInit(void){XdataMemset(&gMacToAppBuf,0,gAppToMacBufMana.Buf=&gAppToMacBuf[0][0];gAppToMacBufMana.Len=gAppToMacSonBufDataLen;gAppToMacBufMana.MaxSize=APP_TO_MAC_BUF_MAX_SIZE; gAppToMacBufMana.WriteIndex=0;gAppToMacBufMana.ReadIndex=0;gAppToMacBufMana.Status=BUF_EMPTY;gMacToAppBufMana.Buf=&gMacToAppBuf[0][0];gMacToAppBufMana.MaxSize=MAC_TO_APP_BUF_MAX_SIZE; gMacToAppBufMana.WriteIndex=0;gMacToAppBufMana.ReadIndex=0;gMacToAppBufMana.Status=BUF_EMPTY;}//功能:ThisprocedurewilldoainitializationongloblePHYPIBvoidPhyPibInit(void){=//channel=//currentpageis = =31;//0x00~0xbfrepresent31~-32dBm =2; //carriersenseonly =266; //themaximumnumberofsymbols =10; //thedurationofthesyncheaderis 3,7,10,40 =2; //thenumberofsymbolsperoctet =BR_115200; }voidDelay1us(WORDEXT_DATAtime){{_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_();}}//功能:ThisfunctionsendsastrobecommandbyteoutoftheSPItothe//入口参数:CC2420//出口参数:CC2420statusregisterBYTECC2420Strobe(BYTEstrobe){BYTEEaSave;if(strobe>0x0F){strobe=}EaSave=GLOBAL_ISR_CTRL_REG(); = //forceNSSlow,startcc2420SPIF=0; //clearSPI0interrupttagSPI0DAT=strobe; //Waituntilstrobe= //readRecv'dSPIF= //clearinterrupt //forceNSShigh,stopcc2420return}//功能:ThisfunctionsendsaNOPcommandandreturnstheCC2420'sstatus//出口参数:CC2420statusregisterBYTECC2420StatusRead(void){BYTEBYTEEaSave=GLOBAL_ISR_CTRL_REG(); = //forceNSSlow,Csn=0,cc2420spiworkSPIF=0; //Waituntilvalue= //readRecv'dSPIF= //clearinterrupt //forceNSShighGLOBAL_ISR_CTRL_REG()=EaSave;return}//功能:ThisfunctionisusedtosetthevalueoftheCC2420//入口参数:add:CC2420registeraddress;value:registervaluetovoidCC2420SetReg(BYTEadd,WORDvalue){BYTEif((add>0x0F)&&(add<EaSave=GLOBAL_ISR_CTRL_REG(); = //forceNSSlow,startcc2420SPIF= //clearSPI0interruptSPI0DAT=add; while(!SPIF){;}SPIF=SPI0DAT=(BYTE)(value>>8); //Sendvaluehigh8bitswhile(!SPIF){;}ACC=SPIF=SPI0DAT=(BYTE)value; //Sendvaluelow8bitswhile(!SPIF){;}SPIF= //forceNSShigh,stopcc2420spiGLOBAL_ISR_CTRL_REG()=EaSave;}}//功能:ThisfunctionisusedtoreadthevalueoftheCC2420//入口参数:add:CC2420register//出口参数:theCC2420registerWORDCC2420GetReg(BYTEadd){BYTEWORDif((add>0x0F)&&(add<add|= //setbitEaSave=GLOBAL_ISR_CTRL_REG(); SPIF=SPI0DAT=add; while(!SPIF){;}SPIF=SPI0DAT=0x00; //Getregisterhigh8bitswhile(!SPIF){;}value=SPI0DAT<<8;SPIF=0;SPI0DAT=(BYTE)value; //Getregisterlow8bitswhile(!SPIF){;}SPIF=return}}//功能:Thisprocedurewillsetcurrenttransmitpowertoanewspecified//入口参数:chan:channelnumber;txPower:transmit//出口参数:stateToreporttheresultofsettinganewtransmitBYTEPhySetTxPower(BYTEphyPower){BYTEif(phyPower>PHY_POWER_LEV_MAX||phyPower<PHY_POWER_LEV_MIN){i=0;return}wRegVal=(rRegVal&0xFFE0)+phyPower;i=1;return}//功能:Thisprocedurewillsetcurrentchanneltoanewspecified//入口参数:phyChannelThenewchannel//出口参数:stateToreporttheresultofsettinganewBYTEPhySetCurrentChannel(BYTEphyChannel){BYTEif(phyChannel<PHY_24G_CHANNEL_MIN||phyChannel>i=return}//readcurrentchannel'sregister//calculatethenewchannel'sregistervalue,lock_thrnumberuse1wRegVal=357+5*(phyChannel-11)+0x40000;//checkifthecurrentchannelisthechannelwewanttosetif((rRegVal&0x3FF)==(wRegVal&0x3FF)){i=return}wRegVal=wRegVal+(rRegVal&0xFC00); //savetheoldregistervalue //turnofftheRFCC2420SetReg(CC2420_FSCTRL,wRegVal); //setthenewchannel'sregistervalue //turnontheRFandsettoRX_ONi=return}//功能:Thisisthe2420initialization//入口参数:chan:channelnumber;txPower:transmitvoidCC2420Init(BYTEchan,BYTEtxPower){ = //resetCCVREGEN= //enable //delayfor = // //turnonCC2420 //delaywhile((CC2420StatusRead()&0x40)== //waitforoscstable;} //flushcc2420'srxfifo //flushcc2420'stxfifo //SettingFIFOPto127bytesactivelow //ChangeSend //setcurrentchannelCC2420SetReg(CC2420_MDMCTRL0,0x02E2);//setCC2420,crcen,ccamodeis3,autoackdisable,addrrecognitiondisable*/ //-20(0xEA80-22)CC2420SetReg(CC2420_MDMCTRL1,0x0500);}//功能:ThisfunctioninitializethebufferinformationofPhytovoidPhyToMacBufInit(void){gPhyToMacBufMana.Buf=&gPhyToMacBuf[0][0];gPhyToMacBufMana.Len=gPhyToMacSonBufDataLen;gPhyToMacBufMana.MaxSize=PHY_TO_MAC_BUF_MAX_SIZE; =PHY_TO_MAC_BUF_MAX_NUM;gPhyToMacBufMana.WriteIndex=0;gPhyToMacBufMana.ReadIndex=0;gPhyToMacBufMana.Status=BUF_EMPTY;}//功能:ThisfunctionisusedtoretrivethedataaddressofRX//入口参数:bufManaMsgandBYTE*ReadGetBufData(tBufManaMsg*bufManaMsg,*len){if(bufManaMsg->Status!=BUF_EMPTY){if(len!=NULL){*len=bufManaMsg->Len[bufManaMsg-}}}

return(&bufManaMsg->Buf[bufManaMsg->ReadIndex*bufManaMsg-return//功能:ThisfunctionreleasesasondatavoidSonBufWriteRelease(tBufManaMsg*bufManaMsg){bufManaMsg->WriteIndex++;if(bufManaMsg->WriteIndex==bufManaMsg->MaxNum){bufManaMsg->WriteIndex=}>ReadIndex){bufManaMsg->Status=}}}

bufManaMsg->Status=BUF_NORMAL;bufManaMsg->Len[bufManaMsg->WriteIndex]=0;//功能:ThisfunctionreleasesasondatavoidSonBufReadRelease(tBufManaMsg*bufManaMsg){bufManaMsg->ReadIndex]=0;bufManaMsg->ReadIndex++;bufManaMsg->ReadIndex=}>WriteIndex){bufManaMsg->Status=}}}

bufManaMsg->Status=//功能:Thisfunctioncopyslength(len)bytesfromsource(src)buffertodestination(dest)//入口参数:dest:destinationbuffer sourcebuffer maximumbytestolen)reentrant{BYTEEXT_DATAif(len==}srcP=(BYTEEXT_DATA*)src;*destP++=}}//功能:Thisfunctionwritesabytetothe//入口参数:value:onebytevoidCC2420WriteFIFOByte(BYTEvalue){BYTEEaSave=GLOBAL_ISR_CTRL_REG();SPIF= //clearwhile(!SPIF){;}SPIF=while(!SPIF){;}ACC=SPI0DAT;SPIF=0;}//功能:ThisfunctionwritesanumberofbytestotheFIFOforRF//入口参数:*point:Txbufferdatapoint; :thedatalengthtoBOOLCC2420WriteFIFO(BYTE*pData,BYTElen){BYTEEaSave;if(len>128||len==0){return}EaSave=GLOBAL_ISR_CTRL_REG();SPIF= //clearwhile(!SPIF){;}while(len--SPIF=0;while(!SPIF){;}}SPIF=0;return}//功能:ThisfunctionisusedtogeteBufStateGetBufStatus(tBufManaMsg*bufManaMsg){return(bufManaMsg-}//功能:ThisfunctionisusedtogetbufferlengthBYTE*WriteGetBufLenAddr(tBufManaMsg*}//功能:Thisfunctionisusedtosendonebyte//入口参数:value:onebytevoidUartTxByte(BYTEvalue){BYTESfrSave=SFRPAGE;SBUF0=value;}//功能:ThisfunctionprocessreceivedappframefromvoidAppToMacDataProc(void){BYTEBYTE//getdatabufferpData=ReadGetBufData(&gAppToMacBufMana,&frmLen);if(pData==NULL){}}//功能:ThisfunctionprocessesthedataframefromPHYvoidPhyToMacDataProc(void) BYTEBYTEpData=ReadGetBufData(&gPhyToMacBufMana,&frmLen);if(pData==NULL)//nodataavailable{} *WriteGetBufLenAddr(&gMacToAppBufMana)=frmLen;}//功能:ThisfunctionsenddatatohigherlayerthroughvoidMacToAppDataProc(void){seDATA_IN_TRANSMIT:caseDEVICE_IDLE:BUF_EMPTY){=gUartTRxManaMsg.TxIndex=1;}case =0;gUartTRxManaMsg.TxIndex=0;gUartTRxManaMsg.TxDataFirstAddr=NULL;gUartTRxManaMsg.TxStatus=DEVICE_IDLE;}}//功能:Thisfunctionreadsaspecifiednumberofbytesfromthe//入口参数:*point:Rxbufferpoint; :datalengthtoBYTEif(len==0||len>127||FIFO==0){returnFALSE;}EaSave= SFRPAGE=SPI0_PAGE; //forceNSS =0; //clearinterruptflagSPI0DAT=CC2420_RXFIFO|0x40; //RXFIFOcommandwhile(!SPIF){;}while(len-- =0;while(!SPIF){ }SPIF= //forceNSSGLOBAL_ISR_CTRL_REG()=return}//功能:ThisfunctionbrushspecifiednumberofFIFO//入口参数:len:numberofBYTECC2420BrushFIFOBytes(BYTElen){BYTEif(len>127||FIFO==0){return}EaSave=GLOBAL_ISR_CTRL_REG();SFRPAGE= //forceNSS =0; //clearinterruptflag //RXFIFOwhile(!SPIF){;} =0;while(len--SPI0DAT=0x00;while(!SPIF){;} SPIF=0;} //forceNSSreturn}//功能:Thisfunctioniscalledtoreceiveonebytedatafrom//入口参数:value:onebyteBOOLSaveByteToBuf(tBufManaMsg*bufManaMsg,BYTE{BYTEdataIndex,BYTEbufMaxSize,if(bufManaMsg->Status==BUF_FULL){//CheckifthebufisfullreturnFALSE;} =bufManaMsg->WriteIndex; =bufManaMsg->Len[dataIndex];bufMaxSize=bufManaMsg->MaxSize; =bufManaMsg->MaxNum;bufManaMsg->Buf[dataIndex*bufMaxSize+dataLen]=value;bufManaMsg->Len[dataIndex]=if(dataLenbufMaxSize){if(bufManaMsg->WriteIndex>=bufMaxNum){bufManaMsg->WriteIndex=}>ReadIndex){bufManaMsg->Status=}}}

bufManaMsg->Status=BUF_NORMAL;return}//功能:Thisfunctionisusedtoretrivethedataaddressof//入口参数:bufManaMsgandBYTE*WriteGetBufDataUartIsr(tBufManaMsg*bufManaMsg,*len){if(bufManaMsg->Status!=*len=bufManaMsg->Len[bufManaMsg-}}}

return//功能:ThisfunctionsetstheUARTtimeouttime//入口参数:brBYTEsaveSfr;WORDoutTime;caseoutTime=caseoutTime=caseoutTime=caseoutTime=outTime=outTime=}saveSfr=SFRPAGE; =0; =1; //Enabletimer0interrupt =(BYTE)(outTime>>8); = =SFRPAGE=}//功能:ThisfunctionisusedtoretrivethedataaddressofBYTE*WriteGetBufDataAddr(tBufManaMsg*bufManaMsg){if(bufManaMsg->Status!=}}}

return//功能:Thisfunctioniscalledtoreceiveonebytedatafrom//入口参数:value:onebytestaticvoidUartIsrRx(BYTEvalue){}gUartTRxManaMsg.RxDataFirstAddr=WriteGetBufDataUartIsr(&gAppToMacBufMana,if(gUartT

温馨提示

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

评论

0/150

提交评论