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 support@ccsinfo.com

PIC18F4620 and SPI

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



Joined: 11 Apr 2007
Posts: 5

View user's profile Send private message

PIC18F4620 and SPI
PostPosted: Wed Apr 11, 2007 7:55 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 11, 2007 8:51 pm     Reply with quote

Oops, I forgot to include the compiler version in the previous post: PCH 3.224
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 11, 2007 9:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 12, 2007 6:09 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 12, 2007 8:53 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 12, 2007 9:31 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 12, 2007 9:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 12, 2007 9:45 am     Reply with quote

Uh oh... the 25LC1024 is holding the SCK line low...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 12, 2007 12:37 pm     Reply with quote

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.
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