Hi,
I'm using the ST32F103G uC and Keli uVision 4.22 IDE.
Something that I don't understand is how to use single bits as variables or as a function return value. With other compilers I know 'bit' and 'bool' but somehow this doesn't seem to work with the armcc compiler. I searched the Internet but could not find anything in that direction.
Who can help me out?
Thanks in advance,
Henk
Most processors don't support bit variables, so there are no C language extension to declare bit variables or have function return single bits.
You can manipulate bits in different ways, but if you have a boolean data type it will be larger than a single bit.
The C++ bool data type is normally not a single bit. Many compilers implements it the same as an int. It's just that the compiler keeps track of the data type for improved type checking.
If you do not want to manually perform bit-and/bit-or/bit-not you can play with bit fields - a method to create a struct where many narrow bit fields are stored in an integer container.
the Cortex-M3 have
• Bit-banding. The bus matrix converts bit-band alias accesses into bit-band region accesses. It performs: — bit field extract for bit-band loads — atomic read-modify-write for bit-band stores.
to use it you must use the variable names/addresses from the ST library
Erik
But note that bit-banding is a processor extension to get an alternative way to access individual bits in integers. A kind of hw-accelerated bit fields. There are no new data types involved so it can't be used to create a function with a bit-sized return type.
Even if there exists library functions that makes use of bit banding, it's possible to use by any program with zero suppot from compiler or libraries.
It is an integral part of the ARM Cortex-M3 architecture - nothing specifically to do with ST, or their library.
"Most processors don't support bit variables, so there are no C language extension to declare bit variables or have function return single bits."
I think you meant to say that the standard C language has no facilities to declare bit variables or have function return single bits?
There certainly are C implementations which provide language extensions to declare bit variables or have function return single bits - Keil C51 being a case in point.
However, this does rely entirely upon specific hardware support from the underlying target architecture.
For the type bool, just #include <stdbool.h>. If you don't have the header, it's easy to put one together simply mapping bool to int or char.
Thanks!
"mapping bool to int or char"
But, as already noted, that does not give a single-bit variable - as per the original question & title
that does not give a single-bit variable - as per the original question & title
Let's just say I was able to see the intent of the OP by looking beyond the literal meaning of the question. Try it, it can be fun :-)
Yes, all too often the literal question asked is not the real question that should be answered.
Hence my fequent quoting of www.catb.org/.../smart-questions.html
But you need to make clear that the reply given is not a literal answer to the literal question.
"... mapping bool to int or char."
However, the mapping and implementation of boolean semantics becomes the responsibility of the user:
typedef int _Bool; _Bool is_nonzero; int value = 1234; { is_nonzero = !!value; }
{ is_nonzero = !!value; }
I would prefer to write:
{ is_nonzero = value != 0; }
I expect some people will look twice (pun intended) at a !! expression.
The second alternative can be read explicitly as "valie is not equal to zero" or shorter "value is not zero".
My typedef of a Bool (coming from years back) has always been of the form:
typedef unsigned char Bool; // TRUE/FALSE(/MAYBE) type conditions