I also found standalone code how to communicate over SPI.
I have written this code,trying to interface a PIC18F4550 with a wiznet811mj.The problem is that, when I'm trying to ping the wiz, the answer is destination unreachable.And those two leds(green and yellow are off).I've tried both..conecting with an utp cable to switch,and direct connection with crossover to my pc..the same problem.
#include <stdio.h>
#include "p18f4550.h"
#include <string.h>
#include <stdlib.h>
#pragma config FOSC = INTOSC_HS
#pragma config WDT = OFF
#pragma config MCLRE = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
#define WIZNET_WRITE_OPCODE 0xF0
#define WIZNET_READ_OPCODE 0x0F// Wiznet W5100 Register Addresses
#define MR 0x0000 // Mode Register
#define GAR 0x0001 // Gateway Address: 0x0001 to 0x0004
#define SUBR 0x0005 // Subnet mask Address: 0x0005 to 0x0008
#define SAR 0x0009 // Source Hardware Address (MAC): 0x0009 to 0x000E
#define SIPR 0x000F // Source IP Address: 0x000F to 0x0012
#define RMSR 0x001A // RX Memory Size Register
#define TMSR 0x001B // TX Memory Size Register
#define DEL_1us 16
#define DEL_10ms 80000
#define DEL_1ms 8000
void InitializePIC(void);
void Delay(unsigned int N);
void SPI_Write(unsigned int addr,unsigned char data);
unsigned char SPI_Read(unsigned int addr);
void W5100_Init(void);
void InitializePIC(void)
{
ADCON1 = 0X0F;
TRISAbits.TRISA5 = 0; // NU STIU DACA E LEGAT ??
TRISDbits.TRISD0=0;
TRISDbits.TRISD1=0;
TRISDbits.TRISD7=0;
TRISCbits.TRISC0=0;//RESET
PORTDbits.RD0=0;
PORTDbits.RD1=0;
PORTDbits.RD7=0;
TRISCbits.TRISC7 = 0; // SDO
TRISBbits.TRISB0 = 1; // SDI
TRISBbits.TRISB1 = 0; // Sck out in master mode
SSPSTATbits.SMP = 0;//smp=0 AND CKE=1
SSPSTATbits.CKE = 1;
SSPCON1bits.SSPEN = 1;//enable SPI
SSPCON1bits.WCOL = 0;
SSPCON1bits.SSPOV = 0;
SSPCON1bits.CKP = 0;
SSPCON1bits.SSPM0 = 0;//Fosc/4
SSPCON1bits.SSPM1 = 0;
SSPCON1bits.SSPM2 = 0;
SSPCON1bits.SSPM3 = 0;
RCONbits.IPEN = 1;
IPR1bits.SSPIP = 0;
PIE1bits.SSPIE = 0;
PIR1bits.SSPIF = 0;
INTCON = 0xC0;
OSCCON=0x72;// 8 mhz
}
void Delay(unsigned int N) {
unsigned int i;
for(i=0; i<N; i++);
Nop();
}
void SPI_Write(unsigned int addr,unsigned char data)
{
// Activate the CS pin
PORTAbits.RA5 = 0;
// Start Wiznet W5100 Write OpCode transmission
SSPBUF = WIZNET_WRITE_OPCODE;
while(!PIR1bits.SSPIF); // Start Wiznet W5100 Address High Bytes transmission
PIR1bits.SSPIF = 0;
SSPBUF = ((addr & 0xFF00) >> 8) ; // Wait for transmission complete
while(!PIR1bits.SSPIF); // Start Wiznet W5100 Address Low Bytes transmission
PIR1bits.SSPIF = 0;
SSPBUF= addr & 0x00FF; // Wait for transmission complete
while(!PIR1bits.SSPIF);
PIR1bits.SSPIF = 0;
SSPBUF = data; // Wait for transmission complete
while(!PIR1bits.SSPIF);
PIR1bits.SSPIF = 0;
// CS pin is not active
PORTAbits.RA5 = 1;
}
unsigned char SPI_Read(unsigned int addr)
{
// Activate the CS pin
PORTAbits.RA5 = 0;
// Start Wiznet W5100 Read OpCode transmission
SSPBUF = WIZNET_READ_OPCODE; // Wait for transmission complete
while(!PIR1bits.SSPIF); // Start Wiznet W5100 Address High Bytes transmission
PIR1bits.SSPIF = 0;
SSPBUF = (addr & 0xFF00) >> 8; // Wait for transmission complete
while(!PIR1bits.SSPIF); // Start Wiznet W5100 Address Low Bytes transmission
PIR1bits.SSPIF = 0;
SSPBUF = addr & 0x00FF; // Wait for transmission complete
while(!PIR1bits.SSPIF);
PIR1bits.SSPIF = 0;
// Send Dummy transmission for reading the data
SSPBUF = 0x00; // Wait for transmission complete
while(!PIR1bits.SSPIF);
PIR1bits.SSPIF = 0;
// CS pin is not active
PORTAbits.RA5 = 1 ;
return(SSPBUF);
}
void W5100_Init(void)
{
// Ethernet Setup
unsigned char mac_addr[] = {0x00,0x08,0xDC,0x00,0x00,0x24};
unsigned char ip_addr[] = {192,168,0,2};
unsigned char sub_mask[] = {255,255,255,0};
unsigned char gtw_addr[] = {192,168,0,1};
PORTDbits.RD0=1;
SPI_Write(MR,0x80);
SPI_Write(GAR + 0,gtw_addr[0]);
SPI_Write(GAR + 1,gtw_addr[1]);
SPI_Write(GAR + 2,gtw_addr[2]);
SPI_Write(GAR + 3,gtw_addr[3]);
Delay(DEL_1ms);
SPI_Write(SAR + 0,mac_addr[0]);
SPI_Write(SAR + 1,mac_addr[1]);
SPI_Write(SAR + 2,mac_addr[2]);
SPI_Write(SAR + 3,mac_addr[3]);
SPI_Write(SAR + 4,mac_addr[4]);
SPI_Write(SAR + 5,mac_addr[5]);
Delay(DEL_1ms);
SPI_Write(SUBR + 0,sub_mask[0]);
SPI_Write(SUBR + 1,sub_mask[1]);
SPI_Write(SUBR + 2,sub_mask[2]);
SPI_Write(SUBR + 3,sub_mask[3]);
Delay(DEL_1ms);
SPI_Write(SIPR + 0,ip_addr[0]);
SPI_Write(SIPR + 1,ip_addr[1]);
SPI_Write(SIPR + 2,ip_addr[2]);
SPI_Write(SIPR + 3,ip_addr[3]);
Delay(DEL_1ms);
// Setting the Wiznet W5100 RX and TX Memory Size, we use 2KB for Rx/Tx 4 channels
SPI_Write(RMSR,0x55);
SPI_Write(TMSR,0x55);
PORTDbits.RD1=1;
}// Assign I/O stream to UART
////////////////
void main(void) {
unsigned int i;
unsigned int gar0;
PORTCbits.RC0=0;
Delay(DEL_1us);
PORTCbits.RC0=1;
Delay(DEL_10ms);
InitializePIC();
PORTAbits.RA5 = 1;
W5100_Init();
Bogdan Baluta
all i want is the Remote IP address (source what's the point, you set that yourserlf, your own local IP) i want the Remote IP
Thanks.