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

📄 main.c

📁 Keil_C51程序,C8051实现的TCP/IP功能源码
💻 C
字号:
//---------------------------------------------------------------------------
// Net MAIN.C
//
// 8051 Web Server project
// See Makefile for build notes 
// Written for Keil C51 V5.1 compiler, notes:
//   It uses big endian order, which is the same as the
//   network byte order, unlike x86 systems.
//   Use OPTIMIZE(2)or higher so that automatic variables get shared
//   between functions, to stay within the 256 bytes idata space
//---------------------------------------------------------------------------
//
#include <string.h>
#include <stdlib.h>
#include "reg52.h"
#include "net.h"
#include "eth.h"
#include "serial.h"
#include "arp.h"
#include "ip.h"
#include "udp.h"
#include "timer.h"
#include "tcp.h"
#include "http.h"

// Global variables
UINT 	volatile	event_word;
char 	xdata 		text[20];  
UCHAR 	idata 		debug;
UCHAR 	idata 		rcve_buf_allocated;

// This sets my hardware address to 00:01:02:03:04:05
UCHAR code my_hwaddr[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}; 

// Hardware addr to send a broadcast
UCHAR code broadcast_hwaddr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

// This sets my IP address to 192.168.0.10
ULONG code  my_ipaddr = 0xC0A8000AL;

//目的地址192.168.0.214
ULONG 	sender_ipaddr=0xC0A800D6L;

// This sets my subnet mask to 255.255.255.0
ULONG code my_subnet = 0xFFFFFF00L;

// Set to 0 if no gateway is present on network192.168.0.1
ULONG code gateway_ipaddr = 0xC0A80001L;

//--------------------------------------------------------------------------
// Initialize the memory management routines
// Initialize variables declared in main
//--------------------------------------------------------------------------

sbit RST	= P1^1;//复位ip等信息
sbit RDY	= P1^3;
//sbit DEBUG 	= P1^4;//如果为高,不发出调试信息
sbit BUSY 	= P1^5;//如果为低,则不允许向串口输入
//sbit RST_LEDS=P1^6;//用于复位大屏的保留

void init_main(void)
{
	// Start the memory pool for incoming and outgoing Ethernet
	// frames at 2000, with length = 1800(1500) bytes. Memory below 500
	// is used for program variables
	init_mempool((void xdata *)1000, 1800);
	memset(text, 0, sizeof(text));//将text的设置为0
	event_word = 0;
	rcve_buf_allocated = FALSE;
	debug = FALSE;//测试条件开启与否FALSE
	//debug = TRUE;	
}

//延时(thetime)毫秒 对于22.1184MHz 
void Delay1ms(unsigned char thetime)
{
	unsigned char j;
	while (thetime!=0)
   	{
    	thetime--;
      	for(j=0;j<226;j++){}          
	}	    	
}

void main (void)
{
	UINT  event_word_copy;
	UCHAR xdata * inbuf;
	UINT  length;
    
	init_main();
	init_tcp();
	init_http();	
	init_serial();
	init_arp();
	LED	= 0;//开灯
	init_8019();
	init_timer2();

	EA=1;//开启中断
	ET2 = 1;	// Enable timer 2 interrupt

	if(debug)SendCommString("Init OK\r\n");	   
	// The code below is a priority based RTOS.  The event
	// handlers are in priority order - highest priority first.
	while (1)
	{
      	// Query CS8900A to see if Ethernet frame has arrived
      	// If so, set EVENT_ETH_ARRIVED bit in event_word
		query_8019();
      	// Use a copy of event word to avoid interference
      	// with interrupts
		ET2=0;//EA = 0;
		event_word_copy = event_word;
		ET2=1;//EA = 1;
      
      	// See if an Ethernet frame has arrived 是否有网络数据传到     
      	if (event_word_copy & EVENT_ETH_ARRIVED)
      	{
         	ET2=0;//EA = 0;
         	event_word &= (~EVENT_ETH_ARRIVED);//去除标志
         	ET2=1;//EA = 1;
         
         	// Allocate a buffer and read frame from 
			//从网络芯片中读取数据
         	inbuf = rcve_frame();
         	if (inbuf != NULL)
         	{
            	// Process the received Ethernet frame 
				//处理接受的数据帧
            	eth_rcve(inbuf); 

         	   // If the memory allocated for the rcve message has
         	   // not already been freed then free it now
         	   if (rcve_buf_allocated)//数据已经接受到了
         	   {
         	      free(inbuf);//释放内存
         	      rcve_buf_allocated = FALSE;
         	   }
         	}
      	}
    	// See if TCP retransmit timer has expired  
		// TCP重发超时        	       
      	else if (event_word_copy & EVENT_TCP_RETRANSMIT)
      	{
        	EA = 0;
        	event_word &= (~EVENT_TCP_RETRANSMIT);
        	EA = 1;
        	tcp_retransmit();
 		}

      	// See if TCP inactivity timer has expired
		//TCP 不活动
      	else if (event_word_copy & EVENT_TCP_INACTIVITY)
      	{
         	EA = 0;
         	event_word &= (~EVENT_TCP_INACTIVITY);
         	EA = 1;
         	tcp_inactivity();
 		}

    	// See if ARP retransmit timer has expired
 		else if (event_word_copy & EVENT_ARP_RETRANSMIT)
    	{
        	ET2=0;//EA = 0;
        	event_word &= (~EVENT_ARP_RETRANSMIT);//去除标志
       		ET2=1;//EA = 1;
        	arp_retransmit();
		}

	    // See if it is time to age the ARP cache
    	else if (event_word_copy & EVENT_AGE_ARP_CACHE)
    	{
        	ET2=0;//EA = 0;
        	event_word &= (~EVENT_AGE_ARP_CACHE);//去除标志
			ET2=1;//EA = 1;
        	age_arp_cache();
		}
		// See if an RS232 message has arrived
		else if((event_word_copy & EVENT_RS232_ARRIVED)&&CommRecFlag)
		{
			//if(debug){SendCommString(" 232 Rec ");Serial_Send_Ch(CommRecLength);}
			
			length=CommRecLength;
			CommRecFlag=0;
			CommRecLength=0;
			if(CommRecBlock)inbuf=0x4000;
				else inbuf=0x5000;
			CommRecBlock=~CommRecBlock;//切换
			ET2=0;
			udp_send(inbuf-42,COMM_PORT1,length);		
			event_word &= (~EVENT_RS232_ARRIVED);//去除标志
			ET2=1;			
		}
	}
}

⌨️ 快捷键说明

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