View previous topic :: View next topic |
Author |
Message |
kazanova64
Joined: 10 Apr 2010 Posts: 8
|
enc28j60 spi read shift 1 bit |
Posted: Wed Aug 04, 2010 7:52 pm |
|
|
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
|
|
Posted: Thu Aug 05, 2010 12:49 am |
|
|
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
|
|
Posted: Fri Aug 06, 2010 4:49 am |
|
|
Ok then, why i can not set it MSB_FIRST?? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Aug 06, 2010 8:47 am |
|
|
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
|
|
Posted: Fri Aug 06, 2010 9:06 am |
|
|
Quote: | why i can not set it MSB_FIRST??
|
What's your compiler version ? |
|
|
kazanova64
Joined: 10 Apr 2010 Posts: 8
|
|
Posted: Sat Aug 07, 2010 5:07 am |
|
|
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
|
|
Posted: Sat Aug 07, 2010 11:15 am |
|
|
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
|
|
Posted: Sat Aug 07, 2010 5:07 pm |
|
|
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 |
|
|
|