Hi, I'm relatively new to programming, but not hardware. I'm trying to use the SPI library but I'm having issues. What i need to do is send a 10 bit word to an IC (cs4192, it's an air core driver) if i use SPI.transfer(variable); MSB first it will not transfer the full width of the data type (unsigned int, int whatever i choose) and therefore it will throw the gauge off. for example if I want to tell it position 2, it would send "10" binary, instead what I need is "0000000010". I'm a complete noob here and I have no clue where to even start, I've been searching for "zero padding spi" but have turned up empty. Any help is greatly appreciated, an explanation or a turn in the right direction is even better.
Sounds like you should be using a variable of type uint_16t. Set that variable to 2 (or whatever you want) and then have the Arduino send the variable over SPI.
Can you post your code?
Power_Broker:
Sounds like you should be using a variable of type uint_16t. Set that variable to 2 (or whatever you want) and then have the Arduino send the variable over SPI.Can you post your code?
here is my code, I will change the data type right now and try that.
you know what, after that suggestion i did some digging, i found a SPI.transfer16(); which includes the leading zero's now it works perfectly. Thank you for sparking that!
#include <SPI.h>
const int slavePin = 53;
SPISettings settings(20000, MSBFIRST, SPI_MODE1);
int gaugePos = 1024;
int stepAmount = 1;
void setup() {
// set the Slave Select Pin as an output:
pinMode (slavePin, OUTPUT);
// initialize SPI:
SPI.begin();
}
void loop() {
SPI.beginTransaction(settings);
digitalWrite (slavePin, HIGH);
SPI.transfer(gaugePos);
digitalWrite (slavePin, LOW);
SPI.endTransaction();
delay(1000);
gaugePos = gaugePos + stepAmount;
delay(100);
// reverse the direction of the gauge at the ends of the travel:
if (gaugePos <= 0 || gaugePos >= 1024) {
stepAmount = +stepAmount;}
}