SPI communication problem (newbie)


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2008
    Posts
    2

    Red face SPI communication problem (newbie)

    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.

  2. #2

    Default

    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.

  3. #3
    Join Date
    Feb 2008
    Posts
    2

    Smile

    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.

  4. #4

    Default

    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.

  5. #5
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by lm555cn View Post
    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?

Similar Threads

  1. 16-bit SPI problem
    By shaiqbashir in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 19th June 2008, 15:42
  2. Replies: 3
    Last Post: - 22nd January 2008, 03:48
  3. SPI communication ??
    By CosMecc in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 3rd November 2006, 00:22
  4. Replies: 5
    Last Post: - 20th March 2006, 01:34
  5. Replies: 8
    Last Post: - 11th November 2004, 20:08

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts