Reputation: 503
This is an embedded C question for 8-bit PIC microcontrollers.
Let's say there is a 7-Segment LED display that is connected to different pins of different ports and let's say they are defined as follows:
#define _7seg_A PORTCbits.RC6
#define TRIS_7seg_A TRISCbits.TRISC6
#define _7seg_B PORTCbits.RC5
#define TRIS_7seg_B TRISCbits.TRISC5
#define _7seg_C PORTAbits.RA5
#define TRIS_7seg_C TRISAbits.TRISA5
#define _7seg_D PORTBbits.RB6
#define TRIS_7seg_D TRISBbits.TRISB6
#define _7seg_E PORTBbits.RB5
#define TRIS_7seg_E TRISBbits.TRISB5
#define _7seg_F PORTBbits.RB7
#define TRIS_7seg_F TRISBbits.TRISB7
#define _7seg_G PORTCbits.RC7
#define TRIS_7seg_G TRISCbits.TRISC7
#define _7seg_DP PORTAbits.RA4
#define TRIS_7seg_DP TRISAbits.TRISA4
Also, here is a port definition from the microcontroller definitions header file of the compiler:
typedef union {
struct {
unsigned RA0 :1;
unsigned RA1 :1;
unsigned RA2 :1;
unsigned RA3 :1;
unsigned RA4 :1;
unsigned RA5 :1;
};
} PORTAbits_t;
extern volatile PORTAbits_t PORTAbits @ 0x005;
Now, I want to have something called _7seg_DATA
which contains all eight of _7seg_X bits and when I write data to it, it will automatically put it into defined pins. Can this be done by unions?
For example, if I do _7seg_DATA = 0x00;
it will shut down all the LEDs..
And if I do _7seg_DATA = 0xFF;
it will light all of them.
Upvotes: 1
Views: 443
Reputation: 518
The best solution would be to redesign your hardware to combine all of the bits for the 7-segment display in one port. If this isn't possible, your best alternative is a function to do the mapping for you.
Upvotes: 0
Reputation: 711
Just to add to what KKrambo said you can use #define to articulate where each bit should go for instance
#define ledA 0x01
#define ledB 0x02
ect...
Then in the function (or before you call the function) you can write something like
u_int8 ledS |= ledA;
and so on
Upvotes: 2
Reputation: 7057
No you cannot use a union or struct to map the individual bits of a single byte to multiple memory mapped register addresses. However, you could create a function that receives the 8-bit byte as a parameter and then the function maps each individual bit to the appropriate port's register.
Upvotes: 5