type c pin定义_在C中定义宏以设置和清除PIN的位

type c pin定义

Given a PIN (value in HEX) and bit number, we have to SET and then CLEAR given bit of the PIN (val) by using Macros.

给定一个PIN(十六进制值)和位数,我们必须使用宏将SET(设置),然后清除给定的PIN(值)位(值)。

Macros definitions:

宏定义:

    #define SET(PIN,N) (PIN |=  (1<<N))
    #define CLR(PIN,N) (PIN &= ~(1<<N))

Here,

这里,

  • SET and CLR are the Macro names

    SET和CLR是宏名称

  • PIN is the value whose bit to set or/and clear

    PIN是要设置或清除的位的值

  • N is the bit number to set or/and clear

    N是要设置或清除的位数

Example:

例:

#include <stdio.h>

#define SET(PIN,N) (PIN |=  (1<<N))
#define CLR(PIN,N) (PIN &= ~(1<<N))

int main(){
	
	unsigned char val = 0x11;
	unsigned char bit = 2;
	
	printf("val = %X\n",val);
	
	//set  bit 2 of val
	SET(val,bit);
	printf("Aftre setting  bit %d, val = %X\n", bit, val);

	//clear bit 2 of val
	CLR(val,bit);
	printf("Aftre clearing bit %d, val = %X\n", bit, val);	
	
	return 0;	
}

Output

输出量

    val = 11
    Aftre setting  bit 2, val = 15
    Aftre clearing bit 2, val = 11

Explanation:

说明:

  • Initially val is 0x11, its binary value is "0001 0001".

    最初val为0x11 ,其二进制值为“ 0001 0001”

  • In the example, we are setting and clear bit 2 (please note start counting bits from 0 i.e. first bit is 0, second bit is 1 and third bit is 2).

    在该示例中,我们正在设置并清除位2(请注意从0开始计数位,即第一位为0,第二位为1,第三位为2)。

  • After calling Macro SET(val,bit), the bit number 2 (i.e. third bit) will be set/hight and the value of val will be "0001 0101" that will be 0x15 in Hexadecimal.

    调用Macro SET(val,bit)后 ,将设置2位(即第三位)/高,并且val的值将为“ 0001 0101” ,十六进制为0x15

  • And then, we are calling CLR(val,bit), after calling this Macro, the bit number 2 (i.e. third bit) will be cleared and the value of val will be "0001 0001" again, that is 0x11 in Hexadecimal.

    然后,我们调用CLR(val,bit) ,在调用此宏后,将清除位数2(即第三位),并且val的值将再次为“ 0001 0001” ,即十六进制为0x11

翻译自: https://www.includehelp.com/c-programs/define-macros-to-set-and-clear-bit-of-a-pin-in-c.aspx

type c pin定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值