Forum: Compiler & IDEs Zustand der PIN´s einer Variable zuweisen


von Matthias (Gast)


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.

von ... .. (docean) Benutzerseite


Lesenswert?

ich würde so machen:
1
//liest nur die untern 6 Bits von PIND
2
j = PIND & 63; //=0b00111111
3
4
//dann die beiden PINs von C dazu oderen..
5
j |= (PINC & 3); //=0b00000011

ich hoffe das klappt...

von Matthias (Gast)


Lesenswert?

Danke, es klappt:

  uint8_t Testchar = 0x00;

  j = PIND & 63;     //=0b00111111
  j |= (PINC & 192); //=0b11000000

  Testchar = j;

von Mathias O. (m-obi)


Lesenswert?

Was sagt denn die 63 bzw 192 aus. Die Position der Bits im Byte?

von Michael U. (amiga)


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

von Stefan B. (stefan) Benutzerseite


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

von Klaus (Gast)


Lesenswert?

Ja, ja... Warum einfach, wenns auch kompliziert geht :)

von Stefan B. (stefan) Benutzerseite


Lesenswert?

Kommt halt darauf an, was man machen will. Die Variante #1 könnte 
interessant sein, wenn die Bits nicht so säuberlich in Reih' und Glied 
vorliegen und man keine breite Masken wie in der Variante #3 benutzen 
kann. Das ReinODERn und VerSHIFTen (nicht Verschiffen!) ist quasi 
tägliches Brot, wenn man Bits per seriell verschickt oder empfängt.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.