📄 main.lst
字号:
C51 COMPILER V7.50 MAIN 12/23/2005 11:15:15 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE main.c LARGE OPTIMIZE(SIZE) BROWSE DEBUG OBJECTEXTEND
line level source
1 // 本程序的晶体为 22.1184mHz
2 // 本程序的缺省IP 192.168.0.7
3 // 本程序的缺省mask 255.255.255.0
4 // 本程序的缺省mac 0x1234567890ab
5 // 本程序的缺省gate 0.0.0.0(无网关)
6 // 本程序的缺省端口号port 9000
7 // 本程序的缺省remote_ip 192.168.0.1
8
9 //#include <reg52.h>
10 #include <absacc.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <intrins.h>
15 #include "24c02.h"
16 #include "ethernet.h"
17 #include "ip.h"
18 #include "udp.h"
19
20
21 //波特率 是在smod=1 的情况下计算的
22 #define FALSE 0
23 #define TRUE 1
24 #define false 0
25 #define true 1
26
27 #define BPS600 0x40
28 #define BPS1200 0xA0
29 #define BPS2400 0xD0
30 #define BPS4800 0xE8
31 #define BPS9600 0xF4
32 #define BPS14400 0xF8
33 #define BPS19200 0xFA
34 #define BPS28800 0xFC
35 #define BPS38400 0xFD
36
37 #define timer_5ms_value 0xDC00
38 #define timer_10ms_value 0xB800
39 #define timer_20ms_value 0x7000
40
41 #define rom_ip 0x00
42 #define rom_mask 0x04
43 #define rom_gate 0x08
44 #define rom_remote_ip 0x0c
45 #define rom_mac 0x10
46 #define rom_port 0x16
47
48 #define ARP_TX 1 /* Client state: sending ARP */
49 #define ARP_RX 2 /* received ARP */
50 #define CLIENT_DONE 3 /* completed */
51
52 #define HIGH(x) (x &0xff00)>>8
53 #define LOW(x) (x &0xff)
54 sbit watch_dog=0xb5;
55 #define BCASTADDR 0xff,0xff,0xff,0xff,0xff,0xff // broadcast address
C51 COMPILER V7.50 MAIN 12/23/2005 11:15:15 PAGE 2
56 BYTE bcast[MACLEN]={BCASTADDR}; // 该变量为源广播地址mac
57 BYTE code default_mac[MACLEN]={0x12,0x34,0x56,0x78,0x90,0xab};
58 bit uart_receive_enable; // 串口接收允许标志
59 bit uart_receiving; // 串口正在接收标志
60 bit uart_received_finished; // 串口接收完成标志
61 bit uart_trans_willing; // 串口将要发送标志
62 bit uart_trans_finished; // 串口发送完成标志,表示串口处于发送空闲状态
63
64 unsigned char uart_receive_buf[250]; // 接收缓冲区
65 unsigned char uart_trans_buf[250]; // 发送缓冲区
66 unsigned char uart_trans_count; // 发送数据计数器
67 unsigned char uart_receive_count; // 接收的数据计数器
68 unsigned char uart_trans_length; // 将要发送的数据长度
69
70 unsigned char uart_char_space; // 串口帧内间隔计数器(10ms) 最大为60ms
71 unsigned char uart_frame_space; // 串口帧间间隔计数器(10ms) 最小为80ms
72 unsigned char data dog; // 看门狗计数器(10ms) 最大为120 ms
73 #define clear_watchdog() watch_dog=1; _nop_();_nop_();_nop_();_nop_();dog=0;watch_dog=0
74 bit being_echo; // 网络要求远端返回数据标志,表示要判断网络超时
75 bit remote_echo;
76 bit first_arp;
77 unsigned int net_overtime_count; // 网络发送超时计数器
78
79 ETHERFRAME etherframe;
80 NODE locnode; /* My Ethernet and IP addresses */
81 NODE remnode; /* Remote node */
82
83 extern BYTE mymac[MACLEN]; // 该变量为ethernet.c 用源地址mac
84 void system_init(void);
85 void flush_gate_mac();
86 void do_uart_process(void);
87 int do_net_process(ETHERFRAME *efp,int rxlen);
88 int udp_receive(ETHERFRAME *efp, int len);
89 void send_ip(void);
90 unsigned long runtime;
91 void system_init(void)
92 {
93 1 EA=0;
94 1 SCON=0xD2; // SM0,SM1,SM2,REN,TB,RB,TI,RI
95 1 // 9位数据位,一位停止位,允许串行中断
96 1 PCON=0x80; // SMOD=1, 波特率加倍
97 1 TMOD=0x21; // GATE C/T M1 M0 GATE C/T M1 M0
98 1 // 定时器1在自动装入方式,0 位16位方式
99 1 TCON=0x50; // TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
100 1 // 定时器运行状态,不允许外部中断
101 1 TH1=BPS4800;
102 1 TL1=BPS4800;
103 1 TH0=HIGH(timer_10ms_value); //16位10ms 定时器
104 1 TL0=LOW(timer_10ms_value);
105 1 IE=0x92; // EA ? ? ES ET1 EX1 ET0 EX0
106 1 IP=0x02;
107 1 clear_watchdog();
108 1 }
109
110
111 void main(void)
112 {
113 1 int rxlen;
114 1 int ret;
115 1 unsigned char temp[6];
116 1 clear_watchdog();
117 1 delay_ms(2);
C51 COMPILER V7.50 MAIN 12/23/2005 11:15:15 PAGE 3
118 1 system_init(); //设置单片机的定时器 T0 (10ms),及 串口的波特率4800bps
119 1 uart_receiving=FALSE; // 串口正在接收标志 为 false
120 1 uart_receive_enable=TRUE; // 串口接收允许为 TRUE
121 1 uart_received_finished=FALSE; // 串口接收完成标志 为 false
122 1 uart_trans_count=0; // 串口发送完成字节数
123 1 uart_trans_willing=0; // 串口将要发送请求标志
124 1 uart_trans_finished=TRUE; // 串口发送完成,处于发送空闲状态
125 1 uart_char_space=0;
126 1 uart_frame_space=0;
127 1 // 因为8051 单片机的Keil C编译器编译结果整数高位字节在前
128 1 // 在读取24c02 中的数据时,读一个整数为顺序读取其中连续的2个字节
129 1 // 读一个长整数为顺序读取其中连续的4个字节
130 1 read24c02(temp,rom_ip,4); //读取本机的ip
131 1 locnode.ip=*((LWORD*)temp);
132 1
133 1 //如果IP未设置,设置IP为 192.168.0.7
134 1 if (locnode.ip==0xffffffff || locnode.ip==0x0) locnode.ip=0xc0a80007;
135 1
136 1 read24c02(temp,rom_mask,4); //读取本机的ip掩码
137 1 locnode.mask=*((LWORD*)temp);
138 1 //如果IP掩码未设置,设置IP掩码为 255.255.255.0
139 1 if (locnode.mask==0xffffffff ||locnode.mask==0x00) locnode.mask=0xffffff00;
140 1 read24c02(temp,rom_gate,4); //读取本机的缺省网关的ip
141 1 locnode.gate=*((LWORD*)temp);
142 1 //如果IP网关未设置,设置IP网关为 0.0.0.0(不存在网关)
143 1 if (locnode.gate==0xffffffff) locnode.gate=0x0;
144 1 read24c02(mymac,rom_mac,6); //读取本机的网卡物理地址
145 1 //如果mac未设置,设置mac 为 0x1234567890ab
146 1 if (!memcmp(mymac,bcast,6))
147 1 memcpy(mymac,default_mac,6);
148 1 memcpy(locnode.mac,mymac,6);
149 1 read24c02(temp,rom_port,2); //读取本机的UDP端口号
150 1 locnode.port=temp[0]*256+temp[1];
151 1 if ((locnode.port==0xffff)||(locnode.port==0x0000))
152 1 locnode.port=9000;
153 1 remnode.port=locnode.port;
154 1 read24c02(temp,rom_remote_ip,4); //读取本机的控制中心的ip,主动报警时用
155 1 remnode.ip=*((LWORD*)temp);
156 1 if (remnode.ip==0xffffffff || remnode.ip==0x0) remnode.ip=0xc0a80002;
157 1 uart_trans_length=0;
158 1 clear_watchdog();
159 1 resetnic();
160 1 net_overtime_count=0;
161 1 runtime=0;
162 1 first_arp=1;
163 1 flush_gate_mac();
164 1 while (1)
165 1 {
166 2 if (dog>=60) { clear_watchdog(); }
167 2 if (uart_received_finished)
168 2 do_uart_process();
169 2 // 只在串口发送完成后才处理网络接收
170 2 // 旨在保证在单发送缓冲区情况下不会覆盖,
171 2 // 在多发送缓冲区的情况下不会溢出。
172 2 if (uart_trans_finished)
173 2 if ((rxlen=get_ethernet(ðerframe))>0)
174 2 {
175 3 ret=do_net_process(ðerframe,rxlen);
176 3 if (ret==ARP_RX)
177 3 {
178 4 being_echo=false; // 表示ARP全过程完成,对方返回了其MAC
179 4 net_overtime_count=0; // 清除等待网络响应延时计数器,为下一次处理初始化
C51 COMPILER V7.50 MAIN 12/23/2005 11:15:15 PAGE 4
180 4 }
181 3 if (ret==CLIENT_DONE) // 表示应用的UDP数据包接收完成
182 3 {
183 4 }
184 3 }
185 2 if (runtime>=120000) //120000 *10ms =20 minute
186 2 {
187 3 runtime=0;
188 3 first_arp=1;
189 3 flush_gate_mac(); //刷新网关的物理地址mac
190 3 }
191 2 }
192 1 }
193 /*************************************************************************/
194 // 通过计数器uart_char_space 判断串口60MS没有新数据到达就认为一帧数据接收完成
195 // 在网络要求远端返回数据状态下,网络超时计数器加 1(10ms)
196 /*************************************************************************/
197
198 void timer0_interrupt(void) interrupt 1
199 {
200 1 TL0=TL0+LOW(timer_10ms_value); //reload timer0
201 1 TH0=TH0+HIGH(timer_10ms_value);
202 1 if (uart_receiving)
203 1 {
204 2 if (uart_char_space<6)
205 2 uart_char_space++;
206 2 else
207 2 {
208 3 uart_receiving=false;
209 3 uart_receive_enable=false;
210 3 uart_received_finished=true;
211 3 uart_frame_space=0;
212 3 }
213 2 uart_frame_space=0;
214 2 }
215 1 else
216 1 {
217 2 if (!uart_receive_enable)
218 2 {
219 3 uart_frame_space++;
220 3 if (uart_frame_space>8)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -