10 years, 2 months ago.

Bite Swapping

Hello! I am trying to control an AD5791 with and LPC1768. I have been using the mbed platform in this endevour. I ran into a couple issues...It seems that the AD5791 is big endian and the LPC1768 is little endian. Additionally, the AD5791 requires 24-bit words and the LPC1768 can only send 8/16 bit words. Needless to say, I have managed to make some progress...

  1. include "mbed.h"

SPI spi(p5, p6, p7); mosi, miso, sclk DigitalOut cs(p8);

int main() { how to insure that 6th place is set (number >> 6) & 1 (via Jared 1/21/14) >>bit shifts to right

Chip must be deselected cs = 1;

Setup the spi for 8 bit data, high steady state clock, second edge capture, with a 1MHz clock rate spi.format(8,1); spi.frequency(50000000);

Select the device by seting chip select low (inverted logic) cs = 0;

Send 0x8f, the command to read the WHOAMI register spi.write(0xAA); spi.write(0xAA); spi.write(0x10);

Send a dummy byte to receive the contents of the WHOAMI register int whoami = spi.write(0x00); int second = spi.write(0x00); int third = spi.write(0x90); printf("WHOAMI register = 0x%X ", whoami); printf("second register = 0x%X ", second); printf("third register = 0x%X ", third); printf("I work.\n");

Deselect the device cs = 1; }

The issue is that when I print whoami, second, and third to the screen I get: 55, 55, and 8. It appears that the bits have been reversed. Does anyone know why this is happening? Or if there is something else awry in my code?

Thank you so much! Natasha

1 Answer

10 years, 2 months ago.

Please use <<code>> and <</code>> tags around posted code to keep it readable.

The SPI frequency seems way to high. Select 1MHz for starters rather than 50Mhz. You need to deselect the device after writing the registeraddress and then select it again before starting to read the register. I dont understand what you mean by WHOAMI register. Seems you are trying to read the controlregister at 010. This is what happens when you send 0xAA (MSB is 1 for Read. next 3 bits are 010 for controlreg, remaining 4bits are not used), remaining 16 bits of the next 2 bytes are not used). Note that the first byte you send represents bits23-16 for the AD5791. You can then send dummy bytes to receive the value of the controlreg. That will be in the reset state after power on.

I got it to work! https://gist.github.com/tashwoods/84c81f87fa6e0f1b98a2 Thank you again!!!

posted by Tash Woods 02 May 2014