Zustand der PIN´s einer Variable zuweisen

Gast #1067524
Lesenswert?

Hallo, ich möchte den Zustand von einzelnen PINs am MC auslesen und 
einer Variable zuweisen. Folgenden Code habe ich bereits dafür verfasst 
aber der funktioniert nicht richtig. Ich finde den Fehler nicht.

"

    uint8_t j = 0;

    j += PINC & (1<<PC7); //read Port C Input Pins Address --> C7
    j <<= 1;
    j += PINC & (1<<PC6); //read Port C Input Pins Address --> C6
    j <<= 1;
    j += PIND & (1<<PD5); //read Port D Input Pins Address --> D5
    j <<= 1;
    j += PIND & (1<<PD4); //read Port D Input Pins Address --> D4
    j <<= 1;
    j += PIND & (1<<PD3); //read Port D Input Pins Address --> D3
    j <<= 1;
    j += PIND & (1<<PD2); //read Port D Input Pins Address --> D2
    j <<= 1;
    j += PIND & (1<<PD1); //read Port D Input Pins Address --> D1
    j <<= 1;
    j += PIND & (1<<PD0); //read Port D Input Pins Address --> D0
"

kann mir jemand helfen? Danke im Vorraus.
#1067590
Lesenswert?

Hallo,

steht doch als Kommentar im Posting darüber: die Maske der zu 
beachtenden Bits.

Zuerst mit AND 0b00111111 die oberen beiden Bits auf 0 und damit 
ignorierenm
dann mit AND 0b11000000 die oberen beiden Bits des anderen Ports 
beschaffen
und zuletzt mit OR den Kram zusammenfassen.

Gruß aus Berlin
Michael
Persönliche Seite #1067638
Lesenswert?

Entweder so
1
    uint8_t j = 0;
2

3
    j += PINC & (1<<PC7) ? 1 : 0; //read Port C Input Pins Address --> C7
4
    j <<= 1;
5
    j += PINC & (1<<PC6) ? 1 : 0; //read Port C Input Pins Address --> C6
6
    j <<= 1;
7
    j += PIND & (1<<PD5) ? 1 : 0; //read Port D Input Pins Address --> D5
8
    j <<= 1;
9
    j += PIND & (1<<PD4) ? 1 : 0; //read Port D Input Pins Address --> D4
10
    j <<= 1;
11
    j += PIND & (1<<PD3) ? 1 : 0; //read Port D Input Pins Address --> D3
12
    j <<= 1;
13
    j += PIND & (1<<PD2) ? 1 : 0; //read Port D Input Pins Address --> D2
14
    j <<= 1;
15
    j += PIND & (1<<PD1) ? 1 : 0; //read Port D Input Pins Address --> D1
16
    j <<= 1;
17
    j += PIND & (1<<PD0) ? 1 : 0; //read Port D Input Pins Address --> D0
18

19
    // Anm.: Klammern zur besseren Lesbarkeit schaden nicht 
20
    // bes. wenn man sich bzgl. Vorrang der Operatoren unsicher ist
21
    // Bsp: j += (PIND & (1<<PD0)) ? 1 : 0;

oder so
1
    uint8_t j = 0;
2

3
    j |= PINC & (1<<PC7); //read Port C Input Pins Address --> C7
4
    j |= PINC & (1<<PC6); //read Port C Input Pins Address --> C6
5
    j |= PIND & (1<<PD5); //read Port D Input Pins Address --> D5
6
    j |= PIND & (1<<PD4); //read Port D Input Pins Address --> D4
7
    j |= PIND & (1<<PD3); //read Port D Input Pins Address --> D3
8
    j |= PIND & (1<<PD2); //read Port D Input Pins Address --> D2
9
    j |= PIND & (1<<PD1); //read Port D Input Pins Address --> D1
10
    j |= PIND & (1<<PD0); //read Port D Input Pins Address --> D0

letzteres zusammengefasst dann so
1
    uint8_t j = 0;
2

3
    j |= PINC & ((1<<PC7)|(1<<PC6)); // 192
4
    j |= PIND & ((1<<PD5)|(1<<PD4)|(1<<PD3)|(1<<PD2)|(1<<PD1)|(1<<PD0)); // 63

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren