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

Thread: SPI padding to 8 bits (Teensy 2.0)

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    2

    SPI padding to 8 bits (Teensy 2.0)

    Hi,

    I have a project based around a PGA4311, but I don't seem to be able to work out how to get the SPI data to be padded out to the required 8 bits per channel. So when 10000010 is written, the correct number of bits are sent, but when 01111000 is, only 7 bits are sent (for example). I've tried using the data variable as an uint8_t type, but this hasn't done what I expected (which was, that it would be an 8 bit value, regardless of how many bits are stored). I'm sure i'm missing something rather simple, as without doubt this is a fairly common scenario.

    My function for writing is:
    Code:
    void updatePGA(int newVolume){
      uint8_t scaledVolume = ((PGA_MAX / VOL_MAX) * newVolume);
      Serial.println("New scaled volume:");
      Serial.println(scaledVolume);
      Serial.println(scaledVolume, BIN);
    
      SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
      digitalWrite(slaveSelectPinPGA, LOW);
      for (int iCh = 0; iCh < 4; iCh++) {
        // 4 times, for each channel on the PGA4311
        //Serial.println(iCh);
        SPI.transfer(scaledVolume);
      }
      digitalWrite(slaveSelectPinPGA, HIGH);
      SPI.endTransaction();
      Serial.println("end updatePGA()");
    }
    The output of the above shows that it isn't strictly an SPI issue, as the 7 bit value can be seen in the serial monitor:
    Code:
    12:35:18.645 -> New scaled volume:
    12:35:18.645 -> 120
    12:35:18.645 -> 1111000
    12:35:19.672 -> New scaled volume:
    12:35:19.672 -> 130
    12:35:19.672 -> 10000010

    Thanks

  2. #2
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Posts
    641
    SPI.transfer sends full bytes, by contrast Serial.print will not display leading zeros when showing as a binary value so it's not actually a 7 bit number.

  3. #3
    Junior Member
    Join Date
    Sep 2020
    Posts
    2
    Quote Originally Posted by vjmuzik View Post
    SPI.transfer sends full bytes, by contrast Serial.print will not display leading zeros when showing as a binary value so it's not actually a 7 bit number.
    Oh that is interesting, thanks for the information.

Posting Permissions

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