element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Sending data using SPI
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 3 replies
  • Answers 1 answer
  • Subscribers 416 subscribers
  • Views 980 views
  • Users 0 members are here
  • bit
  • shift
  • spi.transfer
  • micro
  • register
  • arduino
  • 16
Related

Sending data using SPI

e14 Contributor
e14 Contributor over 13 years ago

Hi all,

 

I need help sending data to a chip using the arduino SPI. The chip I am working with is the LMP91200 (Here is the link for the spec sheet  http://www.ti.com/lit/ds/symlink/lmp91200.pdf, see bottom of page 18) whose serial interface uses a 16 bit shift register to recieve data. The arduino on the other hand uses SPI.transfer which sends in a byte at a time. Does anyone have any clue on how I can send in my data properly? For example I want to send in the 0000011100000000. I am using an Arduino micro.

 

Thanks,

Omar

  • Sign in to reply
  • Cancel
Parents
  • shabaz
    0 shabaz over 13 years ago

    Often, if a target device needs a non-standard (i.e. not 8-bit) serial stream, then it is easier to just write a simple routine to

    'bit-bang' the data yourself.

    Basically, you need to translate the requirements from the timing diagram in the datasheet into code that will manipulate the

    I/O to achieve the correct interface.

     

     

    Here is an example. It's not an efficient method, but is a useful hack at times.

    Note this is just a simple pseudocode example, not necessarily matching the requirements of your device. You can see that the

    function accepts two bytes, and transmits them one after the other.

     

    void write(unsigned char val[2])

    {

      unsigned char i, v;

      for (v=0; v<2; v++)

      {

        for (i=0; i<8; i++)

        {

          if (val[v] & 0x80)

          {

            DATA_SET_HIGH;

          }

          else

          {

            DATA_SET_LOW;

          }

          msec_delay(1);

          CLOCK_SET_HIGH;

          msec_delay(1);

          CLOCK_SET_LOW;

          msec_delay(1);

          val[v] <<= 1;

        } // end for (i..

     

      } // end for (v..

      msec_delay(10); // delay after two bytes

    }

     

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to shabaz

    I essentially did what you suggested, here is what I have for my code

     

    #include <SPI.h>

    int i=0;

    byte b[] = {0, 128};

    void setup()

    {

      SPI.begin();

      SPI.setDataMode(SPI_MODE1);

      SPI.setClockDivider(SPI_CLOCK_DIV4);

      SPI.setBitOrder(MSBFIRST);

      digitalWrite(SS, LOW);

    }

     

    void loop()

    {

      for(i = 0; i<2; i++){

          SPI.transfer(b[i]);

          delay(10);

    }

      delay(100);

      digitalWrite(SS,HIGH);

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 13 years ago in reply to e14 Contributor

    That does look approx. right. (I'm not super-familiar with Arduino but I did take a look at the SPI functions on the arduino.cc site).

     

    I suspect it may be CPOL, because of this text in the datasheet:

    "The SCK signal must be high during the falling and rising edge of CSB." (in your case, I'm presuming SS is connected to the CSB pin).

     

    According to the page here, if CPOL was set to 1, then the clock is by default set high unless you are transferring data.

    So, I suggest changing your mode to SPI_MODE to 3. With that, the data is clocked on the rising edge of SCK, and SCK is high

    when no data is being transmitted.

    Hopefully it resolves your issue.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • shabaz
    0 shabaz over 13 years ago in reply to e14 Contributor

    That does look approx. right. (I'm not super-familiar with Arduino but I did take a look at the SPI functions on the arduino.cc site).

     

    I suspect it may be CPOL, because of this text in the datasheet:

    "The SCK signal must be high during the falling and rising edge of CSB." (in your case, I'm presuming SS is connected to the CSB pin).

     

    According to the page here, if CPOL was set to 1, then the clock is by default set high unless you are transferring data.

    So, I suggest changing your mode to SPI_MODE to 3. With that, the data is clocked on the rising edge of SCK, and SCK is high

    when no data is being transmitted.

    Hopefully it resolves your issue.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube