Onenet Kylin seat Application development: acquisition of accelerometer ADXL345 data

Source: Internet
Author: User

Because the data acquisition station is basically installed in the field or the roof, the installation location and vibration have certain influence on the accuracy of the test data. So want to have a position state data, just found Kylin made on the ADXL345, such a digital output acceleration sensor. As shown in the red box:

1 , ADXL345 Overview

ADXL345 is a 3-axis, digital output accelerometer based on Imems technology from Adi. The characteristics of the accelerometer are as follows:

    • High resolution. Up to 13-bit resolution.
    • Range variable. With +/-2g,+/-4g,+/-8g,+/-16g variable measuring range.
    • High sensitivity. Up to 3.9MG/LSB, can not measure the tilt angle change of 1.0°.
    • Low power consumption. 40~145ua ultra-low power consumption, Standby mode is only 0.1uA.
    • Small size. The entire IC size is only 3mm*5mm*1mm, LGA package.

The ADXL supports a standard I²C or SPI Digital interface with 32-level FIFO storage and features a variety of motion state detection and flexible interrupt modes inside. The detection axis of the ADXL345 sensor is as follows:

When the ADXL345 is accelerating forward along the detection axis, it detects the positive acceleration. When detecting gravity, the user needs to be aware that the positive acceleration is detected when the direction of the detection axis is opposite to the direction of gravity. Shown is the relationship of the output response to the direction of gravity:

2 , Hardware design

The ADXL345 supports SPI and i²c two communication methods, the Kylin seat uses the I²c communication mode, the specific connection as shown:

For any unused pins, there is no internal pull-up or pull-down resistor, so any known state or default state does not exist when the CS PIN or ALT address PIN is dangling or not connected. When using I²C, the CS pin must be connected to VDD I/O and the ALT address pin must be connected to either VDD I/O or ground.

The ALT address pin is high, and the device's 7-bit i²c addresses are 0x1d, followed by r/w bits. This translates to 0x3a write, 0x3b read. With the ALT address PIN (PIN 12) grounded, you can select the alternate i²c addresses 0x53 (then the r/w bit). This translates to 0xa6 write, 0xa7 read.

Since the ALT address pin (PIN 12) is grounded in the Kylin seat, the addresses are 0x53, which is 0xa6 write, 0xa7 read. In addition, 2 terminal signals are also introduced into the MCU.

3 , Software design

Then we started the software design. Since the ADXL345 uses the I²c interface to communicate with the MCU and is connected to the I2C2 (PB10, PB11) port, we will implement this part of the software gradually.

(1) , I²c Read and write operations

Since the ADXL345 accelerometer uses the standard I²C communication interface, let's take a look at the message format (such as) that I²c reads and writes to individual or multiple sub-interpretations.

Based on the above message diagram, and combined with STM32F1 's library function, we encapsulate the function of reading and writing byte operations. Encapsulates the Send command and get data functions:

/*issued instructions to ADXL345, with 1 bytes of instruction format*/voidWriteByteToADXL345 (i2c_typedef*i2cx,uint8_t deviceaddress,uint8_t command) {uint16_t I2ctimeout=TimeoutPeriod; /*generating the i²c start signal*/I2c_generatestart (I2C2, ENABLE); /*detect EV5 events and clear flags*/   while(!i2c_checkevent (I2CX, I2c_event_master_mode_select)) {    if((i2ctimeout--) = =0)return ; }   /*send ADXL345 's address*/i2c_send7bitaddress (I2cx,deviceaddress,i2c_direction_transmitter); I2ctimeout=TimeoutPeriod; /*detect EV6 events and clear flags*/   while(!i2c_checkevent (I2CX, i2c_event_master_transmitter_mode_selected)) {    if((i2ctimeout--) = =0)return ; }   /*issued Operation Command*/i2c_senddata (I2CX, command); I2ctimeout=TimeoutPeriod; /*detect EV8 events and clear flags*/   while(!i2c_checkevent (I2CX, i2c_event_master_byte_transmitted)) {    if((i2ctimeout--) = =0)return ; }   /*generating an i²c stop signal*/i2c_generatestop (I2CX, ENABLE);} /*read the value of multiple bytes of data from ADXL345*/voidReadBytesFromADXL345 (i2c_typedef* i2cx,uint8_t deviceaddress,uint8_t *pdata,uint16_t Bytesnum) {uint16_t I2ctimeout=TimeoutPeriod; /*If the I²c bus is busy, wait for some time*/   while(I2c_getflagstatus (I2CX, i2c_flag_busy)) {if((i2ctimeout--) = =0)return ; }   /*generating the i²c start signal*/I2c_generatestart (I2CX, ENABLE); /*detect EV5 events and clear flags*/   while(!i2c_checkevent (I2CX, I2c_event_master_mode_select)) {    if((i2ctimeout--) = =0)return ; }   /*send ADXL345 's address*/i2c_send7bitaddress (I2cx,deviceaddress,i2c_direction_receiver); I2ctimeout=TimeoutPeriod; /*detect EV6 events and clear flags*/   while(!i2c_checkevent (I2CX, i2c_event_master_receiver_mode_selected)) {    if((i2ctimeout--) = =0)return ; }    while(bytesnum) {if(bytesnum==1) {i2c_acknowledgeconfig (i2cx, DISABLE);//Close Answeri2c_generatestop (I2CX, ENABLE);//Stop Signal} i2ctimeout=TimeoutPeriod; /*detect EV7 events and clear flags*/     while(!i2c_checkevent (I2CX, i2c_event_master_byte_received)) {      if((i2ctimeout--) = =0)return ; }    /*read one byte of data from the device via I²c*/*pdata=I2c_receivedata (I2CX); PData++; Bytesnum--; }    /*enable response for next i²c transfer*/i2c_acknowledgeconfig (I2CX, ENABLE);}

( 2 ) ADXL Register and Operation

The ADXL345 sensor has 29 user-visible registers, as shown in. However, we do not have a specific introduction to it, in the data sheet is very detailed description. We don't use all of these registers, in fact we just use configuration registers and partial data registers.

Before we use ADXL345 to read the number of numbers we want, we first use the relevant registers to configure the sensor to get the value we want. Initialize the configuration process as follows:

voidAdxl345_init_configuration (void) {uint8_t Devid=0, val =0; Delayus ( -); ReadByteFromADXL345 (adxl345_address,0x00, &devid);//Get Device IDDelayus ( -); Val=0x2B; WriteByteToADXL345 (adxl345_address, Data_format_reg,&val);//Data Format ControlDelayus ( -); Val=0x0A; WriteByteToADXL345 (adxl345_address, Bw_rate,&val);//data rate and power mode controlDelayus ( -); Val=0x28; WriteByteToADXL345 (adxl345_address, Power_ctl,&val);//power-on characteristic controlDelayus ( -); Val=0; WriteByteToADXL345 (adxl345_address, int_enable,&val);//Interrupt Enable controlDelayus ( -); WriteByteToADXL345 (adxl345_address, OFSX,&val);//x-axis offsetDelayus ( -); WriteByteToADXL345 (adxl345_address, Ofsy,&val);//y-Axis offsetDelayus ( -); WriteByteToADXL345 (adxl345_address, Ofsz,&val);//z-axis offsetDelayus ( -);}


(3) ADXL345 Data Acquisition

Then we can read the data, and before we get and parse the data we'll look at the format of the data. Rate control is performed in register 0x2c, and data format control is performed in register 0x31. The output data of the ADXL345 can be either right-justified or left-justified.

Right alignment of data:

Data left alignment:

In addition, the ADXL345 is tested by VS = 2.5 V supply voltage and is rated voltage, however, vs can be as high as 3.6 V or as low as 2.0 v. Explain the 3.3VDC feed used in the Kylin seat, and the parameters such as offset, sensitivity, noise, self-test and supply current vary with the supply voltage. As the supply voltage changes, the static electricity also changes slightly, so the offset and sensitivity also have subtle changes. When operating at supply voltage VS = 3.3 V, the x-axis and y-axis offsets are typically higher than vs = 2.5 V runtime. Run at supply voltage 3.3 V compared to VS = 2.5V, the z-axis offset is generally lower than MG. VS = 2.5V, the sensitivity of the X-and Y-axes is nominally lsb/g (full resolution or ±2 G, 10-bit operation), while the supply voltage is 3.3 V, its sensitivity is converted to 265 lsb/G. The z-axis sensitivity is not affected by the supply voltage and is the same at 2.5 V or 3.3 v. A simple linear interpolation can be used to determine the typical conversion of offsets and sensitivities under other supply voltages.

You can read the correct data by figuring out the above questions:

voidGetValue FromADXL345 (void) {unsignedCharDevid =0; unsignedChardatatemp[6]; Delayus ( $); ReadByteFromADXL345 (adxl345_address,0x00, &devid);//Read Device IDDelayus ( $); Readbytes FromADXL345 (adxl345_address,0x32, Datatemp,6);//Get Raw Data (4MG/LSB)adxlinfo.incidence_x= ( Short) (datatemp[0] + ((unsigned Short) datatemp[1] <<8)); Adxlinfo.incidence_y= ( Short) (datatemp[2] + ((unsigned Short) datatemp[3] <<8)); Adxlinfo.incidence_z= ( Short) (datatemp[4] + ((unsigned Short) datatemp[5] <<8)); ADXLINFO.INCIDENCE_XF= (float) adxlinfo.incidence_x *0.0039;//Convert to physical measureADXLINFO.INCIDENCE_YF= (float) adxlinfo.incidence_y *0.0039;//Convert to physical measureAdxlinfo.incidence_zf= (float) Adxlinfo.incidence_z *0.0039;//Convert to physical measure}

This completes the software writing.

4 , results validation

When you are finished writing, compile without error. We look at how the final run results. First we log in to Onenet, configure the device and application, and then download the program to power up to see XG, Yg, ZG data and the last time we read the temperature and humidity data:

Let's look at what the data in the display looks like. There are some differences in the display accuracy, but the basic is consistent.

At this point, we have completed the acquisition of ADXL345 data and uploaded to Onenet, in the following we will continue to the various onenet and Kylin seat Development Board research. Onenet really is more and more interesting.

Onenet Kylin seat Application development: acquisition of accelerometer ADXL345 data

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.