ADXL345 Accelerometer with Pic 16f877A Picbasic Pro Code
Related to the acceleration sensors, many questions unanswered in the forums and saw the unfinished work. Codes associated with acceleration sensors, I'll be posting the help of the codes used in the forums.
Some mathematical formulas taken from their pdf files of the acceleration sensor. Some numbers, because it does not allow the processor bits I wrote using the appropriate numbers. CCS C is very efficient in this regard. 8-bit inefficiency is able to absorb ("math.h"). Proton and Picbasic Pro may be errors. I'm glad you publish corrections.
We may in the future use of these codes to balance the robot and share code.
Good work everyone.
This codes works fine for me.
Links:
http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdfhttp://www.i2cdevlib.com/devices/adxl345#registersCODE:
'****************************************************************
'* Name : ADXL345 PBP 16F877A.BAS *
'* Author : YUKSEL *
'* Notice : Copyright (c) 2015 YUKSEL *
'* : All Rights Reserved *
'* Date : 15.3.2015 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
adcon1 = 7 ' Turns Analogs off
'For 20 MHz config
#config
__config _HS_OSC & _LVP_OFF
#ENDCONFIG
DEFINE OSC 20
TRISA = %00000000
TRISB = %00000000
PORTC = %00000000
PORTD = %00000011
PORTE = %00000000
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
Symbol SDA = PORTD.0 'I2C data pin. Pullup connection is required.
Symbol SCL = PORTD.1 'I2C clock pin. Pullup connection is required.
Symbol WRITE_DATA = $A6 'Used to perform a Write operation
Symbol READ_DATA = $A7 'Used to perform a Read operation
Symbol D_FRM = $31 'Read/Write Register, Selects the operating mode. Default = Single measurement
Symbol BW_RT = $2C
Symbol P_CTL = $2D 'Send continuous Measurement mode.
Symbol F_CTL = $38 'Send Fifo Bypass mode
Symbol X_LSB = $32 'Read Register, Output of X LSB 8-bit value.
Symbol X_MSB = $33 'Read Register, Output of X MSB 8-bit value.
Symbol Y_LSB = $34 'Read Register, Output of Z LSB 8-bit value.
Symbol Y_MSB = $35 'Read Register, Output of Z MSB 8-bit value.
Symbol Z_LSB = $36 'Read Register, Output of Y LSB 8-bit value.
Symbol Z_MSB = $37 'Read Register, Output of Y MSB 8-bit value.
READX VAR Word
READY VAR Word
READZ VAR Word
'X VAR BYTE
'Y VAR BYTE
'AZ VAR BYTE
PAUSE 50 ' Give sensor needed power up time
I2CWRITE SDA,SCL,WRITE_DATA,P_CTL,[$08]
PAUSE 2
I2CWRITE SDA,SCL,WRITE_DATA,F_CTL,[$00]
PAUSE 2
I2CWRITE SDA,SCL,WRITE_DATA,BW_RT,[$0C]
PAUSE 2
I2CWRITE SDA,SCL,WRITE_DATA,D_FRM,[$0B] ' Send continuous output command
PAUSE 2
READI2C:
I2CREAD SDA,SCL,READ_DATA, X_MSB,[READX.HighByte] 'Read the data starting at x_msb
I2CREAD SDA,SCL,READ_DATA ,X_LSB,[READX.LowByte]
I2CREAD SDA,SCL,READ_DATA, Y_MSB,[READY.HighByte]
I2CREAD SDA,SCL,READ_DATA ,Y_LSB,[READY.LowByte]
I2CREAD SDA,SCL,READ_DATA, Z_MSB,[READZ.HighByte]
I2CREAD SDA,SCL,READ_DATA ,Z_LSB,[READZ.LowByte]
PAUSE 100
'X = READX>>2
'Y = READY>>2
'AZ = -Y ATN X
LCDOUT $FE,1, "X=",SDec READX,$FE,2,$FE,$14,$FE,$14,$FE,$14,$FE,$14,$FE,$14,$FE,$14,$FE,$14,$FE,$14,"Y=",SDec READY," "
LCDOUT $FE,$C0,"Z=",SDec READZ," "',$FE,$C0+7,$FE,$C0+8,"AZ=",dec (AZ * 141)/100
GoTo READI2C:
End