Listing 1—This is a UDP datagram laid out byte by byte in an array called udp_packet. The data is added dynamically in the round-robin process running in the C main() module. (This is only a portion of the listing. The entire listing is posted on the Circuit Cellar FTP site.)
void send_udp_datagram()
{
char temp;
unsigned int buffer_addr,i,tx_end,tx_cnt;
tx_end = TXSTART;
//load beginning page for transmit buffer
banksel(EWRPTL);
wr_reg(EWRPTL,LOW_BYTE(TXSTART));
wr_reg(EWRPTH,HIGH_BYTE(TXSTART));
//write ENC28J60 control byte
wr_sram(tx_control_byte);
++tx_end;
//build destination MAC address
udp_packet[enetpacketDest0]=remotemacaddrc[0];
udp_packet[enetpacketDest1]=remotemacaddrc[1];
udp_packet[enetpacketDest2]=remotemacaddrc[2];
udp_packet[enetpacketDest3]=remotemacaddrc[3];
udp_packet[enetpacketDest4]=remotemacaddrc[4];
udp_packet[enetpacketDest5]=remotemacaddrc[5];
tx_end += 6;
//build source MAC address
udp_packet[enetpacketSrc0]=macaddrc[0];
udp_packet[enetpacketSrc1]=macaddrc[1];
udp_packet[enetpacketSrc2]=macaddrc[2];
udp_packet[enetpacketSrc3]=macaddrc[3];
udp_packet[enetpacketSrc4]=macaddrc[4];
udp_packet[enetpacketSrc5]=macaddrc[5];
tx_end += 6;
//build packet type (IP)
udp_packet[enetpacketType0] = 0x08;
udp_packet[enetpacketType1] = 0x00;
tx_end += 2;
//build version, length,tos,packet length
udp_packet[ip_vers_len] = 0x45;
udp_packet[ip_tos] = 0x00;
udp_packet[ip_pktlen] = 0x00;
udp_packet[ip_pktlen+1] = 0x24;
tx_end += 4;
//build packet ID
++cntri;
udp_packet[ip_id] = make8(cntri,0);
udp_packet[ip_id+1] = make8(cntri,1);
tx_end += 2;
//build fragment offset
udp_packet[ip_frag_offset] = 0x00;
udp_packet[ip_frag_offset+1] = 0x00;
tx_end += 2;
//build time to live, UDP protocol ID
udp_packet[ip_ttl] = 0x80;
udp_packet[ip_proto] = 0x11;
//build IP source and destination addresses
for(i=0;i<4;++i)
{
udp_packet[ip_srcaddr+i]=ipaddrc[i];
udp_packet[ip_destaddr+i]=remoteipaddrc[i];
tx_end += 2;
}
//calculate the IP header checksum
udp_packet[ip_hdr_cksum]=0x00;
udp_packet[ip_hdr_cksum+1]=0x00;
.
.
.