CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

enc28j60 spi read shift 1 bit

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kazanova64



Joined: 10 Apr 2010
Posts: 8

View user's profile Send private message

enc28j60 spi read shift 1 bit
PostPosted: Wed Aug 04, 2010 7:52 pm     Reply with quote

I want to communicate pic18f4620 with enc28j60 with spi. ENC has these instructions.
Quote:

The ENC28J60 is designed to interface directly with the
Serial Peripheral Interface (SPI) port available on many
microcontrollers. The implementation used on this
device supports SPI mode 0,0 only. In addition, the SPI
port requires that SCK be at Idle in a low state;
selectable clock polarity is not supported.Commands and data are sent to the device via the SI
pin, with data being clocked in on the rising edge of
SCK. Data is driven out by the ENC28J60 on the SO
line, on the falling edge of SCK. The CS pin must be
held low while any operation is performed and returned
high when finished.


And the figures of these instructions are:


What I did for set spi with my pic is:
Code:
   
mac_enc_spi_tris_init();  //*0xF93=(*0xF93 | 0b11); *0xF94 = (*0xF94 & 0b11010111) | 0x10; *0xF95=*0xF95 & 0xFC

#use spi(FORCE_HW, DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3,MASTER, MODE=0, SAMPLE_RISE, BITS=8, LSB_FIRST)

When I do compile and run it, read byte is shifted left. For example, I look at pic SI with scope and this is 0x05 but pic read it as 0x02. Or SI is 0x07 but pic read it 0x03.

What is wrong with it? Do you have any suggestions?

Omer
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 05, 2010 12:49 am     Reply with quote

Quote:
LSB_FIRST

The diagrams show that the MSB should be first, but you have it set for
LSB first. That's the most obvious problem.
kazanova64



Joined: 10 Apr 2010
Posts: 8

View user's profile Send private message

PostPosted: Fri Aug 06, 2010 4:49 am     Reply with quote

Ok then, why i can not set it MSB_FIRST??
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Fri Aug 06, 2010 8:47 am     Reply with quote

MSB first, is the _default_ for SPI. You don't have to 'set' anything. The point is you are selecting LSB_FIRST, which is _wrong_.

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 06, 2010 9:06 am     Reply with quote

Quote:
why i can not set it MSB_FIRST??

What's your compiler version ?
kazanova64



Joined: 10 Apr 2010
Posts: 8

View user's profile Send private message

PostPosted: Sat Aug 07, 2010 5:07 am     Reply with quote

Quote:
What's your compiler version ?

i don't exactly know, old one i think. School computers

Although i changed it to below code, it doesn't change.
Code:
#use spi(FORCE_HW, MASTER, MODE=0, SAMPLE_RISE, BITS=8)

I cannot init MAC on enc28j60 because there is an if statement that check a register's LSB is 1. But SPI can't read LSB.Do you think that below code is enough to setup the SPI for ENC, which SPI setup is given above?
Code:
 setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Aug 07, 2010 11:15 am     Reply with quote

Quote:
I don't exactly know, old one i think. School computers

Look at the top of the .LST file. It gives the version, which will be in
this format: x.xxx The .LST file will be in your project directory after
a successful compilation. Also, sometimes there may be a letter suffix
on the end, such as x.xxxb if it's an limited version.

In older versions of the compiler, #use spi() was buggy. It defaulted to
incorrect parameters. For example, it defaulted to LSB_FIRST, which
is wrong for at least 95% of SPI devices. Also, early versions were
very picky about having the certain parameters present in the #use spi
statement. If you left some of them out, it wouldn't compile.


Quote:

Do you think that below code is enough to setup the SPI for ENC, which SPI setup is given above?
Code:
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4)

Based on the SPI mode definitions below, which you should use,
you are using SPI Mode 0.
Code:
// SPI mode definitions.
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)
 

To find the SPI mode used by the ENC28J60, look in the data sheet,
http://ww1.microchip.com/downloads/en/DeviceDoc/39662c.pdf
in this section:
Quote:
4.0 SERIAL PERIPHERAL INTERFACE (SPI)

It says:
Quote:

The implementation used on this
device supports SPI mode 0,0 only.

So you are using the correct mode.
kazanova64



Joined: 10 Apr 2010
Posts: 8

View user's profile Send private message

PostPosted: Sat Aug 07, 2010 5:07 pm     Reply with quote

CCS PCH C Compiler, Version 4.068
My version is quite old i think. I will renew it soon.

SAMPLE_RISE is a problem there because ENC sends data in idle to active statement, so i need to read it in active to idle. Change it and no problem in spi communication.

Thanks a lot and good works.

Omer
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group