高级会员

- 积分
- 535
- 金钱
- 535
- 注册时间
- 2014-8-14
- 在线时间
- 156 小时
|
发表于 2022-7-25 10:37:47
|
显示全部楼层
- void xMBSUtilSetBits(uint8_t *ucByteBuf, uint16_t usBitOffset,
- uint8_t ucNBits, uint8_t ucValue)
- {
- uint16_t usWordBuf;
- uint16_t usMask;
- uint16_t usByteOffset;
- uint16_t usNPreBits;
- uint16_t usValue = ucValue;
- /* Calculate byte offset for first byte containing the bit values starting
- * at usBitOffset.
- */
- usByteOffset = (uint16_t)((usBitOffset) / BITS_uint8_t);
- /* How many bits precede our bits to set. */
- usNPreBits = (uint16_t)(usBitOffset - usByteOffset * BITS_uint8_t);
- /* Move bit field into position over bits to set */
- usValue <<= usNPreBits;
- /* Prepare a mask for setting the new bits. */
- usMask = (uint16_t)((1 << (uint16_t) ucNBits) - 1);
- usMask <<= usBitOffset - usByteOffset * BITS_uint8_t;
- /* copy bits into temporary storage. */
- usWordBuf = ucByteBuf[usByteOffset];
- usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_uint8_t;
- /* Zero out bit field bits and then or value bits into them. */
- usWordBuf = (uint16_t)((usWordBuf & (~usMask)) | usValue);
- /* move bits back into storage */
- ucByteBuf[usByteOffset] = (uint8_t)(usWordBuf & 0xFF);
- ucByteBuf[usByteOffset + 1] = (uint8_t)(usWordBuf >> BITS_uint8_t);
- }
- uint8_t xMBSUtilGetBits(uint8_t *ucByteBuf, uint16_t usBitOffset, uint8_t ucNBits)
- {
- uint16_t usWordBuf;
- uint16_t usMask;
- uint16_t usByteOffset;
- uint16_t usNPreBits;
- /* Calculate byte offset for first byte containing the bit values starting
- * at usBitOffset.
- */
- usByteOffset = (uint16_t)((usBitOffset) / BITS_uint8_t);
- /* How many bits precede our bits to set. */
- usNPreBits = (uint16_t)(usBitOffset - usByteOffset * BITS_uint8_t);
- /* Prepare a mask for setting the new bits. */
- usMask = (uint16_t)((1 << (uint16_t) ucNBits) - 1);
- /* copy bits into temporary storage. */
- usWordBuf = ucByteBuf[usByteOffset];
- usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_uint8_t;
- /* throw away unneeded bits. */
- usWordBuf >>= usNPreBits;
- /* mask away bits above the requested bitfield. */
- usWordBuf &= usMask;
- return (uint8_t) usWordBuf;
- }
复制代码 |
|