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

📄 main.c

📁 移植uIP1.0到51单片机上的版本mcu_netV1.00 uIP ARP / ICMP / TCP协议的完全移植
💻 C
字号:
/****************************************************************************
【文  件  名  称】main.c
【功  能  描  述】TCP/IP处理主程序
【程  序  版  本】V1.0
【作          者】gateway
****************************************************************************/

#include "main.h"
#include<absacc.H>

extern void example1_init(void);

/************ 定时器0用来做TCP连接计时和ARP更新使用*******/
clock_time_t tick_cnt;

/*定时器0初始化函数*/
void init_Timer()
{
	TMOD = 0x21;      /*定时器0用于定时*/
	TH0 = 0x7D;       /*20M晶振下,定时20ms*/
	TL0 = 0xCB;
	EA = 1;           /*允许中断*/
	ET0 = 1;          /*允许时间0中断*/
	TR0 = 1;          /*启动TR0*/
}

/*定时器0中断服务函数*/
void  timer0()  interrupt 1
{
	tick_cnt ++;
	TH0 = 0x7D;
	TL0 = 0xCB;
	TR0 = 1;
}

/*时间戳*/
clock_time_t clock_time(void)
{
    return tick_cnt;
}
/**************************************************/

void main(void)
{ 
    u8_t i;
    uip_ipaddr_t ipaddr;
	struct timer periodic_timer, arp_timer;

    timer_set(&periodic_timer, CLOCK_CONF_SECOND / 2);
    timer_set(&arp_timer, CLOCK_CONF_SECOND * 10);

    init_Timer();
    uip_init();
    example1_init();
    etherdev_init();
    uip_arp_init();
    uip_ipaddr(ipaddr, 192,168,1,9);
    uip_sethostaddr(ipaddr);
    uip_ipaddr(ipaddr, 192,168,1,16);
    uip_setdraddr(ipaddr);
    uip_ipaddr(ipaddr, 255,255,255,0);
    uip_setnetmask(ipaddr);
    
    while(1)
    {
    uip_len = etherdev_read();
    if(uip_len > 0)
    {
        if(BUF->type == htons(UIP_ETHTYPE_IP)) 
        {
        	uip_arp_ipin();
        	uip_input();
        	/* If the above function invocation resulted in data that
        	   should be sent out on the network, the global variable
        	   uip_len is set to a value > 0. */
        	if(uip_len > 0) 
            {
        	    uip_arp_out();
        	    etherdev_send();
        	}
        } 
        else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
    	uip_arp_arpin();
    	/* If the above function invocation resulted in data that
    	   should be sent out on the network, the global variable
    	   uip_len is set to a value > 0. */
        	 if(uip_len > 0) 
             {
        	     etherdev_send();
        	 }
          }
        }
        else if(timer_expired(&periodic_timer)) 
        {
            timer_reset(&periodic_timer);
            for(i = 0; i < UIP_CONNS; i++) 
            {
            	uip_periodic(i);
            	/* If the above function invocation resulted in data that
        	   should be sent out on the network, the global variable
        	   uip_len is set to a value > 0. */
            	if(uip_len > 0)
                {
            	   uip_arp_out();
            	   etherdev_send();
        	    }
              }

             #if UIP_UDP
              for(i = 0; i < UIP_UDP_CONNS; i++) 
              {
        	      uip_udp_periodic(i);
        	     /* If the above function invocation resulted in data that
        	     should be sent out on the network, the global variable
        	     uip_len is set to a value > 0. */
        	     if(uip_len > 0) 
                 {
                	  uip_arp_out();
                	  etherdev_send();
            	 }
               }
              #endif /* UIP_UDP */
              
              /* Call the ARP timer function every 10 seconds. */
              if(timer_expired(&arp_timer)) 
              {
            	  timer_reset(&arp_timer);
            	  uip_arp_timer();
              }
        }
  }
  return;
}

⌨️ 快捷键说明

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