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

📄 main.c

📁 51avr tcpip http协议
💻 C
字号:
//---------------------------------------------------------------------------
// Net MAIN.C

// notes:
//   It uses big endian order, which is the same as the
//   network byte order, unlike x86 systems.
//   then rewrite to little endian order
//---------------------------------------------------------------------------

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <stdlib.h>
#include "net.h"
#include "eth.h"
#include "arp.h"
#include "tcp.h"
#include "http.h"
#include "ip.h"
#include "general.h"
#include "timer.h"
#include "Serial.h"



//my IP address: 192.168.0.102
ULONG FLASHDATA my_ipaddr_P=0x6600a8c0;	//for little endian order	
// This sets my hardware address
UCHAR FLASHDATA my_hwaddr_P[]={0,0,0,0,0x12,0x34};
// Hardware addr to send a broadcast
UCHAR FLASHDATA broadcast_hwaddr_P[]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// This sets my subnet mask
ULONG FLASHDATA  my_subnet_P=0x00ffffff;
// Set to 0 if no gateway is present on network
ULONG FLASHDATA  gateway_ipaddr_P=0x0100a8c0;

ULONG my_ipaddr;
ULONG my_subnet;
ULONG gateway_ipaddr;
UCHAR my_hwaddr[6];
UCHAR broadcast_hwaddr[6];

// Global variables
UINT event_word;
UCHAR rcve_buf_allocated;
char text[10];  


//==========================================================
int main (void)
{
	UINT event_word_copy;
	UCHAR * inbuf;

    KeySet;
    RunSet;           //set run led on
	Timer0_Init();
	MCUCR |= 0x80;    //enable access external ram
    SREG = 1<<SREG_I; //enable globle interrupt
    InitSerial();
    PrintStr("avrweb\n");			   

	memcpy_P((char*)&my_ipaddr, (char*)&my_ipaddr_P, sizeof(my_ipaddr));
	memcpy_P((char*)&my_hwaddr, (char*)&my_hwaddr_P, sizeof(my_hwaddr));
	memcpy_P((char*)&broadcast_hwaddr, (char*)&broadcast_hwaddr_P, sizeof(broadcast_hwaddr));
	memcpy_P((char*)&my_subnet, (char*)&my_subnet_P, sizeof(my_subnet));
	memcpy_P((char*)&gateway_ipaddr, (char*)&gateway_ipaddr_P, sizeof(gateway_ipaddr));

	memset(text, 0, sizeof(text));
	event_word = 0;
	rcve_buf_allocated = FALSE;	  
                    	   
	init_tcp();
	init_http();         
	init_arp();		
	init_8019();

   // The code below is a priority based RTOS.  The event
   // handlers are in priority order - highest priority first.
	while (1)
   {

   	  if(!Key1Off)memcpy_P((char*)&my_ipaddr, (char*)&my_ipaddr_P, sizeof(my_ipaddr));

      // 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
      SREG = 0<<SREG_I;
	  event_word_copy = event_word;
      SREG = 1<<SREG_I;
      
      // See if an Ethernet frame has arrived      
      if (event_word_copy & EVENT_ETH_ARRIVED)
      {
         SREG = 0<<SREG_I;
         event_word &= (~EVENT_ETH_ARRIVED);
         SREG = 1<<SREG_I;
         
         // Allocate a buffer and read frame from CS8900A
         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            	       
      else if (event_word_copy & EVENT_TCP_RETRANSMIT)
      {
         SREG = 0<<SREG_I;
         event_word &= (~EVENT_TCP_RETRANSMIT);
         SREG = 1<<SREG_I;
         tcp_retransmit();
 		}

      // See if TCP inactivity timer has expired
      else if (event_word_copy & EVENT_TCP_INACTIVITY)
      {
         SREG = 0<<SREG_I;
         event_word &= (~EVENT_TCP_INACTIVITY);
         SREG = 1<<SREG_I;
         tcp_inactivity();
 	  }

      // See if ARP retransmit timer has expired
 		else if (event_word_copy & EVENT_ARP_RETRANSMIT)
      {
         SREG = 0<<SREG_I;
         event_word &= (~EVENT_ARP_RETRANSMIT);
         SREG = 0<<SREG_I;
         arp_retransmit();
		}

      // See if it is time to age the ARP cache
      else if (event_word_copy & EVENT_AGE_ARP_CACHE)
      {
         SREG = 0<<SREG_I;
         event_word &= (~EVENT_AGE_ARP_CACHE);
         SREG = 0<<SREG_I;
         age_arp_cache();
		}

		// See if it is time to read the analog inputs
		else if (event_word_copy & EVENT_READ_ANALOG)
      {
         SREG = 0<<SREG_I;
         event_word &= (~EVENT_READ_ANALOG);
         SREG = 1<<SREG_I;
			// Read one of the 3 analog inputs each time

      }

		// See if an RS232 message has arrived.  It is
      // not handled - RS232 is used for sending only
		else if (event_word_copy & EVENT_RS232_ARRIVED)
      {
         SREG = 0<<SREG_I;
         event_word &= (~EVENT_RS232_ARRIVED);
         SREG = 1<<SREG_I;
      }       
   }
   return 0;
}


⌨️ 快捷键说明

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