高级会员
- 积分
- 800
- 金钱
- 800
- 注册时间
- 2011-11-18
- 在线时间
- 5 小时
|
楼主 |
发表于 2014-12-8 23:12:04
|
显示全部楼层
以下是我的一些调试的代码,我的一向是这样的:
<br>你的能用是因为你是将数组倒过来给NRF24的,2,3,4,5号地址NRF只接收一位8位的数据,无论你丢多少进去,它只认最后一个字。还有你可能用的是国产的芯片吧,不是原因装的那种。
<br>
<br>
<br>void TX_Mode(u8 CH,u8 RP_EN)
<br>{
<br> u8 rd,tx_dealy;
<br>
<br> rd=(TIM3->CNT/32); //将定时器的计算值拿出来除以32,因为定时器的上限是500 ,除以32刚好能分16份
<br> //N24RF刚好能分成16个延时值
<br> tx_dealy=rd; //将延时代号给tx_dealy
<br> tx_dealy<<=4; //然后将tx_dealy左移4位
<br> tx_dealy=tx_dealy+10; //后边加上重发次数 组成完整的 04号寄存器 SETUP_RETR 设置
<br>
<br>// Clear_line(19,22,10); //显示 tx_dealy 的值,实用中可和谐掉
<br>// Bit_show(19,22,tx_dealy);
<br> delay_ms(100);
<br> //以下正题
<br>
<br> NRF24L01_CE=0;
<br>
<br> NRF24L01_Write_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器
<br>
<br> NRF24L01_Write_Buf(WRITE_REG+TX_ADDR,(u8*)TX_ADDRESS,TX_ADR_WIDTH);//写TX节点地址
<br> NRF24L01_Write_Buf(WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS_0,RX_ADR_WIDTH); //设置TX节点地址,主要为了使能ACK
<br>
<br>
<br>
<br> NRF24L01_Write_Reg(WRITE_REG+EN_AA,RP_EN); //关通道0的自动应答
<br> NRF24L01_Write_Reg(WRITE_REG+EN_RXADDR,0x3f); //使能通道0的接收地址 //01
<br> NRF24L01_Write_Reg(WRITE_REG+SETUP_RETR,0x1a); //设置自动重发间隔时间:500us + 86us;最大自动重发次数:10次 //1a
<br> NRF24L01_Write_Reg(WRITE_REG+RF_CH,CH); //设置RF通道为40
<br> NRF24L01_Write_Reg(WRITE_REG+RF_SETUP,0x0f); //设置TX发射参数,0db增益,2Mbps,低噪声增益开启
<br> NRF24L01_Write_Reg(WRITE_REG+CONFIG,0x0e); //配置基本工作模式的参数WR_UP,EN_CRC,16BIT_CRC,接收模式,开启所有中断
<br>
<br> NRF24L01_CE=1;//CE为高,10us后启动发送
<br>}
<br>
<br>
<br>void RX_Mode(u8 CH,u8 RP_EN)
<br>{
<br> NRF24L01_CE=0;
<br>
<br> NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除TX FIFO寄存器
<br>
<br> NRF24L01_Write_Buf(WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS_0,RX_ADR_WIDTH);//写RX节点地址
<br> NRF24L01_Write_Buf(WRITE_REG+RX_ADDR_P1,(u8*)RX_ADDRESS_1,RX_ADR_WIDTH);//写RX节点地址
<br> NRF24L01_Write_Buf(WRITE_REG+RX_ADDR_P2,(u8*)RX_ADDRESS_2,1);//写RX节点地址
<br> NRF24L01_Write_Buf(WRITE_REG+RX_ADDR_P3,(u8*)RX_ADDRESS_3,1);//写RX节点地址
<br> NRF24L01_Write_Buf(WRITE_REG+RX_ADDR_P4,(u8*)RX_ADDRESS_4,1);//写RX节点地址
<br> NRF24L01_Write_Buf(WRITE_REG+RX_ADDR_P5,(u8*)RX_ADDRESS_5,1);//写RX节点地址
<br>
<br> NRF24L01_Write_Reg(WRITE_REG+EN_AA,RP_EN); //使能通道1-5的自动应答
<br> NRF24L01_Write_Reg(WRITE_REG+EN_RXADDR,0x3f); //使能通道0的接收地址
<br>
<br> NRF24L01_Write_Reg(WRITE_REG+RF_CH,CH); //设置RF通信频率
<br>
<br> NRF24L01_Write_Reg(WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度
<br> NRF24L01_Write_Reg(WRITE_REG+RX_PW_P1,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度
<br> NRF24L01_Write_Reg(WRITE_REG+RX_PW_P2,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度
<br> NRF24L01_Write_Reg(WRITE_REG+RX_PW_P3,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度
<br> NRF24L01_Write_Reg(WRITE_REG+RX_PW_P4,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度
<br> NRF24L01_Write_Reg(WRITE_REG+RX_PW_P5,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度
<br>
<br> NRF24L01_Write_Reg(WRITE_REG+RF_SETUP,0x0f);//设置TX发射参数,0db增益,2Mbps,低噪声增益开启
<br>
<br> NRF24L01_Write_Reg(WRITE_REG+CONFIG, 0x0f);//配置基本工作模式的参数WR_UP,EN_CRC,16BIT_CRC,接收模式
<br>
<br> NRF24L01_CE = 1; //CE为高,进入接收模式
<br>}
<br>
<br>
<br>//启动NRF24L01发送一次数据
<br>//txbuf:待发送数据首地址
<br>//返回值:发送完成状况
<br>
<br>u8 NRF24L01_sta;
<br>
<br>u8 NRF24L01_TxPacket(u8 *txbuf)
<br>{
<br> u8 sta;
<br> u16 i;
<br>
<br> NRF24L01_CE=0;
<br>
<br> NRF24L01_Write_Buf(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);//写数据到TX BUF 32个字节
<br>
<br> NRF24L01_CE=1; //启动发送
<br>
<br> i=0;
<br> while(NRF24L01_IRQ!=0) //等待发送完成
<br> {
<br> delay_ms(1);
<br> i=i+1;
<br> if(i>5000) return 0xff; //其他原因发送失败
<br> }
<br>
<br> delay_ms(1); //STM32太快了,最好这里要延迟1ms
<br>
<br> sta=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值
<br> NRF24L01_sta=sta;
<br>
<br>// Clear_line(17,0,30); //调试用
<br>// Bit_show(17,10,sta); //调试用
<br>
<br> NRF24L01_Write_Reg(WRITE_REG+STATUS,sta); //清除TX_DS或MAX_RT中断标志
<br>
<br>
<br> if(sta&MAX_TX)//达到最大重发次数
<br> {
<br> NRF24L01_Write_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器
<br> return MAX_TX;
<br> }
<br> if(sta&TX_OK)//发送完成
<br> {
<br> return TX_OK;
<br> }
<br> return 0xff;//其他原因发送失败
<br>}
<br>
<br>
<br>
<br>u8 NRF24L01_RxPacket(u8 *rxbuf)
<br>{
<br> u8 sta;
<br> u8 i;
<br>
<br> sta=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值//接收到数据
<br> NRF24L01_sta=sta;
<br>
<br> if(sta&RX_OK)//接收到数据
<br> {
<br> NRF24L01_Write_Reg(WRITE_REG+STATUS,sta); //清除TX_DS或MAX_RT中断标志
<br>
<br> NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据
<br> NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器
<br>
<br> for(i=0;i<32;i++)
<br> {
<br> NewAssignmentRxbuf=rxbuf;
<br> }
<br> return 0;
<br> }
<br>
<br> return 1; //没收到任何数据
<br>}
<br>void MyID_Init(void)
<br>{
<br> u32 temp0;
<br> B_COLOR=color(0,0,0); //清屏
<br> clear_lcd();
<br> linechar(1608,0,0, " \0");
<br>
<br> F_COLOR=color(30,60,30);
<br> B_COLOR=color(0,0,0);
<br>
<br>
<br> //取MCU的序列号后5位为地址码
<br> temp0=*(u32*)(0x1FFFF7F0);
<br> MyAdd[4] = (u8)(temp0 & 0x000000FF);
<br> MyAdd[3] = (u8)((temp0 & 0x0000FF00)>>8);
<br> MyAdd[2] = (u8)((temp0 & 0x00FF0000)>>16);
<br> MyAdd[1] = (u8)((temp0 & 0xFF000000)>>24);
<br> MyAdd[0] = 1; //因为通道1的高4BIT和通道0不一样,所以最底位0BIT是不能为0的!!
<br>
<br> linechar_1608t(1608,9,0, "My ID is:\0");
<br> count_show(1608,10,0, MyAdd[0]);
<br> count_show(1608,10,4, MyAdd[1]);
<br> count_show(1608,10,8, MyAdd[2]);
<br> count_show(1608,10,12, MyAdd[3]);
<br> count_show(1608,10,16, MyAdd[4]);
<br>
<br> printf("My ID0 is:%x\n",MyAdd[0]); //打印 ID
<br> printf("My ID1 is:%x\n",MyAdd[1]); //打印 ID
<br> printf("My ID2 is:%x\n",MyAdd[2]); //打印 ID
<br> printf("My ID3 is:%x\n",MyAdd[3]); //打印 ID
<br> printf("My ID4 is:%x\n",MyAdd[4]); //打印 ID
<br>
<br> delay_ms(1000);
<br>}
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:器件在FOX无线系统中的地址
<br>//*********************************************************************************************************
<br>
<br>void SetPublicCH(void)
<br>{
<br> TX_ADDRESS[0]=0x00;
<br> TX_ADDRESS[1]=0x00;
<br> TX_ADDRESS[2]=0x38;
<br> TX_ADDRESS[3]=0x47;
<br> TX_ADDRESS[4]=0x56;
<br>
<br> RX_ADDRESS_0[0]= 0x00;
<br> RX_ADDRESS_0[1]= 0x00;
<br> RX_ADDRESS_0[2]= 0x38;
<br> RX_ADDRESS_0[3]= 0x47;
<br> RX_ADDRESS_0[4]= 0x56;
<br>
<br> RX_ADDRESS_1[0]=MyAdd[0];
<br> RX_ADDRESS_1[1]=MyAdd[1];
<br> RX_ADDRESS_1[2]=MyAdd[2];
<br> RX_ADDRESS_1[3]=MyAdd[3];
<br> RX_ADDRESS_1[4]=MyAdd[4];
<br>
<br> RX_ADDRESS_2[0]=2;
<br> RX_ADDRESS_3[0]=3;
<br> RX_ADDRESS_4[0]=4;
<br> RX_ADDRESS_5[0]=5;
<br>}
<br>
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:建立初始化数据包 相当于自己的名片
<br>//*********************************************************************************************************
<br>
<br>void SetBuildBag(u8 DestinationCH,u8 SenderCH) //DestinationCH 目标频道 SenderCH 主发频道
<br>{
<br> //指令
<br> txbuf[0]=0;
<br> txbuf[1]=1;
<br> //目标地址
<br> txbuf[2]=0x00;
<br> txbuf[3]=0x00;
<br> txbuf[4]=0x38;
<br> txbuf[5]=0x47;
<br> txbuf[6]=0x56;
<br> //目标地址频道
<br> txbuf[7]=DestinationCH;
<br> //主发地址
<br> txbuf[8]=MyAdd[0];
<br> txbuf[9]=MyAdd[1];
<br> txbuf[10]=MyAdd[2];
<br> txbuf[11]=MyAdd[3];
<br> txbuf[12]=MyAdd[4];
<br> //主发频道
<br> txbuf[13]=SenderCH;
<br> //主发器件属性
<br> txbuf[14]=ProfessionId_H;
<br> txbuf[15]=ProfessionId_L;
<br>}
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:建立回送的的数据包 将收到的数据包装入自己任务专用的储存数给 ReplyBuidBagTXbuf【】
<br>//*********************************************************************************************************
<br>u8 ReplyBuidBagTXbuf[16];
<br>void SetReplyBuildBag(u8 DestinationCH,u8 SenderCH) //DestinationCH 目标频道 SenderCH 主发频道
<br>{
<br> u8 i;
<br> //指令
<br> ReplyBuidBagTXbuf[0]=0;
<br> ReplyBuidBagTXbuf[1]=2;
<br> //目标地址
<br> ReplyBuidBagTXbuf[2]=NewAssignmentRxbuf[8];
<br> ReplyBuidBagTXbuf[3]=NewAssignmentRxbuf[9];
<br> ReplyBuidBagTXbuf[4]=NewAssignmentRxbuf[10];
<br> ReplyBuidBagTXbuf[5]=NewAssignmentRxbuf[11];
<br> ReplyBuidBagTXbuf[6]=NewAssignmentRxbuf[12];
<br> //目标地址频道
<br> ReplyBuidBagTXbuf[7]=DestinationCH;
<br> //主发地址
<br> ReplyBuidBagTXbuf[8]=MyAdd[0];
<br> ReplyBuidBagTXbuf[9]=MyAdd[1];
<br> ReplyBuidBagTXbuf[10]=MyAdd[2];
<br> ReplyBuidBagTXbuf[11]=MyAdd[3];
<br> ReplyBuidBagTXbuf[12]=MyAdd[4];
<br> //主发频道
<br> ReplyBuidBagTXbuf[13]=SenderCH;
<br> //主发器件属性
<br> ReplyBuidBagTXbuf[14]=ProfessionId_H;
<br> ReplyBuidBagTXbuf[15]=ProfessionId_L;
<br>
<br> for(i=0;i<16;i++)
<br> {
<br> NewAssignmentRxbuf=0;
<br> }
<br>}
<br>
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:朋友表查找空位
<br>//*********************************************************************************************************
<br>u8 LookupMyFriendsListEmptyLocation()
<br>{
<br> u8 i;
<br> for(i=0;i<10;i++)
<br> { //MyfriendsList[n][0]~MyfriendsList[n][4] 为朋友地址 MyfriendsList[n][5] 为信道 MyfriendsList[n][6]~MyfriendsList[n][7]为职业
<br> if((MyFriendsList[0]==0)&(MyFriendsList[1]==0)&(MyFriendsList[2]==0)&(MyFriendsList[3]==0)&(MyFriendsList[4]==0)) //只要判断地址为空就行了
<br> {
<br> return i;
<br> }
<br> }
<br> return 0xff;
<br>}
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:从朋友表中查找有可能是子机的地址
<br>//*********************************************************************************************************
<br>u8 LookupMySonFormFriendsList(void) //从朋友表中查找有可能是子机的地址
<br>{
<br> u8 i;
<br> for(i=0;i<10;i++)
<br> {
<br> Clear_line(1,0,30);
<br> count_show(1608,1,0,MyFriendsList[0]);
<br> count_show(1608,1,4,MyFriendsList[1]);
<br> count_show(1608,1,8,MyFriendsList[2]);
<br> count_show(1608,1,12,MyFriendsList[3]);
<br> count_show(1608,1,16,MyFriendsList[4]);
<br> count_show(1608,1,20,MyFriendsList[5]);
<br>
<br> //MyfriendsList[n][0]~MyfriendsList[n][4] 为朋友地址 MyfriendsList[n][5] 为信道 MyfriendsList[n][6]~MyfriendsList[n][7]为职业
<br> if((MyFriendsList[6]==ProfessionId_H)&(MyFriendsList[7]!=0)&(MyFriendsList[5]==2)) //只要职业高位和本主机高位一样,子机不能为0而且信道是2 就一定是要绑定的子机
<br> {
<br> return i;
<br> }
<br> }
<br> linechar_1608t(1608,2,0, "I'm not find SonADD......\0");
<br> delay_ms(1000);
<br> delay_ms(1000);
<br> return 0xff;
<br>
<br>}
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:查找收到的地址是否有在朋友表里有相同的项
<br>//*********************************************************************************************************
<br>
<br>u8 LookupMyFriendsListSameItem() //有相同项返回1,没有相同项返回0
<br>{
<br> u8 i;
<br>
<br> if( (NewAssignmentRxbuf[8]==0) //避免收到的数全是0时导致和空表相同。
<br> &(NewAssignmentRxbuf[9]==0)
<br> &(NewAssignmentRxbuf[10]==0)
<br> &(NewAssignmentRxbuf[11]==0)
<br> &(NewAssignmentRxbuf[12]==0))
<br> {return 0;}
<br>
<br> for(i=0;i<10;i++)
<br> {
<br> if( (MyFriendsList[0]==NewAssignmentRxbuf[8])
<br> &(MyFriendsList[1]==NewAssignmentRxbuf[9])
<br> &(MyFriendsList[2]==NewAssignmentRxbuf[10])
<br> &(MyFriendsList[3]==NewAssignmentRxbuf[11])
<br> &(MyFriendsList[4]==NewAssignmentRxbuf[12])
<br> &(MyFriendsList[5]==NewAssignmentRxbuf[13])
<br> &(MyFriendsList[6]==NewAssignmentRxbuf[14])
<br> &(MyFriendsList[7]==NewAssignmentRxbuf[15]))
<br> {
<br> return i;
<br> }
<br> }
<br> return 0xff;
<br>}
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:查找朋友表里有没有相同的频道
<br>//*********************************************************************************************************
<br>u8 LookupFriendListSameCH(u8 CH) //如果找到相同的频道返回1,没有返回0.
<br>{
<br> u8 i;
<br>
<br> for(i=0;i<10;i++)
<br> {
<br> if(MyFriendsList[5]==CH) return 1;
<br> }
<br> return 0;
<br>}
<br>
<br>
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:新器件建立操作
<br>//*********************************************************************************************************
<br>u8 MyCH;
<br>
<br>void ImNewcomer(u8 WaitTime) //WaitTime 是收朋友回信而等待的时间,单位是秒。
<br>{
<br> u8 i,j;
<br> u8 NetAssignment_1,TX_sta;
<br> u8 TargetTime,AssignmentTimeFlag;
<br> u8 StalyTime,TimeFlag;
<br>
<br> NetAssignment_1=1;
<br> AssignmentTimeFlag=0;
<br> StalyTime=WaitTime; //需要延时的时间数
<br>
<br> if(NetAssignment_1==1)
<br> {
<br> B_COLOR=color(0,0,0); //清屏 //text
<br> clear_lcd(); //text
<br> F_COLOR=color(30,60,30);
<br>
<br> SetPublicCH(); //建立新器件TX地址的设置
<br> MyCH=PublicCH; //将我的地地址设为公共频道
<br>
<br> for(j=1;j<21;j++) //从第1信号通道到20信号通道逐个广播
<br> {
<br> SetBuildBag(j,MyCH); //建立找朋友的数据包
<br>
<br> TX_Mode(j,0x3e); //0x3e=无自动应答,公共频道广播不要自动应答是为了突然信道拥挤。
<br> TXRXFlag=1;
<br>
<br> Clear_line(19,0,3); //text
<br> count_show(1608,19,0,j); //text
<br>
<br> TX_sta=0;
<br> for(i=0;i<10;i++) //每个信道广播10次
<br> {
<br> TX_sta=NRF24L01_TxPacket(txbuf);
<br>
<br> if(TX_sta==MAX_TX) {F_COLOR=color(0,0,0); linechar_1608t(1608,19,8, "MAX_TX......\0"); F_COLOR=color(30,60,30); linechar_1608t(1608,19,8, "MAX_TX......\0");} //text
<br> if(TX_sta==TX_OK) {F_COLOR=color(0,0,0); linechar_1608t(1608,19,8, "TX_OK......\0"); F_COLOR=color(30,60,30); linechar_1608t(1608,19,8, "TX_OK......\0");} //text
<br> if(TX_sta==0xff) {F_COLOR=color(0,0,0); linechar_1608t(1608,19,8, "TX_ERROR......\0"); F_COLOR=color(30,60,30); linechar_1608t(1608,19,8, "TX_OK......\0");} //text
<br> }
<br> Clear_line(19,0,30); //text
<br> }
<br>
<br> NetAssignment_1=2;
<br> }
<br>
<br> if(NetAssignment_1==2)
<br> {
<br> MyCH=PublicCH; //用广播发送完名片后,将自己切换到共公信道
<br> SetPublicCH(); //设置自己的地址
<br>
<br> RX_Mode(MyCH,0x3f); //0x3f=自动应答 切换回接收状态
<br> TXRXFlag=0;
<br>
<br> NetAssignment_1=3;
<br> }
<br>
<br> while(NetAssignment_1==3) //取得延时的开头时间 并延时十秒
<br> {
<br> RTC_Get();
<br> if(AssignmentTimeFlag==0)//时间延时操作第一步 //取得延时时间的开头数值,并取得定时后的数值 AssignmentTemp00到点数
<br> {
<br> if((timer.sec)+StalyTime>=60) {TimeFlag=0; TargetTime=StalyTime-(60-timer.sec);} //当时间的秒读数加上延时时间大于或等于60后(读数是0~59)时标记为TimeFlag=0)
<br> else {TargetTime=timer.sec+StalyTime; TimeFlag=1;} //当时间的秒读数加上延时时间小于60后(读数是0~59)时标记为TimeFlag=1)
<br>
<br> AssignmentTimeFlag=1;
<br> }
<br>
<br> if(AssignmentTimeFlag==1)//时间延时操作第二步
<br> {
<br> count_show(1608,11,0,TargetTime);
<br> Clear_line(11,3,3);count_show(1608,11,3,timer.sec);
<br>
<br> if(TimeFlag==0)
<br> {
<br> if (timer.sec==TargetTime) {return;} //返回
<br> }
<br> if(TimeFlag==1)
<br> {
<br> if(timer.sec==TargetTime) {return;} //返回
<br> }
<br> }
<br>
<br> if(!NRF24L01_RxPacket(rxbuf)) //如果收到有数据包
<br> {
<br> delay_ms(1000);
<br> B_COLOR=color(0,0,0); //清屏 //text
<br> clear_lcd(); //text
<br> Clear_line(19,0,30); linechar_1608t(1608,19,8, "RX_OK......\0");
<br>
<br> linechar_1608t(1608,4,0, "I get this message:\0");
<br> //显示指令
<br> linechar_1608t(1608,5,0, "Instruct:\0");
<br> count_show(1608,5,10,NewAssignmentRxbuf[0]); //这里返回的指令应该是 02
<br> count_show(1608,5,14,NewAssignmentRxbuf[1]);
<br> //显示目标地址
<br> linechar_1608t(1608,6,0, "Destination:\0");
<br> count_show(1608,7,0,NewAssignmentRxbuf[2]);
<br> count_show(1608,7,4,NewAssignmentRxbuf[3]);
<br> count_show(1608,7,8,NewAssignmentRxbuf[4]);
<br> count_show(1608,7,12,NewAssignmentRxbuf[5]);
<br> count_show(1608,7,16,NewAssignmentRxbuf[6]);
<br> //显示目标频道
<br> linechar_1608t(1608,7,19, "CH:\0");
<br> count_show(1608,7,24,NewAssignmentRxbuf[7]);
<br> //显示主发地址
<br> linechar_1608t(1608,8,0, "Sender:\0");
<br> count_show(1608,9,0,NewAssignmentRxbuf[8]);
<br> count_show(1608,9,4,NewAssignmentRxbuf[9]);
<br> count_show(1608,9,8,NewAssignmentRxbuf[10]);
<br> count_show(1608,9,12,NewAssignmentRxbuf[11]);
<br> count_show(1608,9,16,NewAssignmentRxbuf[12]);
<br> //显示主发频道
<br> linechar_1608t(1608,9,19, "CH:\0");
<br> count_show(1608,9,24,NewAssignmentRxbuf[13]);
<br>
<br> //显示主发器件属性
<br> linechar_1608t(1608,10,0, "orfession ID:\0");
<br> count_show(1608,10,16,NewAssignmentRxbuf[14]);
<br> count_show(1608,10,20,NewAssignmentRxbuf[15]);
<br>
<br> if((NewAssignmentRxbuf[0]==0)&(NewAssignmentRxbuf[1]==2)) //如果收到的是返回指令 02
<br> {
<br> if(LookupMyFriendsListSameItem()==0xff) //从地址表中找出是否有同相的朋友地址在里边,如果有就不做压放朋友地址表操作 LookupMyFriendsListSameItem()返回0表示没有相同项,返回1表示有相同项
<br> {
<br> i=LookupMyFriendsListEmptyLocation(); //从地址表(10个空位)中找到空位
<br> if(i<10)
<br> {
<br> //压入朋友地址表中
<br> MyFriendsList[0]=NewAssignmentRxbuf[8]; //朋友地址
<br> MyFriendsList[1]=NewAssignmentRxbuf[9]; //朋友地址
<br> MyFriendsList[2]=NewAssignmentRxbuf[10]; //朋友地址
<br> MyFriendsList[3]=NewAssignmentRxbuf[11]; //朋友地址
<br> MyFriendsList[4]=NewAssignmentRxbuf[12]; //朋友地址
<br> MyFriendsList[5]=NewAssignmentRxbuf[13]; //朋友所在频道
<br> MyFriendsList[6]=NewAssignmentRxbuf[14]; //朋友从事职业
<br> MyFriendsList[7]=NewAssignmentRxbuf[15]; //朋友从事职业
<br>
<br> linechar_1608t(1608,13,0, "Sender:\0");
<br> count_show(1608,14,0,MyFriendsList[0]);
<br> count_show(1608,14,4,MyFriendsList[1]);
<br> count_show(1608,14,8,MyFriendsList[2]);
<br> count_show(1608,14,12,MyFriendsList[3]);
<br> count_show(1608,14,16,MyFriendsList[4]);
<br> //显示主发频道
<br> linechar_1608t(1608,14,19, "CH:\0");
<br> count_show(1608,14,24,MyFriendsList[5]);
<br>
<br> //显示主发器件属性
<br> linechar_1608t(1608,15,0, "orfession ID:\0");
<br> count_show(1608,15,16,MyFriendsList[6]);
<br> count_show(1608,15,20,MyFriendsList[7]);
<br>
<br> linechar_1608t(1608,16,0, "FriendList location:\0");
<br> count_show(1608,16,21,i);
<br> }
<br> else {linechar_1608t(1608,16,0, "FriendList is full!!\0");}
<br> }
<br> else {Clear_line(19,16,0); linechar_1608t(1608,16,0, "I have same item.\0"); count_show(1608,16,18,LookupMyFriendsListSameItem());}
<br> }
<br> }
<br> else {Clear_line(19,0,30); linechar_1608t(1608,19,8, "Nothing......\0");}//否则显示没有收到任何数据包
<br> }
<br>
<br>}
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:主机建立领地操作
<br>//*********************************************************************************************************
<br>u8 ThisIsMyTerritory(void) //主机建立领地操作
<br>{
<br> u16 i,j,k;
<br>
<br> ImNewcomer(10); //先找一次朋友
<br> if((MyFriendsList[0][0]==0) //先找一次朋友,如果第一次一个都没有,尽人事,再找一次。
<br> &(MyFriendsList[0][1]==0)
<br> &(MyFriendsList[0][2]==0)
<br> &(MyFriendsList[0][3]==0)
<br> &(MyFriendsList[0][4]==0)
<br> )
<br> {ImNewcomer(10);}
<br>
<br> B_COLOR=color(0,0,0); //清屏
<br> clear_lcd();
<br> j=0;
<br>
<br> if(ProfessionId_L==0) //进入创建领地操作 先看看自己是否主机
<br> {
<br> k=0;
<br> while(k==0)
<br> {
<br> i=(TIM3->CNT/25); //取一随机数(1~100以内)
<br> linechar_1608t(1608,j,0, "TIM3->CNT=\0"); //text
<br> count_show(1608,j,12,i); //text
<br> j=j+1;
<br>
<br> if((i>=3))
<br> {
<br> if(LookupFriendListSameCH(i)==0) k=1; //查找朋友表中有没有相同的频道
<br> }
<br> } //如果没有,进入建立自己的频道
<br> linechar_1608t(1608,j,0, "My Territory is:\0"); //text
<br> count_show(1608,j,18,i); //text
<br> j=j+1;
<br>
<br> MyCH=i;
<br> delay_ms(1000);
<br> delay_ms(1000);
<br> delay_ms(1000);
<br> return 1;
<br> }
<br> else
<br> {
<br> MyCH=2;
<br> linechar_1608t(1608,2,0, "I need looking for my papa\0"); //text
<br> delay_ms(1000);
<br> delay_ms(1000);
<br> delay_ms(1000);
<br> return 0;
<br> }
<br>}
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:数据发送服务代码
<br>//*********************************************************************************************************
<br>
<br>u8 TXAssignment(u8 AssignmenNo) //数据发送服务代码 AssignmenNo=1:建立包返回任务
<br>{
<br> u8 TX_sta;
<br> u8 SonADD;
<br>
<br> RX_Mode(MyCH,0x3e);
<br> delay_ms(10);
<br> RX_Mode(MyCH,0x3e);
<br> delay_ms(10);
<br> TXRXFlag=0;
<br>
<br> if(AssignmenNo==1)
<br> { //指令
<br> txbuf[0]=ReplyBuidBagTXbuf[0];
<br> txbuf[1]=ReplyBuidBagTXbuf[1];
<br> //目标地址
<br> txbuf[2]=ReplyBuidBagTXbuf[2];
<br> txbuf[3]=ReplyBuidBagTXbuf[3];
<br> txbuf[4]=ReplyBuidBagTXbuf[4];
<br> txbuf[5]=ReplyBuidBagTXbuf[5];
<br> txbuf[6]=ReplyBuidBagTXbuf[6];
<br> //目标地址频道
<br> txbuf[7]=ReplyBuidBagTXbuf[7];
<br> //主发地址
<br> txbuf[8]=ReplyBuidBagTXbuf[8];
<br> txbuf[9]=ReplyBuidBagTXbuf[9];
<br> txbuf[10]=ReplyBuidBagTXbuf[10];
<br> txbuf[11]=ReplyBuidBagTXbuf[11];
<br> txbuf[12]=ReplyBuidBagTXbuf[12];
<br> //主发频道
<br> txbuf[13]=ReplyBuidBagTXbuf[13];
<br> //主发器件属性
<br> txbuf[14]=ReplyBuidBagTXbuf[14];
<br> txbuf[15]=ReplyBuidBagTXbuf[15];
<br>
<br> TX_ADDRESS[0]=ReplyBuidBagTXbuf[2]; //发回去的地址
<br> TX_ADDRESS[1]=ReplyBuidBagTXbuf[3]; //发回去的地址
<br> TX_ADDRESS[2]=ReplyBuidBagTXbuf[4]; //发回去的地址
<br> TX_ADDRESS[3]=ReplyBuidBagTXbuf[5]; //发回去的地址
<br> TX_ADDRESS[4]=ReplyBuidBagTXbuf[6]; //发回去的地址
<br>
<br> RX_ADDRESS_0[0]= ReplyBuidBagTXbuf[2]; //应答回复的地址
<br> RX_ADDRESS_0[1]= ReplyBuidBagTXbuf[3]; //应答回复的地址
<br> RX_ADDRESS_0[2]= ReplyBuidBagTXbuf[4]; //应答回复的地址
<br> RX_ADDRESS_0[3]= ReplyBuidBagTXbuf[5]; //应答回复的地址
<br> RX_ADDRESS_0[4]= ReplyBuidBagTXbuf[6]; //应答回复的地址
<br>
<br> RX_ADDRESS_1[0]=MyAdd[0]; //自己的地址 频道1
<br> RX_ADDRESS_1[1]=MyAdd[1]; //自己的地址 频道1
<br> RX_ADDRESS_1[2]=MyAdd[2]; //自己的地址 频道1
<br> RX_ADDRESS_1[3]=MyAdd[3]; //自己的地址 频道1
<br> RX_ADDRESS_1[4]=MyAdd[4]; //自己的地址 频道1
<br>
<br> RX_ADDRESS_2[0]=2; //自己的地址 频道2
<br> RX_ADDRESS_3[0]=3; //自己的地址 频道3
<br> RX_ADDRESS_4[0]=4; //自己的地址 频道4
<br> RX_ADDRESS_5[0]=5; //自己的地址 频道5
<br>
<br> }
<br>
<br> if(AssignmenNo==2) //在朋友表中找到职业高业相同底位不等于0而且在信道2的子机地址,并设置发送包
<br> {
<br> if(LookupMySonFormFriendsList()==0xff) return 0;
<br> //指令
<br> txbuf[0]=0;
<br> txbuf[1]=3;
<br> //目标地址
<br> SonADD=LookupMySonFormFriendsList();
<br>
<br> txbuf[2]=MyFriendsList[SonADD][0];
<br> txbuf[3]=MyFriendsList[SonADD][1];
<br> txbuf[4]=MyFriendsList[SonADD][2];
<br> txbuf[5]=MyFriendsList[SonADD][3];
<br> txbuf[6]=MyFriendsList[SonADD][4];
<br> //目标地址频道
<br> txbuf[7]=2; //待绑定子机一定在信道2
<br> //主发地址
<br> txbuf[8]=MyAdd[0];
<br> txbuf[9]=MyAdd[1];
<br> txbuf[10]=MyAdd[2];
<br> txbuf[11]=MyAdd[3];
<br> txbuf[12]=MyAdd[4];
<br> //主发频道
<br> txbuf[13]=MyCH;
<br> //主发器件属性
<br> txbuf[14]=ProfessionId_H;
<br> txbuf[15]=ProfessionId_L;
<br>
<br> TX_ADDRESS[0]=MyFriendsList[SonADD][0]; //发回去的地址
<br> TX_ADDRESS[1]=MyFriendsList[SonADD][1]; //发回去的地址
<br> TX_ADDRESS[2]=MyFriendsList[SonADD][2]; //发回去的地址
<br> TX_ADDRESS[3]=MyFriendsList[SonADD][3]; //发回去的地址
<br> TX_ADDRESS[4]=MyFriendsList[SonADD][4]; //发回去的地址
<br>
<br> RX_ADDRESS_0[0]= MyFriendsList[SonADD][0]; //应答回复的地址
<br> RX_ADDRESS_0[1]= MyFriendsList[SonADD][1]; //应答回复的地址
<br> RX_ADDRESS_0[2]= MyFriendsList[SonADD][2]; //应答回复的地址
<br> RX_ADDRESS_0[3]= MyFriendsList[SonADD][3]; //应答回复的地址
<br> RX_ADDRESS_0[4]= MyFriendsList[SonADD][4]; //应答回复的地址
<br>
<br> RX_ADDRESS_1[0]=MyAdd[0]; //自己的地址 频道1
<br> RX_ADDRESS_1[1]=MyAdd[1]; //自己的地址 频道1
<br> RX_ADDRESS_1[2]=MyAdd[2]; //自己的地址 频道1
<br> RX_ADDRESS_1[3]=MyAdd[3]; //自己的地址 频道1
<br> RX_ADDRESS_1[4]=MyAdd[4]; //自己的地址 频道1
<br>
<br> RX_ADDRESS_2[0]=2; //自己的地址 频道2
<br> RX_ADDRESS_3[0]=3; //自己的地址 频道3
<br> RX_ADDRESS_4[0]=4; //自己的地址 频道4
<br> RX_ADDRESS_5[0]=5; //自己的地址 频道5
<br>
<br> }
<br>
<br> Clear_line(5,0,30);
<br> Clear_line(6,0,30);
<br> Clear_line(7,0,30);
<br> Clear_line(8,0,30);
<br> Clear_line(9,0,30);
<br>
<br> linechar_1608t(1608,5,0, "I will reply this message:\0");
<br> //显示指令
<br> count_show(1608,6,0,txbuf[0]);
<br> count_show(1608,6,4,txbuf[1]);
<br> //显示目标地址
<br> count_show(1608,7,0,txbuf[2]);
<br> count_show(1608,7,4,txbuf[3]);
<br> count_show(1608,7,8,txbuf[4]);
<br> count_show(1608,7,12,txbuf[5]);
<br> count_show(1608,7,16,txbuf[6]);
<br> //显示目标频道
<br> count_show(1608,7,24,txbuf[7]);
<br> //显示主发地址
<br> count_show(1608,8,0,txbuf[8]);
<br> count_show(1608,8,4,txbuf[9]);
<br> count_show(1608,8,8,txbuf[10]);
<br> count_show(1608,8,12,txbuf[11]);
<br> count_show(1608,8,16,txbuf[12]);
<br> //显示主发频道
<br> count_show(1608,8,24,txbuf[13]);
<br> //显示主发器件属性
<br> count_show(1608,9,0,txbuf[14]);
<br> count_show(1608,9,4,txbuf[15]);
<br>
<br> TX_Mode(PublicCH,0x3f);
<br> TXRXFlag=1;
<br>
<br> TX_sta=0;
<br> TX_sta=NRF24L01_TxPacket(txbuf);
<br>
<br> if(TX_sta==MAX_TX) {Clear_line(19,0,30); linechar_1608t(1608,19,8, "MAX_TX......\0"); delay_ms(1000); return 0;} //text
<br> if(TX_sta==0xff) {Clear_line(19,0,30); linechar_1608t(1608,19,8, "TX_ERROR......\0"); delay_ms(1000); return 0;} //text
<br>
<br> if(TX_sta==TX_OK)
<br> {
<br> Clear_line(19,0,30); linechar_1608t(1608,19,8, "TX_OK......\0"); delay_ms(1000);//text
<br> SetPublicCH();
<br> RX_Mode(MyCH,0x3e);
<br> TXRXFlag=0;
<br>
<br> return 1;
<br> }
<br> return 0;
<br>}
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:新朋友数据包回应操作
<br>//
<br>// 1:先看朋友表里有没有收到的地址,如果有就压入朋友表
<br>// 2:随机延时 0-500ms
<br>// 3:将收到的数据包装入本任务专有的数组 ReplyBuidBagTXbuf【】
<br>//
<br>//*********************************************************************************************************
<br>u8 IGetANewFriend(void) //回应成功返回1,不成功返回0。
<br>{
<br> u16 ReplyDelayTime;
<br> u8 i;
<br>
<br> if(LookupMyFriendsListSameItem()==0xff) //从地址表中找出是否有同相的朋友地址在里边,如果有就不做压放朋友地址表操作
<br> { //LookupMyFriendsListSameItem()返回的数表示在列表中的相同地址位置,返回255表示没有相同项
<br>
<br> i=LookupMyFriendsListEmptyLocation(); //从地址表(10个空位)中找到空位
<br> if(i<10)
<br> {
<br> //压入朋友地址表中
<br> MyFriendsList[0]=NewAssignmentRxbuf[8]; //朋友地址
<br> MyFriendsList[1]=NewAssignmentRxbuf[9]; //朋友地址
<br> MyFriendsList[2]=NewAssignmentRxbuf[10]; //朋友地址
<br> MyFriendsList[3]=NewAssignmentRxbuf[11]; //朋友地址
<br> MyFriendsList[4]=NewAssignmentRxbuf[12]; //朋友地址
<br> MyFriendsList[5]=NewAssignmentRxbuf[13]; //朋友所在频道
<br> MyFriendsList[6]=NewAssignmentRxbuf[14]; //朋友从事职业
<br> MyFriendsList[7]=NewAssignmentRxbuf[15]; //朋友从事职业
<br>
<br> linechar_1608t(1608,13,0, "Sender:\0");
<br> count_show(1608,14,0,MyFriendsList[0]);
<br> count_show(1608,14,4,MyFriendsList[1]);
<br> count_show(1608,14,8,MyFriendsList[2]);
<br> count_show(1608,14,12,MyFriendsList[3]);
<br> count_show(1608,14,16,MyFriendsList[4]);
<br> //显示主发频道
<br> linechar_1608t(1608,14,19, "CH:\0");
<br> count_show(1608,14,24,MyFriendsList[5]);
<br>
<br> //显示主发器件属性
<br> linechar_1608t(1608,15,0, "orfession ID:\0");
<br> count_show(1608,15,16,MyFriendsList[6]);
<br> count_show(1608,15,20,MyFriendsList[7]);
<br>
<br> linechar_1608t(1608,16,0, "FriendList location:\0");
<br> count_show(1608,16,21,i);
<br> }
<br> else {linechar_1608t(1608,16,0, "FriendList is full!!\0");}
<br>
<br> }
<br> else {Clear_line(19,16,0); linechar_1608t(1608,16,0, "I have same item.\0"); count_show(1608,16,18,LookupMyFriendsListSameItem());}
<br>
<br> linechar_1608t(1608,0,0, "I get this message:\0");
<br> //显示指令
<br> count_show(1608,1,0,NewAssignmentRxbuf[0]);
<br> count_show(1608,1,4,NewAssignmentRxbuf[1]);
<br> //显示目标地址
<br> count_show(1608,2,0,NewAssignmentRxbuf[2]);
<br> count_show(1608,2,4,NewAssignmentRxbuf[3]);
<br> count_show(1608,2,8,NewAssignmentRxbuf[4]);
<br> count_show(1608,2,12,NewAssignmentRxbuf[5]);
<br> count_show(1608,2,16,NewAssignmentRxbuf[6]);
<br> //显示目标频道
<br> count_show(1608,2,24,NewAssignmentRxbuf[7]);
<br> //显示主发地址
<br> count_show(1608,3,0,NewAssignmentRxbuf[8]);
<br> count_show(1608,3,4,NewAssignmentRxbuf[9]);
<br> count_show(1608,3,8,NewAssignmentRxbuf[10]);
<br> count_show(1608,3,12,NewAssignmentRxbuf[11]);
<br> count_show(1608,3,16,NewAssignmentRxbuf[12]);
<br> //显示主发频道
<br> count_show(1608,3,24,NewAssignmentRxbuf[13]);
<br> //显示主发器件属性
<br> count_show(1608,4,0,NewAssignmentRxbuf[14]);
<br> count_show(1608,4,4,NewAssignmentRxbuf[15]);
<br>
<br>
<br> //***********************************************************************************************************随机延时 0-500ms
<br> ReplyDelayTime=(TIM3->CNT); //取随机数,定时器的上限是500.
<br> if(ReplyDelayTime!=0) {delay_ms(ReplyDelayTime); count_show(1608,0,24,ReplyDelayTime);}
<br> //***********************************************************************************************************
<br>
<br> SetReplyBuildBag(PublicCH,MyCH);
<br>
<br> return 1;
<br>}
<br>
<br>//*********************************************************************************************************
<br>// 程式功能:子机绑定操作
<br>//
<br>// 1:
<br>// 2:
<br>// 3:
<br>//
<br>//*********************************************************************************************************
<br>
<br>u8 WaitingForMyPapa(void)
<br>{
<br> while (1)
<br> {
<br> if(TXRXFlag==1)
<br> {
<br> MyCH=PublicCH; //将自己切换到共公信道
<br> SetPublicCH(); //设置自己的地址
<br> RX_Mode(MyCH,0x3f); //0x3f=自动应答 切换回接收状态
<br> delay_ms(10);
<br> linechar_1608t(1608,3,8, "Set RX_Mode......\0");
<br> }
<br> linechar_1608t(1608,3,8, "RX_Mode......\0");
<br>
<br> if(!NRF24L01_RxPacket(rxbuf))
<br> {
<br> F_COLOR=color(0,0,0); linechar_1608t(1608,19,8, "RX_OK......\0");
<br> F_COLOR=color(30,60,30); linechar_1608t(1608,19,8, "RX_OK......\0");
<br>
<br> linechar_1608t(1608,4,0, "I get this message:\0");
<br> //显示指令
<br> linechar_1608t(1608,5,0, "Instruct:\0");
<br> count_show(1608,5,10,NewAssignmentRxbuf[0]); //这里返回的指令应该是 02
<br> count_show(1608,5,14,NewAssignmentRxbuf[1]);
<br> //显示目标地址
<br> linechar_1608t(1608,6,0, "Destination:\0");
<br> count_show(1608,7,0,NewAssignmentRxbuf[2]);
<br> count_show(1608,7,4,NewAssignmentRxbuf[3]);
<br> count_show(1608,7,8,NewAssignmentRxbuf[4]);
<br> count_show(1608,7,12,NewAssignmentRxbuf[5]);
<br> count_show(1608,7,16,NewAssignmentRxbuf[6]);
<br> //显示目标频道
<br> linechar_1608t(1608,7,19, "CH:\0");
<br> count_show(1608,7,24,NewAssignmentRxbuf[7]);
<br> //显示主发地址
<br> linechar_1608t(1608,8,0, "Sender:\0");
<br> count_show(1608,9,0,NewAssignmentRxbuf[8]);
<br> count_show(1608,9,4,NewAssignmentRxbuf[9]);
<br> count_show(1608,9,8,NewAssignmentRxbuf[10]);
<br> count_show(1608,9,12,NewAssignmentRxbuf[11]);
<br> count_show(1608,9,16,NewAssignmentRxbuf[12]);
<br> //显示主发频道
<br> linechar_1608t(1608,9,19, "CH:\0");
<br> count_show(1608,9,24,NewAssignmentRxbuf[13]);
<br>
<br> //显示主发器件属性
<br> linechar_1608t(1608,10,0, "orfession ID:\0");
<br> count_show(1608,10,16,NewAssignmentRxbuf[14]);
<br> count_show(1608,10,20,NewAssignmentRxbuf[15]);
<br>
<br>
<br> F_COLOR=color(30,60,30);
<br> linechar_1608t(1608,11,0, "I finded my papa......\0");
<br> linechar_1608t(1608,12,0, "My Territory is:\0");
<br> count_show(1608,12,18,NewAssignmentRxbuf[13]);
<br> }
<br> else
<br> {
<br> F_COLOR=color(0,0,0); linechar_1608t(1608,19,8, "Nothing......\0");
<br> F_COLOR=color(30,60,30); linechar_1608t(1608,19,8, "Nothing......\0");
<br> }
<br> //开包************************************************************************************************
<br> //任务1
<br> if((NewAssignmentRxbuf[0]==0)&(NewAssignmentRxbuf[1]==3)) //如果指令是03(主机向子机反来的绑定指令)
<br> {
<br>
<br>
<br> MyCH=NewAssignmentRxbuf[13];
<br>
<br> delay_ms(1000);
<br> delay_ms(1000);
<br> delay_ms(1000);
<br> return 1;
<br> }
<br> }
<br>} |
|