Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: How to configure SPI1 on Tennsy3.6

  1. #1
    Junior Member
    Join Date
    Dec 2016
    Posts
    3

    How to configure SPI1 on Tennsy3.6

    Hello,
    I have problem how to configure SPI1 on Tennsy3.6? I try to connect to AD7175.
    Now I configure as below and then i check all pin in oscilloscope nothing happen.
    I test board is work on SPI0 connect to TCP module WIZ820io. Please help me about my mistake.( Thank you very much)

    #include <SPI.h>

    int slaveSelectPin = 31;
    #define ID 0x0CD9

    unsigned int regValue;

    void setup() {
    // put your setup code here, to run once:

    Serial.begin(9600);
    SPI1.setBitOrder(LSBFIRST);
    SPI1.setDataMode(SPI_MODE3);
    SPI1.setClockDivider(SPI_CLOCK_DIV16);
    SPI1.setSCK(32);
    SPI1.setMOSI(0);
    SPI1.setMISO(1);
    SPI1.setCS(31);
    SPI1.begin();
    pinMode(31,OUTPUT);
    pinMode(2,INPUT);
    }

    void loop()
    {

    //delayMicroseconds(1);
    SPI1.transfer(B01000111); // Bit 6 = 1 for read. Register 0x07 for ID register.
    //digitalWrite(slaveSelectPin,HIGH);

    digitalWrite(slaveSelectPin,LOW);
    delayMicroseconds(1);
    byte b1 = SPI1.transfer(0);
    byte b0 = SPI1.transfer(0);
    byte b2 = SPI1.transfer(0);
    digitalWrite(slaveSelectPin,HIGH);


    unsigned int result = b0 * 256 + b2;
    Serial.println(result, HEX);

    delay(1000);

    }
    Attached Files Attached Files

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,723
    Sorry, I don't know if I see anything obvious that would keep it from working at all. What Arduino/Teensyduino? pins actually connected (pictures help catch things like pins not soldered in...)

    Have you tried running it without your hardware connected to see if you get any signals, for example on the SCLK on Pin 31? Is anything printing, so we know the program is running...

    Some thoughts. I believe that some of these methods set some information in some of the registers that then get blasted by the call to begin. In particular the calls:
    Code:
    SPI1.setBitOrder(LSBFIRST); 
    SPI1.setDataMode(SPI_MODE3); 
    SPI1.setClockDivider(SPI_CLOCK_DIV16);
    Better yet replace these with calls to SPI1.beginTransfer(...) endTransfer
    So try putting them after the call to begin.

    The call to set
    Code:
    SPI1.setCS(31);
    Is not needed as the SPI calls to transfer do not use the hardware SPI pins...
    You may want to also do a digitalWrite of that pin HIGH during setup to have it not asserted.

    Not sure of your code in loop is fully correct on usage of CS pin. I have not looked briefly at the spec for the chip you are using. I am not sure but this may be one of those chips that for read operations, that after you assert the CS, you then look at the state of one of the pins MISO? to see when the chip says it is OK to transfer. If so there have been a couple threads that talked about this, with a couple of options, like switch that momentarily to digital input, read, switch back to SPI, alternatively also connect that pin to another unused IO pin, that you configure to input and check the state there...

    Hopefully someone else who has used this chip will chime in.. But if not, maybe the loop needs to look more like:
    Code:
    loop()
    {
    
      digitalWrite(slaveSelectPin,LOW); // probably need to select for this transfer
      SPI1.transfer(B01000111); // Bit 6 = 1 for read. Register 0x07 for ID register.
      
      // Not sure if chip needs the chip select to go deassert after command and then reassert or wants
      // it asserted through transfer.  If it wants to toggle then you need. 
      digitalWrite(slaveSelectPin,HIGH);
      delayMicroseconds(1);   // may or may not need delay... 
      digitalWrite(slaveSelectPin,LOW);
    
      // I don't think you need delays between cs and starting transfer but may need to either delay or check
      // state of IO pin mentioned above.
      delayMicroseconds(1);
      byte b1 = SPI1.transfer(0);
      byte b0 = SPI1.transfer(0);
      byte b2 = SPI1.transfer(0);
      digitalWrite(slaveSelectPin,HIGH);
    
    
      unsigned int result = b0 * 256 + b2;
      Serial.println(result, HEX);
    
    delay(1000);
    
    }
    Again I don't know enough about that chip to know if any of the above will help.

    One of the other threads talking about checking the MISO pin: https://forum.pjrc.com/threads/41608-reading-MISO
    Last edited by KurtE; 01-28-2017 at 03:20 PM.

Posting Permissions

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