Electronic – Intermittent SPI bus

eeprompicspi

I'm attempting to interface a PIC18F4520 to a 25LC640 via SPI protocol. I'm using the built in MSSP hardware of the PIC. The PIC18F4520 is the only master on the bus. I'm reading 16 bytes of data from the EEPROM 32 times per second. I've double-checked the following items:

  1. TRIS Registers for SDI, SDO, SCK, and CS
  2. Baud Rate ~ 1 Mhz
  3. SPI Mode 0,0

I've checked the signals on an oscilloscope and everything looks fine. I've tried using a BusBee to log the data coming from the EEPROM and 99% of the time it's correct. Every once in a while there are a string of requests where the MOSI (SDO) line does not seem to contain the correct EEPROM read instruction causing the data being clocked into the micro to be invalid. This happens even though I'm writing the same read instruction in the SSPBUF each time. What else can go wrong with a SPI bus?

Best Answer

In the past I have noticed that some of the PIC datasheets do not correctly show the SPI CPOL and CPHA registers, there was some issues that when looking at the output on a scope, two of the four combinations were back to front from what was expected. So double check you are actually getting the expected waveform out of your PIC and that the waveform matches what is needed on the EEPROM.

I have also come across some A/D converters that worked intermittently when I had incorrect SPI settings on a HC12, drove me banana's trying to work it out, was one of my first projects as a professional engineer, eventually I worked it out. But I digress. So it is possible to get intermittent problems using SPI bus due to incorrectly set CPOL/CPHA. As it is a shift register after all, and the last bit on one character may be being read of the first of the next character. This type of problem may not be noticed straight away either.

So ensure you have the correct settings on a scope, and do not relay on just setting the registers of the PIC.