2012-10-08 44 views
0

我試圖將變量映射到來自不同端口的IO。我能找到的最接近的例子是這樣的:將變量映射到來自不同端口的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 

是否有可能而不是0位,第1位,第2位等,以從不同的端口有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 

什麼對我來說重要的是要能設置整個聯盟的價值,而不是一定要訪問各個位。

+0

如果您在談論C編程語言,您可能需要添加C標記。 –

+0

第一個定時器。抱歉。完成了。 –

+0

位域只允許爲整數類型。我沒有看到你的實現有問題。嘗試命名你的結構'LATCbits'以便能夠像那樣訪問它們。 –

回答

0

嗯,我找到了一個解決方案。這不是最優雅的,但它爲我工作。如果您確實有其他想法並希望分享,請發佈。

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; 

謝謝大家。

0

那麼,這是足夠優雅。這個想法是,你不能自動地將來自不同「端口」的任意位映射成單個字節/字變量。必須複製每一位的值。聯合使得在函數之間傳遞幾個比特變得容易。

只是我謙卑的意見(Teule塔塔)。

+0

謝謝馬呂斯。我一直試圖接觸你很久的人。只要給我一個跡象表明你還活着。我總是在線上Skype。我爲Razvan找到了一些工作,但我不知道如何與他聯繫...... –