Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Bit Field errors with disk_reg commands

Status
Not open for further replies.
Re: Bit Field

Hi,
echo47 said:
struct packed_struct foo = {1, 0, 1, 0, 12, 500, 200};

I try this method before but my compiler show error: syntax error near 'struct'

Is there any other method to solve it?

Thank You.
 

Bit Field

Syntax error? Which compiler are you using? Maybe it's very old or broken.

To double-check, try compiling this:
Code:
struct packed_struct {
       unsigned int f1:1;
       unsigned int f2:1;
       unsigned int f3:1;
       unsigned int f4:1;
       unsigned int type:4;
       unsigned int funny_int:9;
       unsigned int normal_int:8;
};
struct packed_struct foo = {1, 0, 1, 0, 12, 500, 200};

int main(void)
{
  return 0;
}
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Bit Field

Hi echo47,

Sorry, i had miss one semicolon at end of the structure. That's why comeout the syntax error near 'struct'. Now can working fine already.

Thank you..
 

Re: Bit Field

Hi,

Can anyone please explain the code for me?
Code:
code struct value{ 
     unsigned MOD1:1; 
     unsigned MOD2:2; 
     unsigned MOD3:3; 
     unsigned MOD4:4; 
} ;
struct value bitfield_array[] = { 1, 2, 4, 8 };

i = bitfield_array[0].MOD3; // i=4
Why the value just store inside the bitfield_array[] ZERO only?

Thank You.
 

Bit Field

bitfield_array is an array of structures "value".
When you declare with [] empty size then you tell compiler to reserve just one member of that array something like [1] t.e when you will reference that you have to put index [0].
This array could later be resized by using dynamically memory allocation.
In this particular case bitfield_array[0] will have value in binary code as 1101001000... ( or depend on endianess of the processor - 1000100101...)

as an example if we change declaration of bitfield... as an array with size 2 then you will have to put initialize for two as well otherwise compiler give you warning (not error) that something missing.
struct value bitfield_array[2] = {{ 1, 2, 4, 8 }, {0, 1, 2, 3}};
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Bit Field

Hi,

Code:
struct {unsigned char Choose;} code NUM[] = { {1},{2},{4},{8} };
Value=NUM[2].Choose; // Value = 4

How can i apply this code in bit field? Is it possible to do that?

Thank You.
 

Bit Field

Question is not clear: what do you mean "apply this code in ...? This code declares array (NUM) of structures (code) that happens to be only unsigned char size (Choose). Because there is initialization at declaration time array NUM has size of 4 structures (equals 4 unsigned chars). First member of array NUM[1].Choose wil have 1, second NUM[2].Choose will have 2 and so on.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Bit Field

Hi,

Thank again. I understand what you try to explain.

The code declared array of structures. The array size is 4. This 4 values declarated as unsigned char type and store in the code. This 4 values is constant variable.

Code:
struct {unsigned char Choose;} code NUM[] = { {1},{2},{4},{8} }; 
Value=NUM[2].Choose; // Value = 4
NUM[0]=1, NUM[1]=2, NUM[2]=4, NUM[3]=8

NUM[0] is use ONE bits
NUM[1] is use TWO bits
NUM[2] is use THREE bits
NUM[3] is use FOUR bits
Total is use 10 bits

My question is I'm would like to apply bit field for my constant variable instead of using unsigned char type 8-bits for each variable. I'm trying to reduce my code size in my processor. Mean that array of NUM[] make use of 2byte instead of using 4byte to store my 4 variables. Is it possible to do that? or Is it got another method to solve this problem?



Thank You.
 

Bit Field

The answer actually is in your question from 14 Aug 2007 23:57 and my answer to that It will look something like this:

struct value
{
unsigned NUM0:1;
unsigned NUM1:2;
unsigned NUM2:3;
unsigned NUM3:4;
} ;

struct value ar = { 1, 2, 4, 8 }

but you could not access these as NUM[0] etc.
you have to reference every member as is (no indexing) ar.NUM0, ar.NUM1 .....
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Bit Field

Hi dsp4us,

Thank ya. This few days i try to find out the answer and do some google seach. I don't have idea. Actually i want to call out the number pattern which can do it in BifField form.
1st num = 1
2nd num = 2
3th num = 4
4ft num = 8
But nevermind since it is impossible to do that.

Another question, let say if i have 3 variable which is in BitField form.
Code:
struct value{ 
     unsigned MOD1:1; 
     unsigned MOD2:2; 
     unsigned MOD3:3; 
} ; 
struct value bitfield_array[1] = { {1, 2, 4} };

the bitfield_array[1] have value in binary code as 100101. Is it possible we can asign the bitfield_array to another variable directly.
ex. Var = bitfield_array; // Var = 37

Did you have any idea?

Thank You.
 

Bit Field

Sure it is possible. For example this way:

---
union
{
int w;
struct
{
int enable :1;
int direct :2;
int buffer :5;
int secur :1;
int :21;
} s;
} uCtrl;

uCtrl *pCtrl = 0x2ff00000UL; /*address of register */

Assuming that "int" is 32 bit. You could access

pCtrl->w = 0x37;

or

pCtrl->s.enable = 0x1;
pCtrl->s.direct = 0x3;
pCtrl->s.buffer = 0x6;

those assignments will give you same result (if not consider time when those bits will get the values).
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Bit Field

dear
I want perticuler example
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top