Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 05:37:21 17:37


Login with username, password and session length


Pages: [1]
Print
Author Topic: ADXL345 Accelerometer with Pic 16f877A CCS C Code and PicBasic Pro  (Read 9733 times)
0 Members and 1 Guest are viewing this topic.
intel
Hero Member
*****
Offline Offline

Posts: 520

Thank You
-Given: 53
-Receive: 2141



« on: March 15, 2015, 02:21:10 14:21 »

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.



https://www.youtube.com/watch?v=xOxqRbVzPKk

Links:
http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf
http://www.i2cdevlib.com/devices/adxl345#registers



Main Code
Code:
#include "16f877A.h"
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES HS                       //Dont change
#use    delay(clock=20MHz)  
#use I2C(master, sda=PIN_d0, scl=PIN_d1, slow)  //You can change, according your board ports
#include "EXLCD.C"  //You can change, according your board ports
#include "ADXL345.C"
#include "math.h"
int8  A_data[6]; //6 units 8 bit Accel datas
int16 Xa=0,Ya=0,Za=0;

void main()
{
delay_ms(2);
lcd_init();
adxl345_init();    
  
printf(lcd_putc,"\f");
lcd_gotoxy(1,1);
printf(lcd_putc,"    ADXL345     ");
lcd_gotoxy(1,2);
printf(lcd_putc," Accelerometer  ");
delay_ms(1000);
printf(lcd_putc,"\f");

   while(TRUE)
   {
   A_data[0]=adxl345_read(0x32); //Read X axis(LSB)
   A_data[1]=adxl345_read(0x33); //Read X axis(MSB)
   A_data[2]=adxl345_read(0x34); //Read Y axis(LSB)
   A_data[3]=adxl345_read(0x35); //Read Y axis(MSB)
   A_data[4]=adxl345_read(0x36); //Read Z axis(LSB)
   A_data[5]=adxl345_read(0x37); //Read Z axis(MSB)

   Xa=make16(A_data[1],A_data[0]);//Converting two 8 bit integer to one 16 bit integer
   Ya=make16(A_data[3],A_data[2]);
   Za=make16(A_data[5],A_data[4]);
  
   float Heading = atan2((signed int16)Ya,(signed int16)Xa)* 180 / pi + 180;      
        
   lcd_gotoxy(1,1);
   printf(lcd_putc,"X=%ld  ",Xa);
   lcd_gotoxy(9,1);
   printf(lcd_putc,"Y=%ld  ",Ya);
   lcd_gotoxy(1,2);
   printf(lcd_putc,"Z=%ld  ",Za);
   lcd_gotoxy(9,2);
   printf(lcd_putc,"H=%f  ",Heading);  
  
   delay_ms(100);
   }
}

Library
Code:
// ADXL345  Registers
#define W_DATA      0xA6    //Used to perform a Write operation
#define R_DATA      0xA7    //Used to perform a Read operation
#define D_FRM       0x31    //Read/Write Register, Selects the operating mode. Default = Single measurement
#define BW_RT       0x2C
#define P_CTL       0x2D    //Send continuous Measurement mode.
#define F_CTL       0x38    //Send Fifo Bypass mode
#define X_LSB       0x32    //Read Register, Output of X LSB 8-bit value.
#define X_MSB       0x33    //Read Register, Output of X MSB 8-bit value.
#define Y_LSB       0x34    //Read Register, Output of Z LSB 8-bit value.
#define Y_MSB       0x35    //Read Register, Output of Z MSB 8-bit value.
#define Z_LSB       0x36    //Read Register, Output of Y LSB 8-bit value.
#define Z_MSB       0x37    //Read Register, Output of Y MSB 8-bit value.
 
void adxl345_write(int add, int data)
{
         i2c_start();
         i2c_write(W_DATA);
         i2c_write(add);
         i2c_write(data);
         i2c_stop();
 
}
      
int adxl345_read(int add){
         int retval;
         i2c_start();
         i2c_write(W_DATA);
         i2c_write(add);
         i2c_start();
         i2c_write(R_DATA);
         retval=i2c_read(0);
         i2c_stop();
         return retval;
}
 
void adxl345_init(){
         adxl345_write(D_FRM,0x0B);
         delay_ms(2);
         adxl345_write(P_CTL,0x08);
         delay_ms(2);
}



« Last Edit: March 20, 2015, 05:51:02 17:51 by intel » Logged

In life, most genuine mentor is science.
intel
Hero Member
*****
Offline Offline

Posts: 520

Thank You
-Given: 53
-Receive: 2141



« Reply #1 on: March 16, 2015, 05:22:44 17:22 »

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.pdf
http://www.i2cdevlib.com/devices/adxl345#registers



CODE:
Code:
'****************************************************************
'*  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
Logged

In life, most genuine mentor is science.
intel
Hero Member
*****
Offline Offline

Posts: 520

Thank You
-Given: 53
-Receive: 2141



« Reply #2 on: March 20, 2015, 05:26:25 17:26 »

Hi;

This is Proton  Basic Code.

Code:
'****************************************************************
'*  Name    : ADXL345 PROTON 16F877A.BAS                        *
'*  Author  : YUKSEL                                            *
'*  Notice  : Copyright (c) 2015 YUKSEL                         *
'*          : All Rights Reserved                               *
'*  Date    : 5.3.2015                                          *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
Device = 16F877A
Declare Xtal = 20            ' Kristal frekansı: 20 MHz
CMCON = 7                    ' Tüm comparatorlar devre dışı
Declare All_Digital = True   ' 16f877A nın tüm uçları Dijital
TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000011
TRISE = %00000000

Declare LCD_Type         0   ' Standart 2x16 LCD
Declare LCD_DTPort PORTB     ' LCD data portu
Declare LCD_DTPin  PORTB.0 ' DATA girişi PORTB'nin B0 ucundan başlayacak
Declare LCD_ENPin  PORTB.5   ' Enable (EN) pini E2
Declare LCD_RSPin  PORTB.4   ' Register Select (RS) pini B4
Declare LCD_Interface    4   ' 4 bit LCD arayüzü
Declare LCD_Lines        2 ' 2 satırlık LCD

Symbol SDA = PORTD.0   'I2C data pin. Pullup connection is required.
Symbol SCL = PORTD.1   'I2C clock pin. Pullup connection is required.
Symbol W_DAT = $A6     'Used to perform a Write operation.
Symbol R_DAT = $A7     'Used to perform a Read operation.
Symbol D_FRM = $31     'Data Format.(Full Resultion,Range,Invert)
Symbol BW_RT = $2C     'Low Power,Rate.
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.

Dim READX As Word
Dim READY As Word
Dim READZ As Word

DelayMS 50
I2COut SDA,SCL,W_DAT,P_CTL,[$08]
DelayMS 2
I2COut SDA,SCL,W_DAT,F_CTL,[$00]
DelayMS 2
I2COut SDA,SCL,W_DAT,BW_RT,[$0C]
DelayMS 2
I2COut SDA,SCL,W_DAT,D_FRM,[$0B]  ' Send continuous output command
DelayMS 2

READI2C:
I2CIn SDA,SCL,R_DAT,X_MSB,[READX.HighByte]  'Read the data starting at x_msb
I2CIn SDA,SCL,R_DAT,X_LSB,[READX.LowByte]
I2CIn SDA,SCL,R_DAT,Y_MSB,[READY.HighByte]
I2CIn SDA,SCL,R_DAT,Y_LSB,[READY.LowByte]
I2CIn SDA,SCL,R_DAT,Z_MSB,[READZ.HighByte]
I2CIn SDA,SCL,R_DAT,Z_LSB,[READZ.LowByte]
'Servo PORTA.0,1500+READZ*2  'You can use auto balanse for a box
DelayMS 40
Print At 1,1, "X=",SDec READX," "
Print At 1,9, "Y=",SDec READY,"  "
Print At 2,1, "Z=",SDec READZ,"          "
GoTo READI2C:

End


Logged

In life, most genuine mentor is science.
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC