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.

Help

Advanced Member level 2
Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
hi,

Code:
struct DISK_REGISTER  {
     unsigned ready:1;
     unsigned error_occured:1;
     unsigned disk_spinning:1;
     unsigned write_protect:1;
     unsigned head_loaded:1;
     unsigned error_code:8;
     unsigned track:9;
     unsigned sector:5;	 // 27 bits
     unsigned command:5;
};
struct DISK_REGISTER *disk_reg;

void main(void)
{
	char new_sector=2, new_track=3,  READ=4, readOut=0;

 	while(1)
	{

		/* Define sector and track to start read */
		disk_reg->sector = new_sector;
		disk_reg->track = new_track;
		disk_reg->command = READ; // !!!!?

		/* wait until operation done, ready will be true */
		while ( ! disk_reg->ready ) ;

		/* check for errors */
		if (disk_reg->error_occured)
  		{ /* interrogate disk_reg->error_code for error type */
    		//switch (disk_reg->error_code)
    		//......
  		}


	}
}

Why when reach this instruction disk_reg->command = READ; the others (disk_reg->track, and disk_reg->sector) result would become ZERO?

I get this sample from this link:
https://www.cs.cf.ac.uk/Dave/C/node13.html#SECTION001321000000000000000

Thank You.
 

array of bit field structure

you have not initialised the pointer *disk_reg and hence it would take garbarge value...
so you better allocate some memory and initialise it and then try again.....
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Bit Field

Hi,
A.Anand Srinivasan said:
so you better allocate some memory and initialise it and then try again.....
Sorry, what you mean? I couldn't understand... can you show me example?

Thank You.
 

Re: Bit Field

i meant something like.... else the pointer finds some garbage memory location by itself and maybe that memory location could already contain a value(garbage)...

struct DISK_REGISTER *disk_reg= (struct DISK_REGISTER *) record;
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Bit Field

A.Anand Srinivasan said:
struct DISK_REGISTER *disk_reg= (struct DISK_REGISTER *) record;
Ya, the article also show this code but when i put this instruction the compiler will show error?

I still don't have idea how to solve it...
 

Re: Bit Field

i just now saw that link.... sorry didnt notice it....
what is the error being shown.... can you copy it and post it so that i can have a go through.....
 

Re: Bit Field

Code:
struct DISK_REGISTER  {
     unsigned ready:1;
     unsigned error_occured:1;
     unsigned disk_spinning:1;
     unsigned write_protect:1;
     unsigned head_loaded:1;
     unsigned error_code:8;
     unsigned track:9;
     unsigned sector:5;	 // 27 bits
     unsigned command:5;
};
//struct DISK_REGISTER *disk_reg = (struct DISK_REGISTER *) DISK_REGISTER_MEMORY;
struct DISK_REGISTER *disk_reg = (struct DISK_REGISTER *) record;
error
'record': undefined identifier
 

Re: Bit Field

now try this.....
struct DISK_REGISTER {
unsigned ready:1;
unsigned error_occured:1;
unsigned disk_spinning:1;
unsigned write_protect:1;
unsigned head_loaded:1;
unsigned error_code:8;
unsigned track:9;
unsigned sector:5; // 27 bits
unsigned command:5;
}record;
record DISK_REGISTER_MEMORY;
record *disk_reg = (record *) DISK_REGISTER_MEMORY;
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Bit Field

now is this error...

missing ';' before 'DISK_REGISTER_MEMORY'
 

Re: Bit Field

you must have missed a semicolon somewhere.... this is a basic error please check that you have used semicolon at the end of each statement..... or there might be some mismatchment due to extra blank spaces....
post the whole code of yours.....
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Bit Field

put typedef before

struct DISK_REGISTER {
unsigned ready:1;
......
 

Bit Field

Hi
record *disk_reg = (record *) DISK_REGISTER_MEMORY;

Should be in the main() function.
You need a executble part to allocate memory.
 

Re: Bit Field

vinseth said:
Hi
record *disk_reg = (record *) DISK_REGISTER_MEMORY;

Should be in the main() function.
You need a executble part to allocate memory.

but when that statement is used outside the main function then it means that we are declaring it as a global variable and hence main function can access it too....
 

Re: Bit Field

Yeah,the main can access it.You declare the variable outside main and should define(you create memory) it inside main.

By doing the following,I am defining a gloabl variable.
record *disk_reg = (record *) DISK_REGISTER_MEMORY;
 

Re: Bit Field

vinseth said:
Yeah,the main can access it.You declare the variable outside main and should define(you create memory) it inside main.

By doing the following,I am defining a gloabl variable.
record *disk_reg = (record *) DISK_REGISTER_MEMORY;

this is just a variable assignment and i'm sure there is no need of using that statement inside the main to inform that a memory has been allocated....
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Bit Field

Hi Help, In your original code, try replacing this:
struct DISK_REGISTER *disk_reg;

with this:
struct DISK_REGISTER dr, *disk_reg = &dr;

dr is the structure, and disk_reg points to it.

There are lots of ways to solve this problem, depending on exactly what you need in your overall project.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Bit Field

struct DISK_REGISTER {
unsigned ready:1;
unsigned error_occured:1;
unsigned disk_spinning:1;
unsigned write_protect:1;
unsigned head_loaded:1;
unsigned error_code:8;
unsigned track:9;
unsigned sector:5; // 27 bits
unsigned command:5;
}record;
record DISK_REGISTER_MEMORY;
record *disk_reg = (record *) DISK_REGISTER_MEMORY;


this may not work.As C compilers may not accept dynamic definition.
record *disk_reg = (record *) DISK_REGISTER_MEMORY;

maybe try the following,

record *disk_reg; // pointer declaration

disk_reg = (record *) DISK_REGISTER_MEMORY;
or
disk_reg = (record *) &DISK_REGISTER_MEMORY;// defining, and passing address.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Bit Field

Hi,

Thank echo47, you already help me to solve it. and thank for everyone shared the knowladge.

ex: Bit Field Structure
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;	 
}

I have another question. How can we initial the bit field variable in global?
Example structure asign the variable in global:
Code:
struct result  {                  
  signed char paper1;            
  signed char paper2;             
  signed char paper3;            
};							   
struct result fileA = { 100,  98,  39 };  
struct result fileB = { 70,   60,  40 };
How can we do that for bit field?

Thank You.
 

Re: Bit Field

any variable declared outside the functions is a global variable.... it is just like any other variable though it is a collection of values(variables).... hence just declare the variable outside.....
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Bit Field

You can initialize a struct containing bit fields just like other structs. For example:
struct packed_struct foo = {1, 0, 1, 0, 12, 500, 200};
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top