We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I want to define a 8 BIT variable and char variable that occoupy the same memory loaction in the memory. so any change in the char variable should reflect the bit variable. In C we can define a structure for bit struct num {
unsigned char X:1; unsigned char Y:1; unsigned char Z:1; unsigned char A:1; unsigned char B:1; unsigned char C:1; unsigned char D:1; unsigned char E:1; };
struct num Axis_Move ;
How i can define a Union having one char and 8 BIT variable
union num { unsigned char byte; struct { unsigned char X:1; unsigned char Y:1; unsigned char Z:1; unsigned char A:1; unsigned char B:1; unsigned char C:1; unsigned char D:1; unsigned char E:1; } bits; } union num Axis_Move;
A little easier to read when you use the 'pre' tags like it says in the third bullet item right above where you entered your message, don't you think?
We can do by following way also
unsigned char bdata Flag;
sbit One = Flag^0; sbit Two = Flag^1; sbit Three = Flag^2; sbit Four = Flag^3; sbit Five = Flag^4; sbit Six = Flag^5; sbit Seven = Flag^6; sbit Eight = Flag^7;
but what if the controller dosent have the bdata
"We can do by following way also"
Of course you can. Why did you ask for a union, then?
"but what if the controller dosent have the bdata"
What do you mean? All 8051 derivatives have bit-addressable memory. Whether you use it as such or not is up to you.
Hi there,
this is exactly what i want for my ARM7 C-Programm. But didn't got it to run by myself.
As i understand this is a mixture of a struct which defines the bitfield and a union which allows to acsess to it as single bit and byte. I hope someone will post a more detailed version of this example, how to declare and define and acsess.
Unfortunately my german textbook is not so good, my english is too bad to enjoy an english textbook. Hope someone can recommand me a good (german) textbook?
Cincerly yours
"But didn't got it to run by myself."
Other than missing a semicolon (;) after the union's closing brace, what problems did you have?
Remember that the order of bit allocation in C bit fields is left to the compiler. In the above struct, "X" may or may not be the least significant bit (or most significant bit, either). Always check your manuals and/or write some test code, especially if you plan on sharing the structure between processors or compilers.
"... if you plan on sharing the structure between processors or compilers."
I think the last time I used bitfields in software that I designed was around 1984. The design was application-oriented enough to get away with it. Since then, everything I do is talking to the hardware, over a wire, or through the air. In those cases bitfields are just not worth the trouble.
You wouldn't believe the amount of trouble my prestigious clients (who should know better) have gotten themselves into using bitfields.
JUST SAY NO!
Yes i know that one Compiler will sort the bits in a Byte from 0 to 7 while the other Compiler may sort the bits from 7 to 0. But normaly portability is not required for my applications. (i hope so)
I often use the SPI-interface. Many boolean sensors, digital inputs and switches are connected to a PISO (a shift-register / Parallel In Serial Out) Also many Led's, Relais, Lamps and Digital Outputs are connected to a SIPO (shift-register / Serial In Parallel Out) There are sometimes obout 110 or more boolean objects to be managed. Using the 8051 bitadressable area i got a very readable Programm even in Assembler. I can control each of my boolean object with one or more names for example:
Led_09 EQU Bit_adresable_3.5 Led_10 EQU Bit_adresable_3.6 * * * * Led_protect_on EQU Led_09 Led_low_temp EQU Led_10
Later i can receive the contence of all my Inputs in a SPI-data-stream and actualize all my outputs by sending a SPI-data-stream. My refresh-rate is 100Hz.
How can i port all this benefit features to ARM7 and C without using Bitfields?
Your suggestions :-)
Use an include file with lots of #defines that access the bits the conventional way (using masks and boolean operators on words).
Do you suggest something like this:
#define SETB_Led_low_temp led_status_3 |= 0x00400000 #define CLR_Led_low_temp led_status_3 &= 0xFFBFFFFF #define CPL_Led_low_temp led_status_3 ^= 0x00400000 unsigned int led_status_0; unsigned int led_status_1; unsigned int led_status_2; unsigned int led_status_3;
??? How can i replace
MOV C,Led_blabla MOV Led_blabla,C JB Led_blabla,target JNB Led_blabla,target
without a lot of kludge?
Yes. That's the way to do it in an HLL, or on any architecture that does not support manipulation of single bits.
If you're using a processor that does not have instructions for the manipulation of single bits, you will have to use bitmasks. And as far as I know, the ARM architecture does not have these instructions. You may hide the "kludge" behind macros if you need to.
Also, are you talking about using assembly or C ? You're jumping between the two without clarifying.
Hi, thanks for your reply, what is HLL? Hindustan Lever Limited (HLL)
After studing the ARM7 architekture for some months i come to conclusion: it's a kludge for a microcontroller. For example in 8051 assembler you have single bit manipulations with only one command:
SETB My_weird_thing
while in ARM7 assembler you have the kludge of five commands: 1. load Register 1 with adress of word to be manipulated 2. load Register 2 with contence of this adress 3. load Register 3 with adress of mask 4. load Register 3 with mask and do Boolean AND 5. store Register back
This in combination with having no detailed interrupt-vectors is a real bullshit kludge. Also, are you talking about using assembly or C ? You're jumping between the two without clarifying. Yes i must jump between them to compare advantages and disadvantages. In my prior project i allways used 8051-assembler, now i want to get familiar with ARM7-C. What i need is some suggestment how to produce some code which high speed on execution-time, and also readable on paper. How can i replace the 8051 assembler-code
;this is just a weird example MOV C,My_sensor_1 ANL C,\My_sensor_2; wow this looks good to my eyes MOV My_actor_1,C
in ARM7-C ?
Hi, thanks for your reply, what is HLL?
High level language. (That's the first meaning wikipedia offers, Hindustan Lever Limited is the second). Basically, anything that's not assembly.
After studing the ARM7 architekture for some months i come to conclusion: it's a kludge for a microcontroller.
A screwdriver is a kludge for pounding a nail.
For example in 8051 assembler you have single bit manipulations with only one command:
That's because the 8051 was designed particularly with bit manipulation in mind. Many, many other architectures are not.
I don't really want to think about what I need to do on a '51 to do a 32x32 multiply-accumulate. Or to count leading zeros of a 32 bit number.
Maybe the ARM architecture isn't right for your task. There are some other architectures out there that have some kind of support for bit manipulation.
1. load Register 1 with adress of word to be manipulated
If you're going to do several of these manipulations in a row, this step needs to be done only once.
2. load Register 2 with contence of this adress 3. load Register 3 with adress of mask 4. load Register 3 with mask and do Boolean AND
Since the mask usually contains only one set bit, it can be loaded directly. Or you can use the LDR pseudo-instruction.
How can i replace the 8051 assembler-code
if(GET_MY_SENSOR_1() && !GET_MY_SENSOR_2()) { SET_MY_ACTOR_1(1); } else { SET_MY_ACTOR_1(0); }
... with appropriately defined macros.