ADXL345 accelerometer
+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2016
    Posts
    57

    Default ADXL345 accelerometer

    Hello all,

    I'm trying to connect ADXL345 accelerometer to MCU in SPI 4wire mode, but no success. The reading is allways zero.
    In I2c mode the accelerometer works just fine.
    I think in SPI mode I forgot something, because I'm not familiar with SPI communication.
    Here is my code, it reads two axis, and display raw value on lcd.
    Can someone check the code and point me what I'm doing wrong.
    Thanks for any suggestion.

    Code:
    '******************************************************************
    '*  Name    : ADXL345 accelerometer test  SPI mode                *
    '*  Author  : Louis                                               *
    '*  Notice  :                                                     *
    '*          : All Rights Reserved                                 *
    '*  Date    : 3. 5. 2019                                          *
    '*  Version : 1.0                                                 *
    '*  Notes   : 16F688                                              *
    '*          :                                                     *
    '******************************************************************
    
    #CONFIG
    cfg = _INTRC_OSC_NOCLKOUT
    cfg&= _WDT_OFF
    cfg&= _PWRTE_OFF
    cfg&= _MCLRE_OFF
    cfg&= _CP_OFF
    cfg&= _CPD_OFF
    cfg&= _BOD_ON
    cfg&= _IESO_ON
    cfg&= _FCMEN_ON
      __CONFIG cfg
    #ENDCONFIG
    
    DEFINE OSC 8 
    OSCCON = %01110000
    
    CMCON0 = 7
    ANSEL = %00000000
    TRISA = %000000     
    TRISC = %00000000
    
    DEFINE LCD_DREG PORTC ;Define PIC port used for LCD Data
    DEFINE LCD_DBIT 0 ;Define first pin of portb
    DEFINE LCD_RSREG PORTC ;Define PIC port used for RS line of
    DEFINE LCD_RSBIT 4 ;Define Portb pin used for RS
    DEFINE LCD_EREG PORTC ;Define PIC prot used for E line of LCD
    DEFINE LCD_EBIT 5 ;Define PortB pin used for E
    DEFINE LCD_BITS 4 ;Define the 4 bit communication
    DEFINE LCD_LINES 2 ;Define using a 2 line LCD
    DEFINE LCD_COMMANDUS 2000 ;Define delay between sending LCD
    DEFINE LCD_DATAUS 50 ;Define delay time between data sent.
    pause 30
    
    CS      var PortA.2     'chip select
    CLK     var PortA.1     'clock
    SDI     var PortA.4     'mosi
    SDO     var PortA.5     'miso
    
    AX1	VAR	WORD  'X dir accel
    AY1	VAR	WORD  'Y dir accel
    
    
    ACDATA1	var	byte    'shiftin bytes
    ACDATA2	var	byte    'shiftin bytes
    ACDATA3	var	byte    'shiftin bytes
    ACDATA4	var	byte    'shiftin bytes
    
    lcdout $FE,1
    
    low CS
    pause 1
    shiftout SDI,CLK,1,[$2D\8,%00001000\8] 'setting to measure mode
    high CS
    
    pause 1
    
    low CS
    pause 1
    shiftout SDI,CLK,1,[$31\8,%00000000\8] 'setting to +-2g range, SPI 4wire mode
    high CS
    
    pause 1
    
    start_measure:
    low cs
    pause 1
    shiftout SDI,CLK,1,[$32\8] ;get FIFO to read first adress
    shiftin SDO,CLK,1,[ACDATA1,ACDATA2,ACDATA3,ACDATA4] 'shiftin acceleration values
    high CS
    
    AX1.highbyte=ACDATA1
    AX1.lowbyte=ACDATA2
    AY1.highbyte=ACDATA3
    AY1.lowbyte=ACDATA4
    
    lcdout $FE,$80, "X: ", dec5 AX1
    lcdout $FE,$C0, "Y: ", dec5 AY1
    
    goto start_measure

  2. #2

    Default Re: ADXL345 accelerometer

    Is your pic running on 5v or 3.3v?



    https://learn.sparkfun.com/tutorials...okup-guide/all

  3. #3
    Join Date
    Mar 2016
    Posts
    57

    Default Re: ADXL345 accelerometer

    Hi,

    PIC is running on 3.3V

  4. #4
    Join Date
    Mar 2016
    Posts
    57

    Default Re: ADXL345 accelerometer

    Hi Folks,
    finally I made it. In my previous attempt I made a few newbie mistakes, like missing INCLUDE "modedefs.bas", and I shiftout the clock iddle low which is wrong, because datasheet says clock must be iddle high. Then I figured out how to send R/W bit and multiple byte read bit.
    Here is the code which reads RAW data from three axis.
    Code:
    '******************************************************************
    '*  Name    : ADXL345 accelerometer, 3wire SPI mode               *
    '*  Author  : Louis                                               *
    '*  Notice  :                                                     *
    '*          : All Rights Reserved                                 *
    '*  Date    : 6. 5. 2019                                          *
    '*  Version : 1.0                                                 *
    '*  Notes   : 16F688                                              *
    '*          :                                                     *
    '******************************************************************
    
    #CONFIG
    cfg = _INTRC_OSC_NOCLKOUT
    cfg&= _WDT_OFF
    cfg&= _PWRTE_OFF
    cfg&= _MCLRE_OFF
    cfg&= _CP_OFF
    cfg&= _CPD_OFF
    cfg&= _BOD_ON
    cfg&= _IESO_ON
    cfg&= _FCMEN_ON
      __CONFIG cfg
    #ENDCONFIG
    
    DEFINE OSC 8 
    OSCCON = %01110111
    
    INCLUDE "modedefs.bas"
    
    CMCON0 = 7
    ANSEL = %00000000
    TRISA = %000000     
    TRISC = %00000000
    
    DEFINE LCD_DREG PORTC ;Define PIC port used for LCD Data
    DEFINE LCD_DBIT 0 ;Define first pin of portb
    DEFINE LCD_RSREG PORTC ;Define PIC port used for RS line of
    DEFINE LCD_RSBIT 4 ;Define Portb pin used for RS
    DEFINE LCD_EREG PORTC ;Define PIC prot used for E line of LCD
    DEFINE LCD_EBIT 5 ;Define PortB pin used for E
    DEFINE LCD_BITS 4 ;Define the 4 bit communication
    DEFINE LCD_LINES 2 ;Define using a 2 line LCD
    DEFINE LCD_COMMANDUS 2000 ;Define delay between sending LCD
    DEFINE LCD_DATAUS 50 ;Define delay time between data sent.
    pause 30
    
    CS      var PortA.2     'CS chip select
    CLK     var PortA.5     'SCL/SCLK clock
    SDI     var PortA.4     'SDA/SDI/SDIO Serial Data Input and Output (SPI 3-Wire)
    
    AX1	var	word  'X dir accel
    AY1	var	word  'Y dir accel
    AZ1     var     word  'Z dir accel
    
    ACDATA1	var	byte    'shiftin temp bytes
    ACDATA2	var	byte    'shiftin temp bytes
    ACDATA3	var	byte    'shiftin temp bytes
    ACDATA4	var	byte    'shiftin temp bytes
    ACDATA5	var	byte    'shiftin temp bytes
    ACDATA6	var	byte    'shiftin temp bytes
    
    lcdout $FE,1 'clear LCD
    
    high cs 'CS pin should be brought high before changing the clock polarity and phase
    
    pause 10
      
    low CS
    pause 1
    shiftout SDI,CLK,5,[%0\1,%0\1,$31\6,%01000000\8] 'setting to +-2g range, SPI 3 wire mode
    high CS
    
    pause 1 
    
    low CS
    pause 1
    shiftout SDI,CLK,5,[%0\1,%0\1,$2D\6,%00001000\8] 'setting to measure mode
    high CS
    
    pause 1
    
    start_measure:
    low cs
    pause 1
    shiftout SDI,CLK,5,[%1\1,%1\1,$32\6] 'get FIFO to read first adress, allow to read multiple bytes
    shiftin SDi,CLK,6,[ACDATA1,ACDATA2,ACDATA3,ACDATA4,ACDATA5,ACDATA6] 'shiftin acceleration values
    high CS
    
    AX1.highbyte=ACDATA1
    AX1.lowbyte=ACDATA2
    AY1.highbyte=ACDATA3
    AY1.lowbyte=ACDATA4
    AZ1.highbyte=ACDATA5
    AZ1.lowbyte=ACDATA6
    
    lcdout $FE,$80, "X: ", dec5 AX1  'display X axis RAW value on LCD
    lcdout $FE,$C0, "Y: ", dec5 AY1  'display Y axis RAW value on LCD
    lcdout $FE,$D0, "Z: ", dec5 AZ1  'display Z axis RAW value on LCD
    pause 100
    goto start_measure

Similar Threads

  1. Trouble interfacing ADXL345/375 accelerometer
    By achilles03 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th September 2015, 20:13
  2. ADXL345 Accelerometer using I2C
    By Terry in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th August 2015, 18:23
  3. Accelerometer reading
    By joseph Degorio in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 8th February 2011, 11:43
  4. accelerometer question
    By griffin in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 27th February 2009, 21:00
  5. Adxl213ae Accelerometer
    By DougB in forum mel PIC BASIC
    Replies: 0
    Last Post: - 25th July 2005, 21:32

Members who have read this thread : 16

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts