Embedded C/C++

Moderators: Lundin
Number of threads: 209
Number of posts: 392

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Can you help me with Defining header file and setting bits ?? Posted by nickatnite on 13 Apr 2009 at 1:26 PM
I need to do the following--
define a bit --
unsigned char data Bits 0x20;

then define the bits:
sbit GoingHigh Bits^0;

then i need a counter byte, i should define it in my header file

unsigned char data Counter 0x30;


Can you help me with this , I am a complete newbie . I keep getting errors. Thank you.

Report
Re: Can you help me with Defining header file and setting bits ?? Posted by Lundin on 13 Apr 2009 at 11:14 PM
You should do like this:

#define BIT_MASK 0x20

unsigned char some_data;

some_data |= BIT_MASK;  /* set bit */
some_data &= ~BIT_MASK; /* clear bit */


Which is the same thing as

some_data = some_data | BIT_MASK;
some_data = some_data & ~BIT_MASK;

The above operators are called the "bit-wise" operators. This code works on all C/C++ compilers in the world.


A poor way to do the same is like this:

typedef struct
{
  unsigned int some_data : 1;

} MyData;

MyData data;
data.some_data = 1; /* set bit */
data.some_data = 0; /* clear bit */


This code will compile on all compilers, but it is poor programming. The above is called "bit fields", and bit fields are very poorly defined by the standard. The only allowed type is int, you cannot use them with any other type.

And you shouldn't use them, but know that they exist. Sadly, they are very commonly used by so-called programmers in embedded systems.

You shouldn't use them because of the following reasons (advanced topic, beginners may stop reading here ):

- You don't know if the first bit in the struct is msb or lsb.
- You don't know how the bits are stored in memory.
- The code will be little/big endian dependant.
- You don't know whether "int" will be treated as signed or unsigned.
- You don't know if arrays of bit fields will work or not.
- Padding bytes are allowed inside bit fields.
- There is a risk that beginner programmers use other types than int for bit fields, which is undefined behavior in the standard and will cause the code to behave completely randomly.
Report
Re: Can you help me with Defining header file and setting bits ?? Posted by nickatnite on 14 Apr 2009 at 11:45 AM
Can you actually explain to me in layment terms -

what it means by
define a bit
unsigned char data Bits 0x20;
then define the bits:
sbit GoingHigh Bits^0;
then you need a counter byte, define it in your .h
unsigned char data Counter 0x30;

Going high is just a bit( either 1 or 0)

and Counter is a byte ( I will be counting from 0 to 50 )



Report
Re: Can you help me with Defining header file and setting bits ?? Posted by Lundin on 14 Apr 2009 at 11:17 PM
: define a bit
: unsigned char data Bits 0x20;

That's what I did in the first example. Try to dissect that one:

Since you can only work on byte level on most computers, you need to mask out the bit you are interested in, in this case bit 5, since 0x20 equals bit number 5. You mask out this bit without destroying the other bits. That's when the bitwise OR (|) and AND (&) operators comes in.

Needless to say, you must understand binary and hexadecimal numbers before starting to learn any form of programming.



: sbit GoingHigh Bits^0;

This means nothing, it is gibberish.


: then you need a counter byte, define it in your .h
: unsigned char data Counter 0x30;

What do you mean "counter byte" and what has 0x30 to do with that? I don't understand what you are trying to do.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.