Hello,
I try to make a connection between ADXL345 accelerometer and KL25Z via SPI.
I have connected the ADXL345 like that:
PTD0 SPI0_PCS0 to CS
PTD2 SPI0_MOSI to SDA
PTD3 SPI0_MISO to SDO
PTD1 SPI0_SCK to SCL
Next I have used the SPIMaster_LDD component
LDD_TDeviceData *masterDevData; | |
uint8_t Adresse,Value,Error; |
PE_low_level_init();
masterDevData=SM1_Init(NULL); |
Adresse=0x00+0x80;
Error=SM1_SendBlock(masterDevData,&Adresse,1); //to get value at adress 0x00
Error=SM1_ReceiveBlock(masterDevData,&Value,1); //I should received 0x5A but I received 0x00
Any idea?
Thanks
Hello,
After a lot of failure, I have decided to write my own SPI-read function.
The source is quite simple and it works!! but only one time :-( I think there is a problem with SPMF but I don't understand very well in the documentation:
------------------
SPMF is set after SPRF is 1 when the value in the receive data buffer matches the value in the M register.
To clear the flag, read SPMF when it is set and then write a 1 to it.
0 Value in the receive data buffer does not match the value in the M register
1 Value in the receive data buffer matches the value in the M register
--------------------
In fact the first time I read the adress 0x00 of the ADXL345, I get 0xe5 wich is ok, but after if I try to read an other adresses, I allways get 0xe5!!In fact it's impossible to write a new value in SPI0_D.
my code is:
------------------------------
uint8_t spi_read(uint8_t adresse)
{
uint8_t retour=0,dummy;
while(!(SPI_S_SPTEF_MASK & SPI0_S))
{ } //While buffer is not empty do nothing
SPI0_D =adresse+0x80; //write adress+0x80 (read mode)
while(!(SPI_S_SPRF_MASK & SPI0_S))
{ } //While received buffer is not full do nothing
retour=SPI0_D; //read the received value
dummy=(SPI_S_SPMF_MASK & SPI0_S); |
SPI0_S |= SPI_S_SPMF_MASK; |
return retour;
}
---------------------------------------------
Thanks
Michel
Hello Michel,
- The S register must be read when SPTEF is set before writing to the SPI data register; otherwise, the
write is ignored.
- About the SPMF register , i think it is have no link with your problem .
Hope it helps
Have a great day,
Alice Yang
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
- The S register must be read when SPTEF is set before writing to the SPI data register; otherwise, the
write is ignored.
Hello Alice,
I don't see what you mean, because when I do:
while(!(SPI_S_SPTEF_MASK & SPI0_S))
{ } //While buffer is not empty do nothing
I read S register, no?
for
- About the SPMF register , i think it is have no link with your problem .
you are right, it's about the compare register, and have nothing to do with my problem.
Michel
Hello Michel,
After you read the 0X00 address of ADXL345 , then initialize the ADXL345 ,
especially configure the POWER_CTL(0x2D) register the D3 bit to Measure(1) mode ,
then read the other register .
BR
Alice
Hello Michel,
What function do you want to realize about this :
Adresse=0x00+0x80;
Error=SM1_SendBlock(masterDevData,&Adresse,1); //to get value at adress 0x00
Do you want to read the DEVID ? While the DEVID value is 0xE5 , so could you please tell me what do you want
to do , thank !
BR
Alice
Hello,
Of course my final goal is to read DEVID of the ADXL345, but to read the coordinate of 1g vector.
But before to write the full code, I try to make a test by reading the DEVID. For the value of DEVID, you are right, it was a mistake from me, it's 0xE5 the good value. So, if the test program is ok, I will write all the program.
Thanks for your help.
Michel
Hi Michel,
Yes , first check whether can read the DEVID data correctly .
Have a nice day !
Alice