PDA

View Full Version : SPI woes



Charles Linquis
- 25th September 2013, 23:20
I'm trying to talk to an accelerometer via software SPI, but can't seem to get the SHIFTIN command working.





iSDO = 0

AccRegister = $F

ACC_Chip_SEL = 0
AccRegister.7 = 1
AccRegister.6 = 0
shiftout SDo,SPICL,5,[AccRegister]; 5 is the mode
shiftin SDI,SPICL,5,[SPIData]
ACC_Chip_SEL = 1

hserout [cr,lf,hex2 SPIData]



A screenshot of what I get out of my logic analyzer is at:

www.charleslinquist.com/SPI.jpg

The SHIFTOUT command appears to be working well, but the SHIFTIN command shows "0" even though the logic analyzer shows that 0x32 is being returned.
What am I doing wrong?

HenrikOlsson
- 26th September 2013, 06:08
Can you read the state of the SDI-pin manually?
I'm thinking perhaps there's some other function enabled on that pin preventing it from being read properly. Though if analog functions are enabled the pin usually reads high, not low...
If you CAN read it manually then that's probably not the case. Are you probing directly at the input pin on the PIC itself and is that PIN actually the correct one? Or are you probing at the output pin of the accelerometer?

Which PIC are you using?
Which pins are you using?

/Henrik.

Charles Linquis
- 26th September 2013, 17:27
I'm using a PIC 18F8723 and Pin H.2 for SDI. It is tristated, and the external memory function is turned OFF.

HenrikOlsson
- 27th September 2013, 06:56
Hi Charles,
I can't help thinking it's a hardware issue.
Can you read the state of pin "manually" and get the correct state, perhaps the input is damaged?
Are you probing directly on the pin on the PIC, perhaps there's a bad solder joint or PCB track?

/Henrik.

languer
- 27th September 2013, 07:24
Maybe I read the manual wrong, but in Mode5 yhr data is clocked on the falling edge (right before the falling edge); which could potentially give you a 0x10 reading (based on your picture). I don't think that directly answers your question, but you may as well make sure you are using the right mode (and the analyzer picture matches that mode).

Charles Linquis
- 27th September 2013, 14:04
I found part of the problem: The accelerometer is running at 2.5 V, the PIC is running at 5. I was using a Tiny Logic buffer to do the level shifting. The buffer was a Schmitt trigger type with a 2.2V threshold. The accelerometer output didn't always reach that level and the output didn't toggle reliably. I'm getting data through the buffer now (I raised the accelerometer Vcc to 3V by choosing a different LDO regulator for it). But the data still isn't right. Languer is probably right, and I'll try changing the mode.

I don't know why the PBP manual can't use normal datasheet terms, such as "the data is sampled on the clock rising edge" or something like that instead of the way they describe it!