Go Down

Topic: Setting 9 Bit Serial Register with SPI-Library (Read 1 time) previous topic - next topic

xlaura_htlx

Nov 15, 2016, 10:12 am Last Edit: Nov 15, 2016, 07:18 pm by xlaura_htlx
Hey there,

I am an engineering student from Austria and I'm currently working on a project with my Arduino UNO. I need to communicate via 4-wire-SPI with the AD5262 digital potentiometer to divide voltage.
My problem is, that I can't figure out how to set the 9 bit serial register (see attached datasheet). As far as I know the command SPI.transfer() from the SPI-library is only transferring 8 bit. To set the register properly I need to transfer 8 data bits and 1 address bit, MSB First. What I tried in the code below was to transmit the address bit first with the SPI.transmit() command, and then transmit the data byte.  It didn't work. I checked it with a logic analyzer (see attached pictures), but the register was never set properly.
The next thing I thought I could try is bit banging, what do you think about it, could it work?

This is my code:
Code: [Select]
const int CS = 10;
const int PR = 5;
const int SHDN = 6;

void setup() {
  pinMode(CS, OUTPUT); //select Pin 10 as output
  pinMode(PR, OUTPUT);
  pinMode(SHDN, OUTPUT);
  SPI.setDataMode(SPI_MODE3);
  SPI.begin();  // initialize SPI
}

void loop() {
  digitalPotiWrite(0, 255);

}

void digitalPotiWrite(int channel, float value) {
  digitalWrite(PR, HIGH);
  digitalWrite(SHDN, HIGH);
  digitalWrite(CS, LOW); //data can be loaded into the register
  SPI.transfer(channel); //Poti Channel, 0 = RDAC1, 1 = RDAC2
  SPI.transfer(value); //Poti value
  digitalWrite(CS, HIGH); //end of communication
}


I hope you can help me and I'm looking forward to your comments
Many thanks

Laura

xlaura_htlx


CrossRoads

Yes, looks like bit banging could be the way to go.
Browse around in the Arduino code and find the shiftOut() code, tweak it a little for 9 bits vs 8.
Or just write it, it isn't complicated.

Are you sure you're sending the A0 bit first?
I'd go with
Code: [Select]

digitalWrite (csPin, LOW);
SPI.transfer(byteWithA0bit);
SPI.transfer(otherByte);
digitalWrite (csPin, HIGH);

and see if just the last 9  bits clocked in behaved as expected.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Also,
These 2 lines need to be swapped


  SPI.setDataMode(SPI_MODE3);
  SPI.begin();  // initialize SPI


SPI.begin() needs to go before other SPI.whatever commands.
Looks like you're okay with the default 4 MHz clock.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up