Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 15th January 2008, 08:28 PM   (permalink)
Default SPI Testing with ENC28J60

Hi
I have connected my PIC18LF252 to an ENC28J60 via SPI.

I want to test that the SPI is working. I have written a routine to query one of the ENC28J60 register...but I'm not getting anything back.

Can someone shed any light on the ENC28J60 and its SPI interface...

THanks

Code:
unsigned int spi_rxbuf;


void spi_rw(void);
void init_spi(void);


void main(void)

{
	
	TRISA=0b00001011;	//AN0,AN1,AN3 =>Inputs
	TRISC=0b10010000;	//RC7(RX), RC4(SDI) =>Inputs.

	
	init_spi();
	LED1 = ON;


	while(1)
	{
		spi_rw();
	}
}


void init_spi(void)		//Setup SPI comms
{
	SSPSTAT=0b01000000;	//SPI data at middle of output time,
				//Data tx on RISING edge of SCK *ENC28J60
	SSPCON1=0b00100001;	//Fosc/16, SSPEN=enabled, 
}
	

void spi_rw(void)
{
	CS = LOW;		//Start 
	SSPBUF = 0b00011110;	//Read (000) ECON2 at addr 0x1E (11110)
	spi_rxbuf = SSPBUF;
	CS = HIGH;		//Stop
}
__________________
"//Some day we'll look back and Laugh"
mikesmixes777 is offline   Reply With Quote
Old 16th January 2008, 12:25 AM   (permalink)
Default

You need till the end of the transfer before reading SSPBUF.

Code:
/* writes to SPI. BF is checked inside the procedure */
/* returns SSPBUF	*/ 
BYTE wr_SPI ( BYTE data )
{
 SSPBUF = data;          		// write byte to SSPBUF register
 while( !SSPSTATbits.BF ); 	// wait until bus cycle complete 
 return ( SSPBUF );         //
}
felis is offline   Reply With Quote
Old 16th January 2008, 06:48 AM   (permalink)
Default

I'm not sure if its working properly now, I'm not sure of the data received back. Are there any constant registers that I can poll in the ENC28J60 that I will know what to expect?

Code:
	while(1)
	{
		spi_rw(0x1E);		//0b00011110
		
	}	


char spi_rw(char data)
{
	CS = LOW;
	CS = LOW;			//Start 
	SSPBUF = data;			//Read (000) ECON2 at addr 0x1E (11110
	while (!SSPSTATbits.BF);
	return (SSPBUF);
	CS = HIGH;			//Stop
}
__________________
"//Some day we'll look back and Laugh"
mikesmixes777 is offline   Reply With Quote
Old 16th January 2008, 07:18 AM   (permalink)
Default

Why is CS = LOW done twice in a row?

The ENC28J60 has a hardware revision register, or you could try to flash its LEDs.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline   Reply With Quote
Old 16th January 2008, 07:22 AM   (permalink)
Default

Quote:
Originally Posted by mikesmixes777
I'm not sure if its working properly now
It's not - the 'CS=HIGH' will never execute. Place it before 'return'.

As far as registers - write 0x05 to MACON1 and read it back.
felis is offline   Reply With Quote
Old 16th January 2008, 07:53 AM   (permalink)
Default

I'm REALLY new to this SPI so you have to exscuse my lack of understanding...

Im trying to get the steps to write to MACON1...

I see MACON1 is in BANK2 & addr 00h (pg12)

Now to select BANK2 I need to write to the ECON1, but when do i actually send 010(op-code) 00000 (addr of MACON1) 00000101 (data) ?
__________________
"//Some day we'll look back and Laugh"
mikesmixes777 is offline   Reply With Quote
Old 16th January 2008, 02:30 PM   (permalink)
Default

The ENC28J60 is a tricky chip to use, there is a C library for it that you should use you can download it here.
http://www.ljcv.net/home.html
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline   Reply With Quote
Old 16th January 2008, 02:36 PM   (permalink)
Default

Ahh... I am actually just on the site at the moment...Thanks for the link...

I came to the conclusion that it is a VERY tricky chip. I been looking thru all the project details at this site and I found that there applications are very indepth, even the mini10T application (http://www.ljcv.net/projects/minipic10t/index.html).

I want to have my own html webpage aswell as log some analog inputs and control digital outputs...but they are running "multi-layer" programs and to see where or how i can add my application and code in seems a nightmare.

Any suggestions...Is there a plain ENC28J60 driver generic for the PIC18F...?

Thanks for the help.
__________________
"//Some day we'll look back and Laugh"
mikesmixes777 is offline   Reply With Quote
Old 16th January 2008, 03:01 PM   (permalink)
Default

That is the generic version. Writing an Ethernet stack is no cakewalk.
An ENC28J60 / 18F4620 is likely the next kit coming out from us. I've got a working prototype just working on the "super I/O" part.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline   Reply With Quote
Old 17th January 2008, 07:41 AM   (permalink)
Default

I agree that writing an ethernet stack and driver for the ENC28J60 is NOT a cakewalk.

Will I have any luck with my small 18F252? I was thinking of adding a 25LC256 SPI eeprom.

So would your advice be to get my project up and running?
Thanks
Mike
__________________
"//Some day we'll look back and Laugh"
mikesmixes777 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
DS1302 Real time clock questions, PIC18F2620 SPI mode and mcu memory usage ssylee Micro Controllers 2 18th December 2007 06:37 AM
Both parallel and spi in PIC microcontroller PLEASE HELP jitun2 Micro Controllers 12 24th October 2007 02:08 PM
Sytem Testing -=GST=- Nemisis (cs/cz) Electronic Projects Design/Ideas/Reviews 3 3rd April 2006 01:00 PM
testing a thermocouple evandude General Electronics Chat 8 29th October 2004 04:25 PM
Capacitor testing zachtheterrible General Electronics Chat 9 31st May 2004 06:20 AM



All times are GMT. The time now is 02:42 PM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.