📄 main.c
字号:
//---------------------------------------------------------------------------
// Net MAIN.C
//
// 8051 Web Server project
// See Makefile for build notes
// Written for Keil C51 V5.1 compiler, notes:
// It uses big endian order, which is the same as the
// network byte order, unlike x86 systems.
// Use OPTIMIZE(2)or higher so that automatic variables get shared
// between functions, to stay within the 256 bytes idata space
//---------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
//#include <reg52.h>
//#include <stdio.h>
#include "net.h"
#include "eth.h"
#include "serial.h"
#include "timer.h"
#include "analog.h"
#include "arp.h"
#include "tcp.h"
#include "http.h"
#include "ip.h"
#include "general.h"
//#include "mystring.h"
#include "myconfig51.h"
//#include "word.h"
//#include "myshell.h"
#define StatInputCom 0
#define StatExeCom 1
sbit led=P1^0;
// Global variables
UINT volatile event_word;
char xdata text[20];
UCHAR idata rcve_buf_allocated;
// This sets my hardware address to 00:01:02:03:04:05
UCHAR my_hwaddr[6]={0,0,0,0,0x12,0x34};
// Hardware addr to send a broadcast
UCHAR code broadcast_hwaddr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// This sets my IP address to 192.168.0.10
//ULONG code my_ipaddr = 0x0A01541DL;
ULONG my_ipaddr=0xc0a80066;
// This sets my subnet mask to 255.255.255.0
ULONG my_subnet=0xffffff00;
// Set to 0 if no gateway is present on network
ULONG gateway_ipaddr=0xc0a80001;
//--------------------------------------------------------------------------
// Initialize the memory management routines
// Initialize variables declared in main
//--------------------------------------------------------------------------
unsigned int Count1msInc;
unsigned char Count1ms,Count10ms,Count1s,Count100us;
unsigned char TimeSecond,TimeMinute;
//--------------------------------------------------------------------------
void init_main(void)
{
// Start the memory pool for incoming and outgoing Ethernet
// frames at 1000, with length = 1500 bytes. Memory below 500
// is used for program variables
init_mempool(3000, 1500);
memset(text, 0, sizeof(text));
event_word = 0;
rcve_buf_allocated = FALSE;
// getadr(MACADR,&my_hwaddr[0]);//读出网卡的物理地址存到my_ethernet_address.bytes[6]里
// getadr(HOST,(unsigned char *)&my_ipaddr);//读出本机IP地址存到my_ip_address.bytes[4]里
// getadr(MASK,(unsigned char *)&my_subnet);//读出本机掩码存到mask_ip_address.bytes[4]里
// getadr(GATEWAY,(unsigned char *)&gateway_ipaddr);//读出网关IP地址存到gateway_ip_address.bytes[4]里
// getadr(SNMPHOST,(unsigned char *)&SNMP_ip_address);//读出SNMP网管主机IP地址存到SNMP_ip_address.bytes[4]里
}
void Timer_Init (void)
{
TMOD = 0X12;
TF0 = 0;
// TH0 = (65536-OSC*1000/12)/256; //0XF8; // set Timer0 to overflow in 1ms
// TL0 = 65536-OSC*1000/12; //0X30;
TH0 = 256-OSC*100/12;
TL0 = 256-OSC*100/12;
TR0 = 1; // START Timer0
ET0 = 1;
TF1=0;
TH1=(65536-OSC*25000/12)/256; //0X4C;
TL1=65536-OSC*2500/12;
TR1=1;
ET1=1;
Count100us=10;
}
void Timer0_ISR (void) interrupt 1 //1ms
{
// TH0 = (65536-OSC*1000/12)/256; //0XF8; // set Timer0 to overflow in 1ms
// TL0 = 65536-OSC*1000/12; //0X30;
sck=!sck;
if(--Count100us!=0) return;
Count100us=10;
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;
}
}
}
}
void Delay1ms(unsigned char T)
{
Count1ms=T;
while (Count1ms);
}
void LightONOFF(bit b)
{
led=!b;
}
//==========================================================
void main (void)
{
UINT j, event_word_copy;
UCHAR xdata * inbuf;
Timer_Init();
init_tcp();
init_http();
EA=1;
InitSerial();
InitSerialBuffer();
init_main();
init_arp();
init_8019();
clrscr();
PrintStr("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
PrintStr("\t\t* Welcom to YG-51WEB-Server *\n");
PrintStr("\t\t* Author: QinYuanGao *\n");
PrintStr("\t\t* Web: www.mcusky.com *\n");
PrintStr("\t\t* mobile: 13316529329 *\n");
PrintStr("\t\t* Date: 2004.8.1 *\n");
PrintStr("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n\n\n");
j = 0;
// The code below is a priority based RTOS. The event
// handlers are in priority order - highest priority first.
while (1)
{
// 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
EA = 0;
event_word_copy = event_word;
EA = 1;
// See if an Ethernet frame has arrived
if (event_word_copy & EVENT_ETH_ARRIVED)
{
EA = 0;
event_word &= (~EVENT_ETH_ARRIVED);
EA = 1;
// 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)
{
EA = 0;
event_word &= (~EVENT_TCP_RETRANSMIT);
EA = 1;
tcp_retransmit();
}
// See if TCP inactivity timer has expired
else if (event_word_copy & EVENT_TCP_INACTIVITY)
{
EA = 0;
event_word &= (~EVENT_TCP_INACTIVITY);
EA = 1;
tcp_inactivity();
}
// See if ARP retransmit timer has expired
else if (event_word_copy & EVENT_ARP_RETRANSMIT)
{
EA = 0;
event_word &= (~EVENT_ARP_RETRANSMIT);
EA = 1;
arp_retransmit();
}
// See if it is time to age the ARP cache
else if (event_word_copy & EVENT_AGE_ARP_CACHE)
{
EA = 0;
event_word &= (~EVENT_AGE_ARP_CACHE);
EA = 1;
age_arp_cache();
}
// See if it is time to read the analog inputs
else if (event_word_copy & EVENT_READ_ANALOG)
{
EA = 0;
event_word &= (~EVENT_READ_ANALOG);
EA = 1;
// Read one of the 3 analog inputs each time
read_analog_inputs();
}
// 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)
{
EA = 0;
event_word &= (~EVENT_RS232_ARRIVED);
EA = 1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -