appliclication of structure bitfields in embedded c

Status
Not open for further replies.

bagavathi

Member level 3
Joined
Nov 15, 2011
Messages
58
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Tamil Nadu,India
Visit site
Activity points
1,686
Hi friends,

1. where structure bitfield used in embedded c programming for microcontroller?
2. uses of function pointer in embedded
3. where union used in embedded c programming for microcontroller?
 

Where are they used ?
Example of union would be when you have floating point value and you are serialize it for sending it via uart/spi .. Then you can create

Code C - [expand]
1
2
3
4
union {
      float fvalue;
      unsigned char svalue[4];
}u;


Then you can use u.fvalue = 3.1415; and later on u.svalue as chunk of bytes.

Pointers are also useful when you have buffer and need to change few bytes inside it. Then you can assign pointer on that buffer and position yourself by increment the pointer.


Code C - [expand]
1
2
3
4
5
unsigned char buffer[500];
unsigned char *buf = &buffer;
...
buf+=20;
unsigned char buffer_29 = *(buf+9);




Hope it helped.
 
Last edited:
She asked for function pointer and not pointer.


Code C - [expand]
1
2
3
4
5
6
7
8
struct time
{
     int hour;
     int minute;
     int second;
};
 
display(time.hour, time.minute, time.second);   //function call

 
Last edited:

She asked for function pointer and not pointer.

Ah ok, i missed that. I use function pointers all the time. When you create "generic" library for some peripheral or device and needs to extend it in your own implementation then in library you can define function pointer. One function that will assign the pointer to some function in your extension and call it within the library if pointer is different than 0.

example:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//library:
void (*loc_func) ( *) = 0;
void setPointer (void(*func)() ptr)
{
   loc_func = func;
}
void doSomething()
{
if (loc_func != 0) loc_func();
} 
 
//main code
void func(){}
setPointer (&func);

 

It is the first one. Without &. Because you are sending the pointer to the function as parameter and assigning that pointer to local function pointer. So no need to put address of the pointer on the right hand side.
 

Structure Bit fields are used in
1. processor or controller which doesn't have access to single bit easily
Code:
struct
{
       unsigned char led_status:1;
       unsigned char key_status:1;
}status;
2. some variable are needs to use within certain limit.like if variable counter is use only for 15 counts then it will use as follows.
Code:
struct 
{
     unsigned char counter1:4;
     unsigned char counter2:3;
}counters;
then counter1 will increment upto 15
then counter2 will increment upto 8

regards
 

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…