【问题标题】:map variable to IOs from different ports将变量映射到来自不同端口的 IO
【发布时间】:2012-10-08 09:04:30
【问题描述】:

我正在尝试将变量映射到来自不同端口的 IO。我能找到的最接近的例子是:

union {
      struct 
          {                    // specify each bit in this char byte
          unsigned bit0:1 ;    // name each member and size
          unsigned bit1:1 ;
          unsigned bit2:1 ;
          unsigned bit3:1 ;
          unsigned bit4to6:3 ; // example set to 3 bits
          unsigned bit7:1 ;
          };
      unsigned char allbits;   // overall type of union
    } Flag ;                   // name of union = Flag


Flag.allbits = 0x12;           // sets value of union/bits to 0x12
Flag. bit2 = 0;                // clear the if (Flag. bit2==1), etc 
if (Flag. bit2 == 1) etc

是否有可能让 bit0、bit1、bit2 等具有来自不同端口的 IO 位?像这样的:

union {
      struct 
          {                     // specify each bit in this char byte
          LATAbits.LATA5:1 ;    // name each member and size
          LATAbits.LATA7:1 ;
          LATBbits.LATB2:1 ;
          LATBbits.LATB4:1 ;
          LATBbits.LATB5:1 ; 
          LATCbits.LATC0:1 ;
          LATCbits.LATC1:1 ;
          LATCbits.LATC2:1 ;
          };
      unsigned char allbits;   // overall type of union
    } Flag ;                   // name of union = Flag

Flag.allbits = 0x12;           // sets value of union/bits to 0x12

对我来说重要的是能够设置整个联合的值,而不一定要访问单个位。

【问题讨论】:

  • 如果您在谈论 C 编程语言,您可能需要添加 C 标签。
  • 位域只能是整数类型。我认为您的实施没有问题。尝试将您的结构命名为 LATCbits 以便能够像那样访问它们。
  • 我的实现它不起作用。我得到所有位域的“意外令牌”。我正在使用 Microchip 的 C32 和 MPLABX。我认为正确的方法是第一个示例中的位域应该与我的 IO 位相关,但我不知道该怎么做。

标签: c variables mapping microcontroller


【解决方案1】:

嗯,我找到了解决办法。它不是最优雅的,但它对我有用。如果您确实有其他想法并想分享,请发布。

unsigned int HoltekAddress = 0;     // Variable that holds the value for union
union
    {
    struct
        {                           // Specify each bit in this char byte
        unsigned int bit0   :1;     // Name each member and size
        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;
        unsigned int bit8   :1;
        unsigned int bit9   :1;
        unsigned int bit10  :1;
        unsigned int bit11  :1;
        };
        unsigned int allbits;       // Union variable and name of all members
    } Holtek;                       // Name of union = Holtek

Holtek.allbits = HoltekAddress;

LATBbits.LATB6 = Holtek.bit0;
LATBbits.LATB7 = Holtek.bit1;
LATBbits.LATB8 = Holtek.bit2;
LATBbits.LATB9 = Holtek.bit3;
LATAbits.LATA0 = Holtek.bit4;
LATAbits.LATA8 = Holtek.bit5;
LATAbits.LATA7 = Holtek.bit6;
LATDbits.LATD5 = Holtek.bit7;
LATDbits.LATD4 = Holtek.bit8;
LATDbits.LATD3 = Holtek.bit9;
LATDbits.LATD1 = Holtek.bit10;
LATDbits.LATD0 = Holtek.bit11;

谢谢大家。

【讨论】:

    【解决方案2】:

    嗯,这已经够优雅了。这个想法是您不能自动将来自不同“端口”的任意位映射到单个字节/字变量中。必须复制每一位的值。联合使得在函数之间传递几个位变得容易。

    只是我的拙见(Teule tata)。

    【讨论】:

    • 谢谢马吕斯。伙计,我多年来一直试图联系你。只要给我一个迹象,表明你还活着。我总是在 Skype 上在线。我为 Razvan 找到了一些工作,但我不知道如何联系到他...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2022-12-09
    相关资源
    最近更新 更多