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

📄 main.c

📁 c8051F120_Web,是基于51MCU下的开发
💻 C
字号:
//------------------------------------------------------------------------------// main.c//------------------------------------------------------------------------------// Copyright (C) 2005 Silicon Laboratories, Inc.//// Date: 11/08/07 15:24:21// Target: C8051F12x //// Description://    This file contains the main routine, MCU initialization code, and//    callback functions used by the TCP/IP Library.//// Generated by TCP/IP Configuration Wizard Version 3.23//#include "mn_userconst.h"                      // TCP/IP Library Constants#include "mn_stackconst.h"                     // TCP/IP Library Constants#include "mn_errs.h"                           // Library Error Codes#include "mn_defs.h"                           // Library Type definitions#include "mn_funcs.h"                          // Library Function Prototypes#include "VFILE_DIR\index.h"#include <c8051F120.h>                         // Device-specific SFR Definitions//------------------------------------------------------------------------------// Function Prototypes//------------------------------------------------------------------------------// Initialization Routinesvoid PORT_Init (void);void SYSCLK_Init (void);void EMIF_Init(void);int establish_network_connection();
//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
void main(void)
{
   int retval;

   // Disable watchdog timer
   WDTCN = 0xde;
   WDTCN = 0xad;

   // Initialize the MCU
   PORT_Init();
   SYSCLK_Init();
   EMIF_Init();

   while(1)
   {
      // Initialize the TCP/IP stack.
      if (mn_init() < 0)
      {
         // If code execution enters this while(1) loop, the stack failed to initialize.
         // Verify that all boards are connected and powered properly.
         while(1);
      }

      // Connect to the network
      establish_network_connection();

      // Add web page to virtual file system.
      // The main page MUST be called index.htm or index.html.
      mn_vf_set_entry((byte *)"index.html", INDEX_SIZE, index_html, VF_PTYPE_FLASH);

      // Use DHCP to obtain an IP address.
      if (mn_dhcp_start(PTR_NULL, dhcp_default_lease_time) <= 0)
      {
         // DHCP Error
         // The DHCP server did not assign a valid IP address.
         while(1);

      }

      // Start the Application Layer Services
      // If this routine exits, check the return value for an error code.
      retval = mn_server();

   }
}

//-----------------------------------------------------------------------------
// establish_network_connection
//-----------------------------------------------------------------------------
//
// This function calls mn_ether_init() to initialize the CP2200 and attach to
// the network.
//
// If there is a network connection, the function returns 1.
//
// In the call to mn_ether_init(), NUM_AUTONEG_ATTEMPTS is set to 0, so the
// function will not return until it successfully auto-negotiates.
//
// mn_ether_init() will not be a blocking call if NUM_AUTONEG_ATTEMPTS is set
// to a value greater than 0.
//
int establish_network_connection()
{
   int retval;

   do
   {
      // mn_ether_init() initializes the Ethernet controller.
      // AUTO_NEG indicates that the controller will auto-negotiate.
      retval = mn_ether_init(AUTO_NEG, 0, 0);

      // If there is no link, poll link_status until it sets or the
      // CP2200 resets and then call mn_ether_init() again.
      if (retval == LINK_FAIL)
      {
         while(!link_status && !ether_reset);
      }

      // If retval is less than zero and is not LINK_FAIL, there is a 
      // hardware error.
      else if (retval < 0)
      {
         // Verify that the Ethernet controller is connected and powered properly.
         // Verity that the EMIF has been configured at a speed compatible with the
         //    Ethernet controller.
         while(1);
      }

   }while(retval < 0);

   return (1);

}

//-----------------------------------------------------------------------------// Initialization Routines//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// PORT_Init//-----------------------------------------------------------------------------//// Configure the Interrupts, Crossbar and GPIO ports//void PORT_Init (void){   char SFRPAGE_SAVE = SFRPAGE;     // Save Current SFR page   SFRPAGE = CONFIG_PAGE;           // set SFR page   P1MDOUT |= 0x40;                 // Set P1.6(TB_LED) to push-pull   P2MDOUT |= 0x08;                 // Set P2.2(AB4_LED1)   // all pins used by the external memory interface are in push-pull mode   P4MDOUT =  0xDF;   P5MDOUT =  0xFF;   P6MDOUT =  0xFF;   P7MDOUT =  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   SFRPAGE = LEGACY_PAGE;   TCON &= ~0x01;                   // Make /INT0 level triggered   SFRPAGE = CONFIG_PAGE;    // Enable UART0, CP0, and /INT0. This puts /INT0 on P0.3.   XBR0 = 0x84;   XBR1 = 0x04;   XBR2 = 0x40;   SFRPAGE = SFRPAGE_SAVE;       // Restore SFR page}//-----------------------------------------------------------------------------// EMIF_Init//-----------------------------------------------------------------------------//// Configure the External Memory Interface for both on and off-chip access.//void EMIF_Init (void){   char SFRPAGE_SAVE = SFRPAGE;  // Save Current SFR page   SFRPAGE = LEGACY_PAGE;   EMI0CF = 0xFB;             // Split-mode, non-multiplexed on P0 - P3   EMI0TC = EMIF_TIMING;      // This constant may be modified                              // according to SYSCLK to meet the                              // timing requirements for the CP2200   EMI0CN = BASE_ADDRESS;     // Page of XRAM accessed by EMIF   SFRPAGE = SFRPAGE_SAVE;    // Restore SFR page}//-----------------------------------------------------------------------------// SYSCLK_Init//-----------------------------------------------------------------------------//// This routine initializes the system clock.//void SYSCLK_Init (void){   int i;                           // software timer   char SFRPAGE_SAVE = SFRPAGE;     // Save Current SFR page   SFRPAGE = CONFIG_PAGE;           // set SFR page   OSCICN = 0x83;                   // set internal oscillator to run                                    // at its maximum frequency   CLKSEL = 0x00;                   // Select the internal osc. as                                    // the SYSCLK source   //Turn on the PLL and increase the system clock by a factor of M/N   PLL0CN  = 0x00;                  // Set internal osc. as PLL source   SFRPAGE = LEGACY_PAGE;   FLSCL   = 0x30;                  // Set FLASH read time for 100 MHz clk   SFRPAGE = CONFIG_PAGE;   PLL0CN |= 0x01;                  // Enable Power to PLL   PLL0DIV = 0x01;                  // Set Pre-divide value to N (N = 1)   PLL0MUL = 0x04;                  // Multiply SYSCLK by M (M=4)   PLL0FLT = 0x01;                  // Set the PLL filter register for                                    // a reference clock from 12.2 - 19.5 MHz                                    // and an output clock from 65 - 100 MHz   for (i=0; i < 256; i++) ;        // Wait at least 5us   PLL0CN  |= 0x02;                 // Enable the PLL   while(!(PLL0CN & 0x10));         // Wait until PLL frequency is locked   CLKSEL  = 0x02;                  // Select PLL as SYSCLK source   SFRPAGE = SFRPAGE_SAVE;          // Restore SFR page}//-----------------------------------------------------------------------------// ether_reset_low//-----------------------------------------------------------------------------//// This routine drives the reset pin of the ethernet controller low.//void ether_reset_low(){   unsigned char SFRPAGE_Save;   SFRPAGE_Save = SFRPAGE;    // Save Current SFR page   SFRPAGE = CONFIG_PAGE;     // Switch to ports SFR page   P4 &= ~0x20;               // Pull reset low   SFRPAGE = SFRPAGE_Save;    // Restore SFR page}//-----------------------------------------------------------------------------// ether_reset_high//-----------------------------------------------------------------------------//// This routine places the reset pin in High-Z allowing it to be pulled up // using the external pull-up resistor.//// Additionally, this routine waits for the reset pin to read high before// exiting.//void ether_reset_high (void){   unsigned char SFRPAGE_Save;   SFRPAGE_Save = SFRPAGE;    // Save Current SFR page   SFRPAGE = CONFIG_PAGE;     // Switch to ports SFR page   P4 |= 0x20;               // Allow /RST to rise   while(!(P4 & 0x20));      // Wait for /RST to go high   SFRPAGE = SFRPAGE_Save;    // Restore SFR page}

⌨️ 快捷键说明

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