PDA

View Full Version : SPI communication problem (newbie)



lm555cn
- 29th February 2008, 18:23
Hello everyone. I have a device which communicates through SPI, but I don't know how to do it or what I'm doing wrong. I'm using a PIC16F84, all the connections are direct, meaning from the PIC to the SPI there are no resistors.

The information on the SPI device is as follows:

Connections:
------------
VCC
Data
Clock
GND
SelectKey (When pulled low it will measure continuously)
Timeoue >2 ms

The timing diagram for SPI describes that between each bit there should be 500us for the clock cycle. And the information is MSB first.

I have written the following code to test it, with no luck.

DEFINE OSC 10
INCLUDE "modedefs.bas"


TX VAR PORTA.0
RX VAR PORTA.1

CS VAR PORTB.0 'Select low or high to activate This is for the SelectKey connection
CLK VAR PORTB.1 'Clock
DI VAR PORTB.2 'Data Input
DO VAR PORTB.2 'Data Output

BYTEIN VAR WORD

ATM CON $32 'CODE
TMSB CON $AC 'MSB
LLSB CON $F3 'LSB

START
GOSUB EREAD
GOTO START


EREAD
Low CS
Shiftout DI,CLK,MSBFIRST,[ATM,TMSB,LLSB]
Shiftin DO,CLK,MSBPRE,[BYTEIN]
High Cs
SEROUT2 TX,16468,[BYTEIN,10,13] '9600-8-N-1
PAUSE 3000
RETURN
END
------------------------------------------------------------------------------

Thanks in advance for your help.

falingtrea
- 29th February 2008, 20:14
What you have to watch out for with SPI is there are specific setup requirements. Either the part clocks data in on rising edge and out on falling edge, or in on falling edge and out on rising edge. Also the clock is either idle low or idle high. This is controlled by the mode byte in the shiftin and shiftout commands.

The next thing is whether the SPI part is unidirectional or biderectional. If there is only one data pin it is unidirectional and you need only one I/O pin. If it is bidirectional
it will have seperate data in and data out pins. You would need to use two different I/O pins for this type.

The other issue you may run into with SPI is that data may need to be shifted in and out at the same time, if the part is bidirectional. So the basic shiftin and shiftout commands wouldn't work. If the part is unidirectional, you need to send a command, using shiftout, then read data, using shiftin.

lm555cn
- 1st March 2008, 15:58
Thank you very much for your answer falingtrea. I forgot to mention on the first post I'm using PicBasic Pro from microengineering labs with a PIC16F84 and a 10MHz crystal. I'm powering the PIC with 5 volts and the SPI sensor with 3.1 volts. It's still not clear to me if just by using the shiftin and shiftout commands I get the 2KHz that the datasheet specifies for the clock, or if there's a formula on how to calculate that depending on your crystal.

Now, from your reply I believe I have the following:

1) From what I understand the part clocks data in on the falling edge and out on rising edge.
2) The part is unidirectional.
3) I don't know I hope I can use the shiftin and shiftout commands.

I would like to attach part of the datasheet for what we are discussing in case you have some spare time to look at it and are willing to help me in case I'm not explaining myself clear or I'm confusing things, but unfortunately I don't see the option for it.

Kind regards.

falingtrea
- 3rd March 2008, 16:23
One thing you should do is preset the CLK and Data lines before calling Shiftout or Shiftin. Just set the clock to output and the correct idle state level, and Data to an input. Some parts could be picky about the starting state before you drop CS. Also, you didn't mention the clock idle state for the part you are using. That is very important also.

I would also just define one data pin variable. Since it is unidirectional, you only have one data pin anyway. Less confusing that way.

And is the output high level of the SPI device high enough for the PIC to accept as a high? The portB pins you are using are TTL and need at least 2V for a high.

skimask
- 3rd March 2008, 16:33
SPI sensor with 3.1 volts.

SPI sensor... Kinda vague, dontcha think?
What are you trying to communicate with?
An SPI capable frog or what?