⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 89c52ctr.lst

📁 使用51单片机实现TCP/IP协议,模块化编程
💻 LST
📖 第 1 页 / 共 2 页
字号:
 178              if(LastSendStartPage==SEND_START_PAGE0)
 179          	 {
 180          	 StartPage=SEND_START_PAGE1;
 181              LastSendStartPage==SEND_START_PAGE1;
 182          	 }
 183          	 else
 184          	 {
 185          	    StartPage==SEND_START_PAGE0;
 186                 LastSendStartPage==SEND_START_PAGE0;
 187          	 }
 188              RTLWriteRam(((word)StartPage)<<8,size,buffer);
 189          	                   //等待上一次传输结束
 190             while ((ReadReg(CR)&CR_TXP)==CR_TXP);
 191          	           //写传输起始页和大小
 192             RTLPage(0);
 193          	WriteReg(TPSR_WPAGE0,StartPage);   //TPSR
 194          	WriteReg(TBCRL_WPAGE0,(byte)size);   //LOW
 195          	WriteReg(TBCRH_WPAGE0,(byte)((size>>8)&0XFF));   //HIGH
 196          	WriteReg(CR,((PrePage&0xc0)|CR_ABORT_COMPLETE|CR_TXP|CR_START_COMMAND);
 197          	return TRUE;
 198          }
 199          
 200          
 201          
 202          void RTLReceivePacket()
 203          {
 204               byte curr,bnry;
 205          	  word adrress;
 206          	  word PacketSize;
 207          	  byte MemPage;
 208          	  struct MemHeader xdata *pMemHead;
 209          
 210          	  RTLPage(1);
 211          	  curr=ReadReg(CURR_RPAGE1);
 212          	  RTLPage(0);
 213                                   //在接收缓存中读所有包 
 214          	  while(TRUE)
 215          	  {            //检测起始页是否未知吃错误
 216          	       if(StartPageOfPacket>=RECEIVE_STOP_PAGE||StartPageOfPacket<RECEIVE_START_PAGE)
 217          			 {         //用curr作为startpageofpacket
 218          			    StartPageOfPacket=curr;
 219          				 break;
 220          			 }
 221          			           //检测是否有包读到
 222                     if(StartPageOfPacket==curr)
 223          			    break;
 224          				 //读一个包
 225          
 226          				 //读包头信息
 227          			address=((wodr)StartPageOfPacket)<<8;
 228          			RTLReadRam(address,4,Head);
 229          
 230          			    //校验rsr
 231          			if(Head[0]&RSR_RECEIVE_NO_ERRO)
 232          			 {
 233          			                //好包
 234          								 //得到mac校验和
 235                         PacketSize=((word)Head[3])*256+Head[2]-4;
 236          					         //分配buffer,读包到buffer
 237          				MemPage=MemAllocation(PacketSize);
 238          				if(MemPage!=PAGE_NOT_FOUND)
C51 COMPILER V6.12  89C52CTR                                                               05/16/2008 11:16:47 PAGE 5   

 239          				{
 240          					  pMemHead=(struct MemHeader xdata*)MemPageToPoint(MemPage);
 241                           pMemHead->StartPos=(byte xdata *)pMemHead+sizeof(struct MemHeader);
 242          					  pMemHead->StopPos=pMemhead->StopPos+PacketSize;
 243          					                 //停止pos
 244                                address+=4;
 245                                if(StartPageOfPacket>Head[1]&&Head[1]!=RECEIVE_START_PAGE)
 246                                 {
 247                                   RTLReadRam(address,(((word)RECEIVE_STOP_PAGE)<<8)-address,pMemHead->StartPos);
 248                                                    //read rtl
 249                                   RTLReadRam(((word)RECEIVVE_START_PAGE)<<8,PacketSize-((((word)RECEIVE_STOP_PAGE)
 250                                        <<8)-address),pMemHead->StartPos+((((word)RECEIVE_STOP_PAGE)<<8)-address));
 251                                 }
 252                                else
 253                                 {
 254                                   RTLReadRam(address,PacketSize,pMemHead->StartPos);//read rtl 
 255                                 }
 256                                if(WriteQueue(MemPage,&QueueNetPacketIn)==PAGE_NOT_FOUND)//写到队列
 257                                 {    //队列满
 258                                   #ifdef DEBUG
                                       printf("\n----queue full----");
                                       #endif
 261                                   FreePage(MemPage);
 262                                   break;
 263                                 }
 264          				}
 265                          else
 266                          {     //mem over
 267                             #ifdef DEBUG
                                 printf("\n----queue over----");
                                 #endif
 270          						 break;
 271                          }
 272          			 }
 273          			                  //得到下一包的起始页
 274          			 StartPageOfPacket=Head[1];
 275          	  }
 276          	                        //重置bnry
 277               bnry=StartPageOfPacket-1;
 278          	  if(bnry<RECEIVE_START_PAGE)
 279          	     bnry=RECEIVE_STOP_PAGE-1;
 280          		  WriteReg(BNRY_WPAGE0,bnry);
 281          }
 282          
 283          
 284          void Start8019()
 285          {
 286               WriteReg(CR,CR_ABORT_COMPLETE_DMA|CR_START_COMMAND);
 287          }
 288          
 289          void Stop8019()
 290          {
 291              WriteReg(CR,CR_ABORT_COMPLETE_DMA|CR_STOP_COMMAND);
 292          }
 293          
 294          
 295          
 296          void main(void)
 297          {
 298               byte temp;
 299          	  word port=1001;
 300          	  LocalMACAddr[0]=0x52;
C51 COMPILER V6.12  89C52CTR                                                               05/16/2008 11:16:47 PAGE 6   

 301          	  LocalMACAddr[1]=0x68;
 302          	  LocalMACAddr[2]=0x3a;
 303          	  LocalMACAddr[3]=0x1c;
 304          	  LocalMACAddr[4]=0x2b;
 305          	  LocalMACAddr[5]=0x56;
 306          	  LocalIPAddress=0x0a8020d; //本地ip192.168.2.14
 307          	  ServerIPAddress=0x0a8020e;//目的ip192.168.2.13
 308          
 309          	                          //初始化
 310               SerialInitial();
 311          	  MemInitial();
 312          	  NetInInitial();
 313          	  
 314          	  RTLInitial();
 315          	  Start8019();
 316          	  InterruptInitial();
 317          
 318          
 319          	                          //建立一个arp包
 320               p[0]=0xff;
 321          	  p[1]=0xff;
 322          	  p[2]=0xff;
 323          	  p[3]=0xff;
 324          	  p[4]=0xff;
 325          	  p[5]=0xff;
 326          	  p[6]=0x52;
 327          	  p[7]=0x54;
 328          	  p[8]=0x4c;
 329          	  p[9]=0x30;
 330          	  p[10]=0x2e;
 331          	  p[11]=0x2f;
 332          	  p[12]=0x08;
 333          	  p[13]=0x06;
 334          	  p[14]=0x00;
 335          	  p[15]=0x01;
 336          	  p[16]=0x08;
 337          	  p[17]=0x00;
 338          	  p[18]=0x06;
 339          	  p[19]=0x04;
 340          	  p[20]=0x00;
 341          	  p[21]=0x01;
 342          	                  //发送arp包
 343          	  RTLSendPacket(p,60);
 344          	  while(1);
 345          
 346          	#ifdef DEBUG
              	    printf("\n----bigine-----");
              	#endif
 349          	                    //处理
 350            TCPBind(port);
 351            if(TCPConnect(ServerIPAddress,1001)==TRUE)
 352            {
 353               while(UserFunc());
 354            }
 355                                //延迟
 356            for(temp;temp<255;temp++);
 357            #ifdef DEBUG
                      printf("\n run over!");
              	#endif
 360          	                     //存储
 361             Stop8019();
 362          	while(1);
C51 COMPILER V6.12  89C52CTR                                                               05/16/2008 11:16:47 PAGE 7   

 363          }

C51 COMPILATION COMPLETE.  0 WARNING(S),  2 ERROR(S)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -