📄 main.c~
字号:
#include <mega64.h>
#include <delay.h>
#include <stdio.h>
#include "enc28j60.h"
#include "ip_arp_udp_tcp.h"
#include "net.h"
//#include "usart.h"
// please modify the following two lines. mac and ip have to be unique
// in your local area network. You can not have the same numbers in
// two devices:
static unsigned char mymac[6] = {0x00,0x55,0x58,0x10,0x00,0x24};
static unsigned char myip[4] = {192,168,1,15};
// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
unsigned int plen;
#define BUFFER_SIZE 250
unsigned char buffer[BUFFER_SIZE+1];
void setup()
{
enc28j60Init(mymac);
enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
delay_ms(10);
enc28j60PhyWrite(PHLCON,0x880);
delay_ms(500);
enc28j60PhyWrite(PHLCON,0x990);
delay_ms(500);
enc28j60PhyWrite(PHLCON,0x880);
delay_ms(500);
enc28j60PhyWrite(PHLCON,0x990);
delay_ms(500);
enc28j60PhyWrite(PHLCON,0x476);
delay_ms(100);
init_ip_arp_udp_tcp(mymac,myip,80);
}
void main()
{
unsigned int i=0;
UCSR1A=0x00;UCSR1B=0x18;UCSR1C=0x06;UBRR1H=0x00;UBRR1L=0x0C;
PORTD.2=1;PORTD.3=1;
DDRD.3=1;
//putchar1('W');putchar1('e');putchar1('l');putchar1('c');
//putchar1('o');putchar1('m');putchar1('e');
setup();
DDRD.4=1;
while(1)
{
plen = enc28j60PacketReceive(BUFFER_SIZE, &buffer[0]);
if(plen!=0)
{
/*putchar1(' ');
putchar1((plen/100)%10+'0');
putchar1((plen/10)%10+'0');
putchar1(plen%10+'0');
putchar1(' ');
for(i=0;i<plen;i++)
{
convert(i);
putchar1(' ');
convert(buffer[i]);
putchar1(' ');
putchar1(' ');
}
putchar1('\n');
putchar1('\r');*/
}
if(plen!=0)
{
if(eth_type_is_arp_and_my_ip(buffer,plen))
{
make_arp_answer_from_request(buffer);
}
// check if ip packets (icmp or udp) are for us:
if(eth_type_is_ip_and_my_ip(buffer,plen)!=0)
{
if(buffer[IP_PROTO_P]==IP_PROTO_ICMP_V && buffer[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V)
{
// a ping packet, let's send pong
make_echo_reply_from_request(buffer,plen);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -