 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Sep 16, 2009 2:30 am |
|
|
result=spi_xfer(0xXX); //result = the value of register 0xXX
This is wrong. The reason is, as you clock out your value 0xXX it clocks in at the same time the value you are reading. Therfore it cannot select the register from that value but uses the previously selected register!
In mutlibyte mode it will increment the register address and allow you to read or write to the next register without having to send the address again.
Last edited by Wayne_ on Wed Sep 16, 2009 2:48 am; edited 2 times in total |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Sep 16, 2009 2:44 am |
|
|
I have to appologise big time
The address stuff is I2C related, your device covers both SPI and I2C. With SPI you don't shift the address and I was looking at the wrong thing. And as I am working with both + a lot of other comms stuff I got a bit confused, Sry
In SPI mode you don't shift the address, what you have is the MSb is the R/W bit, the next bit is the multi byte bit then a 6 bit register address.
This makes it a little easier for you.
To Read
| Code: |
spi_xfer(addr | 0x80); // Send reg address and the read bit set
val = spi_xfer(0); // Read value, 0 = dummy data, ignored
|
to write
| Code: |
spi_xfer(addr); // register address
xpi_xfer(val); // New register val
|
The CS line determines which device you are using
Again, you have the option of specfying the stream in the spi_xfer function. |
|
 |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Wed Sep 16, 2009 4:22 am |
|
|
Hey, no worries! You've been more than patient. Now it all makes perfect sense. I didn't realize the SPI control stays at a selected register for more than one frame, or tick.
So using your provided code, all I should have to do to set the measure bit to 1 is:
| Code: |
// Power_CTRL Register = 0x2D, contains Measure bit
// Measure Bit = D3, setting to 1 enters measure mode, which is the goal
output_low(CS PIN);
spi_xfer(0x2D);
xpi_xfer(0x01);
output_high(CS PIN);
|
I see that this would write 1 to the register 0x2D, but I'm not sure how to tell it to set the D3 bit to 1. Unless simply sending 0xD3 to the 0x2D register calls the bit and toggles it to 1? In which case the code would be:
| Code: |
output_low(CS PIN);
spi_xfer(0x2D);
xpi_xfer(0xD3);
output_high(CS PIN);
|
Are either of these correct? |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Sep 16, 2009 4:56 am |
|
|
| vinniewryan wrote: |
I see that this would write 1 to the register 0x2D, but I'm not sure how to tell it to set the D3 bit to 1. Unless simply sending 0xD3 to the 0x2D register calls the bit and toggles it to 1? In which case the code would be:
| Code: |
output_low(CS PIN);
spi_xfer(0x2D);
xpi_xfer(0xD3);
output_high(CS PIN);
|
Are either of these correct? |
LOL, I understand now after looking at the datasheet again
D3 refers to the Data Bit 3 or 0x08 in hex or 0b00001000 in binary
Register 0x2D is read/write so to set the bit you would
| Code: |
int 8 val;
output_low(CS PIN);
spi_xfer(0x2D | 0x80); // Select the register in read mode
val = spi_xfer(0x2D); // read the value
val |= 0x08; // Set bit 3 (D3) of the value
output_high(CS PIN);
output_low(CS PIN);
spi_xfer(0x2D); // Select the register in write mode
spi_xfer(val); // write the value
output_high(CS PIN);
|
or using your accel_reg_read/write functions.
| Code: |
accel_reg_write(0x2D, accel_reg_read(0x2D) | 0x08)); // Read, set bit 3 (D3), write
|
Which was proberbly already shown to you and I did say it was wrong at one point.
But do you understand it now  |
|
 |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Wed Sep 16, 2009 11:52 pm |
|
|
I get it! It makes sense now! Thanks!
The X axis has 2 registers, data0 0x32, and data1 0x33, both holding 8 bit values. Any idea why there's two registers for each axis value? It seems there should only be one. I just want to get the tilt value out of the accelerometer, but I'm not sure how to turn both values into one.
| Code: |
spi_xfer(0x32);
Xvalue0=spi_xfer(0); //data0
spi_xfer(0x33);
Xvalue1=spi_xfer(0); //data1
|
I'm expecting the spi to return a number between 0 and 255 based on the tilt of the x axis, but for some reason the 0x33 register is returning 0's, I think. It's hard to tell for sure, because the only visual indication I have is a blinking LED, which doesn't change blinking speed at all as I tilt the accelerometer. |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Sep 17, 2009 2:27 am |
|
|
Reading the first page of the manual:-
"The ADXL345 is a small, thin, low power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. Digital output data is formatted as 16-bit twos complement and is acces-sible through either a SPI (3- or 4-wire) or I2C digital interface."
As you can see, it has programable resolution 10 bit upto 13 bit, This requires 2 bytes (16 bits) to represent the data. X0:X1, Y0:Y1, Z0:Z1
You will also note it is in twos compliment.
Also, the datasheet sugests you read the 2 bytes using multi byte mode so that the second value isn't chaged between reads.
Is this the only device on your SPI bus other than the PIC ?
X0 is the LSB and X1 is the MSB.
Something like the following may help, I have not compiled this so there may a a bug or 2
| Code: |
struct {
int8 X0, X1;
int8 Y0, Y1;
int8 Z0, Z1;
} accel_data;
void read_block(int8 reg, int8 *data, int8 count)
{
output_low(CS_PIN);
spi_xfer(reg | 0xC0); // Select reg in read/multibyte mode
while(count--)
{
*data++ = spi_xfer(0); // Read and store values
}
output_high(CS_PIN);
}
void main()
{
int16 val;
read_block(0x32, &accel_data, sizeof(accel_data));
val = ((int16)accel_data.X1 << 8) | accel_data.X0;
// val is now the 2s compliment value for X
}
|
|
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|