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

📄 main.c

📁 c8051f020_uip1.0.rar
💻 C
字号:
/******************************************************************************
** Copyright(C) jerkoh		  
** CPU:C8051F020
** MAC+PHY:CP2200
** RAM:256+4096 byte
** Flash:64k byte
** OSC:22.118400Mhz
** Tcp/ip protocol stack: Adam Dunkels uip1.0
** Written for Keil C51 V9.00  
** compiler Model:large  Level:8
** Email:jerkoh@163.com   
******************************************************************************/
/******************************************************************************
** log:			 	  	
** 10-02-06 创建工程c8051f020_uip1.0 搭建编译环境
******************************************************************************/
#include "main.h"
extern void app1_init(void);
extern u8_t xdata my_hwaddr[6];
#define BUF ((struct uip_eth_hdr *)&uip_buf[0])

#define USE_STATIC_IP		//dhcp 不使能
u8_t code cdbuf[34]={0xc0,0xa8,0x02,0xEA,			/*192.168.2.234*/
	                  0xff,0xff,0xff,0x00,		    /*255.255.255.0*/
				      0xc0,0xa8,0x02,0x01,			/*192.168.2.1*/
				      0x0f,0xa1,					/*4001*/
				      0xce,0xb4,0xc9,0xe8,0xd6,0xc3,0xc9,0xe8,0xb1,0xb8,  /*未设置设备名称*/
				      0xc3,0xfb,0xb3,0xc6,0x00,0x00,0x00,0x00,0x00,0x00};

unsigned int  Count1msInc;
unsigned char Count1ms,Count10ms,Count1s;
unsigned char TimeSecond,TimeMinute;
clock_time_t tick_cnt;


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;
	static struct uip_eth_addr sTempAddr;

	WDTCN = 0xDE;              /* Disable watchdog timer*/	 
	WDTCN = 0xAD;			     

	SYSCLK_Init();            /* initialize oscillator*/		 	
		
	PORT_Init();              
	//UART0_Init();
		
 	Timer0_Init();
    Timer2_Init();


    timer_set(&periodic_timer, CLOCK_CONF_SECOND / 2);
    timer_set(&arp_timer, CLOCK_CONF_SECOND * 10);
	EA=1;
   // init_Timer();
    uip_init();
    app1_init();
    etherdev_init();
    uip_arp_init();



#ifdef USE_STATIC_IP
    uip_ipaddr(ipaddr, 192,168,0,100);
    uip_sethostaddr(ipaddr);
    uip_ipaddr(ipaddr, 192,168,0,1);
    uip_setdraddr(ipaddr);
    uip_ipaddr(ipaddr, 255,255,255,0);
    uip_setnetmask(ipaddr);

#endif


    httpd_init();
	
	sTempAddr.addr[0] = my_hwaddr[0];
    sTempAddr.addr[1] = my_hwaddr[1];
    sTempAddr.addr[2] = my_hwaddr[2];
    sTempAddr.addr[3] = my_hwaddr[3];
    sTempAddr.addr[4] = my_hwaddr[4];
    sTempAddr.addr[5] = my_hwaddr[5];
#ifndef USE_STATIC_IP
    //
    // Initialize the DHCP Client Application.
    //
    dhcpc_init(&sTempAddr.addr[0], 6);
    dhcpc_request();
#endif



     /* by gjk read flash128byte  ip,subnet,gateway,port*/

	  flash_Pageerase(0x0000,1);

	  Delay1ms(100);

	  flash_write(0x0000,cdbuf,1);

	  Delay1ms(100);

	  Delay1ms(100);
	  flash_read(0x0000,cdbuf,1);
	
    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;
}

//*****************************************************************************
//
// Callback for when DHCP client has been configured.
//
//*****************************************************************************
void
dhcpc_configured(const struct dhcpc_state *s)
{
    uip_sethostaddr(&s->ipaddr);
    uip_setnetmask(&s->netmask);
    uip_setdraddr(&s->default_router);
    //DisplayIPAddress((void *)&s->ipaddr, 18, 24);
}

/*------------------------------------------------------------------*/
void PORT_Init (void)
{
   P0MDOUT |= 0x81;                   /* enable TX0 as a push-pull output*/
   
   P1MDOUT |= 0x4f;                   /*Set P1.6(TB_LED) to push-pull*/		    P2MDOUT |= 0x28;                   /* Set P2.2(AB4_LED1)*/   P74OUT   = 0xFF;					  /*推挽方式*/  
   P3MDOUT |= 0Xff;
  
   P4 = 0xC0;                         /* /WR, /RD, are high, RESET is low*/		       P5 = 0xFF;   P6 = 0xFF;                         /* P5, P6 contain the address lines*/		     P7 = 0xFF;                         /* P7 contains the data lines*/		    TCON &= ~0x01;                     /*Make /INT0 level triggered*/	    								      /*定时器控制寄存器*/   
									    

   /*Enable UART0, CP0, and /INT0. This puts /INT0 on P0.3*/		     XBR0 = 0x84;				       /*CP0连接到端口引脚    RX0 TX0 连接到2个端口引脚*/     XBR1 = 0x04;			       	   /*INT0连接到端口引脚*/      XBR2 = 0x40;			           /*交叉开关允许*/     
 
   TCON &= ~0x01;               /* Make /INT0 level triggered*/	  
    
   EMI0CF = 0xFB;               /* Split-mode, non-multiplexed on P0 - P3*/		 							    /*外部存储器接口配置     P4-P7 非复用*/ 
							    /*ALE高低电平宽度   =4个sysclk周期*/   
							    /*带块选择的分片 方式   ,寻址低于 4KB边界的地址时*/ 
							    /*访问 片内 存储器  ,反之,访问片外存储器。*/ 
								/*8位片外MOVX操作使用EMI0CN的内容作为地址的高字节*/  
   EMI0TC = 0xef;             /* This constant may be modified*/			                               /* according to SYSCLK to meet the*/	                                  /* timing requirements for the CP2200*/		   							  /* 外部 存储器时序控制*/   

						      /*11 1011 11	=0xEF*/
							  /*地址建立时间 3个SYSCLK周期  	    11 */
							  /*WR和/RD脉宽=  12个SYSCLK周期     	 1011*/
							  /* 地址保持时间   =3个SYSCLK周期           11 */


   EMI0CN = 0x20;     /* Page of XRAM accessed by EMIF*/	
                      /* XRAM页选择位,当使用8位的MOVX命令时XRAM页选择位提供16位*/
					  /*外部数据存储器地址的高字节,实际上是 选择一个256B的RAM页 */ 
   	                  /*	0x2000__0x20ff 外部存储器接口控制*/  
					  /* EX0 = 1; */

   					
 					    
}

/********************************************************************************	
//                  初始化定时器0		 
********************************************************************************/			   
void Timer0_Init (void)
{
   	CKCON|=0x8;	    /*	时钟控制寄存器    计数器/定时器使用系统时钟*/  
   	TMOD|=0x1;    	/*16Bit	 方式1 16位计数器/定时器*/  
	Count10ms=10;
	Count1s=0;
   	TR0 = 0;                         	/* STOP Timer0*/	  
   	TH0 = (-SYSCLK/1000) >> 8;    		/* set Timer0 to overflow in 1ms*/	   
   	TL0 = -SYSCLK/1000;
   	TR0 = 1;   	                        /* START Timer0*/		 
   	IE|= 0x2; 
}


/********************************************************************************			  
**                    晶振初始化	 
********************************************************************************/			  

void SYSCLK_Init (void)
{
    int i;                             /* delay counter*/	       OSCXCN = 0x67;                      /* start external oscillator with*/	                                          /* 22.1184MHz crystal*/	     for (i=0; i < 256; i++) ;           /* wait for oscillator to start*/	       while (!(OSCXCN & 0x80)) ;          /* Wait for crystal osc. to settle*/	       OSCICN = 0x88;                      /*select external oscillator as SYSCLK*/		                                        /* source and enable missing clock*/	                                           /* detector*/
									   
									   	   
}
/********************************************************************************			 
//              定时器0   溢处   中断	    	  
********************************************************************************/			 
void Timer0_ISR (void) interrupt 1  /*1ms*/
{
	TH0 = (-SYSCLK/1000) >> 8;  
   	TL0 = -SYSCLK/1000;

   
	tick_cnt++;
	if (Count1ms) Count1ms--;
	Count1msInc++;
	if (Count10ms) Count10ms--;
	else
	{
		Count10ms=10;    			/*10ms*/
		if (Count1s) Count1s--;
		
		else
		{

			Count1s=100;			/*1s*/
			TimeSecond++;
			if (TimeSecond>=60)
			{
				TimeSecond=0;		/*1min*/
				TimeMinute++;
				if	(TimeMinute==60)	TimeMinute=0;
			}
		}
	}
}

/***************************************************************************
** Timer 2 interrupt service routing. Intr vector location is
** address 0x002B. The timer generates an interrupt every 25 msec
** It is set to auto reload so do not need to reload it.
***************************************************************************/
void timer2_interrupt(void) interrupt 5		   //对应TIME2中断 
{	
	TF2 = 0;		// Clear interrupt flag
}

/***************************************************************************
** Setup Timer2 in mode 1 which is 16 bit auto reload
** Intr rate = 22.1184 MHz /(12 * (65536 - reload value))
** For 25 msec reload value = 19456 = 0x4C00
***************************************************************************/
void Timer2_Init(void)
{
	T2CON = 0x00;
	RCAP2H = 0x4C; 		// Reload value
	RCAP2L = 0x00;
	TH2 = 0x4C;				// Initial load value
	TL2 = 0x00;
	TR2 = 1;					// Timer 2 run
	ET2 = 1;		             /* Enable timer 2 interrupt*/	 
}

/********************************************************************************			    
//                    延   时 t ms		   
********************************************************************************/				 
void Delay1ms(unsigned char T)
{
	Count1ms=T;
	while (Count1ms);
}

  

⌨️ 快捷键说明

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