Sei sulla pagina 1di 3

# define F_CPU 8000000UL

#include <avr/io.h>
#include <stdlib.h>
#include <string.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "ip_arp_udp_tcp.h"
#include "websrv_help_functions.h"
#include "enc28j60.h"
#include "net.h"
#define REMOTE_PORT 9600
#define LOCAL_PORT 2000
#define BUFFER_SIZE 650
static uint8_t mac_address[6] = {0x22,0x22,0x22,0x10,0x00,0x33};
static uint8_t local_host[4] = {192,168,100,101};
static uint8_t gateway[4] = {192,168,100,1};
static uint16_t local_port = 2000;
static uint8_t buf[BUFFER_SIZE+1];
static uint8_t data[] = "OKey";
static uint8_t ok[]="123456";
static char State_Led[1];
static void net_process(void);
static void net_init(void);
static void Int_Init(void);
int main(void)
{
sei();
Int_Init();
net_init();
net_process();
return(0);
}
//Trnh phuc vu ngat cua INT0
ISR(INT2_vect)
{
PORTC^=~PIND;
}
static void Int_Init(void)
{
DDRC=0xFF;
PORTC=0x00;

DDRB&=~(0x04);
PORTB|=0x40;
DDRD=0x00; // input
PORTD=0xFF;
GICR |= (1<<INT2); // Enable external interrupt INT2
MCUCSR |= (0<<ISC2); // INT2 is executed on rising edge - 1( r
ising)-0(falling)
}
static void net_init (void)
{
enc28j60Init(mac_address);
_delay_ms(20);
enc28j60clkout(2);
_delay_ms(20);
enc28j60PhyWrite(PHLCON,0x476);
_delay_ms(20);
init_ip_arp_udp_tcp(mac_address,local_host,local_port);
client_set_gwip(gateway);
}
static void net_process(void)
{
uint16_t plen;
uint8_t payloadlen=0;
while(1)
{
// get the next new packet:
plen = enc28j60PacketReceive(BUFFER_SIZE
, buf);

/*plen will be unequal to zero if there


is a valid
* packet (without crc error) */
if(plen==0){
continue;
}
// arp is broadcast if unknown but a hos
t may also
// verify the mac address by sending it
to
// a unicast address.
else if(eth_type_is_arp_and_my_ip(buf,pl
en)){
make_arp_answer_from_req
uest(buf,plen);
continue;
}
// check if ip packets (icmp or udp) are
for us:
else if(eth_type_is_ip_and_my_ip(buf,ple
n)==0){
continue;
}
else if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V
&& buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
// a ping packet, let's
send pong
make_echo_reply_from_req
uest(buf,plen);
continue;
}
if (buf[IP_PROTO_P]==IP_PROTO_UDP_V)
{
payloadlen=buf[U
DP_LEN_L_P]-UDP_HEADER_LEN;
//LED PORTC
if (buf[UDP_DATA
_P]>='0' &&buf[UDP_DATA_P]<='7'&& payloadlen==1)
{
PORTC^=1
<<(buf[UDP_DATA_P]-48);
continue
;
}
// Cap nhat trang thai led
if (buf[UDP_DATA
_P]=='X'&& payloadlen==1)
{
State_Le
d[0]=PORTC;
make_udp
_reply_from_request(buf,State_Led,2,local_port);
continue
;
}
}
}
}

Potrebbero piacerti anche