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

📄 main.#2

📁 硬件是8051f340+cp2200。插上网线
💻 #2
字号:
//------------------------------------------------------------------------------// main.c//------------------------------------------------------------------------------// Copyright (C) 2005 Silicon Laboratories, Inc.//// Date: 05/19/06 09:38:45// Target: C8051F34x //// 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//#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 <string.h>                    // Standard 'C' Libraries
#include <intrins.h>
#include <stdio.h>
#include <ctype.h> #include <c8051F340.h>                         // Device-specific SFR Definitions#include "CP2200DK.H"
//------------------------------------------------------------------------------// Function Prototypes//------------------------------------------------------------------------------ sbit LED=P2^4;// Initialization Routinesvoid PORT_Init (void);void SYSCLK_Init (void);void EMIF_Init(void);int establish_network_connection();
void add();void Timer2_Init(void);
void get_data (PSOCKET_INFO socket_ptr);
void DelayMs(unsigned int n);//延时(n)MS
SystemInit();//系统配置
	SysClkInit();//配置系统时钟
void SysClkInit(void);/配置系统时钟
void SystemInit(void);/系统配置
void PortInit(void);/端口配置	

unsigned char num_buff[4]={0,20,30,40};
unsigned char HTML_BUFFER[400];
unsigned char num;
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F34x
//-----------------------------------------------------------------------------
sfr16 TMR2RL   = 0xca;                    // Timer2 reload value
sfr16 TMR2     = 0xcc;                    // Timer2 counter
sfr16 ADC0     = 0xbd;                    // ADC0 data register

//------------------------------------------------------------------------------
// Global Constants
//------------------------------------------------------------------------------
#define SYSCLK                  48000000L      // System Clock Frequency in Hz
#define T2_OVERFLOW_RATE        80L            // Timer 2 Overflow Rate in Hz

//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
void main(void)
{
   unsigned char KeyBuff;
   // Disable watchdog timer
   	PCA0MD&=~0x40;//禁止看门狗定时器

   // Initialize the MCU
   //PORT_Init();
   //SYSCLK_Init();
   //EMIF_Init();
   //Timer2_Init();
   
	SystemInit();//系统配置
	SysClkInit();//配置系统时钟

	Uart0Init();//串口初始化
	Uart0SendString("OK\n",3);
	LED=0;

	

   while(1)
   {  /* KeyBuff=KeyScan();
		if(KeyBuff==1)
		{  LED=1;
			DelayMs(100);
			LED=0;
	   num=Uart0Get();
		} */
		KeyBuff=KeyScan();
		if(KeyBuff==1)
		{
			LED=1;
			DelayMs(100);
			LED=0;
			Uart0SendString("KEY1\n",5);
		}
		if(KeyBuff==2)
		{
			LED=1;
			DelayMs(100);
			LED=0;
			Uart0SendString("KEY2\n",5);
		}
		if(KeyBuff==3)
		{
			LED=1;
			DelayMs(100);
			LED=0;
			Uart0SendString("KEY3\n",5);
		}
      // 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);
      
	  // Add CGI Script to Virtual File System	   
      mn_pf_set_entry
      (
      	(byte*)"get_data",	             // Script Name (ASCII)
      	get_data		                      // Function Pointer
      );
      //add();
      // Start the Application Layer Services.
      mn_server();
     
      //add(); */
   }
   
}

//-----------------------------------------------------------------------------
// 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.
//
// If there is no network connection, the function waits until either a
// connection appears or the CP2200 is reset before calling mn_ether_init()
// again. (The application may perform other tasks while polling
// link_status and ether_reset).
//
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, 3, 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 AUTO_NEG_FAIL, there is a 
      // hardware error.
      else if ((retval < 0) && (retval != AUTO_NEG_FAIL))
      {
         // 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) && (retval != AUTO_NEG_FAIL));

   return (1);

}//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Timer2_ISR (T2_OVERFLOW_RATE Hz)
//-----------------------------------------------------------------------------
//
void Timer2_ISR (void) interrupt 5
{

   // Define static counters for real time clock (RTC).
   static unsigned int RTC_counter = 0;

   // Clear Timer 2 Overflow Flag
   TF2H = 0;

   // Check if one second has passed and update RTC.
   if(RTC_counter >= T2_OVERFLOW_RATE){

      // Clear counter and update real time clock
      RTC_counter = 0;
       num_buff[0]++;
      //netfinder_update_RTC();

   } else {
      // Increment interrupt count
      RTC_counter++;
   }

}
//-----------------------------------------------------------------------------
// CGI Script: get_data
//-----------------------------------------------------------------------------
//
// This routine is called when the following is typed into the address bar
// of a web browser:
//    
//    http://<ip-address>/get_data?arg1=hello&arg2=donotdisplay
//
//    where <ip-address> = the IP address of the embedded system
//void get_data (PSOCKET_INFO socket_ptr)
{
   byte msg_buff1[50];
   
   int status1,status2,status3;   

   // Search for the type field and store the result in <msg_buff1>.
   status1 = mn_http_find_value (BODYptr, (byte*)"b1", msg_buff1);
   
   // Check status1 and status2 to determine if msg_buff1 and msg_buff2 are valid.
   if(status1){    
	    // num_buff[0]=2;
		 num_buff[1]=2; 
		 sprintf(HTML_BUFFER, "<html><body bgcolor=blue text=yellow><center><span style=\"font-family: sans-serif; font-size: 28pt; font-weight: bold;\">%d.%d</span></center></body></html>",num,num_buff[1]);     
      
      // Fill the socket with data to send.
      socket_ptr->send_ptr = HTML_BUFFER;
      socket_ptr->send_len = strlen(HTML_BUFFER);
		  }
   status2 = mn_http_find_value (BODYptr, (byte*)"b2", msg_buff1);
	  if(status2){   
	    num_buff[0]=3;
	    num_buff[1]=3;	   
	  sprintf(HTML_BUFFER, "<html><body bgcolor=blue text=yellow><center><span style=\"font-family: sans-serif; font-size: 28pt; font-weight: bold;\">%d.%d</span></center></body></html>",num,num_buff[1]);     
      
      // Fill the socket with data to send.
      socket_ptr->send_ptr = HTML_BUFFER;
      socket_ptr->send_len = strlen(HTML_BUFFER);
    }
	
  status3 = mn_http_find_value (BODYptr, (byte*)"To", msg_buff1);
  if(status3){   
       	    
		   
	 //sprintf(HTML_BUFFER, "<html><body bgcolor=blue text=yellow><center><span style=\"font-family: sans-serif; font-size: 28pt; font-weight: bold;\">%d.%d</span></center></body></html>",num_buff[0],num_buff[1]);     
      sprintf(HTML_BUFFER, "<html><body bgcolor=blue text=yellow><center><span style=\"font-family: sans-serif; font-size: 28pt; font-weight: bold;\">%s</span></center></body></html>",msg_buff1);
      // Fill the socket with data to send.
      socket_ptr->send_ptr = HTML_BUFFER;
      socket_ptr->send_len = strlen(HTML_BUFFER);
  }
}
//-----------------------------------------------------------------------------// Initialization Routines//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// PORT_Init//-----------------------------------------------------------------------------//// Configure the Interrupts, Crossbar and GPIO ports//void PORT_Init (void){   IT01CF = 0x07;                      // Enable Interrupt 0 on P0.7   TCON &= ~0x01;                      // Make /INT0 level triggered   XBR0    = 0x01;                     // Enable UART on P0.4(TX) and P0.5(RX)   XBR1    = 0x40;                     // Enable crossbar and enable                                       // weak pull-ups   P0MDOUT = 0x40;                    // enable UTX as push-pull output   P1MDOUT = 0xC8;                    // /WR and /RD are push-pull   P2MDOUT = 0xF0;   P3MDOUT = 0xFF;   P4MDOUT = 0xFF;
  	P0MDIN    = 0xF3;
    P1MDIN    = 0xDF;
    P2MDIN    = 0xF0;    
    P0SKIP    = 0x0C;
    P1SKIP    = 0x20;}//-----------------------------------------------------------------------------// EMIF_Init//-----------------------------------------------------------------------------//// Configure the External Memory Interface for both on and off-chip access.//void EMIF_Init (void){   EMI0CF = 0x0B;             // Multiplex mode   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}//-----------------------------------------------------------------------------// SYSCLK_Init//-----------------------------------------------------------------------------//// This routine initializes the system clock.//void SYSCLK_Init (void){   int i;   OSCICN = 0x03;                     // Configure internal oscillator for                                       // its maximum frequency     CLKMUL = 0x00;                      // Reset Clock Multiplier and select                                       // internal oscillator as input source   CLKMUL |= 0x80;                     // Enable the Clock Multiplier   for(i = 0; i < 256; i++);           // Delay at least 5us      CLKMUL |= 0xC0;                     // Initialize the Clock Multiplier      while(!(CLKMUL & 0x20));            // Wait for MULRDY => 1      RSTSRC = 0x04;                      // Enable missing clock detector                                       // and VDD monitor      FLSCL |= 0x10;                      // Set Flash Scale for 48MHz      CLKSEL |= 0x03;                     // Select output of clock multiplier                                       // as the system clock.}//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// This routine initializes Timer 2 to <T2_OVERFLOW_RATE> Hz.
//
void Timer2_Init(void)
{
   TMR2CN  = 0x00;                     // Stop Timer2; Use SYSCLK/12 as timebase
   CKCON  &= ~0x60;                    // Timer2 clock based on T2XCLK;

   // Initialize Reload Value
   TMR2RL = -(SYSCLK/12/T2_OVERFLOW_RATE);
   TMR2 = TMR2RL;

   ET2 = 1;                            // Enable Timer 2 interrupts
   TR2 = 1;                            // Start Timer 2

}//-----------------------------------------------------------------------------// ether_reset_low//-----------------------------------------------------------------------------//// This routine drives the reset pin of the ethernet controller low.//void ether_reset_low(){   P0 &= ~0x40;               // Pull reset low}//-----------------------------------------------------------------------------// 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){   P0 |= 0x40;               // Allow /RST to rise   while(!(P0 & 0x40));      // Wait for /RST to go high}

void DelayMs(unsigned int n)//延时(n)MS
{
	unsigned int i;
	for(;n>0;n--)
	{
		for(i=1200;i>0;i--);
	}
}

void SysClkInit(void)//配置系统时钟
{
	OSCICN=0x83;
	RSTSRC=0x04;
}

void SystemInit(void)//系统配置
{
	PCA0MD&=~0x40;//禁止看门狗定时器
	//EA=1;
}

void PortInit(void)//端口配置
{
    P0MDIN    = 0xF3;
    P1MDIN    = 0xDF;
    P2MDIN    = 0xF0;
    P0MDOUT   = 0x40;
    P2MDOUT   = 0xF0;
    P0SKIP    = 0x0C;
    P1SKIP    = 0x20;
    XBR0      = 0x01;
    XBR1      = 0x40;
} 

⌨️ 快捷键说明

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