超低功耗、3轴MEMS加速度计——ADXL362(二)

作者: 恋上你的芯
上传时间为: 2016-02-17 09:37 AM
2016-02-17
阅读:

上一篇说原理,简单介绍,这篇只要讲编程的

四、加速度轴方向

1.png

符合迪卡儿坐标系(右手坐标系)

2.jpg

五、关键代码如下

值得注意的是 SPI定时方案遵循CPHA = CPOL = 0

第一个上升沿采样,空闲状态为低电平

ADSENSORAPP_RESULT_TYPE ADXL362_Init()

{

ADSENSORAPP_RESULT_TYPE result;

ADI_SPI_RESULT_TYPE status = ADI_SPI_SUCCESS;

uint8_t TxD[10]= {0}, RxD[10]= {0};

status = adi_SPI_SetChipSelect(hSPI1Dev, ADXL362_CS);

if (ADI_SPI_SUCCESS == status)

status = adi_SPI_SetBitrate (hSPI1Dev, ADXL362_SPI_CLK); //4MHz SCLK

//设置运动 与静止阀值

TxD[0]= XL362_REG_WRITE; //Command Byte = WRITE (0x0A)

TxD[1]= XL362_THRESH_ACTL; //Starting Register Address XL362_THRESH_ACTL = 0x20

TxD[2]= 150; //Data to write to register XL362_THRESH_ACTL (0x20) = 105

TxD[3]= 0; //Data to write to register XL362_THRESH_ACTH (0x21) = 0

TxD[4]= 0; //Data to write to register XL362_TIME_ACT (0x22) = 3 (secs)

TxD[5]= 105; //Data to write to register XL362_THRESH_INACTL (0x23) = 105

TxD[6]= 0; //Data to write to register XL362_THRESH_INACTH (0x24) = 0

TxD[7]= 0; //Data to write to register XL362_TIME_INACTL (0x25) = 37 (3 secs)

TxD[8]= 0; //Data to write to register XL362_TIME_INACTH (0x26) = 0

TxD[9]= 0x3F;//Data to write to register XL362_ACT_INACT_CTL (0x27) = 0x3F

//TxD[10]= 0x00;//Data to write to register XL362_FIFO_CONTROL (0x28) = 0x00

//TxD[11]= 0x00;//Data to write to register XL362_FIFO_SAMPLES (0x29) = 0x00

if (ADI_SPI_SUCCESS == status)

status = API_SPI_RW_Byte(&TxD[0],&RxD[0], 10); //Write & Read Register Data

//配置中断模式 电低为效中断, 使能唤醒与运动中断

//+-2G 滤波0DR=12.5HZ

//最后设置 POWER_CTL 开始工作

TxD[0]= XL362_REG_WRITE; //Command Byte = WRITE (0x0A)

TxD[1]= XL362_INTMAP1; //Starting Register Address XL362_INTMAP1 = 0x2A

TxD[2]= 0x50;//Data to write to register XL362_INTMAP1 (0x2A) = 0x00

TxD[3]= 0x00;//Data to write to register XL362_INTMAP2 (0x2B) = 0x00

TxD[4]= 0x00;//Data to write to register XL362_FILTER_CTL (0x2C) = 0x00

TxD[5]= XL362_MEASURE_3D;//Data to write to register XL362_POWER_CTL (0x2D) = 0x00

if (ADI_SPI_SUCCESS == status)

status = API_SPI_RW_Byte(&TxD[0],&RxD[0], 6); //Write & Read Register Data

if(ADI_SPI_SUCCESS == status)

result = API_SUCCESS;

else

result = API_FAIL;

return result;

}

ADSENSORAPP_RESULT_TYPE Get_Data_From_ADXL362()

{

ADI_SPI_RESULT_TYPE status = ADI_SPI_SUCCESS;

uint8_t TxD[6]= {0}, RxD[6]= {0};

int16_t RxD_temp[3]= {0};

status = adi_SPI_SetChipSelect(hSPI1Dev, ADXL362_CS);

if(ADXL362_SPI_CLK != (adi_SPI_GetBitrate(hSPI1Dev)))

status = adi_SPI_SetBitrate (hSPI1Dev, ADXL362_SPI_CLK); //4MHz SCLK

#ifdef READ_8_BIT_DATA

TxD[0]= XL362_REG_READ;//Command Byte = READ (0x0B)

TxD[1]= XL362_XDATA8;//X-Data Reg.Addr = XDATA8 (0x08)

TxD[2]= XL362_YDATA8;//Y-Data Reg.Addr = YDATA8 (0x09)

TxD[3]= XL362_ZDATA8;//Z-Data Reg.Addr = ZDATA8 (0x0A)

status = API_SPI_RW_Byte(&TxD[0],&RxD[0],6); //Write & Read Register Data

if (status == ADI_SPI_SUCCESS) {

SensData.ADXL362[0] = RxD[2];

SensData.ADXL362[1] = RxD[3];

SensData.ADXL362[2] = RxD[4];

return API_SUCCESS;

}

#else

TxD[0]= XL362_REG_READ;//Command Byte = READ (0x0B)

TxD[1]= XL362_XDATAL;//XL-Data Reg.Addr = XL362_XDATAL (0x0E)

TxD[2]= XL362_XDATAH;//XH-Data Reg.Addr = XL362_XDATAH (0x0F)

status = API_SPI_RW_Byte(&TxD[0],&RxD[0],4); //Write & Read Register Data

RxD_temp[0] = (int16_t) ((RxD[3]<<8) | RxD[2]);

//RxD_temp[0] = RxD_temp[0] & 0x0FFF;

TxD[0]= XL362_REG_READ;//Command Byte = READ (0x0B)

TxD[1]= XL362_YDATAL;//YL-Data Reg.Addr = XL362_YDATAL (0x10)

TxD[2]= XL362_YDATAH;//YH-Data Reg.Addr = XL362_YDATAH (0x11)

status = API_SPI_RW_Byte(&TxD[0],&RxD[0],4); //Write & Read Register Data

RxD_temp[1] = (int16_t) ((RxD[3]<<8) | RxD[2]);

//RxD_temp[1] = RxD_temp[1] & 0x0FFF;

TxD[0]= XL362_REG_READ;//Command Byte = READ (0x0B)

TxD[1]= XL362_ZDATAL;//ZL-Data Reg.Addr = XL362_ZDATAL (0x12)

TxD[2]= XL362_ZDATAH;//ZH-Data Reg.Addr = XL362_ZDATAH (0x13)

status = API_SPI_RW_Byte(&TxD[0],&RxD[0],4); //Write & Read Register Data

RxD_temp[2] = (int16_t) ((RxD[3]<<8) | RxD[2]);

//RxD_temp[2] = RxD_temp[2] & 0x0FFF;

if (status == ADI_SPI_SUCCESS) {

SensData.ADXL362[0] = RxD_temp[0];

SensData.ADXL362[1] = RxD_temp[1];

SensData.ADXL362[2] = RxD_temp[2];

return API_SUCCESS;

}

#endif

else {

SensData.ADXL362[0] = 0;

SensData.ADXL362[1] = 0;

SensData.ADXL362[2] = 0;

return API_FAIL;

}

}

ADSENSORAPP_RESULT_TYPE SoftReset_ADXL362_Sensor()

{

ADSENSORAPP_RESULT_TYPE result = API_SUCCESS;

ADI_SPI_RESULT_TYPE status=ADI_SPI_SUCCESS;

uint8_t TxD[3]= {0}, RxD[3]= {0};

TxD[0]= XL362_REG_WRITE;//Command Byte = WRITE (0x0A)

TxD[1]= XL362_SOFT_RESET;//ADXL362_SOFT_RESET_REG = 0x1F

TxD[2]= XL362_SOFT_RESET_KEY; //ADXL362_SOFT_RESET_KEY = 0x52

status = adi_SPI_SetChipSelect(hSPI1Dev, ADXL362_CS);

if(ADXL362_SPI_CLK != (adi_SPI_GetBitrate(hSPI1Dev)))

status = adi_SPI_SetBitrate (hSPI1Dev, ADXL362_SPI_CLK); //4MHz SCLK

status = API_SPI_RW_Byte(TxD, RxD, 3);

if (status == ADI_SPI_SUCCESS)

result = API_SUCCESS;

else

result = API_FAIL;

return result;

}

uint16_t ADXL362_Delay = 1000;

ADSENSORAPP_RESULT_TYPE Detect_ADXL362_Sensor()

{

ADI_SPI_RESULT_TYPE status=ADI_SPI_SUCCESS;

uint8_t TxD[5]= {0}, RxD[5]= {0};

status = adi_SPI_SetChipSelect(hSPI1Dev, ADXL362_CS);

if(ADXL362_SPI_CLK != (adi_SPI_GetBitrate(hSPI1Dev)))

status = adi_SPI_SetBitrate (hSPI1Dev, ADXL362_SPI_CLK); //4MHz SCLK

SoftReset_ADXL362_Sensor();//复位

adsAPI_Delay(ADXL362_Delay);

//读取ID

TxD[0]= XL362_REG_READ;//Command Byte = READ (0x0B)

TxD[1]= XL362_DEVID_AD;//ADXL362_DEVID_AD Reg.Addr = 0x00

TxD[2]= XL362_DEVID_MST; //ADXL362_DEVID_MST Reg.Addr = 0x01

TxD[3]= XL362_PARTID;//ADXL362_PARTID Reg.Addr = 0x02

if (ADI_SPI_SUCCESS == status)

status = API_SPI_RW_Byte(&TxD[0],&RxD[0],5); //Write & Read Register Data

if (ADI_SPI_SUCCESS == status) {

if((RxD[2] == 0xAD)&&(RxD[3] == 0x1D)&&(RxD[4] == 0xF2))

return API_SUCCESS;

else

return API_FAIL;

}

return API_FAIL;

}

六、结果

3.png

值很大,实质是没进行转换,比例因子如下

4.jpg

代码修改如下

ADSENSORAPP_RESULT_TYPE Get_Data_From_ADXL362()

{

。。。。。。。。。。。。。。。。。。。。。。。。。。。。

if (status == ADI_SPI_SUCCESS) {

#if 0

SensData.ADXL362[0] = RxD_temp[0];

SensData.ADXL362[1] = RxD_temp[1];

SensData.ADXL362[2] = RxD_temp[2];

#else

SensData.ADXL362[0] = (float)RxD_temp[0]/1000;//2g ->1000 4g ->500

SensData.ADXL362[1] = (float)RxD_temp[1]/1000;// 8g->250

SensData.ADXL362[2] = (float)RxD_temp[2]/1000;

#endif

return API_SUCCESS;

}

#endif

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

}

5.png

运动学中,g的单位为m/s^2,1g = 9.8m/s^2 重力加速度

全部评论 ()
条评论
写评论

创建讨论帖子

登录 后参与评论
系统提示