SPI padding to 8 bits (Teensy 2.0)

Status
Not open for further replies.

wally2112

New member
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
 
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.
 
Status
Not open for further replies.
Back
Top