MikroC - Ethernet Library ping demo for ENC28J60

General discussion on mikroC.
Author
Message
User avatar
milos.vicentijevic
Posts: 965
Joined: 23 Apr 2013 08:09

Re: MikroC - Ethernet Library ping demo for ENC28J60

#31 Post by milos.vicentijevic » 04 Nov 2013 10:33

Hello Pinalamrutiya,

if you are using 5v power supply for your MCU please note that you will need logic level translator since your ENC28J60 needs 3.3v power supply. ENC28J60 has +5v logic tolerant inputs but it's output has to be translated from 3.3v to 5v logic so MCU can properly recognize logic states. Please check this first, since this might be a problem.

Best regards,
Milos Vicentijevic

pinalamrutiya
Posts: 8
Joined: 01 Aug 2013 11:00

Re: MikroC - Ethernet Library ping demo for ENC28J60

#32 Post by pinalamrutiya » 17 Nov 2013 15:25

hello ..@milos.vicentijevic

yes i have my logic level shifter i.e. sn74hc573n for the SO pin of enc28j60 and the rest of the pins are 5v compitible for the 5v microcontroller.


hey reply me on my thread
"Ethernet PICtail plus daughter board and PIC18F4520"


thanks in advance

User avatar
milos.vicentijevic
Posts: 965
Joined: 23 Apr 2013 08:09

Re: MikroC - Ethernet Library ping demo for ENC28J60

#33 Post by milos.vicentijevic » 18 Nov 2013 16:39

Hello,

I have answered you there.

Best Regards,
Milos Vicentijevic

nilkumar
Posts: 7
Joined: 15 Feb 2019 01:45

Re: MikroC - Ethernet Library ping demo for ENC28J60

#34 Post by nilkumar » 21 Feb 2019 00:28

In regards to the project, I am working on to communicate between dspic33ep512mu810 and ENC28J60. Where I am trying to PING ethernet chip from my PC through the router, but I am getting an error "host is unreachable". All the connection seems perfect. I am able to PING the router and my self (PC) but not to chip. Here below I am attaching source code and requesting to guide us to solve this problem.



#include <stdint.h>

#include "__EthEnc28j60.h"


#define SPI_Ethernet_HALFDUPLEX 0x00
#define SPI_Ethernet_FULLDUPLEX 0x01

///////////////////////////////////////////////////////////////////
#define MIREGADR 0x14
#define ECON1 0x1f
#define PHLCON 0x14
#define MIWRL 0x16
#define MIWRH 0x17
////////////////////////////////////////////////////////////////////

#define WCRCMD 0b01000000 // write control register
#define RCRCMD 0b00000000 // read control register
#define BFCCMD 0b10100000 // bit field clear
#define BFSCMD 0b10000000 // bit field set
////////////////////////////////////////////////////////////////////
#define MIRDL 0x18
#define MIRDH 0x19
#define PHLCONL 0x8A
#define PHLCONH 0x38

//MAC initialization
#define MACON1 0x00
#define MACON3 0x02
#define MACON4 0x03

#define MAMXFLL 0x0A
#define MAMXFLH 0x0B


#define MAADR0 0x01
#define MAADR1 0x00
#define MAADR2 0x03
#define MAADR3 0x02
#define MAADR4 0x05
#define MAADR5 0x04

#define ERXNDH 0x0B

#define MABBIPG 0x04
#define MAIPGL 0x06
// To select and deselect an ENC28J60
#define Select() LATGbits.LATG0=0; //making cs=0, to start communication
#define Deselect() LATGbits.LATG0=1; //making cs=1, to stop communication


const char httpHeader[] = "HTTP/1.1 200 OK\nContent-type: "; // HTTP header
const char httpMimeTypeHTML[] = "text/html\n\n"; // HTML MIME type
const char httpMimeTypeScript[] = "text/plain\n\n"; // TEXT MIME type


// default html page
char indexPage[] =
"<html><head><title>mikroElektronika</title></head><body>\
<h3 align=center>MikroElektronika Home Automatization System</h3>\
<form name=\"input\" action=\"/\" method=\"get\">\
<table align=center width=200 bgcolor=#4974E2 border=2><tr>\
<td align=center colspan=2><font size=4 color=white><b>Heat Control</b></font>\
</td></tr><tr><td align=center bgcolor=#4974E2><input name=\"tst1\" width=60 \
type=\"submit\" value=\"ON\"></td><td align=center bgcolor=#FFFF00>\
<input name=\"tst2\" type=\"submit\" value=\"OFF\"></td></tr></table>\
</form></body></html>";


//global variables
unsigned char MACAddr[6] = {0x00, 0x14, 0xa5, 0x76, 0x19, 0x3f} ; //my MAC adress
unsigned char IpAddr[4] = {192,168,0,111}; // Static Ip adress privided to ENC28J60
unsigned char getRequest[10];

// please configure it while you connect i/o board to PC/ client ip
char gwIpAddr[4] = {192, 168, 0,1}; // static gateway IP address of the router
char dnsIpAddr[4] = {192, 168, 0,20}; // static dns IP address
char ipMask[4] = {255, 255, 255, 0}; // static subnet mask


sbit SPI_Ethernet_CS at LATG0_bit;
sbit SPI_Ethernet_CS_Direction at TRISG0_bit;

sbit SPI_Ethernet_Rst at LATG1_bit;
sbit SPI_Ethernet_Rst_Direction at TRISG1_bit;

//Discription: oscillator confuguration

void int_mcu(void)
{
OSCCON=0x3E0;
CLKDIV=0x006;
PLLFBD=278;
ANSELD=0x0000;
ANSELA=0x0000;
}




//SPI1 configuration

void SPI1_pin_config (void)
{
//setup for direction control
TRISG.b0= 0; //cs pin , as an output
TRISGbits.TRISG14=0; //SO, serial data output
TRISEbits.TRISE0= 0; //Sck, serial clock out

TRISEbits.TRISE1=1; //SI, serial data input

TRISGbits.TRISG12=1; //interrupt from ENC28J60
TRISGbits.TRISG1=0; // reset to ENC28J60

//setup for ,as digital pins
ANSELG.b0= 0; // configure this and all below pins as digital logic
LATGbits.LATG0=1;
ANSELEbits.ANSE1=0;
ANSELEbits.ANSE0=0;


//pin configuration copies from James main file
TRISB= 0x01DF; // RB10-15 should be CS which are output
ANSELB= 0x01DF;
TRISC = 0xFFE7; // RC3,4 is AO CS
ANSELC = 0x0000;
TRISE = 0xFE5E;//RE0 is SCK of SPI1, RE1 is SDI of SPI1
ANSELE = 0x0000;
TRISF = 0x0104;
TRISG = 0xFEBE; // RG9 FLT(input) for AO,RG8 is SDO2(output), RG7 SDI2(input), RG6 SCK2(output) , RG0 CS for SPI1
ANSELG = 0x0000;
TRISG.b14=0;// RG14 is SDO of SPI1


}




// Mapping the SPI1 module for ENC28J60

void map_io(void)
{
Unlock_IOLOCK();
PPS_Mapping_NoLock(126, _OUTPUT, _SDO1); // map pin 95 as a SDO1
PPS_Mapping_NoLock(80, _OUTPUT, _SCK1OUT); // map pin 93 as SCK1
PPS_Mapping_NoLock(81, _INPUT, _SDI1); // map pin 94 as a SDI1
//PPS_Mapping_NoLock(89, _INPUT, _INT1);
Lock_IOLOCK();
}





//Discription: Coniguration of SPI in MODE-00, as ENC28J60 need MODE-0 configuration to work with

void SPI1_regi_config (void)
{
SPI1CON1 = 0x012F; // (120) device frequency= 140Mhz, FCY=Fosc/2=70Mhz,SPI freuency = FCY/( primary * secondry prescale )= 16Mhz/ (4:1 * 1:1) =17.5Mhz
SPI1STAT = 0x8000; // SPIx module is enabled and configures the SCK1, SDO1, SDI1 and SS1 pins as serial port pins

SPI1CON2 = 0x0000; //defualt value
}


// putting all functions of the SPI1 togather to initialize SPI1 module

void initialize_spi1_module(void)
{
SPI1_pin_config();
map_io();
SPI1_regi_config();
}

//set ENC memory bank 2

void bank_1select (void)
{
Select() ;
SPI1_Write(ECON1|BFCCMD ) ;
SPI1_Write(0b11111111) ;
Deselect() ;

Select() ;
SPI1_Write( ECON1| BFSCMD ) ;
SPI1_Write(0b00000001) ;
Deselect() ;


}
//end of funtion




//set ENC memory bank 3

void bank_3select (void)
{
Select() ;
SPI1_Write(ECON1|BFCCMD ) ;
SPI1_Write(0b11111111) ;
Deselect() ;

Select() ;
SPI1_Write( ECON1| BFSCMD ) ;
SPI1_Write(0b00000011) ;
Deselect() ;


}
//end of funtion



unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost,
unsigned int remotePort,
unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags)
{return 0;}

/*
{
unsigned int len; // my reply length
if(localPort != 80)
return(0); // I listen only to web request on port 80




//get 10 first bytes only of the request, the rest does not matter here
for(len = 0; len<15;len++)
{
getRequest[len] = SPI_Ethernet_getByte();
}
getRequest[len] = 0;

len=0;


if(memcmp(getRequest, "GET /", 5))
{
return(0); // only GET method
}

if(!memcmp(getRequest+11, "ON", 2)) // do we have ON command
{
PORTB.F0 = 1; // set PORTB bit0
}


else if(!memcmp(getRequest+11, "OFF", 3))
{
{ // do we have OFF command
PORTB.F0 = 0; // clear PORTB bit0
}

if (PORTB.F0)
{
memcpy(indexPage+340, "#FFFF00", 6); // highlight (yellow) ON
memcpy(indexPage+431, "#4974E2", 6); // clear OFF
}
else
{
memcpy(indexPage+340, "#4974E2", 6); // clear ON
memcpy(indexPage+431, "#FFFF00", 6); // highlight (yellow) OFF
}
}


{
len = SPI_Ethernet_putConstString(httpHeader); // HTTP header
len += SPI_Ethernet_putConstString(httpMimeTypeHTML); // with HTML MIME type
len += SPI_Ethernet_putString(indexPage); // HTML page first part
return len; // return to the library with the number of bytes to transmit
}


}

*/


// user UDP function, not using this so returnig zero

unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength, TEthPktFlags *flags)

{
return 0; // back to the library with the length of the UDP reply

}




//**********Function below is used to initialized MAC module as stated in ENC28J60 data sheet*************//

/*void mac_inti(void)
{


//default variables to initialize MAC
char macon1_value;
char macon3_value;
char macon4_value;
char mamxfll_value;
char mamxflh_value;
char mabbipg_value;
char maipgl_value;

//default values to initialize MAC
macon1_value=0x01;
macon3_value=0xF5;
macon4_value=0x40;
mamxfll_value=0x00;
mamxflh_value=0x06;
mabbipg_value=0x15;
maipgl_value=0x12;


//writing defualt values
Select();
SPI1_Write(MACON1|WCRCMD);
SPI1_Write(macon1_value);
Deselect();

Select();
SPI1_Write(MACON3|WCRCMD);
SPI1_Write(macon3_value);
Deselect();

Select();
SPI1_Write(MACON4|WCRCMD);
SPI1_Write(macon4_value);
Deselect();

Select();
SPI1_Write(MAMXFLL|WCRCMD);
SPI1_Write(mamxfll_value);
Deselect();

Select();
SPI1_Write(MAMXFLH|WCRCMD);
SPI1_Write(mamxflh_value);
Deselect();

Select();
SPI1_Write(MABBIPG|WCRCMD);
SPI1_Write(mabbipg_value);
Deselect();

Select();
SPI1_Write(MAIPGL|WCRCMD);
SPI1_Write(maipgl_value);
Deselect();

}

// end of function */




///*****************MAIN PROGRA********************************************************//

char temp; // random variable defined to send dummy data while reading of ENC28J60 is needed
void main(void)
{
temp= 0xAB;
int_mcu ();
initialize_spi1_module();
SPI_Set_Active(SPI1_Read, SPI1_Write);
SPI_Ethernet_Init(MACAddr, IPAddr, SPI_Ethernet_FULLDUPLEX); // Initialize Ethernet module
SPI_Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr);
SPI_Ethernet_Enable(_SPI_Ethernet_CRC | _SPI_Ethernet_UNICAST |_SPI_Ethernet_BROADCAST);
// writeReg(0x14, 0x14);
//bank_2select();
//writeReg(0x16, 0x2A);
//writeReg(0x17, 0x01);


while (1)
{

/* bank_1select();
Select();
SPI1_Write(0x00|RCRCMD);
SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(0x01|RCRCMD);
SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(0x02|RCRCMD);
SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(0x03|RCRCMD);
SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(0x04|RCRCMD);
SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(0x05|RCRCMD);
SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(0x06|RCRCMD);
SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(0x07|RCRCMD);
SPI1_Write(temp);
Deselect();


/*SPI1_Write(0x1F);
Deselect();

Select();
SPI1_Write(0x06|WCRCMD);
SPI1_Write(0xFF);
Deselect(); */


/*bank_0select();
Select();
SPI1_Write(0x07|RCRCMD);
SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(0x06|RCRCMD);
SPI1_Write(temp);
Deselect(); */



//code lines below is used to read MAC adress form MAADRXx 0f bank 03
/*bank_3select();
//reading back MAC address
Select();
SPI1_Write(MAADR0|RCRCMD);
SPI1_Write(temp);
//SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(MAADR1|RCRCMD);
SPI1_Write(temp);
//SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(MAADR2|RCRCMD);
SPI1_Write(temp);
// SPI1_Write(temp);
Deselect();


Select();
SPI1_Write(MAADR3|RCRCMD);
SPI1_Write(temp);
//SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(MAADR4|RCRCMD);
SPI1_Write(temp);
//SPI1_Write(temp);
Deselect();

Select();
SPI1_Write(MAADR5|RCRCMD);
SPI1_Write(temp);
// SPI1_Write(temp);
Deselect();








/* this function is to write into PHLCON1
//wrting to the PHLCON1
Select();
SPI1_Write(MIREGADR|WCRCMD);
SPI1_Write(PHLCON);
Deselect();

Select();
SPI1_Write(MIWRL|WCRCMD);
SPI1_Write(PHLCONL);
Deselect();

Select();
SPI1_Write(MIWRH|WCRCMD);
SPI1_Write(PHLCONH);
Deselect(); */






//reading back from EOCN1 of bank 02
/*Select();
bank_2select();
SPI1_Write(ECON1|RCRCMD); */



SPI_Ethernet_doPacket();
}
}

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: MikroC - Ethernet Library ping demo for ENC28J60

#35 Post by stefan.filipovic » 22 Feb 2019 15:33

Hi,

I believe that you have been answered on the following forum topics.
Just be sure to set your IP configs properly in the code.
Type "ipconfig /all" in the command prompt to check this out.

viewtopic.php?f=88&t=59261&p=297399#p297399

Kind regards,
Stefan Filipović

Post Reply

Return to “mikroC General”