[英]Converting raw Accelerometer values to 12 bit number
I am coding an Accelerometer that is in built in one of the microcontroller (TZ1000 series). 我正在对内置在一个微控制器(TZ1000系列)中的加速度计进行编码。 I am trying send the data BLE to PC application.
我正在尝试将数据BLE发送到PC应用程序。 I have to send the number in 2 bytes and receive it in application side and combine it to form a 12-bit number.
我必须以2个字节发送该数字,并在应用程序端接收它,并将其组合成一个12位数字。 I am facing problem in converting the actual raw accelerometer values to 12 bit number.
我在将实际的原始加速度计值转换为12位数字时遇到问题。 I am reading it in following format.
我正在阅读以下格式。
acc->ReadAcceleration((uint16_t*)buf);
ChannelX[M] = (buf[0].acceleration << 4) & 0xFFF0;
ChannelY[M] = (buf[1].acceleration << 4) & 0xFFF0;
ChannelZ[M] = (buf[2].acceleration << 4) & 0xFFF0;
Where the variables are of type and size as follows, 如果变量的类型和大小如下,
int16_t ChannelX[4] = {0,0,0,0};
int16_t ChannelY[4] = {0,0,0,0};
int16_t ChannelZ[4] = {0,0,0,0};
buf
is of type static ACCEL_ACCELERATION buf[12];
buf
类型为static ACCEL_ACCELERATION buf[12];
and the structure of it is as follows, 它的结构如下
typedef struct _ACCEL_ACCELERATION {
uint16_t updated : 1;
uint16_t reserved : 3;
int16_t acceleration :12;
} ACCEL_ACCELERATION;
Now when i am converting this data to two bytes and transmit over BLE, I am following below logic. 现在,当我将数据转换为两个字节并通过BLE传输时,我遵循以下逻辑。
uart_tx_data[i] = (uint8_t) (ChannelX[j] & 0xFF);
uart_tx_data[i+1] = (uint8_t) ((ChannelX[j]>>12) & 0x0F) ;
uart_tx_data[i+2] = (uint8_t) (ChannelY[j] & 0xFF);
uart_tx_data[i+3] = (uint8_t) ((ChannelY[j]>>12) & 0x0F) ;
uart_tx_data[i+4] = (uint8_t) (ChannelZ[j] & 0xFF);
uart_tx_data[i+5] = (uint8_t) ((ChannelZ[j]>>12) & 0x0F) ;
where variable is of type static uint8_t uart_tx_data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
其中变量的类型为
static uint8_t uart_tx_data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
At receive side i am converting it to a 12 bit number as follows, 在接收端,我将其转换为12位数字,如下所示:
X[i]= (num[2] & 0xFF) | ((num[3] & 0x0F) << 12);
where int[] X= new int[2];
其中
int[] X= new int[2];
The values that I am getting is wrong, I checked it by doing 1g test on the accelerometer. 我得到的值是错误的,我通过在加速度计上进行1g测试来检查它。 For example when its flat it should give a number closer to zero.
例如,当其展平时,其数值应接近零。 and at 1g it should give a number according to the sensitivity I have set ( in my case its 8G and value is 256).
并且在1g时,应根据我设置的灵敏度给出一个数字(在我的情况下为8G,值为256)。
Is there anything wrong in the logic I have used?? 我使用的逻辑有什么问题吗? I request some one to help me on this.
我要求有人帮助我。
Thank you in advance. 先感谢您。
Your shifts are all off. 你的班次都没有了。 Use a constant to see what's happening.
使用常量查看正在发生的事情。 Example: put 0x1234 into the structure (note I'm assuming 16bit variable and not caring about the bit fields in the structure which change things a bit but not the reason why it's not working)
示例:将0x1234放入结构中(请注意,我假设16bit变量,而不在乎结构中的位字段,这些位字段会稍微改变事情,但不会更改其无效的原因)
ChannelX[M] = (buf[0].acceleration << 4) & 0xFFF0;
This will result in 0x2340. 这将导致0x2340。 Not sure why you want the lowest four bits zero but let's go with it.
不知道为什么要使最低的四位为零,但让我们继续吧。 Then you send it:
然后发送:
uart_tx_data[i] = (uint8_t) (ChannelX[j] & 0xFF);
uart_tx_data[i+1] = (uint8_t) ((ChannelX[j]>>12) & 0x0F) ;
First part will be 0x2340 & 0xff == 0x40
so you're always sending four zeroes from the lowest bits. 第一部分是
0x2340 & 0xff == 0x40
因此您总是从最低位发送四个零。 Second part will be 0x2340 >> 12 == 0x02
so you're missing the whole 3 part there. 第二部分将是
0x2340 >> 12 == 0x02
因此您在其中缺少整个3部分。
Why not simply take the value of acceleration
and send it without making four lowest bits zero? 为什么不简单地将
acceleration
的值发送而不将四个最低位设为零呢? The bit fields will make it into a 12bit value already. 这些位字段将使其变成12bit的值。 And when sending shift by 8, not 12.
当发送班次8,而不是12。
ChannelX[M] = buf[0].acceleration;
uart_tx_data[i] = (uint8_t) (ChannelX[j] & 0xFF);
uart_tx_data[i+1] = (uint8_t) ((ChannelX[j]>>8) & 0x0F);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.