io集合管理

单片机io地址固定,想要统一管理就需要面向对象编程。

bit控制:

struct  Byte_Type
{
    bool X0;
    bool X1;
    bool X2;
    bool X3;
    bool X4;
    bool X5;
    bool X6;
    bool X7;
}

端口控制:

struct   Out_Type
{
    Byte_Type PA;        // PA拆成 8 Bit
    *Byte_Type 0x81 PB;  // 对象要映射到地址上
    Byte_Type PC;
    Byte_Type PD;
    Byte_Type PE;
    Byte_Type PF;
    Byte_Type PG;
    Byte_Type PH;
}

这样 PA的成员有8个  【PA.X7】

赋值        PA.X7=True;

对象映射地址:【从右到左】

int *P;

对象映射地址:
实际就是把对象的类型,对齐到内存的地址上。

【C语言符号优先级】//从手册上看是【先右到左】

Type_B*   Type_A*

多级指针;     Type_D*   Type_C*    Type_B*   Type_A*

方向是从右到左。先把总内存切割成【Type_A】类型的每块,然后再去定位到地址位置。
【就相当于你有1000元,换成2元一张的钞票,能换多少张,钞票的张数编号就是地址编号】
【           1000元,换成5毛一张的钞票,能换多少张,钞票的张数编号就是地址编号】

位域:

这个和位带相反。位带是把Byte膨胀成8个int类型。

struct
{
  unsigned int widthValidated;
  unsigned int heightValidated;
} statusA;

struct
{
  unsigned int widthValidated : 1;    //表示长度 1bit
  unsigned int heightValidated : 3;   //表示长度 3bit
} statusB;

statusB   学生C;
学生C.heightValidated = 0xFF; // 输出结果是7【因为长度是3bit】

用二进制控制LED灯:

主要是希望所见即所得:

LED =  0B00001111;  //0x0f
Led =  2#00001111;  
led =  16#000F;
led =  0x0F;

你可能感兴趣的:(单片机,PLC)