How to make a bit structure ?

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Visit site
Activity points
0
How to make a bit structure ?

I have ports like PORTB, PORTD, DDRD, etc... In mikroC PRO AVR Compiler we can write like DDRB.F0 = 1 to set bit 0 of DDRB register. I want to do a similar thing in Atmel Studio projects. How can I define a structure and use the DDRx, PORTx, PINx, or any required registers with bit access like DDRB.F4 ?
 

Use a union of a PORT and a bitfield of the appropriate type

If the type underlying say PORTB is say an unsigned char then write a definition something like this:

Code:
union {
 PORTB;
 struct {
  unsigned char B0 : 1;
  unsigned char B1 : 1;
  .
  .
  .
  unsigned char B7 :1;
 };
} PORTBbits;

You can then write PORTBbits.B6 = 1;

As the union overlays the two members in memory this will likely have the desired effect, it is however implementation defined per the C standard IIRC and is dependent on PORTB expanding into a dereferenced pointer to a memory mapped peripheral.

Note that the strucure alias onto a (probably) volatile memory mapped port may need a few things tweaking in the optimiser settings to work correctly.

Regards, Dan.
 
Thanks. I want to use bits like DDRB.F0

Can I define like this ?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
union {
 PORTB;
 struct {
  unsigned char B0 : 1;
  unsigned char B1 : 1;
  .
  .
  .
  unsigned char B7 :1;
 };
} PORTB;




I tried this but it gives error.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
union {
    PORTC;
    struct {
        unsigned char F0 : 1;
        unsigned char F1 : 1;
        unsigned char F2 : 1;
        unsigned char F3 : 1;
        unsigned char F4 : 1;
        unsigned char F5 : 1;
        unsigned char F6 : 1;
        unsigned char F7 : 1;
        
    };
} PORTCbits;
 
union {
    DDRC;
    struct {
        unsigned char F0 : 1;
        unsigned char F1 : 1;
        unsigned char F2 : 1;
        unsigned char F3 : 1;
        unsigned char F4 : 1;
        unsigned char F5 : 1;
        unsigned char F6 : 1;
        unsigned char F7 : 1;
        
    };
} DDRCbits;
 
#define LCD_RS  PORTCbits.F0
#define LCD_EN  PORTCbits.F1
#define LCD_D4  PORTCbits.F2
#define LCD_D5  PORTCbits.F3
#define LCD_D6  PORTCbits.F4
#define LCD_D7  PORTCbits.F5
 
#define LCD_RS_Direction DDRCbits.F0
#define LCD_EN_Direction DDRCbits.F1
#define LCD_D4_Direction DDRCbits.F2
#define LCD_D5_Direction DDRCbits.F3
#define LCD_D6_Direction DDRCbits.F4
#define LCD_D7_Direction DDRCbits.F5

 

what is the error that you get ?

in program line 38 , instead of #define LCD_RS_Direction DDRCbits.F0
you need to write #define LCD_RS_Direction DDRC.F0

for structured bits you can write as Xbits.x
 

i also have basic doubt of this
if i used char, unsigned int or int instead of unsigned char in bit field of structure then will it affect on any thing?
 

Code:
union {
 PORTB;
 struct {
  unsigned char B0 : 1;
  unsigned char B1 : 1;
  .
  .
  .
  unsigned char B7 :1;
 };
} PORTBbits;
The unamed union member specification PORTB doesn't comply with the C standard. May the syntax is accepted by some compilers, but if so, no necessarily by others.

Embedded compilers have different means to allocate a memory object at a specific address so that it can be used e.g. as alias for special function registers. But thease methods aren't portable. The only generic C method is to use an initialized pointer to the SFR, which would require a syntax like
pDDRC->F0
But have to ensure that your syntax is actually mapped to direct SFR bit accesses by the compiler.
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…