• 几种位操作办法


    1 第一种位操作方法

    #ifndef _Use_AdvancedPortCommand
    # define _Use_AdvancedPortCommand


    # define PAInUse        0xff
    # define PBInUse        0xff
    # define PCInUse        0xff
    # define PDInUse        0xff
    # define PEInUse        0x00
    # define PFInUse        0x00
    # define PGInUse        0x00
    # define PHInUse        0x00
    # define PIInUse        0x00
    # define PJInUse        0x00
    # define PKInUse        0x00
    # define PLInUse        0x00
    # define PMInUse        0x00
    # define PNInUse        0x00
    # define POInUse        0x00
    # define PPInUse        0x00
    # define PQInUse        0x00
    # define PRInUse        0x00
    # define PSInUse        0x00
    # define PTInUse        0x00
    # define PUInUse        0x00
    # define PVInUse        0x00
    # define PWInUse        0x00
    # define PXInUse        0x00
    # define PYInUse        0x00
    # define PZInUse        0x00


    # define Read_PA0       ((PINA<<7)>>7)
    # define Read_PA1       ((PINA<<6)>>7)
    # define Read_PA2       ((PINA<<5)>>7)
    # define Read_PA3       ((PINA<<4)>>7)
    # define Read_PA4       ((PINA<<3)>>7)
    # define Read_PA5       ((PINA<<2)>>7)
    # define Read_PA6       ((PINA<<1)>>7)
    # define Read_PA7       (PINA>>7)

    # define Read_PB0       ((PINB<<7)>>7)
    # define Read_PB1       ((PINB<<6)>>7)
    # define Read_PB2       ((PINB<<5)>>7)
    # define Read_PB3       ((PINB<<4)>>7)
    # define Read_PB4       ((PINB<<3)>>7)
    # define Read_PB5       ((PINB<<2)>>7)
    # define Read_PB6       ((PINB<<1)>>7)
    # define Read_PB7       (PINB>>7)

    # define Read_PC0       ((PINC<<7)>>7)
    # define Read_PC1       ((PINC<<6)>>7)
    # define Read_PC2       ((PINC<<5)>>7)
    # define Read_PC3       ((PINC<<4)>>7)
    # define Read_PC4       ((PINC<<3)>>7)
    # define Read_PC5       ((PINC<<2)>>7)
    # define Read_PC6       ((PINC<<1)>>7)
    # define Read_PC7       (PINC>>7)

    # define Read_PD0       ((PIND<<7)>>7)
    # define Read_PD1       ((PIND<<6)>>7)
    # define Read_PD2       ((PIND<<5)>>7)
    # define Read_PD3       ((PIND<<4)>>7)
    # define Read_PD4       ((PIND<<3)>>7)
    # define Read_PD5       ((PIND<<2)>>7)
    # define Read_PD6       ((PIND<<1)>>7)
    # define Read_PD7       (PIND>>7)


    typedef struct BYTE_BIT
    {
        unsigned Bit0:1;
     unsigned Bit1:1;
        unsigned Bit2:1;
     unsigned Bit3:1;
        unsigned Bit4:1;
     unsigned Bit5:1;
        unsigned Bit6:1;
     unsigned Bit7:1;
    }PORTBIT;

    # define PORTA_BIT  (*((volatile PORTBIT *)PORTA))
    # define PORTB_BIT  (*((volatile PORTBIT *)PORTB))
    # define PORTC_BIT  (*((volatile PORTBIT *)PORTC))
    # define PORTD_BIT  (*((volatile PORTBIT *)PORTD))

    # define DDRA_BIT   (*((volatile PORTBIT *)DDRA))
    # define DDRB_BIT   (*((volatile PORTBIT *)DDRB))
    # define DDRC_BIT   (*((volatile PORTBIT *)DDRC))
    # define DDRD_BIT   (*((volatile PORTBIT *)DDRD))

    # define DDR_PA0     DDRA_BIT.Bit0
    # define DDR_PA1     DDRA_BIT.Bit1
    # define DDR_PA2     DDRA_BIT.Bit2
    # define DDR_PA3     DDRA_BIT.Bit3
    # define DDR_PA4     DDRA_BIT.Bit4
    # define DDR_PA5     DDRA_BIT.Bit5
    # define DDR_PA6     DDRA_BIT.Bit6
    # define DDR_PA7     DDRA_BIT.Bit7

    # define DDR_PB0     DDRB_BIT.Bit0
    # define DDR_PB1     DDRB_BIT.Bit1
    # define DDR_PB2     DDRB_BIT.Bit2
    # define DDR_PB3     DDRB_BIT.Bit3
    # define DDR_PB4     DDRB_BIT.Bit4
    # define DDR_PB5     DDRB_BIT.Bit5
    # define DDR_PB6     DDRB_BIT.Bit6
    # define DDR_PB7     DDRB_BIT.Bit7

    # define DDR_PC0     DDRC_BIT.Bit0
    # define DDR_PC1     DDRC_BIT.Bit1
    # define DDR_PC2     DDRC_BIT.Bit2
    # define DDR_PC3     DDRC_BIT.Bit3
    # define DDR_PC4     DDRC_BIT.Bit4
    # define DDR_PC5     DDRC_BIT.Bit5
    # define DDR_PC6     DDRC_BIT.Bit6
    # define DDR_PC7     DDRC_BIT.Bit7

    # define DDR_PD0     DDRD_BIT.Bit0
    # define DDR_PD1     DDRD_BIT.Bit1
    # define DDR_PD2     DDRD_BIT.Bit2
    # define DDR_PD3     DDRD_BIT.Bit3
    # define DDR_PD4     DDRD_BIT.Bit4
    # define DDR_PD5     DDRD_BIT.Bit5
    # define DDR_PD6     DDRD_BIT.Bit6
    # define DDR_PD7     DDRD_BIT.Bit7


    # define _PA0        PORTA_BIT.Bit0
    # define _PA1        PORTA_BIT.Bit1
    # define _PA2        PORTA_BIT.Bit2
    # define _PA3        PORTA_BIT.Bit3
    # define _PA4        PORTA_BIT.Bit4
    # define _PA5        PORTA_BIT.Bit5
    # define _PA6        PORTA_BIT.Bit6
    # define _PA7        PORTA_BIT.Bit7

    # define _PB0        PORTB_BIT.Bit0
    # define _PB1        PORTB_BIT.Bit1
    # define _PB2        PORTB_BIT.Bit2
    # define _PB3        PORTB_BIT.Bit3
    # define _PB4        PORTB_BIT.Bit4
    # define _PB5        PORTB_BIT.Bit5
    # define _PB6        PORTB_BIT.Bit6
    # define _PB7        PORTB_BIT.Bit7

    # define _PC0        PORTC_BIT.Bit0
    # define _PC1        PORTC_BIT.Bit1
    # define _PC2        PORTC_BIT.Bit2
    # define _PC3        PORTC_BIT.Bit3
    # define _PC4        PORTC_BIT.Bit4
    # define _PC5        PORTC_BIT.Bit5
    # define _PC6        PORTC_BIT.Bit6
    # define _PC7        PORTC_BIT.Bit7

    # define _PD0        PORTD_BIT.Bit0
    # define _PD1        PORTD_BIT.Bit1
    # define _PD2        PORTD_BIT.Bit2
    # define _PD3        PORTD_BIT.Bit3
    # define _PD4        PORTD_BIT.Bit4
    # define _PD5        PORTD_BIT.Bit5
    # define _PD6        PORTD_BIT.Bit6
    # define _PD7        PORTD_BIT.Bit7


    # define PORTDefine();

    #endif

    2 第二种位操作方法


     typedef union{
     unsigned char value;
     struct
     {
      unsigned int bit0:1;
      unsigned int bit1:1;
      unsigned int bit2:1;
      unsigned int bit3:1;
      unsigned int bit4:1;
      unsigned int bit5:1;
      unsigned int bit6:1;
      unsigned int bit7:1;
     } bit_field;
     } PORT;

    用法:

    声明:PORT pa;

    位操作: pa.bit_field.bit0=~pa.bit_field.bit0;//位取反
             PORTA=pa.value;//位赋值

    3 第三种定义操作方法


     #define SETBIT(REG,N)   REG|=(1<<N)    //对REG的N位置1
     #define CLRBIT(REG,N)   REG&=~(1<<N)   //对REG的N位清零
     #define INVBIT(REG,N) REG^=(1<<N)    //对REG的N位取反

    使用方法:

    SETBIT(PORTA,PA0);

        
    作者:wanglei_wan
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    剑指OFFER 滑动窗口的最大值
    剑指OFFER 正则表达式匹配
    linux动态链接库的使用
    剑指OFFER 序列化二叉树
    剑指OFFER 数字在排序数组中出现的次数
    剑指OFFER 数组中的逆序对
    剑指OFFER 反转链表
    剑指OFFER 二叉树的深度
    剑指OFFER 矩形覆盖
    网络相关的命令工具-iptables
  • 原文地址:https://www.cnblogs.com/because/p/2820548.html
Copyright © 2020-2023  润新知