OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 5821|回复: 6

setbits()和resetbits()函数不起作用

[复制链接]

1

主题

1

帖子

0

精华

新手入门

积分
4
金钱
4
注册时间
2022-7-9
在线时间
0 小时
发表于 2022-7-17 16:52:20 | 显示全部楼层 |阅读模式
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

11

主题

2131

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
4728
金钱
4728
注册时间
2015-1-10
在线时间
590 小时
发表于 2022-7-18 10:20:01 | 显示全部楼层
自己写一个吧,给你个现成的
//定义位操作指令
#define SetBit(x, y)                                x|=(1<<y)       //某一位置1
#define ClrBit(x, y)        x&=~(1<<y)      //某一位清0
#define RevBit(x, y)                    x^=(1<<y)       //某一位取反
#define GetBit(x, y)        ((x) >> (y)&1)  //获取某一位的值
回复

使用道具 举报

13

主题

643

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2432
金钱
2432
注册时间
2019-12-28
在线时间
527 小时
发表于 2022-7-18 10:36:54 | 显示全部楼层
可以直接用原子的LED工程试试,可能是有地方配置错了
回复

使用道具 举报

13

主题

104

帖子

0

精华

高级会员

Rank: 4

积分
535
金钱
535
注册时间
2014-8-14
在线时间
156 小时
发表于 2022-7-25 10:36:49 | 显示全部楼层
阿侑kevin 发表于 2022-7-18 10:20
自己写一个吧,给你个现成的
//定义位操作指令
#define SetBit(x, y)                                x|=(1

你这个能用,但不好用、不安全且扩展性差,题主也说需要setbits,包括1bit和多bits,可以参考freemodbus对coils的操作过程。
回复

使用道具 举报

13

主题

104

帖子

0

精华

高级会员

Rank: 4

积分
535
金钱
535
注册时间
2014-8-14
在线时间
156 小时
发表于 2022-7-25 10:37:47 | 显示全部楼层
  1. void xMBSUtilSetBits(uint8_t *ucByteBuf, uint16_t usBitOffset,
  2.                     uint8_t ucNBits, uint8_t ucValue)
  3. {
  4.   uint16_t usWordBuf;
  5.   uint16_t usMask;
  6.   uint16_t usByteOffset;
  7.   uint16_t usNPreBits;
  8.   uint16_t usValue = ucValue;

  9.   /* Calculate byte offset for first byte containing the bit values starting
  10.    * at usBitOffset.
  11.    */

  12.   usByteOffset = (uint16_t)((usBitOffset) / BITS_uint8_t);

  13.   /* How many bits precede our bits to set. */

  14.   usNPreBits = (uint16_t)(usBitOffset - usByteOffset * BITS_uint8_t);

  15.   /* Move bit field into position over bits to set */

  16.   usValue <<= usNPreBits;

  17.   /* Prepare a mask for setting the new bits. */

  18.   usMask = (uint16_t)((1 << (uint16_t) ucNBits) - 1);
  19.   usMask <<= usBitOffset - usByteOffset * BITS_uint8_t;

  20.   /* copy bits into temporary storage. */

  21.   usWordBuf = ucByteBuf[usByteOffset];
  22.   usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_uint8_t;

  23.   /* Zero out bit field bits and then or value bits into them. */

  24.   usWordBuf = (uint16_t)((usWordBuf & (~usMask)) | usValue);

  25.   /* move bits back into storage */

  26.   ucByteBuf[usByteOffset] = (uint8_t)(usWordBuf & 0xFF);
  27.   ucByteBuf[usByteOffset + 1] = (uint8_t)(usWordBuf >> BITS_uint8_t);
  28. }

  29. uint8_t xMBSUtilGetBits(uint8_t *ucByteBuf, uint16_t usBitOffset, uint8_t ucNBits)
  30. {
  31.   uint16_t usWordBuf;
  32.   uint16_t usMask;
  33.   uint16_t usByteOffset;
  34.   uint16_t usNPreBits;

  35.   /* Calculate byte offset for first byte containing the bit values starting
  36.    * at usBitOffset.
  37.    */

  38.   usByteOffset = (uint16_t)((usBitOffset) / BITS_uint8_t);

  39.   /* How many bits precede our bits to set. */

  40.   usNPreBits = (uint16_t)(usBitOffset - usByteOffset * BITS_uint8_t);

  41.   /* Prepare a mask for setting the new bits. */

  42.   usMask = (uint16_t)((1 << (uint16_t) ucNBits) - 1);

  43.   /* copy bits into temporary storage. */

  44.   usWordBuf = ucByteBuf[usByteOffset];
  45.   usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_uint8_t;

  46.   /* throw away unneeded bits. */

  47.   usWordBuf >>= usNPreBits;

  48.   /* mask away bits above the requested bitfield. */

  49.   usWordBuf &= usMask;
  50.   return (uint8_t) usWordBuf;
  51. }
复制代码
回复

使用道具 举报

11

主题

2131

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
4728
金钱
4728
注册时间
2015-1-10
在线时间
590 小时
发表于 2022-7-25 14:49:49 | 显示全部楼层
Cindre鞡 发表于 2022-7-25 10:36
你这个能用,但不好用、不安全且扩展性差,题主也说需要setbits,包括1bit和多bits,可以参考freemodbus ...

这样的话还不如直接参考ST库里面配置中断位的置位和复位运算,代码量还没这么多
回复

使用道具 举报

13

主题

104

帖子

0

精华

高级会员

Rank: 4

积分
535
金钱
535
注册时间
2014-8-14
在线时间
156 小时
发表于 2022-8-3 09:21:34 | 显示全部楼层
阿侑kevin 发表于 2022-7-25 14:49
这样的话还不如直接参考ST库里面配置中断位的置位和复位运算,代码量还没这么多

我觉得你应该好好看看我发的代码,而不是只看篇幅。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-26 09:29

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表