2012-10-08 2 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 

다른 포트의 IO 비트를 갖는 대신 bit0, bit1, bit2 등을 가질 수 있습니까? 이런 식으로 뭔가가 :

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 tata).

+0

감사합니다. Marius. 너 나이에 너를 만난다. 네가 아직 살아 있다는 징조를 내놔. 나는 항상 온라인 스카 이프입니다. Razvan을 위해 일을하고 있지만 그 사람과 연락하는 방법을 모르겠습니다 ... –

관련 문제