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

📄 main.c

📁 51单片机实现网页显示功能
💻 C
字号:
/*
 * Copyright (c) 2003 Electric Application Laboratory of NAN KAI University
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * Author: Li Zhanglin <http://www.zlmcu.com>
 *
 */

/*
MCU:STC89C58
Clock:22.1184MHz
修改内容:
时钟修改为22.1184MHz 12T
RTL8019.h  RTL_BASE_ADDRESS 修改为 0xc000
RTL8019.h ADDRESS_SHIFT  修改为0x100
RTL8019.h 增加RTL8019的硬件复位信号在RTLInit(BYTE LocalMACAddr[])中
main.c  OnAcceptRecv()中增加串口透明输出

*/

#include "..\GloblDef\GloblDef.h"
#include "..\TCPIP\TCPIPmem.h"
#include "..\Netif\RTL8019.h"
#include "..\TCPIP\IP.h"
#include "..\Netif\etherif.h"
#include "..\Netif\ARP.h"
#include "..\TCPIP\Netif.h"
#include "..\TCPIP\TCP.h"
#include "..\App\main.h"
#include <stdio.h>    //for "pirntf"
#include <1820.c>
////////////////////////////////////////////////////////////////////////

#define HTTP_SERVER_PORT 80
socket  DT_XDATA * DT_XDATA HttpSocket = NULL;
socket  DT_XDATA * DT_XDATA ExAccept = NULL;


void OnHttpAccept(socket DT_XDATA *) REENTRANT_MUL;

char xdata  htmlpage[] =
{
"HTTP/1.0 200 OK\nContent-Type: text/html\n\n"
"<HTML><HEAD><TITLE>Welcome to BD7BQ's Website</TITLE></HEAD>"
"<table border=\"1\" width=\"49%\" height=\"81\">"
"<tr>"
"<td width=\"100%\" colspan=\"2\" height=\"24\">"
"<p align=\"center\">"
"<font color=\"#FF0000\">"
"测试,欢迎访问我的单片机以太网测试板</font></td>"
"</tr>"
"<tr>"
"<td width=\"48%\" align=\"center\" height=\"45\"><font size=\"6\">温度</font></td>"
"<td width=\"52%\" align=\"center\" height=\"45\"><font size=\"6\">28.5度</font></td>"
"</tr>"
"</table>"
"<br>"
"你是本次启动后第00000个访问者,Good luck to you .</br>"
//"该板外观请访问 http://www.e-diy.net/dispbbs.asp?boardID=2&ID=1065&page=1"
"</HTML>"
};

unsigned int count=0;

#define DATA_SIZE 	0x500
BYTE DT_XDATA DataBlock[DATA_SIZE];
BYTE DT_XDATA str[]="hello";
socket		DT_XDATA * DT_XDATA ExConn;
socket		DT_XDATA * DT_XDATA	ExAccept;
socket		DT_XDATA * DT_XDATA	ExListen;


void OnReceive(void DT_XDATA * buf,WORD size)  REENTRANT_MUL
{
	/* send back data */
	TCPSend(ExConn,buf,size);
}

void send_char(unsigned char sendc)   
{
	TI = 0;
	SBUF = sendc;
	while(TI == 0) ; 
	TI = 0;
}

void send_BYTE(BYTE temp)
{
	if(temp/100!=0)send_char(temp/100+0x30);
	if(temp>9) send_char(temp%100/10+0x30);
	send_char(temp%10+0x30);
}

void send_IP(IP_ADDR IP_temp)
{
	BYTE k;
	
	send_char(' ');
	send_char(' ');
	k=IP_temp/0x1000000;
	send_BYTE(k);
	send_char('.');
	k=IP_temp%0x1000000/0x10000;
	send_BYTE(k);
	send_char('.');
	k=IP_temp%0x10000/0x100;
	send_BYTE(k);
	send_char('.');
	k=IP_temp%0x100;
	send_BYTE(k);

//printf(" %lx ",TCBPool[0].IPScr);

	send_char(0x0d);
	send_char(0x0a);

}



void OnAcceptRecv(void DT_XDATA *buf,WORD size) REENTRANT_MUL
{
	WORD i;
//	for(i=0;i<size;i++)  printf("%c",*(BYTE DT_XDATA *)(buf+i));  //串口透明输出
//	*((BYTE DT_XDATA *)buf+3)='$';
//	size=8;
	/* send back data */
//	TCPSend(ExAccept,buf,size);
 	send_IP(ExAccept->IPDest);


 //  
    TCPAbort(ExAccept);  TCPClose(ExAccept);

}

void OnAccept(socket DT_XDATA *pNewSocket) REENTRANT_MUL
{
	ExAccept = pNewSocket;
	pNewSocket->recv = OnAcceptRecv;
	pNewSocket->close = OnClose;
}
void OnClose(socket DT_XDATA * pSocket) REENTRANT_MUL
{
	TCPClose(pSocket);	/* we close too */
}
void Timer() REENTRANT_MUL interrupt 1
{
	TH0 = 0x4C;	//0x4C:TIMER_22.1184M_25MS_H    0x3C:TIMER_24M_25MS_H
	TL0 = 0x06;	//0x06:TIMER_22.1184M_25MS_L    0xAA:TIMER_24M_25MS_L
	
	NetIfTimer();
	ARPTimer();
	TCPTimer();
}

void SerialInit()
{
	/* set TI to 1, set TR1 to 1 */
	SCON = 0x52;/* SM0 SM1 =1 SM2 REN TB8 RB8 TI RI */
	TMOD = 0x20;/* GATE=0 C/T-=0 M1 M0=2 GATE C/T- M1 M0 */
	TH1 = 0xF4;	// TH1=F4 9600 when at 22.1184MHz,TH1=FA,19200,22.1184MHz */
	            // TH1=E6 4800 when at 24MHz,TH1=F3,9600,24MHz 
	PCON = 0x80;
	TCON = 0x40;/* 01101001 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT */
}

void InterruptInit()
{
	TMOD |= 0x01;
	TH0 = 0x4C;	//0x4C:TIMER_22.1184M_25MS_H    0x3C:TIMER_24M_25MS_H
	TL0 = 0x06;	//0x06:TIMER_22.1184M_25MS_L    0xAA:TIMER_24M_25MS_L
	
	TR0 = 1;
	
	/* open interrupt for rtl and timer */
	EA = 1;
	/* EX0 = 1; */
	ET0 = 1;
}

sfr  P4       = 0xe8;
sbit CONT = P4^3;

void OnHttpClose(socket DT_XDATA * pSocket) REENTRANT_MUL
{
 printf("\nConnection will been gracefully closed");
 TCPClose(pSocket); /* we close too */
}

void OnHttpAccept(socket DT_XDATA *pNewSocket) REENTRANT_MUL
{
	WORD i;
	BYTE te;

 //   printf("\nAccept a connection");


    ExAccept = pNewSocket;
    pNewSocket->recv  = OnAcceptRecv;
    pNewSocket->close = OnHttpClose;


    te=get_temperatuer();
	htmlpage[416]=te/100+0x30;
	htmlpage[417]=te%100/10+0x30;
	htmlpage[419]=te%10+0x30;

	count++;
	if(count>65534) count=65534;
	htmlpage[467]=count/10000+0x30;
	htmlpage[468]=count%10000/1000+0x30;
	htmlpage[469]=count%1000/100+0x30;
	htmlpage[470]=count%100/10+0x30;
	htmlpage[471]=count%10+0x30;

    for(i = 0; htmlpage[i]; i++);

//for(j=0;j<7;j++)  printf("%c",*(BYTE DT_XDATA *)(htmlpage+j));  //串口透明输出
    TCPSend(ExAccept, htmlpage, i);
  //  TCPClose(ExAccept);
 //TCPAbort(ExAccept); 
}



void main(void)
{
//	struct SMemHead DT_XDATA *MemHead;
	struct SEtherDevice DT_XDATA DevRTL;
	BYTE	DT_XDATA EtherAddr[ETHER_ADDR_LEN] = {0x52,0x54,0x4c,0x30,0x2e,0x2f};
	IP_ADDR		IPAddr	= 0xc0a80009;	// 192.168.0.9
	IP_ADDR		NetMask	= 0xffffff00;	// 255.255.255.0 
	IP_ADDR		GateWay	= 0xc0a80001;	// 192.168.0.1  
	

	NetIfInit();
	ARPInit();
	TCPInit();
	MemInit();
	RTLInit(EtherAddr);	
	
	/* init Devcie struct and init this device */
	EtherDevInit(&DevRTL,EtherAddr,RTLSendPacket,RTLReceivePacket);

	/* add this device to NetIf */
	NetIfAdd(IPAddr,NetMask,GateWay,EtherInput,EtherOutput,&DevRTL);

	SerialInit();
	InterruptInit();


 /* Illustrate how to listen as a server */
 HttpSocket = TCPSocket(IPAddr);
 ExAccept   = NULL;
 if(TCPListen(HttpSocket,HTTP_SERVER_PORT,OnHttpAccept))  printf("\nListen...");
 else  printf("\nListen Failed!");

// printf("\nPress Any Key to End TCP Test!");
 //WAIT_FOR_KEY_PRESS;
while(1);

	TCPAbort(ExAccept); 
 	TCPAbort(HttpSocket); 

 /* cancel the timer */
// TimerEnd();  
}




⌨️ 快捷键说明

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