Pic18f4550 Enc28j60 Bağlantısı

Başlatan bymerag, 07 Ocak 2014, 13:50:05

bymerag

kolay Gelsin arkadaşlar
Enc28j60 Ethernet modülünü Bugün aldım deneme yapmaya çalıştım.
http://www.teckler.com/pt/Ftnjr/WebServerPIC18F4550ENC28J60-14409
bu sitedeki bağlantıya göre yapmaya çalıştım.
olmadı.
ping atamıyorum yani
örnek kod olarak mikroc HTTP Demo kodlarını derledim.
Kodlar
#include  "__EthEnc28j60.h"

#define Spi_Ethernet_HALFDUPLEX     0x00  // half duplex
#define Spi_Ethernet_FULLDUPLEX     0x01  // full duplex

// mE ehternet NIC pinout
sfr sbit SPI_Ethernet_Rst at LATC0_bit;  // for writing to output pin always use latch (PIC18 family)
sfr sbit SPI_Ethernet_CS  at LATC1_bit;  // for writing to output pin always use latch (PIC18 family)
sfr sbit SPI_Ethernet_Rst_Direction at TRISC0_bit;
sfr sbit SPI_Ethernet_CS_Direction  at TRISC1_bit;

const code unsigned char httpHeader[] = "HTTP/1.1 200 OKnContent-type: " ;  // HTTP header
const code unsigned char httpMimeTypeHTML[] = "text/htmlnn" ;              // HTML MIME type
const code unsigned char httpMimeTypeScript[] = "text/plainnn" ;           // TEXT MIME type
unsigned char httpMethod[] = "GET /";

const code   char    *indexPage =                   // Change the IP address of the page to be refreshed
"<meta http-equiv="refresh" content="3;url=http://192.168.2.60">
<HTML><HEAD></HEAD><BODY>
<h1>PIC   ENC28J60 Mini Web Server</h1>
<a href=/>Reload</a>
<script src=/s></script>
<table><tr><td valign=top><table border=1 style="font-size:20px ;font-family: terminal ;">
<tr><th colspan=2>ADC</th></tr>
<tr><td>AN2</td><td><script>document.write(AN2)</script></td></tr>
<tr><td>AN3</td><td><script>document.write(AN3)</script></td></tr>
</table></td><td><table border=1 style="font-size:20px ;font-family: terminal ;">
<tr><th colspan=2>PORTB</th></tr>
<script>
var str,i;
str="";
for(i=0;i<8;i  )
{str ="<tr><td bgcolor=pink>BUTTON #" i "</td>";
if(PORTB&(1<<i)){str ="<td bgcolor=red>ON";}
else {str ="<td bgcolor=#cccccc>OFF";}
str ="</td></tr>";}
document.write(str) ;
</script>
" ;

const code   char    *indexPage2 =  "</table></td><td>
<table border=1 style="font-size:20px ;font-family: terminal ;">
<tr><th colspan=3>PORTD</th></tr>
<script>
var str,i;
str="";
for(i=0;i<8;i  )
{str ="<tr><td bgcolor=yellow>LED #" i "</td>";
if(PORTD&(1<<i)){str ="<td bgcolor=red>ON";}
else {str ="<td bgcolor=#cccccc>OFF";}
str ="</td><td><a href=/t" i ">Toggle</a></td></tr>";}
document.write(str) ;
</script>
</table></td></tr></table>
This is HTTP request #<script>document.write(REQ)</script></BODY></HTML>
" ;


unsigned char   myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f} ;   // my MAC address
unsigned char   myIpAddr[4]  = {192, 168,  2, 60 } ;                   // my IP address
unsigned char   gwIpAddr[4]  = {192, 168,  2,  1 } ;                   // gateway (router) IP address
unsigned char   ipMask[4]    = {255, 255, 255,  0 } ;                   // network mask (for example : 255.255.255.0)
unsigned char   dnsIpAddr[4] = {4, 4,  4,  2 } ;                   // DNS server IP address

unsigned char   getRequest[15] ;                                        // HTTP request buffer
unsigned char   dyna[29] ;                                              // buffer for dynamic response
unsigned long   httpCounter = 0 ;                                       // counter of HTTP requests


#define putConstString  SPI_Ethernet_putConstString

#define putString  SPI_Ethernet_putString


unsigned int  SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags)
        {
        unsigned int    len;            // my reply length

        if(localPort != 80)                         // I listen only to web request on port 80
                {
                return(0) ;
                }

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

        if(memcmp(getRequest, httpMethod, 5))       // only GET method is supported here
                {
                return(0) ;
                }

        httpCounter   ;                             // one more request done

        if(getRequest[5] == 's')                    // if request path name starts with s, store dynamic data in transmit buffer
                {
                // the text string replied by this request can be interpreted as javascript statements
                // by browsers

                len = putConstString(httpHeader) ;              // HTTP header
                len  = putConstString(httpMimeTypeScript) ;     // with text MIME type

                // add AN2 value to reply
                WordToStr(ADC_Read(2), dyna) ;
                len  = putConstString("var AN2=") ;
                len  = putString(dyna) ;
                len  = putConstString(";") ;

                // add AN3 value to reply
                WordToStr(ADC_Read(3), dyna) ;
                len  = putConstString("var AN3=") ;
                len  = putString(dyna) ;
                len  = putConstString(";") ;

                // add PORTB value (buttons) to reply
                len  = putConstString("var PORTB=") ;
                WordToStr(PORTB, dyna) ;
                len  = putString(dyna) ;
                len  = putConstString(";") ;

                // add PORTD value (LEDs) to reply
                len  = putConstString("var PORTD=") ;
                WordToStr(PORTD, dyna) ;
                len  = putString(dyna) ;
                len  = putConstString(";") ;

                // add HTTP requests counter to reply
                WordToStr(httpCounter, dyna) ;
                len  = putConstString("var REQ=") ;
                len  = putString(dyna) ;
                len  = putConstString(";") ;
                }
        else if(getRequest[5] == 't')                           // if request path name starts with t, toggle PORTD (LED) bit number that comes after
                {
                unsigned char   bitMask = 0 ;                   // for bit mask

                if(isdigit(getRequest[6]))                      // if 0 <= bit number <= 9, bits 8 & 9 does not exist but does not matter
                        {
                        bitMask = getRequest[6] - '0' ;         // convert ASCII to integer
                        bitMask = 1 << bitMask ;                // create bit mask
                        PORTD ^= bitMask ;                      // toggle PORTD with xor operator
                        }
                }

        if(len == 0)                                            // what do to by default
                {
                len =  putConstString(httpHeader) ;             // HTTP header
                len  = putConstString(httpMimeTypeHTML) ;       // with HTML MIME type
                len  = putConstString(indexPage) ;              // HTML page first part
                len  = putConstString(indexPage2) ;             // HTML page second part
                }

        return(len) ;                                           // return to the library with the number of bytes to transmit
        }


unsigned int  SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength, TEthPktFlags *flags)
        {
        unsigned int    len ;                           // my reply length

        // reply is made of the remote host IP address in human readable format
        ByteToStr(remoteHost[0], dyna) ;                // first IP address byte
        dyna[3] = '.' ;
        ByteToStr(remoteHost[1], dyna   4) ;            // second
        dyna[7] = '.' ;
        ByteToStr(remoteHost[2], dyna   8) ;            // third
        dyna[11] = '.' ;
        ByteToStr(remoteHost[3], dyna   12) ;           // fourth

        dyna[15] = ':' ;                                // add separator

        // then remote host port number
        WordToStr(remotePort, dyna   16) ;
        dyna[21] = '[' ;
        WordToStr(destPort, dyna   22) ;
        dyna[27] = ']' ;
        dyna[28] = 0 ;

        // the total length of the request is the length of the dynamic string plus the text of the request
        len = 28   reqLength;

        // puts the dynamic string into the transmit buffer
        SPI_Ethernet_putBytes(dyna, 28) ;

        // then puts the request string converted into upper char into the transmit buffer
        while(reqLength--)
                {
                SPI_Ethernet_putByte(toupper(SPI_Ethernet_getByte())) ;
                }

        return(len) ;           // back to the library with the length of the UDP reply
        }

/*
 * main entry
 */
void    main()
        {
        ADCON1 = 0x0B ;         // ADC convertors will be used with AN2 and AN3
        CMCON  = 0x07 ;         // turn off comparators

        PORTA = 0 ;
        TRISA = 0xff ;          // set PORTA as input for ADC

        PORTB = 0 ;
        TRISB = 0xff ;          // set PORTB as input for buttons

        PORTD = 0 ;
        TRISD = 0 ;             // set PORTD as output

        /*
         * starts ENC28J60 with :
         * reset bit on RC0
         * CS bit on RC1
         * my MAC & IP address
         * full duplex
         */
        SPI1_Init();
        SPI_Rd_Ptr = SPI1_Read;
        SPI_Ethernet_Init(myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX) ;

        // dhcp will not be used here, so use preconfigured addresses
        SPI_Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr) ;

        while(1)                            // do forever
                {
                /*
                 * if necessary, test the return value to get error code
                 */
                SPI_Ethernet_doPacket() ;   // process incoming Ethernet packets

                /*
                 * add your stuff here if needed
                 * Spi_Ethernet_doPacket() must be called as often as possible
                 * otherwise packets could be lost
                 */
                }
        }

4 MHz Kristal ve 22 pF lik kullandım. Mclr ye de 1k Direnç bağlayıp 5 Volt verdim. //Burda Herhangi Bir sıkıntı varmı  ?
buda proje ayarları burda herhangi bir sıkıntı varmı

sfr sbit SPI_Ethernet_Rst at LATC0_bit;  // for writing to output pin always use latch (PIC18 family)
sfr sbit SPI_Ethernet_CS  at LATC1_bit;  // for writing to output pin always use latch (PIC18 family)
sfr sbit SPI_Ethernet_Rst_Direction at TRISC0_bit;
sfr sbit SPI_Ethernet_CS_Direction  at TRISC1_bit;

bu tanımlamanın anlamı ne C0 ve C1'i kullanacakmıyız?
sıkıntıyı çözemedim yardım ederseniz sevinirim.

bymerag

PIC18F4550    ENC28J60
C7       SO
B0       SI
B1       SCK
B3       CS      //cs ve reset pinini biz belirliyoruz ama
B5       RST    // diğer pinleri nasıl ayarlayacaz
D2       INT
D3       WOL

meftun arkadaşımız şöyle  yapın demiş
pic(sdo)<>enc(si)
pic(sdi)<>enc(so)
pic(sck)<>enc(sck)
pic(int0)<>enc(int)
pic(int1)<>enc(wo)
aşağıdaki 2 pini biz belirliyoruz yazılımda:
pic(c0)<>enc(reset)
pic(c1)<>enc(cs)

yalnız pic18f4550 te
int0,sdi,sda yazan bacak rb0 bacağı
aynı şekil int1 ve sck demiş ama bu ikisde rb1 bacağı
nasıl bağlanacak bunlar çözemedim

başka bir sayfada böyle bir bağlantı yapmışlar bunlarıda denemdim
#define  PIN_ENC_MAC_SO    PIN_C7   // Conectar con PIN MISO del ENC28J60.
#define  PIN_ENC_MAC_SI    PIN_B0   // Conectar con PIN MOSI del ENC28J60.
#define  PIN_ENC_MAC_CLK   PIN_B1   // Conectar con PIN SCK del ENC28J60.
#define  PIN_ENC_MAC_CS    PIN_B3   // Conectar con PIN CS del ENC28J60.
#define  PIN_ENC_MAC_RST   PIN_B5   // Conectar con PIN RST del ENC28J60.
#define  PIN_ENC_MAC_INT   PIN_D2   // Conectar con PIN INT del ENC28J60.
#define  PIN_ENC_MAC_WOL   PIN_D3   // Conectar con PIN WOL del ENC28J60.

Lo cual me trajo confusión por lo que revisé los datasheet de ambos y todo indica que se deberían intercambiar con el SI, o sea quedaría de esta forma.:
#define  PIN_ENC_MAC_SO    PIN_B0   // Conectar con PIN MISO del ENC28J60.
#define  PIN_ENC_MAC_SI    PIN_C7   // Conectar con PIN MOSI del ENC28J60.
#define  PIN_ENC_MAC_CLK   PIN_B1   // Conectar con PIN SCK del ENC28J60.
#define  PIN_ENC_MAC_CS    PIN_B3   // Conectar con PIN CS del ENC28J60.
#define  PIN_ENC_MAC_RST   PIN_B5   // Conectar con PIN RST del ENC28J60.
#define  PIN_ENC_MAC_INT   PIN_D2   // Conectar con PIN INT del ENC28J60.
#define  PIN_ENC_MAC_WOL   PIN_D3   // Conectar con PIN WOL del ENC28J60.


yardımlarınızı bekliyorum...

muzafferturk

Dostum bende 18f887a ile yapyım int kodları buldum bende ip değiştirip yeni hex dosysı oluşturamadım ama kendi hex dosyası çalışıyor..bendeki kodları yollayayım belki işine yarar ..microc 5.6.1     aşağıdaki adreste var..
http://olivier.fournet.free.fr/electronique/PIC_16F877A-8MHz_ENC28J60_Mini_Web_Server/

http://olivier.fournet.free.fr/electronique/PIC_18F4620-8MHz_ENC28J60_Mini_Web_Server/


yapabilirsen banada bir haber verebilirmisin.

elektronart

Modulü nereden aldınız? Türkiye'de uygun fiyatlı satan bir yer var mı? Ben de ebaydan sipariş ettim ama daha hızlı ve fiyatı uygun bir yer varsa beklemeye gerek yok.
Açık Elektronik

Mr.Java


muzafferturk

#5
http://www.direnc.net/ENC28J60-Ethernet-LAN-Network-Module,PR-13427.html
bu adreste muadili var ben onu kullanıyorum oburde var ama pahali elektronıkcilerdede var..

HanRUN enc28j60 hazır modul diye geciyor

muzafferturk

bymeray kardeşim yanlız senden bi ricam var o linkteki orjinal hex çalışıyor ;ama ben projenın hiç bir ayarını değiştirmeden derlediğimde hex dosyasıını boyutu 9kb oluyor ve çalışmıyor yanı ip adresini filan değiştiremiyorum...

Sende projeyi indirip bi derleyip banada kaç hex dosysının kaç kb olduğunu soylermisin..
microc pro 5.6.1 var bende

mur@t

CS ve RST pinlerini siz atayacaksınız, diğerleri (clk,sdi, sdo) SPI pinleridir ve kullandığınız PIC'e göre farklıdır.
mikroC örnekleri için INT ve WOL pinlerini bağlamanıza gerek yok diye hatırlıyorum. Bağlayacaksanız da, bunları da siz atamalısınız. (CS ve RST gibi)

28J60 3.3V ile çalışır, PIC'i de 3.3V ile besliyorsanız ve arada seviye çevirici kullanmadıysanız sigorta ayarlarında brown out voltajı 2.05V yapın.
(önce sigorta ayarlarını default seçin, sonra kullandığınız kristale göre gerekli ayarlamaları yapın, elinizde varsa, 8 veua 10MHz xtal kullanın)

Başka PIC kullanma şansınız varsa (örn: 18F4620) çalışan kod ve örnek gönderebilirim.




muzafferturk

Murat Dostum linki ekleyebilirmisin...

elektronart

Ben en iyisi ebaydan gelecek olanı bekleyim. İlk demelerde nasılsa 1 tane bozacağım, durduk yere 25 lira gitmesin.
Açık Elektronik

frederic

O modüllere yüksek voltaj uygulamadığınız sürece bozamazsınız, alıp deneyebilirsiniz bence.

LukeSkywalker

Acaba projeye eklemediğiniz header dosyaları olabilir mi?

muzafferturk

#12
yok dostum bende veya yazılımda bir sorun var...
Murat arkadaşım dosyayı indirip derlemiş onda olmuş bende olmuyor microc por 5.6.1 yuklu.
yanlız urun orjinal değil ondamıdır acaba ...

LukeSkywalker

Buradaki önemli noktalardan birisi de eğer ENC28j60'ı doğrudan bilgisayara bağlıyorsanız çarpraz kablo kullanmanız gerekliliği.

mur@t

Alıntı yapılan: muzafferturk - 24 Ocak 2014, 15:49:48
yok dostum bende veya yazılımda bir sorun var...
Murat arkadaşım dosyayı indirip derlemiş onda olmuş bende olmuyor microc por 5.6.1 yuklu.
yanlız urun orjinal değil ondamıdır acaba ...

Hatırladığım kadarıyla mikroC'yi yeniden kurunca derleyebilmiştiniz.
Sorun kodu derlemenizde mi? yoksa yüklenmiş kodun çalışmasında mı?