本帖最后由 小马哥-1650185 于 2018-11-19 22:17 编辑
加速度传感器是一种可以检测"倾斜"、"撞击"、"振动"等这些传感器动作状态的传感器。传感器有1轴2轴3轴等种类,例如1轴可检测1个方向(只检测纵向),2轴可检测2个方向(纵向和横向),3轴可检测3个方向(纵向、横向和高度)。
传感器评估套件中搭载的加速度传感器为3轴,所以可检测XYZ三个方向。
芯片资料
原理图
pcb 布局图
使用时,电源电压的设定(1.8 或 3.0V)
KX224初始化
代码:
- init(void)
- {
- byte rc;
- unsigned char reg;
- unsigned char gsel;
- rc = read(KX224_WHO_AM_I, ®, sizeof(reg));
- if (rc != 0) {
- Serial.println(F("Can't access KX224"));
- return (rc);
- }
- Serial.print(F("KX224_WHO_AMI Register Value = 0x"));
- Serial.println(reg, HEX);
-
- if (reg != KX224_WAI_VAL) {
- Serial.println(F("Can't find KX224"));
- return (rc);
- }
- reg = KX224_CNTL1_VAL;
- rc = write(KX224_CNTL1, ®, sizeof(reg));
- if (rc != 0) {
- Serial.println("Can't write KX224 CNTL1 register at first");
- return (rc);
- }
- reg = KX224_ODCNTL_VAL;
- rc = write(KX224_ODCNTL, ®, sizeof(reg));
- if (rc != 0) {
- Serial.println("Can't write KX224 ODCNTL register");
- return (rc);
- }
- rc = read(KX224_CNTL1, ®, sizeof(reg));
- if (rc != 0) {
- Serial.println(F("Can't read KX224 CNTL1 register"));
- return (rc);
- }
- gsel = reg & KX224_CNTL1_GSELMASK;
- reg |= KX224_CNTL1_PC1;
- rc = write(KX224_CNTL1, ®, sizeof(reg));
- if (rc != 0) {
- Serial.println(F("Can't write KX224 CNTL1 register at second"));
- return (rc);
- }
-
- switch(gsel) {
- case KX224_CNTL1_GSEL_8G :
- // (Equivalent counts) / (Range) = (32768 / 8)
- _g_sens = 4096;
- break;
- case KX224_CNTL1_GSEL_16G :
- // (Equivalent counts) / (Range) = (32768 / 16)
- _g_sens = 2048;
- break;
- case KX224_CNTL1_GSEL_32G :
- // (Equivalent counts) / (Range) = (32768 / 32)
- _g_sens = 1024;
- break;
- default:
- break;
- }
- return (rc);
- }
读取数据
代码:
- byte KX224::get_rawval(unsigned char *data)
- {
- byte rc;
- rc = read(KX224_XOUT_L, data, 6);
- if (rc != 0) {
- Serial.println(F("Can't get KX224 accel value"));
- }
- return (rc);
- }
- byte KX224::get_val(float *data)
- {
- byte rc;
- unsigned char val[6];
- signed short acc[3];
- rc = get_rawval(val);
- if (rc != 0) {
- return (rc);
- }
- acc[0] = ((signed short)val[1] << 8) | (val[0]);
- acc[1] = ((signed short)val[3] << 8) | (val[2]);
- acc[2] = ((signed short)val[5] << 8) | (val[4]);
- // Convert LSB to g
- data[0] = (float)acc[0] / _g_sens;
- data[1] = (float)acc[1] / _g_sens;
- data[2] = (float)acc[2] / _g_sens;
- return (rc);
- }
串口输出结果
波形信息