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