单片机GD32F303RCT6 (Macos环境)开发 (三十四)—— 数字加速度计 (ADXL345)

news2024/11/24 3:33:45

数字加速度计 (ADXL345)- 计算xyz轴重力值

1、i2c总线读取

1、接线
上一节的软件模式i2c的方式,选择PB10(SCL) PB11(SDA)。

	GD32                       ADXL345
	PB10      ---------------  SCL
	PB11      ---------------  SDA
	3.3       ---------------  3.3
	GND       ---------------  GND
	3.3       ---------------  CS

CS接高电平时,才会使能ADXL345 的i2c总线。

2、封装对寄存器读写函数。

可根据自己的平台换i2c读写函数,或者spi读写函数。

static void adxl345_write_reg(uint8_t reg,uint8_t data)
{
#if SW_IIC == 1
    swi2c_device_write_data(ADXL345_ADDR,&reg,1,&data,1);
#endif    
}

static void adxl345_read_reg(uint8_t reg,uint8_t *data)
{
#if SW_IIC == 1
    swi2c_device_read_data(ADXL345_ADDR,&reg,1,data,1);
#endif 
}

static void adxl345_get_x_y_z_data(uint8_t reg,uint8_t *data,uint16_t len)
{
#if SW_IIC == 1
    swi2c_device_read_data(ADXL345_ADDR,&reg,1,data,len);
#endif   
}

3、ADXL345的寄存器

在这里插入图片描述
上图看出,xyz轴的数据存在0x32-0x37寄存器中,所以我们通过设置其他寄存器去初始化ADXL345后,从这六个寄存器读取数据就好了。

4、ADXL345的初始化代码

每个寄存器的每个功能都有初始化,对着规格书很容易就能明白。

void adxl345_xyz_read_init()
{
    uint8_t id = 0;
    uint8_t reg;
    uint8_t watermark;
    uint8_t x_offset;
    uint8_t y_offset;
    uint8_t z_offset;
    uint8_t tap_threshold;
    uint8_t tap_duration;
    uint8_t tap_window;
    uint8_t latent;
    uint8_t action_threshold;
    uint8_t inaction_threshold;
    uint8_t inaction_time;
    uint8_t fall_threshold;
    uint8_t free_fall_time;
    adxl345_interrupt_active_level_t it_level;
    adxl345_bool_t auto_sleep_enable;
    adxl345_bool_t sleep_enable;
    adxl345_sleep_frequency_t sleep_freq;
    adxl345_mode_t fifo_mode;
    adxl345_interrupt_pin_t interrupt_pin;

    adxl345_get_device_id(&id);
    if (id == 0xe5)
    {
        adxl345_set_interrupt_active_level(ADXL345_INTERRUPT_ACTIVE_LEVEL_LOW);     // 设置中断为低电平
        adxl345_get_interrupt_active_level(&it_level);
        printf("adxl345_get_interrupt_active_level == 0x%02x\r\n",it_level);

        adxl345_set_auto_sleep(ADXL345_BOOL_FALSE);                                 // 设置自动休眠为关
        adxl345_get_auto_sleep(&auto_sleep_enable);
        printf("adxl345_get_auto_sleep == 0x%02x\r\n",auto_sleep_enable);

        adxl345_set_sleep(ADXL345_BOOL_FALSE);                                      // 置为0,普通工作模式,置为1 ,休眠模式
        adxl345_get_sleep(&sleep_enable);
        printf("adxl345_get_sleep == 0x%02x\r\n",sleep_enable);

        adxl345_set_sleep_frequency(ADXL345_SLEEP_FREQUENCY_1HZ);
        adxl345_get_sleep_frequency(&sleep_freq);
        printf("adxl345_get_sleep_frequency == 0x%02x\r\n",sleep_freq);

        adxl345_set_fifo_mode(ADXL345_MODE_BYPASS);
        adxl345_get_fifo_mode(&fifo_mode);
        printf("adxl345_get_fifo_mode == 0x%02x\r\n",fifo_mode);

        adxl345_set_trigger_pin(ADXL345_INTERRUPT_PIN1);
        adxl345_get_trigger_pin(&interrupt_pin);
        printf("adxl345_get_trigger_pin == 0x%02x\r\n",interrupt_pin);

        adxl345_set_watermark(0x1F);
        adxl345_get_watermark(&watermark);
        printf("adxl345_get_watermark == 0x%02x\r\n",watermark);

        //set x y z offset
        adxl345_offset_convert_to_register(0.0f,&reg);
        adxl345_set_offset(reg,reg,reg);
        adxl345_get_offset((uint8_t *)&x_offset,(uint8_t *)&y_offset,(uint8_t *)&z_offset);
        printf("x_offset == 0x%02x y_offset == 0x%02x z_offset= 0x%02x\r\n",x_offset,y_offset,z_offset);

        /* set threshold 3g */
        adxl345_tap_threshold_convert_to_register(3.0f,&reg);
        /* set tap threshold */
        adxl345_set_tap_threshold(reg);
        adxl345_get_tap_threshold(&tap_threshold);
        printf("adxl345_get_tap_threshold == 0x%02x\r\n",tap_threshold);

        /* set 10 ms */
        adxl345_duration_convert_to_register(10 * 1000, (uint8_t *)&reg);
        /* set duration */
        adxl345_set_duration(reg);
        adxl345_get_duration(&tap_duration);
        printf("adxl345_get_duration == 0x%02x\r\n",tap_duration);

        /* set 20 ms */
        adxl345_latent_convert_to_register(20.0f, (uint8_t *)&reg);
        /* set latent */
        adxl345_set_latent(reg);
        adxl345_get_latent(&latent);
        printf("adxl345_get_latent == 0x%02x\r\n",latent);

        /* set 80 ms */
        adxl345_window_convert_to_register(80.0f, (uint8_t *)&reg);
        /* set windows time */
        adxl345_set_window(reg);
        adxl345_get_window(&tap_window);
        printf("adxl345_get_window == 0x%02x\r\n",tap_window);

        /* set x axis */
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_X, ADXL345_BOOL_FALSE);
        adxl345_get_tap_axis(ADXL345_TAP_AXIS_X,&reg);
        printf("adxl345_get_tap_x_axis == 0x%02x\r\n",reg);
  
        /* set y axis */
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_Y, ADXL345_BOOL_FALSE);
        adxl345_get_tap_axis(ADXL345_TAP_AXIS_Y,&reg);
        printf("adxl345_get_tap_y_axis == 0x%02x\r\n",reg);
 
        /* set z axis */
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_Z, ADXL345_BOOL_FALSE);
        adxl345_get_tap_axis(ADXL345_TAP_AXIS_Z,&reg);
        printf("adxl345_get_tap_z_axis == 0x%02x\r\n",reg);

        /* disable suppress */
        adxl345_set_tap_suppress(ADXL345_BOOL_FALSE);
        adxl345_get_tap_suppress(&reg);
        printf("adxl345_get_tap_suppress == 0x%02x\r\n",reg);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_SINGLE_TAP, ADXL345_INTERRUPT_PIN1);
        adxl345_get_interrupt_map(ADXL345_INTERRUPT_SINGLE_TAP, &reg);
        printf("adxl345_get_interrupt_map == 0x%02x\r\n",reg);

        /* set single tap */
        adxl345_set_interrupt(ADXL345_INTERRUPT_SINGLE_TAP, ADXL345_BOOL_FALSE);
        adxl345_get_interrupt(ADXL345_INTERRUPT_SINGLE_TAP,&reg);
        printf("adxl345_get_interrupt == 0x%02x\r\n",reg);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_DOUBLE_TAP, ADXL345_INTERRUPT_PIN1);
        adxl345_get_interrupt_map(ADXL345_INTERRUPT_DOUBLE_TAP, &reg);
        printf("adxl345_get_interrupt_map == 0x%02x\r\n",reg);

        /* set double tap */
        adxl345_set_interrupt(ADXL345_INTERRUPT_DOUBLE_TAP, ADXL345_BOOL_FALSE);
        adxl345_get_interrupt(ADXL345_INTERRUPT_DOUBLE_TAP,&reg);
        printf("adxl345_get_interrupt == 0x%02x\r\n",reg);

        /* link activity and inactivity */
        adxl345_set_link_activity_inactivity(ADXL345_BOOL_FALSE);
        adxl345_get_link_activity_inactivity(&reg);
        printf("adxl345_get_link_activity_inactivity == 0x%02x\r\n",reg);

        adxl345_action_threshold_convert_to_register(2.0f,(uint8_t *)&reg);
        adxl345_set_action_threshold(reg);
        adxl345_get_action_threshold(&action_threshold);
        printf("adxl345_get_action_threshold == 0x%02x\r\n",action_threshold);

        adxl345_inaction_threshold_convert_to_register(1.0f, (uint8_t *)&reg);
        /* set inaction threshold */
        adxl345_set_inaction_threshold( reg);
        adxl345_get_inaction_threshold(&inaction_threshold);
        printf("adxl345_get_inaction_threshold == 0x%02x\r\n",inaction_threshold);

        /* set 3 s */
        adxl345_inaction_time_convert_to_register(3, (uint8_t *)&reg); 
        /* set inaction time */
        adxl345_set_inaction_time(reg);
        adxl345_get_inaction_time(&inaction_time);
        printf("adxl345_get_inaction_time == 0x%02x\r\n",inaction_time);

        /* set action x */
        adxl345_set_action_inaction(ADXL345_ACTION_X, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_ACTION_X,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);

        /* set action y */
        adxl345_set_action_inaction(ADXL345_ACTION_Y, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_ACTION_Y,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);   
        
        /* set action z */
        adxl345_set_action_inaction(ADXL345_ACTION_Z, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_ACTION_Z,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);    
        
        /* set inaction x */
        adxl345_set_action_inaction(ADXL345_INACTION_X, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_INACTION_X,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);
        
        /* set inaction y */
        adxl345_set_action_inaction(ADXL345_INACTION_Y, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_INACTION_Y,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);    
        
        /* set inaction z */
        adxl345_set_action_inaction(ADXL345_INACTION_Z, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_INACTION_Z,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);

        /* set action ac coupled */
        adxl345_set_action_coupled(ADXL345_COUPLED_AC);
        adxl345_get_action_coupled(&reg);
        printf("adxl345_get_action_coupled == 0x%02x\r\n",reg);

        /* set inaction dc coupled */
        adxl345_set_inaction_coupled(ADXL345_COUPLED_DC);
        adxl345_get_inaction_coupled(&reg);
        printf("adxl345_get_inaction_coupled == 0x%02x\r\n",reg);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_ACTIVITY, ADXL345_INTERRUPT_PIN1);
        /* set activity */
        adxl345_set_interrupt(ADXL345_INTERRUPT_ACTIVITY, ADXL345_BOOL_FALSE);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_INACTIVITY, ADXL345_INTERRUPT_PIN1);
        /* set inactivity */
        adxl345_set_interrupt(ADXL345_INTERRUPT_INACTIVITY, ADXL345_BOOL_FALSE);
   
        /* set free fall */
        adxl345_free_fall_threshold_convert_to_register( 0.8f, (uint8_t *)&reg);     
        /* set free fall threshold */
        adxl345_set_free_fall_threshold(reg);
        adxl345_get_free_fall_threshold(&fall_threshold);
        printf("adxl345_get_free_fall_threshold == 0x%02x\r\n",fall_threshold);

        /* set 10 ms */
        adxl345_free_fall_time_convert_to_register(10, (uint8_t *)&reg);
        /* set free fall time */
        adxl345_set_free_fall_time(reg);
        adxl345_get_free_fall_time(&free_fall_time);
        printf("adxl345_get_free_fall_time == 0x%02x\r\n",free_fall_time);

        /* set interrupt 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_FREE_FALL, ADXL345_INTERRUPT_PIN1);
   
        /* set fall */
        adxl345_set_interrupt(ADXL345_INTERRUPT_FREE_FALL, ADXL345_BOOL_FALSE);

        /* set interrupt 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_DATA_READY, ADXL345_INTERRUPT_PIN1);
        /* set data ready */
        adxl345_set_interrupt(ADXL345_INTERRUPT_DATA_READY, ADXL345_BOOL_FALSE);
   
        /* set interrupt 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_WATERMARK, ADXL345_INTERRUPT_PIN1);
        /* set water mark */
        adxl345_set_interrupt(ADXL345_INTERRUPT_WATERMARK, ADXL345_BOOL_FALSE);

        /* set interrupt 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_OVERRUN, ADXL345_INTERRUPT_PIN1);
        /* set over run */
        adxl345_set_interrupt(ADXL345_INTERRUPT_OVERRUN, ADXL345_BOOL_FALSE);

        /* set justify right */
        adxl345_set_justify(ADXL345_JUSTIFY_RIGHT);
        adxl345_get_justify(&reg);
        printf("adxl345_get_justify == 0x%02x\r\n",reg);
        /* set 100 rate */
        adxl345_set_rate(ADXL345_RATE_100);
        adxl345_get_rate(&reg);
        printf("adxl345_get_rate == 0x%02x\r\n",reg);

        /* set full resolution */
        adxl345_set_full_resolution(ADXL345_BOOL_TRUE);
        adxl345_get_full_resolution(&reg);
        printf("adxl345_get_full_resolution == 0x%02x\r\n",reg);
        /* 2g */
        adxl345_set_range(ADXL345_RANGE_4G);
        adxl345_get_range(&reg);
        printf("adxl345_get_range == 0x%02x\r\n",reg);

        /* start measure */
        adxl345_set_measure(ADXL345_BOOL_TRUE);
    
        adxl345_get_measure(&reg);
        printf("adxl345_get_measure == 0x%02x\r\n",reg);

        uint8_t xyz_data[6] = {0};
        uint16_t x_data;
        uint16_t y_data;
        uint16_t z_data;

        while(1)
        {
            adxl345_get_x_y_z_data(ADXL345_REG_DATAX0,xyz_data,6);
            x_data = (int16_t)(xyz_data[1] << 8) | xyz_data[0];                                              /* set raw z */
            y_data = (int16_t)(xyz_data[3] << 8) | xyz_data[2];                                              /* set raw y */
            z_data = (int16_t)(xyz_data[5] << 8) | xyz_data[4];  
            
            printf("adxl345_get_x_data == %0.2f g\r\n",(float)x_data * 0.0039f);
            printf("adxl345_get_y_data == %0.2f g\r\n",(float)y_data * 0.0039f);
            printf("adxl345_get_z_data == %0.2f g\r\n",(float)z_data * 0.0039f);	
            
            delay_1ms(1000);
        }
    }  

5、各寄存器设置跟读取函数

/**
 * 获取设备ID的函数
*/
static void adxl345_get_device_id(uint8_t *id)
{
    adxl345_read_reg(ADXL345_REG_DEVID,id);
}

/**
 * 设置中断是高电平还是低电平的函数
*/
static uint8_t adxl345_set_interrupt_active_level(adxl345_interrupt_active_level_t active_level)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_DATA_FORMAT,&value);
    value &= ~(1 << 5);                                                                          /* clear config */
    value |= (active_level << 5); 
    delay_1ms(1);     
    adxl345_write_reg(ADXL345_REG_DATA_FORMAT,value);

    return 0;
}

static uint8_t adxl345_get_interrupt_active_level(adxl345_interrupt_active_level_t *active_level)
{
    uint8_t value = 0;
    adxl345_read_reg(ADXL345_REG_DATA_FORMAT,&value);
    value &= (0x01<<5);
    *active_level = (adxl345_interrupt_active_level_t )(value >> 5);
    return 0;
}

static uint8_t adxl345_set_auto_sleep(adxl345_bool_t enable)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= ~(1 << 4);                                                                        /* clear config */
    value |= enable << 4;  
    delay_1ms(1);      
    adxl345_write_reg(ADXL345_REG_POWER_CTL,value);

    return 0;
}

static uint8_t adxl345_get_auto_sleep(adxl345_bool_t *enable)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= (0x01 << 4);
    *enable = (adxl345_bool_t )(value >> 4);

    return 0;
}

static uint8_t adxl345_set_sleep(adxl345_bool_t enable)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= ~(1 << 2);                                                                        /* clear config */
    value |= enable << 2;    
    delay_1ms(1);    
    adxl345_write_reg(ADXL345_REG_POWER_CTL,value);

    return 0;
}

static uint8_t adxl345_get_sleep(adxl345_bool_t *enable)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);

    value &= (0x01 << 2);                                                                        /* clear config */
    *enable = (adxl345_bool_t) (value >>2);
    return 0;
}

static uint8_t adxl345_set_sleep_frequency(adxl345_sleep_frequency_t sleep_frequency)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= ~(0x03 << 0);                                                                        /* clear config */
    value |= (sleep_frequency << 0);    
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_POWER_CTL,value);
    return 0;
}

static uint8_t adxl345_get_sleep_frequency(adxl345_sleep_frequency_t *sleep_frequency)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= (0x03 << 0);                                                                        /* clear config */
    *sleep_frequency = (adxl345_sleep_frequency_t) (value >> 0);
    return 0;
}

static uint8_t adxl345_set_fifo_mode(adxl345_mode_t fifo_mode)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= ~(0x03 << 6);                                                                       /* clear config */
    value |= (fifo_mode << 6); 
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_FIFO_CTL,value);
    return 0;
}

static uint8_t adxl345_get_fifo_mode(adxl345_mode_t *fifo_mode)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= (0x03 << 6);                                                                       /* clear config */
    *fifo_mode = (adxl345_mode_t) value >> 6;
    return 0;
}

static uint8_t adxl345_set_trigger_pin(adxl345_interrupt_pin_t pin)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= ~(0x01<< 5);                                                                       /* clear config */
    value |= (pin << 5); 
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_FIFO_CTL,value);

    return 0;
}

static uint8_t adxl345_get_trigger_pin(adxl345_interrupt_pin_t *pin)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= (0x01<< 5);                                                                       /* clear config */
    *pin = (adxl345_interrupt_pin_t) value >> 5;
    return 0;
}

static uint8_t adxl345_set_watermark(uint8_t level)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= ~(0x1F<< 0);                                                                       /* clear config */
    value |= (level << 0); 
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_FIFO_CTL,value);
    return 0;
}

static uint8_t adxl345_get_watermark(uint8_t *level)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= (0x1F<< 0);                                                                       /* clear config */
    *level = value >> 0;
    return 0;
}

static uint8_t adxl345_offset_convert_to_register(float g, uint8_t *reg)
{
    *reg = (int8_t)(g / 0.0156f);    
    return 0;                                                                                   // 比例因子为15.6 mg/LSB(即0x7F = 2 g)。
}

static uint8_t adxl345_offset_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0156f;    
    return 0;
}

static uint8_t adxl345_set_offset(uint8_t x, uint8_t y, uint8_t z) 
{
    adxl345_write_reg(ADXL345_REG_OFSX,x);
    adxl345_write_reg(ADXL345_REG_OFSY,y);
    adxl345_write_reg(ADXL345_REG_OFSZ,z);

    return 0;
}

static uint8_t adxl345_get_offset(uint8_t *x, uint8_t *y, uint8_t *z) 
{
    adxl345_read_reg(ADXL345_REG_OFSX,x);
    adxl345_read_reg(ADXL345_REG_OFSY,y);
    adxl345_read_reg(ADXL345_REG_OFSZ,z);

    return 0;
}

static uint8_t adxl345_tap_threshold_convert_to_register(float g, uint8_t *reg)
{
    *reg = (uint8_t)(g / 0.0625f);        /* convert real data to register data */
    return 0;                             /* success return 0 */
}

static uint8_t adxl345_tap_threshold_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0625f;        /* convert raw data to real data */
    return 0;                           /* success return 0 */
}

static uint8_t adxl345_set_tap_threshold(uint8_t threshold)
{
    adxl345_write_reg(ADXL345_REG_THRESH_TAP ,threshold);        /* write config */
    return 0;
}

static uint8_t adxl345_get_tap_threshold(uint8_t *threshold)
{
    adxl345_read_reg(ADXL345_REG_THRESH_TAP ,threshold);       /* write config */
    return 0;
}

static uint8_t adxl345_duration_convert_to_register(uint32_t us, uint8_t *reg)
{
    *reg = (uint8_t)(us / 625);        /* convert real data to register data */
    return 0;                          /* success return 0 */
}

static uint8_t adxl345_duration_convert_to_data(uint8_t reg, uint32_t *us)
{
    *us = (uint32_t)((float)(reg) * 625.0f);        /* convert raw data to real data */
    return 0;                                       /* success return 0 */
}

static uint8_t adxl345_set_duration(uint8_t t) 
{
    adxl345_write_reg(ADXL345_REG_DUR, t);          /* write config */
    return 0;
}

static uint8_t adxl345_get_duration(uint8_t *t) 
{
    adxl345_read_reg(ADXL345_REG_DUR, t);          /* write config */
    return 0;
}

static uint8_t adxl345_latent_convert_to_register(float ms, uint8_t *reg)
{
    *reg = (uint8_t)(ms / 1.25f);        /* convert real data to register data */
    return 0;                            /* success return 0 */
}

static uint8_t adxl345_latent_convert_to_data(uint8_t reg, float *ms)
{
    *ms = (float)(reg) * 1.25f;        /* convert raw data to real data */
    return 0;                          /* success return 0 */
}

static uint8_t adxl345_set_latent(uint8_t t) 
{
    adxl345_write_reg(ADXL345_REG_LATENT, t);       /* write config */
    return 0;
}

static uint8_t adxl345_get_latent(uint8_t *t) 
{
    adxl345_read_reg(ADXL345_REG_LATENT, t);       /* write config */
    return 0;
}

static uint8_t adxl345_window_convert_to_register(float ms, uint8_t *reg)
{
    *reg = (uint8_t)(ms / 1.25f);        /* convert real data to register data */
    return 0;                            /* success return 0 */
}

static uint8_t adxl345_window_convert_to_data(uint8_t reg, float *ms)
{
    *ms = (float)(reg) * 1.25f;        /* convert raw data to real data */
    return 0;                          /* success return 0 */
}

uint8_t adxl345_set_window(uint8_t t)
{
    adxl345_write_reg(ADXL345_REG_WINDOW, t);       /* write config */
    return 0;
}

uint8_t adxl345_get_window(uint8_t *t)
{
    adxl345_read_reg(ADXL345_REG_WINDOW, t);       /* write config */
    return 0;
}

static uint8_t adxl345_set_tap_axis(adxl345_tap_axis_t axis, adxl345_bool_t enable)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_TAP_AXES,(uint8_t *)&prev);                        /* read config */
 
    prev &= ~(1 << axis);                                                                   /* clear axis */
    prev |= enable << axis;                                                                 /* set axis */
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_TAP_AXES, prev);             /* write config */
    return 0;
}

static uint8_t adxl345_get_tap_axis(adxl345_tap_axis_t axis, adxl345_bool_t *enable)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_TAP_AXES,(uint8_t *)&prev);                                /* read config */  
    prev &= (1 << axis);                                                                   /* clear axis */
    *enable = (adxl345_bool_t)(prev >> axis);                                                            /* set axis */
    
    return 0;
}

static uint8_t adxl345_set_tap_suppress(adxl345_bool_t enable)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_TAP_AXES,(uint8_t *)&prev);                        /* read config */
 
    prev &= ~(1 << 3);                                                                       /* clear suppress */
    prev |= enable << 3;                                                                   /* set axis */
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_TAP_AXES, prev);             /* write config */
    return 0;
}

static uint8_t adxl345_get_tap_suppress(adxl345_bool_t *enable)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_TAP_AXES,(uint8_t *)&prev);                                /* read config */  
    prev &= (1 << 3);                                                                      /* clear config */
    *enable = (adxl345_bool_t)(prev >> 3);   

    return 0;
}

static uint8_t adxl345_set_interrupt_map(adxl345_interrupt_t type, adxl345_interrupt_pin_t pin)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_INT_MAP, (uint8_t *)&prev);                                /* read config */

    prev &= ~(1 << type);                                                                   /* clear type */
    prev |= pin << type;  
    delay_1ms(1);            
    adxl345_write_reg(ADXL345_REG_INT_MAP, prev);                                          /* write config */
    return 0;
}

static uint8_t adxl345_get_interrupt_map(adxl345_interrupt_t type, adxl345_interrupt_pin_t *pin)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_INT_MAP, (uint8_t *)&prev);                                /* read config */
    prev &= (1 << type);                                                                    /* clear config */
    *pin = (adxl345_interrupt_pin_t)(prev >> type);   

    return 0;
}

static uint8_t adxl345_set_interrupt(adxl345_interrupt_t type, adxl345_bool_t enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_INT_ENABLE, (uint8_t *)&prev);                              /* read config */
 
    prev &= ~(1 << type);                                                                       /* clear interrupt */
    prev |= enable << type; 
    delay_1ms(1);                                                                    /* set interrupt */
    adxl345_write_reg(ADXL345_REG_INT_ENABLE, prev);                             /* write config */

    return 0;
}

static uint8_t adxl345_get_interrupt(adxl345_interrupt_t type, adxl345_bool_t *enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_INT_ENABLE, (uint8_t *)&prev);                              /* read config */
    
    prev &= (1 << type);                                                                     /* clear config */
    *enable = (adxl345_bool_t)(prev >> type);    

    return 0;
}

static uint8_t adxl345_set_link_activity_inactivity(adxl345_bool_t enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_POWER_CTL, (uint8_t *)&prev);                                  /* read config */
 
    prev &= ~(1 << 5);                                                                          /* clear config */
    prev |= enable << 5;    
    delay_1ms(1);                                                                    /* set interrupt */
    adxl345_write_reg(ADXL345_REG_POWER_CTL, prev);                                             /* write config */

    return 0;

}

static uint8_t adxl345_get_link_activity_inactivity(adxl345_bool_t *enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_POWER_CTL, (uint8_t *)&prev);                                  /* read config */
 
    prev &= 1 << 5;                                                                             /* get config */
    *enable = (adxl345_bool_t)(prev >> 5);   

    return 0;

}

static uint8_t adxl345_action_threshold_convert_to_register(float g, uint8_t *reg)
{
    *reg = (uint8_t)(g / 0.0625f);        /* convert real data to register data */
    return 0;                             /* success return 0 */
}

static uint8_t adxl345_action_threshold_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0625f;        /* convert raw data to real data */
    return 0;                           /* success return 0 */
}

static uint8_t adxl345_set_action_threshold(uint8_t threshold)
{
    adxl345_write_reg(ADXL345_REG_THRESH_ACT, threshold);       /* write config */
    return 0;
}

static uint8_t adxl345_get_action_threshold(uint8_t *threshold)
{
    adxl345_read_reg(ADXL345_REG_THRESH_ACT, threshold);       /* write config */
    return 0;
}

static uint8_t adxl345_inaction_threshold_convert_to_register(float g, uint8_t *reg)
{
    *reg = (uint8_t)(g / 0.0625f);        /* convert real data to register data */
    return 0;                             /* success return 0 */
}

static uint8_t adxl345_inaction_threshold_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0625f;        /* convert raw data to real data */
    return 0;                           /* success return 0 */
}

static uint8_t adxl345_set_inaction_threshold(uint8_t threshold)
{
    adxl345_write_reg(ADXL345_REG_THRESH_INACT, threshold);       /* write config */
    return 0;
}

static uint8_t adxl345_get_inaction_threshold(uint8_t *threshold)
{
    adxl345_read_reg(ADXL345_REG_THRESH_INACT, threshold);       /* read config */
    return 0;
}

static uint8_t adxl345_inaction_time_convert_to_register(uint8_t s, uint8_t *reg)
{
    *reg = s;                       /* convert real data to register data */
    return 0;                       /* success return 0 */
}

static uint8_t adxl345_inaction_time_convert_to_data(uint8_t reg, uint8_t *s)
{
    *s = reg;                       /* convert raw data to real data */
    return 0;                       /* success return 0 */
}

static void adxl345_set_inaction_time(uint8_t t)
{
    adxl345_write_reg(ADXL345_REG_TIME_INACT, t);       /* write config */
}

static void adxl345_get_inaction_time(uint8_t *t)
{
    adxl345_read_reg(ADXL345_REG_TIME_INACT, t);       /* write config */
}

static void adxl345_set_action_inaction(adxl345_action_inaction_t type, adxl345_bool_t enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);          /* read config */

    prev &= ~(1 << type);                                                                         /* clear type */
    prev |= (enable << type);                                                                     /* set type */
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_ACT_INACT_CTL,prev);                          /* write config */
}

static void adxl345_get_action_inaction(adxl345_action_inaction_t type, adxl345_bool_t *enable)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);       /* read config */

    prev &= (1 << type);                                                                        /* get type */
    *enable = (adxl345_bool_t)(prev >> type);                                                   /* set type */                                                                                /* success return 0 */
}

static void adxl345_set_action_coupled(adxl345_coupled_t coupled)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);         /* read config */
 
    prev &= ~(1 << 7);                                                                            /* clear coupled */
    prev |= (coupled << 7);                                                                       /* set coupled */
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_ACT_INACT_CTL,prev);       /* write config */
}

static void adxl345_get_action_coupled(adxl345_coupled_t *coupled)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);       /* read config */

    prev &= (1 << 7);                                                                           /* clear config */
    *coupled = (adxl345_coupled_t)(prev >> 7);                                                  /* set config */                                                                               /* success return 0 */
}

static void adxl345_set_inaction_coupled(adxl345_coupled_t coupled)
{
    uint8_t prev;

    
    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);       /* read config */

    prev &= ~(1 << 3);                                                                          /* clear config */
    prev |= (coupled << 3);                                                                     /* set inaction coupled */
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_ACT_INACT_CTL, prev);     /* write config */
}

static void adxl345_get_inaction_coupled(adxl345_coupled_t *coupled)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);       /* read config */

    prev &= (1 << 3);                                                                           /* clear config */
    *coupled = (adxl345_coupled_t)(prev >> 3);                                                  /* set config */                                                                               /* success return 0 */
}

static void adxl345_free_fall_threshold_convert_to_register(float g, uint8_t *reg)
{
    *reg = (uint8_t)(g / 0.0625f);        /* convert real data to register data */
}

static void adxl345_free_fall_threshold_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0625f;        /* convert raw data to real data */
}

static void adxl345_set_free_fall_threshold(uint8_t threshold)
{
    adxl345_write_reg(ADXL345_REG_THRESH_FF, threshold);       /* write config */
}

static void adxl345_get_free_fall_threshold(uint8_t *threshold)
{
    adxl345_read_reg(ADXL345_REG_THRESH_FF, threshold);       /* write config */
}

static void adxl345_free_fall_time_convert_to_register(uint16_t ms, uint8_t *reg)
{
    *reg = (uint8_t)(ms / 5);        /* convert real data to register data */
}

static void adxl345_free_fall_time_convert_to_data(uint8_t reg, uint16_t *ms)
{
    *ms = reg * 5;                  /* convert raw data to real data */
}

static void adxl345_set_free_fall_time(uint8_t t)
{
    adxl345_write_reg(ADXL345_REG_TIME_FF, t);       /* write config */
}

static void adxl345_get_free_fall_time(uint8_t *t)
{
    adxl345_read_reg(ADXL345_REG_TIME_FF, t);       /* write config */
}

static void adxl345_set_justify(adxl345_justify_t enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);         /* read config */
    prev &= ~(1 << 2);                                                                          /* clear config */
    prev |= (enable << 2);                                                                      /* set justify */
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_DATA_FORMAT, prev);       /* write config */
}

static void adxl345_get_justify(adxl345_justify_t *enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);                                /* read config */

    prev &= (1 << 2);                                                                         /* get config */
    *enable = (adxl345_justify_t)(prev >> 2);                                                 /* get justify */
}

static void adxl345_set_rate(adxl345_rate_t rate)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_BW_RATE, (uint8_t *)&prev);                                    /* read config */

    prev &= ~(0x1F);                                                                            /* clear rate */
    prev |= rate;                                                                               /* set rate */
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_BW_RATE, prev);                                               /* write config */
}

static void adxl345_get_rate(adxl345_rate_t *rate)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_BW_RATE, (uint8_t *)&prev);                            /* read config */

    prev &= 0x1F;                                                                         /* clear config */
    *rate = (adxl345_rate_t)(prev);                                                       /* get rate */                                                                          /* success return 0 */
}

static void adxl345_set_full_resolution(adxl345_bool_t enable)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);         /* read config */
 
    prev &= ~(1 << 3);                                                                          /* clear resolution */
    prev |= (enable << 3);  
    delay_1ms(1);                                                                 /* set resolution */
    
    adxl345_write_reg(ADXL345_REG_DATA_FORMAT, prev );       /* write config */
}

static void adxl345_get_full_resolution(adxl345_bool_t *enable)
{
    uint8_t  prev;

    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);       /* read config */
    prev &= (1 << 3);                                                                         /* clear config */
    *enable = (adxl345_bool_t)(prev >> 3);                                                    /* set resolution */                                                                            /* success return 0 */
}

static void adxl345_set_range(adxl345_range_t range)
{
    uint8_t res, prev;
    

    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);         /* read config */

    prev &= ~(3 << 0);                                                                          /* clear config */
    prev |= (range << 0);                                                                       /* set range */
    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_DATA_FORMAT, prev);       /* write config */
}

static void adxl345_get_range(adxl345_range_t *range)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);       /* read config */
    prev &= (3 << 0);                                                                         /* get config */
    *range = (adxl345_range_t)(prev >> 0);                                                    /* set range */
}

static void adxl345_set_measure(adxl345_bool_t enable)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_POWER_CTL, (uint8_t *)&prev);                                  /* read config */

    prev &= ~(1 << 3);                                                                          /* clear config */
    prev |= enable << 3;                                                                        /* set measure */

    delay_1ms(1);
    adxl345_write_reg(ADXL345_REG_POWER_CTL,prev);                                              /* write config */
}

static void adxl345_get_measure(adxl345_bool_t *enable)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_POWER_CTL, (uint8_t *)&prev);       /* read config */
    prev &= 1 << 3;                                                                         /* get config */
    *enable = (adxl345_bool_t)(prev >> 3);                                                  /* get measure */
}

6、头文件定义

#ifndef __ADXL345_H__
#define __ADXL345_H__

#include "gd32f30x.h"

#define ENABLE_ADXL345      1
#define ADXL345_ADDR        0xA6

#define ADXL345_REG_DEVID                 0x00        /**< device id register */
#define ADXL345_REG_THRESH_TAP            0x1D        /**< thresh tap register */
#define ADXL345_REG_OFSX                  0x1E        /**< x offset register */
#define ADXL345_REG_OFSY                  0x1F        /**< y offset register */
#define ADXL345_REG_OFSZ                  0x20        /**< z offset register */
#define ADXL345_REG_DUR                   0x21        /**< duration time register */
#define ADXL345_REG_LATENT                0x22        /**< latent register */
#define ADXL345_REG_WINDOW                0x23        /**< window register */
#define ADXL345_REG_THRESH_ACT            0x24        /**< act thresh register */
#define ADXL345_REG_THRESH_INACT          0x25        /**< inact thresh register */
#define ADXL345_REG_TIME_INACT            0x26        /**< inact time register */
#define ADXL345_REG_ACT_INACT_CTL         0x27        /**< act inact ctl register */
#define ADXL345_REG_THRESH_FF             0x28        /**< free fall thresh register */
#define ADXL345_REG_TIME_FF               0x29        /**< free fall time register */
#define ADXL345_REG_TAP_AXES              0x2A        /**< tap axes register */
#define ADXL345_REG_ACT_TAP_STATUS        0x2B        /**< act tap status register */
#define ADXL345_REG_BW_RATE               0x2C        /**< bandwidth rate register */
#define ADXL345_REG_POWER_CTL             0x2D        /**< power control register */
#define ADXL345_REG_INT_ENABLE            0x2E        /**< interrupt enable register */
#define ADXL345_REG_INT_MAP               0x2F        /**< interrupt map register */
#define ADXL345_REG_INT_SOURCE            0x30        /**< interrupt source register */
#define ADXL345_REG_DATA_FORMAT           0x31        /**< data format register */
#define ADXL345_REG_DATAX0                0x32        /**< data X0 register */
#define ADXL345_REG_DATAX1                0x33        /**< data X1 register */
#define ADXL345_REG_DATAY0                0x34        /**< data Y0 register */
#define ADXL345_REG_DATAY1                0x35        /**< data Y1 register */
#define ADXL345_REG_DATAZ0                0x36        /**< data Z0 register */
#define ADXL345_REG_DATAZ1                0x37        /**< data Z1 register */
#define ADXL345_REG_FIFO_CTL              0x38        /**< fifo control register */
#define ADXL345_REG_FIFO_STATUS           0x39        /**< fifo status register */

typedef enum
{
    ADXL345_BOOL_FALSE = 0x00,        /**< false */
    ADXL345_BOOL_TRUE  = 0x01,        /**< true */
} adxl345_bool_t;


typedef enum
{
    ADXL345_INTERRUPT_ACTIVE_LEVEL_HIGH = 0x00,        /**< interrupt active level high */
    ADXL345_INTERRUPT_ACTIVE_LEVEL_LOW  = 0x01,        /**< interrupt active level low */
} adxl345_interrupt_active_level_t;

/**
 * @brief adxl345 sleep frequency enumeration definition
 */
typedef enum
{
    ADXL345_SLEEP_FREQUENCY_8HZ = 0x00,        /**< sleep 8Hz */
    ADXL345_SLEEP_FREQUENCY_4HZ = 0x01,        /**< sleep 4Hz */
    ADXL345_SLEEP_FREQUENCY_2HZ = 0x02,        /**< sleep 2Hz */
    ADXL345_SLEEP_FREQUENCY_1HZ = 0x03,        /**< sleep 1Hz*/
} adxl345_sleep_frequency_t;

/**
 * @brief adxl345 mode enumeration definition
 */
typedef enum
{
    ADXL345_MODE_BYPASS  = 0x00,        /**< bypass mode */
    ADXL345_MODE_FIFO    = 0x01,        /**< fifo mode */
    ADXL345_MODE_STREAM  = 0x02,        /**< stream mode */
    ADXL345_MODE_TRIGGER = 0x03,        /**< trigger mode */
} adxl345_mode_t;

/**
 * @brief adxl345 interrupt pin enumeration definition
 */
typedef enum
{
    ADXL345_INTERRUPT_PIN1 = 0x00,        /**< interrupt pin 1 */
    ADXL345_INTERRUPT_PIN2 = 0x01,        /**< interrupt pin 2 */
} adxl345_interrupt_pin_t;

typedef enum
{
    ADXL345_TAP_AXIS_X = 0x02,        /**< x axis tap */
    ADXL345_TAP_AXIS_Y = 0x01,        /**< y axis tap */
    ADXL345_TAP_AXIS_Z = 0x00,        /**< z axis tap */
} adxl345_tap_axis_t;

typedef enum
{
    ADXL345_INTERRUPT_DATA_READY = 0x07,        /**< data ready */
    ADXL345_INTERRUPT_SINGLE_TAP = 0x06,        /**< single tap */
    ADXL345_INTERRUPT_DOUBLE_TAP = 0x05,        /**< double tap */
    ADXL345_INTERRUPT_ACTIVITY   = 0x04,        /**< activity */
    ADXL345_INTERRUPT_INACTIVITY = 0x03,        /**< inactivity */
    ADXL345_INTERRUPT_FREE_FALL  = 0x02,        /**< free fall */
    ADXL345_INTERRUPT_WATERMARK  = 0x01,        /**< watermark */
    ADXL345_INTERRUPT_OVERRUN    = 0x00,        /**< overrun */
} adxl345_interrupt_t;

typedef enum
{
    ADXL345_ACTION_X   = 0x06,        /**< x axis action */
    ADXL345_ACTION_Y   = 0x05,        /**< y axis action */
    ADXL345_ACTION_Z   = 0x04,        /**< z axis action */
    ADXL345_INACTION_X = 0x02,        /**< x axis inaction */
    ADXL345_INACTION_Y = 0x01,        /**< y axis inaction */
    ADXL345_INACTION_Z = 0x00,        /**< z axis inaction */
} adxl345_action_inaction_t;

typedef enum
{
    ADXL345_COUPLED_DC = 0x00,        /**< DC coupled */
    ADXL345_COUPLED_AC = 0x01,        /**< AC coupled */
} adxl345_coupled_t;

typedef enum
{
    ADXL345_JUSTIFY_RIGHT = 0x00,        /**< right justify */
    ADXL345_JUSTIFY_LEFT  = 0x01,        /**< left justify */
} adxl345_justify_t;

typedef enum
{
    ADXL345_RATE_0P1            = 0x00,        /**< 0.1Hz */
    ADXL345_RATE_0P2            = 0x01,        /**< 0.2Hz */
    ADXL345_RATE_0P39           = 0x02,        /**< 0.39Hz */
    ADXL345_RATE_0P78           = 0x03,        /**< 0.78Hz */
    ADXL345_RATE_1P56           = 0x04,        /**< 1.56Hz */
    ADXL345_RATE_3P13           = 0x05,        /**< 3.13Hz */
    ADXL345_RATE_6P25           = 0x06,        /**< 6.25Hz */
    ADXL345_RATE_12P5           = 0x07,        /**< 12.5Hz */
    ADXL345_RATE_25             = 0x08,        /**< 25Hz */
    ADXL345_RATE_50             = 0x09,        /**< 50Hz */
    ADXL345_RATE_100            = 0x0A,        /**< 100Hz */
    ADXL345_RATE_200            = 0x0B,        /**< 200Hz */
    ADXL345_RATE_400            = 0x0C,        /**< 400Hz */
    ADXL345_RATE_800            = 0x0D,        /**< 800Hz */
    ADXL345_RATE_1600           = 0x0E,        /**< 1600Hz */
    ADXL345_RATE_3200           = 0x0F,        /**< 3200Hz */
    ADXL345_LOW_POWER_RATE_12P5 = 0x17,        /**< low power 12.5Hz */
    ADXL345_LOW_POWER_RATE_25   = 0x18,        /**< low power 25Hz */
    ADXL345_LOW_POWER_RATE_50   = 0x19,        /**< low power 50Hz */
    ADXL345_LOW_POWER_RATE_100  = 0x1A,        /**< low power 100Hz */
    ADXL345_LOW_POWER_RATE_200  = 0x1B,        /**< low power 200Hz */
    ADXL345_LOW_POWER_RATE_400  = 0x1C,        /**< low power 400Hz */
} adxl345_rate_t;

typedef enum
{
    ADXL345_RANGE_2G  = 0x00,        /**< ±2G */
    ADXL345_RANGE_4G  = 0x01,        /**< ±4G */
    ADXL345_RANGE_8G  = 0x02,        /**< ±8G */
    ADXL345_RANGE_16G = 0x03,        /**< ±16G */
} adxl345_range_t;

void adxl345_xyz_read_init();
#endif

7、实验现象

main函数直接调用void adxl345_xyz_read_init()即可。

打印结果如下:

在这里插入图片描述

8、代码

代码路径:https://gitee.com/xiaoguo-tec_0/gd32-iap-code.git

9、注意

代码里要打印float数据,修改APPlication Makefile文件如下:
在这里插入图片描述
如果编译后文件太大,超出我们定义的50K,那么修改GD32F303RCTx_FLASH.ld文件,
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/606070.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Eclipse 教程 完结

Eclipse 快捷键 关于快捷键 Eclipse 的很多操作都提供了快捷键功能&#xff0c;我们可以通过键盘就能很好的控制 Eclipse 各个功能&#xff1a; 使用快捷键关联菜单或菜单项使用快捷键关联对话窗口或视图或编辑器使用快捷键关联工具条上的功能按钮 Eclipse 快捷键列表可通过…

《crossfire》游戏分析

文章目录 一、 穿越火线简介和定位二、 游戏发行三、 用户基础四、 游戏玩法枪王排位团队竞技爆破模式歼灭模式突围模式幽灵模式生化模式个人竞技挑战模式跳跳乐地图工坊 五、 游戏竞技公平性cf竞技公平性 六、CF火热到现在的原因分析1.时代、空间背景2.用户基础3.丰富的游戏模…

【iOS】—— nil、Nil、NULL和NSNull学习

nil、Nil、NULL和NSNull 文章目录 nil、Nil、NULL和NSNullnilNSNullNilNULL总结&#xff1a; 我们先来看看这几个苹果官方文档的解释&#xff1a; nil&#xff1a;Defines the id of a null instance.&#xff08;定义空实例的id&#xff09;Nil&#xff1a;Defines the id of…

给编程初学者的一封信

提醒&#xff1a;以下内容仅做参考&#xff0c;具体请自行设计。 随着信息技术的快速发展&#xff0c;编程已经成为一个越来越重要的技能。那么&#xff0c;我们该如何入门编程呢&#xff1f;欢迎大家积极讨论 一、自学编程需要注意什么&#xff1f; 要有足够的时间、精力等…

大数据治理入门系列:数据目录

在元数据管理一文中&#xff0c;我们曾将数据比喻为一本本的书&#xff0c;将书的作者、出版时间等信息比喻为元数据。试想一下&#xff0c;假如你是一名新任的图书管理员&#xff0c;如何快速掌握图书馆的馆藏情况呢&#xff1f;假如你是一名读者&#xff0c;如何快速找到你需…

Redis GEO功能详细介绍与实战

一、概述 Redis的Geo功能主要用于存储地理位置信息&#xff0c;并对其进行操作。该功能在Redis 3.2版本新增。Redis Geo操作方法包括&#xff1a; geoadd&#xff1a;添加地理位置的坐标&#xff1b;geopos&#xff1a;获取地理位置的坐标&#xff1b;geodist&#xff1a;计算…

第五届湖北省大学生程序设计竞赛(HBCPC 2023)vp赛后补题

Problem - B - Codeforces 思路&#xff1a; 数位dp&#xff0c;如果我们暴力的计算的状态的话&#xff0c;显然就是记录每个数字出现几次。但是显然这样难以发挥数位dp的记忆化功效&#xff0c;因为只有出现次数相同&#xff0c;你是什么数字&#xff0c;实际是无所谓的。所…

I2C学习笔记——I2C协议学习

1、I2C简介&#xff1a;一种简单、双线双向的同步串行总线&#xff0c;利用串行时钟线(SCL)和串行数据线(SDA)在连接总线的两个器件之间进行信息传递&#xff1b; 数据传输是通过对SCL和SDA线高低电平时序的控制&#xff0c;来产生I2C总线协议所需要的信号。在总线空闲状态时&a…

【Linux C】基于树莓派/香橙派的蓝牙服务端——支持多蓝牙设备接入

一、需求 在树莓派/香橙派上利用开发板自带的蓝牙作为一个蓝牙服务端&#xff08;主机&#xff09;&#xff0c;允许外来设备&#xff08;从机&#xff09;通过蓝牙接入进行通信&#xff0c;通信格式为透传方式&#xff1b;采用的编程语言为Linux C 二、环境准备 bluez安装 …

三波混频下的相位失配原理

原理推导 在四波混频情况下&#xff0c;实现零相位失配是一件很困难的事情。因为在四波混频中&#xff0c;相位调制和增益都依赖于相同的参数&#xff0c;即克尔非线性 γ \gamma γ。这个问题可以用嵌入在传输线上的辅助共振元件的复杂色散工程来部分解决。 但是在三波混频中…

ceph集群监控

文章目录 Ceph Dashboard启用dashboard插件dashboard启用ssl Promethues监控ceph启用prometheus模块配置prometheus采集数据grafana数据展示 Ceph Dashboard ceph-dashboard官方介绍&#xff1a;https://docs.ceph.com/en/latest/mgr/dashboard/ Ceph Dashboard是一个内置的c…

数据库系统概论---选择题刷题实训

&#xff08;一&#xff09;选择题 1.下列选项中&#xff0c;不属于关系模型三要素的是&#xff08; C &#xff09; A&#xff0e;数据结构 B&#xff0e;数据操纵 C&#xff0e;数据安全 D&#xff0e;数据完整性规则 2.保证数据库…

【Spring】透过Spring源码查看Bean的命名转换规则

近期在写Spring项目的时候&#xff0c;需要通过注解的形式去替代之前直接将Bean存放在Spring容器这种方式&#xff0c;以此来简化对于Bean对象的操作&#xff0c;但是这样无法通过准确的Id去获取到相应的Bean对象了 测试观察 首先&#xff0c;如果要将指定的对象存放到Spring中…

SQL注入基础知识

文章目录 一、注入的分类1.基于服务器收到的响应2.基于处理输入的SQL查询&#xff08;数据类型&#xff09;3.基于程度和顺序的注入&#xff08;哪里受了影响&#xff09;4、基于注入点位置 二、系统函数1.字符串连接函数2.一般用于尝试的语句3.union操作符的介绍 总结 一、注入…

caught (in promise) RangeError: Maximum call stack size exceeded-vue前置导航守卫死循环

报错图 产生场景 1.近期在搭建移动端的架子时&#xff0c;在写路由守卫时&#xff0c;发现陷入死循环&#xff0c;报错意思是循环超出栈。。 2.后面排查了一圈问题之后&#xff0c;发现这个问题很小&#xff0c;但很难发现&#xff0c;在此记录。 3.vue 路由的导航守卫并不是…

类和对象【4】static成员、const对象、友元

全文目录 引言static成员static成员变量static成员函数 const对象友元友元函数友元类 总结 引言 通过前面的三篇文章&#xff0c;相信大家对类和对象已经有了一个基本的认识。 类和对象1&#xff08;初识&#xff09; 类和对象2&#xff08;默认成员函数&#xff09; 类和对象…

数据结构与算法11:堆

目录 【堆】 堆中插入和删除元素 堆排序 【堆的常见应用】 应用1&#xff1a;优先级队列 &#xff08;1&#xff09;合并有序小文件 &#xff08;2&#xff09;定时器功能 应用2&#xff1a;计算排行榜中前K个数据 应用3&#xff1a;求中位数 应用4&#xff1a;计算…

算法基础--MD5算法介绍

1、简介 MD5再开发过程中经常碰到的一种算法&#xff0c;因此感觉有必要对其原理进行更深入的了解一下。 2、算法概念 散列函数&#xff0c;也称作哈希函数&#xff0c;消息摘要函数&#xff0c;单向函数或者杂凑函数。散列函数主要用于验证数据的完整性。通过散列函数&#x…

自然语言处理从入门到应用——自然语言处理的应用任务

分类目录&#xff1a;《自然语言处理从入门到应用》总目录 本文介绍信息抽取、情感分析、问答系统、机器翻译和对话系统等自然语言处理应用任务。这些任务可以直接或间接地以产品的形式为终端用户提供服务&#xff0c;是自然语言处理研究应用落地的主要技术。 信息抽取 信息抽…

天气预报信息获取程序--GUI--可以使用

上次正对项目中需要填写项目日志&#xff0c;制作了一个命令行版本的下载天气信息的程序&#xff0c;满足日常需要&#xff0c;调整一下界面版本的程序 如果大家使用命令行的可以使用下面的版本&#xff08;连接&#xff09; https://ht666666.blog.csdn.net/article/details…