Stm32 ADXL345 Sensor

Source: Internet
Author: User
Tags bit set sleep sleep function quiesce

Acceleration Sensitivity Axis
The corresponding output voltage increases when accelerating along the sensitive axis

Register Mappings

Register Definition
0x31-data_format

Self_test bit: Set to 1, self-Test force applied to the sensor, resulting in output data conversion. A value of 0 o'clock disables self-test force
SPI bit: value 1, set device to 3-wire SPI mode, value 0, set to 4-wire SPI mode
Int_invert bit: value 0, set interrupt to High active, value 1, set to low active
Full_res bit: Set to 1, the device is full-resolution mode, and the output resolution increases with a scale factor of 4 MG/LSB as the range bit sets the G range. The Full_res bit is set to 0 o'clock, the device is a 10-bit mode, and the range bit determines the maximum G range and scale factor
Align bit: Set to 1, select left-aligned (MSB) mode, set to 0, select right-justified mode, and have symbol extension function
Range bit: Set G range

0x2c-bw_rate

Low_power bit: Set to 0, select Normal operation, set to 1, select low power operation, the noise increases at this time
Rate bit: Select the device bandwidth and output data rate. The default value is 0x0a, and the output data rate is converted to 100Hz

0x2d-power_ctl

Link bit: The link bit for the active and QUIESCE functions is set to 1, and the deferred activity function starts until it detects a standstill. When activity is detected, standstill detection starts and activity detection stops. The bit serial link activity and Quiesce function. This bit is set to 0 o'clock, and the QUIESCE function and the active function are simultaneously
Auto_sleep bit: Set link bit, Auto_sleep bit set to 1, auto sleep function enable
Measurement bit: Set the measuring bit to 0, put the device in standby mode, set to 1, put in measurement mode
Sleep bit: The sleep bit is set to 0, the device is placed in normal operation mode, set to 1, put into sleep mode
Wake-up bit: control read frequency in sleep mode

0x2e-int_enable

Set the bit value to 1, enable the corresponding function, generate interrupts, set to 0 o'clock, prevent these features from interrupting

Registers 0x1e, 0x1F, 0X20-OFSX, Ofsy, Ofsz. Provide user set offset adjustment

Registers 0x32 to 0x37-datax0, DATAX1, DATAY0, DATAY1, DATAZ0, and DATAZ1. Save output data for each axis

Example

#define DEVICE_ID 0x00//device ID #define THRESH_TAP 0X1D #define OFSX 0X1E #define OFSY 0X1F #define OFSZ 0X20 #define DUR 0X21 #define Latent 0X22 #define Window 0X23 #def INE thresh_ack 0X24 #define THRESH_INACT 0X25 #define TIME_INACT 0X26 #define ACT_INACT_CTL 0X27 #def INE thresh_ff 0X28 #define TIME_FF 0X29 #define Tap_axes 0X2A #define Act_tap_status 0X2B #defin E bw_rate 0X2C #define POWER_CTL 0X2D #define Int_enable 0x2e #define INT_MAP 0X2F #define INT_SOURC E 0X30 #define DATA_FORMAT 0X31 #define DATA_X0 0X32 #define DATA_X1 0X33 #define DATA_Y0 0x34 #define DA Ta_y1 0X35 #define DATA_Z0 0X36 #define DATA_Z1 0X37 #define FIFO_CTL 0X38 #define Fifo_status 0X39 #d Efine Adxl_read 0X3B//Read instruction #define Adxl_write 0X3A//write instruction typedef enum {x,//x axis angle Y,//with y axis angle z//with Z axis

Angle of}dir_t; void ADXL345_write (U8 addr, U8 data) {I2c_start ();
    I2c_send_byte (Adxl_write);
    I2c_wait_ack ();
    I2c_send_byte (addr);
    I2c_wait_ack ();
    I2c_send_byte (data);
    I2c_wait_ack ();  

    I2c_stop ();
Delay_ms (10);

    } U8 Adxl345_read (U8 addr) {U8 d = 0;    
    I2c_start ();
    I2c_send_byte (Adxl_write);
    I2c_wait_ack ();
    I2c_send_byte (addr);
    I2c_wait_ack ();    
    I2c_start ();
    I2c_send_byte (Adxl_read);
    I2c_wait_ack ();
    D = I2c_recv_byte ();
    I2c_ack (1);

    I2c_stop ();   
return D;

    } void Adxl345_init () {U8 D;

    I2c_init ();

    D = Adxl345_read (device_id);

    printf ("Adxl345_read%d\n", D); if (d = = 0xe5) {adxl345_write (Data_format, 0x2B);//Interrupt low active, 13-bit full resolution mode, 16g range Adxl345_write (power_ctl, 0 X28); Link Enable, Measurement mode Adxl345_write (int_enable, 0x00);
        Do not use interrupt Adxl345_write (OFSX, 0x00);
        Adxl345_write (Ofsy, 0x00);
    Adxl345_write (Ofsz, 0x00); }} void Adxl345_read_xyz (short *x, ShoRT *y, Short *z) {U8 i = 0;

    U16 Buf[6] = {0};    
    I2c_start ();
    I2c_send_byte (Adxl_write);
    I2c_wait_ack (); I2c_send_byte (0x32);
    Data first address i2c_wait_ack ();    
    I2c_start ();
    I2c_send_byte (Adxl_read);

    I2c_wait_ack (); for (i = 0; i < 6; i++)//A total of 6 {buf[i] = I2c_recv_byte ();//printf ("i2c_recv_byte [%d]%d\r\n", I, BU
        F[i]);
        if (i = = 5) {i2c_ack (1);
        } else {i2c_ack (0);

    }} i2c_stop ();         
    *x = (short) ((buf[1]<<8) + buf[0]);         
    *y = (short) ((buf[3]<<8) + buf[2]);
*z = (short) ((buf[5]<<8) + buf[4]);
    } void Adxl345_read_times (short *x, short *y, short *z, U8 times) {U8 i = 0;

    Short TX, Ty, TZ;

    *x = *y = *z = 0;

        for (i = 0; i < time; i++)//Read times {adxl345_read_xyz (&tx, &ty, &tz);
        *x + = TX;
        *y + = Ty;       

        *z + = TZ; Delay_ms (5); } *x/= times;
    Find average *y/= times;    
*z/= times;
    } short Adxl345_get_result (float x, float y, float z, U8 dir)//calculate angle {float T;

    float Res;
            Switch (dir) {Case x:t = x/sqrt (y*y + z*z); res = Atan (t);
        Find the arc break;
            Case y:t = y/sqrt (x*x + z*z);
            res = Atan (t);
        Break
            Case z:t = sqrt (x*x + y*y)/Z;
            res = Atan (t);
        Break
    Default:break; } return (res * 180/3.14);
    Find the angle} void Adxl345_main () {short x, y, Z;

    Short Rx, Ry, RZ; Adxl345_read_times (&x, &y, &z, 10);
    Read the x, Y, z-direction acceleration values printf ("×%d\n", x);
    printf ("y%d\n", y);

    printf ("Z%d\n", z);
    Rx = Adxl345_get_result (x, Y, z, x);
    ry = Adxl345_get_result (x, y, z, y);
    RZ = Adxl345_get_result (x, y, z, z);
    printf ("Rx%d\n", RX);
    printf ("Ry%d\n", ry);
printf ("RZ%d\n", RZ); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.