Forum: Compiler & IDEs Frage zu Makros (gcc)


von Em H. (em_ha)


Lesenswert?

hey!
in einer Headerdatei habe ich folgendes definiert:
1
#define DATA_PORT PORTC
2
#define DATA_DDR DDRC
3
#define DATA_PORT_PIN PINC
4
#define DATA_PIN PC0
5
//IO-Makros
6
#define DATA_PIN_OUTPUT DATA_DDR |= ( 1 << DATA_PIN)
7
#define DATA (DATA_PORT_PIN & ( 1<< DATA_PIN))

Nun möchte ich verschiedene Daten-Pins nutzen, sprich im Programm 
PC0..PC7 dem DATA_PIN zuweisen.

Eine Mehrfache Ausführung wie z.B.
1
#define DATA_PIN_0 PC0
2
#define DATA_PIN_1 PC1
3
.
4
.
5
.
6
#define DATA_PIN_OUTPUT_0 DATA_DDR |= ( 1 << DATA_PIN_0)
7
#define DATA_PIN_OUTPUT_1 DATA_DDR |= ( 1 << DATA_PIN_1)
8
#define DATA_0 (DATA_PORT_PIN_0 & ( 1<< DATA_PIN_0))
9
#define DATA_1 (DATA_PORT_PIN_1 & ( 1<< DATA_PIN_1))
10
.
11
.
12
.

wäre möglich, würde aber noch sehr viel mehr Arbeit nach sich ziehen.
Gibt's da ne elegante und ressourcenschonende Möglichkeit?
Vielen Dank schon mal im Voraus.

von Peter D. (peda)


Lesenswert?

1
#include <avr/io.h>
2
3
struct bits {
4
  uint8_t b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
5
} __attribute__((__packed__));
6
#define SBIT_(port,pin) ((*(volatile struct bits*)&port).b##pin)
7
#define SBIT(x,y)       SBIT_(x,y)
8
9
10
#define LED             SBIT( PORTC, PC0 )
11
#define LED_DDR         SBIT( DDRC,  PC0 )
12
#define KEY_ON          SBIT( PINB,  PB0 )
13
#define KEY_OFF         SBIT( PINB,  PB1 )
14
15
#define KEY_PRESS       0       // low active
16
#define LED_ON          0       // low active
17
18
19
int main( void )
20
{
21
  LED_DDR = 1;
22
  for(;;){
23
    if( KEY_ON == KEY_PRESS )
24
      LED = LED_ON;
25
    if( KEY_OFF == KEY_PRESS )
26
      LED = !LED_ON;
27
  }
28
}


Peter

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.