1 2 Previous Next 20 Replies Latest reply on Mar 2, 2015 1:16 PM by user_78878863

    URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5

    bathula.navya.deepika

       Hi..

         

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

        • 1. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
          user_14586677

          This thread that may be of some help -

             

           

             

          www.cypress.com/

             

           

             

          Regards, Dana.

          • 2. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
            bathula.navya.deepika

             yeah.. i already posted in that forum also :(

            • 3. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
              user_78878863

              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);
              1 of 1 people found this helpful
              • 5. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
                bathula.navya.deepika

                 // 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

                • 6. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
                  user_1377889

                  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
                   

                  • 8. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
                    user_1377889

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

                       

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

                       

                     

                       

                    Happy coding

                       

                    Bob

                    • 9. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
                      bathula.navya.deepika

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

                      • 10. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
                        user_78878863

                        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.

                        • 11. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
                          bathula.navya.deepika

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

                          • 12. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
                            user_78878863

                            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...

                            1 of 1 people found this helpful
                            • 13. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
                              bathula.navya.deepika

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

                              • 14. Re: URGENT : INTERFACING AXDL345 ACCELEROMETER TO PSOC5
                                user_78878863

                                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.

                                1 2 Previous Next