URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 Hi..

   

can anyone help me in interfacing axdl345 accelerometer to psoc5 using SPI

0 Likes
1 Solution
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

This thread that may be of some help -

   

 

   

www.cypress.com/

   

 

   

Regards, Dana.

View solution in original post

0 Likes
20 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

This thread that may be of some help -

   

 

   

www.cypress.com/

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 yeah.. i already posted in that forum also 😞

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Do you have any actual problems with that connections? I used an UDB-based SPI master component to interface with a ADXL375 which is similar to the ADXL345. Its quite straight-forward in the end - just read the manual how to read and write registers.

   
  void waitFinished() {     while(0==(SPI_ReadTxStatus()&SPI_STS_SPI_DONE))     {         /* Wait while Master completes transfer */     } }  void writeRegister(uint8 address, uint8 value) {     Reg_CS_Write(0);     SPI_WriteTxData(address);     SPI_WriteTxData(value);     waitFinished();     Reg_CS_Write(1);     SPI_ClearRxBuffer(); }   int main() {     /* Place your initialization/startup code here (e.g. MyInst_Start()) */      CyGlobalIntEnable;         LCD_Start();         SPI_Start();         Reg_CS_Write(1); // disable     CyDelay(10);     Reg_CS_Write(0);     CyDelayUs(5);         SPI_WriteTxData(0x80); // read register 0     SPI_WriteTxData(0x00); // dummy byte     waitFinished();     Reg_CS_Write(1);         LCD_Position(1,0);     LCD_PrintDecUint16(SPI_GetRxBufferSize());     SPI_ReadRxData(); // read dummy byte     uint32 id=SPI_ReadRxData();         LCD_Position(1,0);     LCD_PrintString("id=");     LCD_PrintHexUint8((uint8)id);         CyDelay(2000);     LCD_ClearDisplay();      writeRegister(0x2c, 0x07);     // set FIFO to streaming     writeRegister(0x38, 0x88);     // enable measurement     writeRegister(0x2D, 0x08);
Anonymous
Not applicable

 is it a 3 axis accelerometer?

0 Likes
Anonymous
Not applicable

 // ADXL345 Register Definition

   

#define _POWER_CTL      0x2D

   

#define _DATA_FORMAT    0x31

   

#define _BW_RATE        0x2C

   

#define _DATAX0         0x32

   

#define _DATAX1         0x33

   

#define _DATAY0         0x34

   

#define _DATAY1         0x35

   

#define _DATAZ0         0x36

   

#define _DATAZ1         0x37

   

#define _FIFO_CTL       0x38

   

 

   

#define _SPI_READ       0x80

   

#define _SPI_WRITE      0x00

   

 

   

#define _SPEED          0x0F                       // Buffer Speed - 3200Hz

   

//

   

 

   

sbit CS_bit at PORTB0_bit;

   

sbit CS_Direction_bit at DDB0_bit;

   

 

   

unsigned short temp;

   

char out[16];

   

int readings[3] = {0, 0, 0};                       // X, Y and Z

   

 

   

void ADXL345_Write(unsigned short address, unsigned short data1) {

   

  unsigned short internal = 0;

   

  internal = address | _SPI_WRITE;                 // Register and Write bit

   

 

   

  CS_bit = 0;

   

  SPI1_Write(internal);

   

  SPI1_Write(data1);

   

  CS_bit = 1;

   

}

   

 

   

unsigned short ADXL345_Read(unsigned short address) {

   

  unsigned short internal = 0;

   

  internal = address | _SPI_READ;                  // Register and Read bit

   

 

   

  CS_bit = 0;

   

  SPI1_Write(internal);

   

  internal = SPI1_Read(0);

   

  CS_bit = 1;

   

 

   

  return internal;

   

}

   

 

   

void main() {

   

 

   

  CS_bit = 1;                                      // Deselect Chip before initalization

   

  CS_Direction_bit = 1;                            // Set direction

   

 

   

  // SPI Initialization, Fcy/4, when idle SPI High, Data transmit on low to high edge

   

  SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV4, _SPI_CLK_HI_LEADING);

   

 

   

  // UART Initialization

   

  UART1_Init(9600);

   

 

   

  UART1_Write_Text("Starting ADXL345 test.");

   

  UART1_Write(13);

   

  UART1_Write(10);

   

 

   

  Delay_ms(1000);

   

 

   

  // Go into standby mode to configure the device.

   

  ADXL345_Write(_POWER_CTL, 0x00);

   

 

   

  // Full resolution, +/-16g, 4mg/LSB.

   

  ADXL345_Write(_DATA_FORMAT, 0x0B);

   

 

   

  // Set data rate.

   

  ADXL345_Write(_BW_RATE, _SPEED);

   

 

   

  // Measurement mode.

   

  ADXL345_Write(_POWER_CTL, 0x08);

   

 

   

  while (1) {

   

 

   

    Delay_ms(800);

   

 

   

    // Read X Hi

   

    readings[0] = ADXL345_Read(_DATAX0) << 8;

   

 

   

    // Read X Lo

   

    readings[0] = readings[0] | ADXL345_Read(_DATAX1);

   

 

   

    // Read Y Hi

   

    readings[1] = ADXL345_Read(_DATAY0) << 8;

   

 

   

    // Read Y Lo

   

    readings[1] = readings[1] | ADXL345_Read(_DATAY1);

   

 

   

    // Read Z Hi

   

    readings[2] = ADXL345_Read(_DATAZ0) << 8;

   

 

   

    // Read Z Lo

   

    readings[2] = readings[2] | ADXL345_Read(_DATAZ1);

   

 

   

// Send values via UART

   

    //   X

   

    UART1_Write_Text("X: ");

   

    IntToStr(readings[0], out);

   

    UART1_Write_Text(out);

   

    UART1_Write(13);

   

    UART1_Write(10);

   

 

   

    Delay_ms(100);

   

 

   

    //   Y

   

    UART1_Write_Text("Y: ");

   

    IntToStr(readings[1], out);

   

    UART1_Write_Text(out);

   

    UART1_Write(13);

   

    UART1_Write(10);

   

 

   

    Delay_ms(100);

   

 

   

    //   Z

   

    UART1_Write_Text("Z: ");

   

    IntToStr(readings[2], out);

   

    UART1_Write_Text(out);

   

    UART1_Write(13);

   

    UART1_Write(10);

   

   //

   

    UART1_Write(13);

   

    UART1_Write(10);

   

   //

   

//

   

 }

   

}

   

 

   

this code doesnt work

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

It is always easier for us to use the help of the IDE to analyze your code. Best is always to post your complete project or at least a reduced project that shows the error. To post use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.



Bob
 

0 Likes
Anonymous
Not applicable

 How to define sbit in psoc5? 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

When sbit means "set bit" you can define it in C-language as

   

#define sbit(Val,BitNo) (Val |= (0x0001ul << BitNo))

   

 

   

Happy coding

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 i attached my compressed file.please help me in debugging.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Each write also reads data from the SPI bus. That means you code has two problems:

   
        
  1. your SPIWriteTxData calls fill the RX buffer, but you don't read the values, so they stay there
  2.     
  3. when you actually want to read data from the SPI bus, the master needs to write a dummy byte. YOu don't so your readRxData() call just reads ther value that got read when the adress is written to the bus (which is just garbage)
  4.    
   

Look at the place where I read the chip ID in my code, that shows how the reads needs to be done. Also note that after the register writes I clear the RX buffer.

0 Likes
Anonymous
Not applicable

 hmmmm......even im getting errors in the UART section..

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

What kind of errors do you get? Maybe its a good idea to go piece by pieve to get it going, starting with what you need for debugging first (e.g. UART or LCD) and then go to the more difficult parts...

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

i tried using dummy byte but no use... i think i have to initiate UART and then use that variable to control SPI

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

In the screenshot I cannot see any error messages, the image is cut off. For compile errors I suggest you open a new thread, since this has nothing to do with the interface to the ADXL345 as such.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Now you are confusing me - what has a dummy variable to do with the UART, and why do you need the UART to control the ADXL? Ajnd what variable are you talking about?

   

Try to read the ADXL device ID first, using the code I posted above (it writes to a LCD, which is available on the -050 kits, using the CharLCD_MP component. It also uses an UDB-based SPI master). It should be the same for the ADXL345, but check the register IDs first.

ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Looks like you have an extra brace, "}", at bottom of code view screen

   

which will produce a lot of errors. Just before the "[] END OF FILE" comment.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 hi..how do u display the values on lcd

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Look at the LCD_ functions in my code. Or just look at the API description of the TextLCD component.

0 Likes
Anonymous
Not applicable

how I can read this data?

   

#define _DATAX0         0x32

   

#define _DATAX1         0x33

   

#define _DATAY0         0x34

   

#define _DATAY1         0x35

   

#define _DATAZ0         0x36

   

#define _DATAZ1         0x37

   

sekond question how I can test my accelerometer adxl345 with psoc 5 by using debug or UART? the protocol I used is SPI master...

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Please don't post new questions in a thread thats already one year dead. Especally when you are already asking in another thread.

   

Please read the data sheet for the SPI master, and the data sheet for the ADXL345. Especially the latter explains how to communicate with it.

0 Likes