View previous topic :: View next topic |
Author |
Message |
pireau
Joined: 11 Apr 2007 Posts: 5
|
PIC18F4620 and SPI |
Posted: Wed Apr 11, 2007 7:55 pm |
|
|
Hello,
I'm trying to use a 25LC1024 (1MBit, SPI EEPROM) with the PIC18F4620.
The line SCK seams allright SDO never moves (it's stuck low). At first I taught it was a open-drain output so i tried to pull it high but that wasn't the problem...
Here are the fuses and test code:
Code: |
#fuses NOWDT,EC, NOPROTECT, NOIESO, BROWNOUT, BORV46, PUT, NOCPD, NOSTVREN
#fuses NODEBUG, NOLVP, NOWRT, NOWRTD, NOEBTR, NOCPB, NOEBTRB, NOWRTC, NOWRTB
#fuses NOFCMEN, NOXINST, NOPBADEN, LPT1OSC, MCLR
[...]
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4);
[...]
printf("SPI Write Byte.\r\n");
while(1)
{
printf("SPI Write Byte.\r\n");
spi_write(BYTE_WRITE);
spi_write(0x00); spi_write(0x00); spi_write(0x00);
spi_write(0x55);
printf("Ecrit 0x55 @ 0x000000.\r\n");
spi_write(BYTE_READ);
spi_write(0x00); spi_write(0x00); spi_write(0x00);
printf("Lu: %02X @ 0x000000\r\n", spi_read());
}
|
Any help will be appreciated. |
|
|
pireau
Joined: 11 Apr 2007 Posts: 5
|
|
Posted: Wed Apr 11, 2007 8:51 pm |
|
|
Oops, I forgot to include the compiler version in the previous post: PCH 3.224 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 11, 2007 9:01 pm |
|
|
Quote: | printf("Lu: %02X @ 0x000000\r\n", spi_read()); |
I didn't look closely at your code, but I noticed one problem.
The PIC is the master, and that means it must supply the SCLK for
all SPI operations. In CCS, to make the spi_read() function supply
a clock, you have to give it a parameter. Example:
Code: | data = spi_read(0); |
The parameter is the value that will be shifted out on SDO while data
from the slave is shifted in on SDI. |
|
|
pireau
Joined: 11 Apr 2007 Posts: 5
|
|
Posted: Thu Apr 12, 2007 6:09 am |
|
|
Okay, I'll try that, but the data doesn't get shifted when I call spi_write(0x55) ... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 12, 2007 8:53 am |
|
|
Your posted code is not the best for testing the operation of the
spi_write() function. That's because the spi_read() function waits
in a loop and tests the "Buffer Full" bit. If it's stuck in that loop,
your code will appear to lock up.
Change your while() loop to this:
Code: | while(1)
{
spi_write(0x55);
delay_us(100);
} |
Also, remove all the code in front of that loop, except for the setup_spi()
and the initial printf() statement. That way, if any other code is causing
a problem with the SPI module, it won't be there in this test.
If this test fails,
1. Make sure you're looking at the correct pins for SCK and SDO.
2. Post your compiler version. |
|
|
pireau
Joined: 11 Apr 2007 Posts: 5
|
|
Posted: Thu Apr 12, 2007 9:31 am |
|
|
The data seems to come out at the right frequency (That is, 10MHz, my clock is 40MHz).
The clock line is always low... I verified the PIC's pinout and it matches my board.
thanks a lot for time.
I'm using PCH 3.224 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 12, 2007 9:42 am |
|
|
Try a lower SCLK frequency. Use a higher divisor value in your
setup_spi() statement. See if that works. Also, disconnect all
external circuits from the SCLK pin. It's possible that something
is holding it a low level. |
|
|
pireau
Joined: 11 Apr 2007 Posts: 5
|
|
Posted: Thu Apr 12, 2007 9:45 am |
|
|
Uh oh... the 25LC1024 is holding the SCK line low... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 12, 2007 12:37 pm |
|
|
Check the connections to the eeprom (including power and ground).
Also try another eeprom chip. That one may be bad.
Also, make sure that you understand how the connections should be
done with SPI.
1. SCK on the PIC goes to SCK on the eeprom.
2. SDO on the PIC goes to SI on the eeprom.
3. SDI on the PIC goes to SO on the eeprom.
4. Use some suitable pin (specified in the eeprom driver file)
to connect from the PIC to the \CS pin on the eeprom.
5. Connect \WP and \HOLD to Vcc on the eeprom, to disable them.
6. Vcc on the eeprom must be connected to +5v (or whatever
voltage is used to run the PIC).
7. Gnd on the eeprom must be connected to Ground.
8. A ground connection is needed between the PIC and the eeprom. |
|
|
|