473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Setting two structures at the same adress

8
I'm trying to set two bitfield structures on the same adres. These two structures represent two different functions. And they control a register of 8 bits off a microcontroller. Which on their turn, they drive 8 outputs.

Why I use bitfields... is because I want to quickly set a variable to an individual port or to a defined set off ports. Depending of the function and a hardware switch.

So the first structure is nested in a header file that was giving with the compiler and example projects. This header contains all references to registers and such. It contains the first struct part with individual variables per port...

Expand|Select|Wrap|Line Numbers
  1. typedef union{   /*  PORT DATA */
  2.     IO_BYTE    byte;
  3.     struct{
  4.     IO_BYTE P00 :1;
  5.     IO_BYTE P01 :1;
  6.     IO_BYTE P02 :1;
  7.     IO_BYTE P03 :1;
  8.     IO_BYTE P04 :1;
  9.     IO_BYTE P05 :1;
  10.     IO_BYTE P06 :1;
  11.     IO_BYTE P07 :1;
  12.   }bit;
  13. }PDR0STR;
  14.  
  15. __IO_EXTERN __io PDR0STR _pdr0
  16. #define PDR0 _pdr0.byte
  17. #define PDR0_P00 _pdr0.bit.P00
  18. #define PDR0_P01 _pdr0.bit.P01
  19. #define PDR0_P02 _pdr0.bit.P02
  20. #define PDR0_P03 _pdr0.bit.P03
  21. #define PDR0_P04 _pdr0.bit.P04
  22. #define PDR0_P05 _pdr0.bit.P05
  23. #define PDR0_P06 _pdr0.bit.P06
  24. #define PDR0_P07 _pdr0.bit.P07
  25.  
Now within my main program file I've built a second struct and I want link it to the same adres. And this is the part where I'm clueless...

Expand|Select|Wrap|Line Numbers
  1. #include "mb90560.h"
  2. #include "prototypes.h"
  3.  
  4. struct myport{
  5.    int adc_act: 7;
  6.    int adc_indicator : 1;
  7. };
  8.  
  9. void main(void)
  10. {
  11. myport = &PDR0
  12. and so on...
  13.  
I've also tried things like a struct pointer and load that with the adress off &PDR0 but that doesn't work either. I've read some things about casting, but I can't seem to understand what that does and why it's necesarry.

Simply put, I've tried a different approach with bitmasking but in combination with interrupts... i don't like the idea that an interrupt happens in the middle of the bitmasking proces.

I hope someone can shine a little light on where I'm stuck. A second bitfield struct seemed the best solution, but it is also the hardest (for me then) to make it work at the same register location.
May 30 '10 #1
6 1817
weaknessforcats
9,208 Expert Mod 8TB
Carefully read section 6.9 of The C Programming Language -2nd Edition.

Then explain why you can't use a bitshifted unsigned int for this.
May 31 '10 #2
alexis4
113 100+
You are messing it too much. First of all an interrupt will not come in the middle of a command! The command will be executed and then the interrupt will come. But even if you were right, what is the actual problem? After the interrupt, registers will be restored and that's the end of it, program flow will continue normally!

And the bitfield way is not the quick way as you imply. C commands are split into assembly commands. Giving value to bitfields is not the exception of this rule. So your code can still be paused for interrupt handling routine. Or you can disable interrupts, make the bit operations and then re-enable interrupts, something totally unnecessary.

Here is an easier way with same speed and memory consumption:

Expand|Select|Wrap|Line Numbers
  1. #define PORTA_0     0
  2. #define PORTA_1     1
  3. #define PORTA_2     2
  4. //and so on.....
  5.  
  6. PORTA |=    (1 << PORTA_0);     //set pin A0
  7. PORTA &= ~(1 << PORTA_4); //clear pin A4
  8.  
Or:

Expand|Select|Wrap|Line Numbers
  1. #define PORTA_0     1
  2. #define PORTA_1     2
  3. #define PORTA_2     4
  4. //and so on.....
  5.  
  6. PORTA |=    PORTA_0;     //set pin A0
  7. PORTA &= ~PORTA_4; //clear pin A4
  8.  
But if you insist on the bitfields way, then you should absolutely use an unsigned char:

Expand|Select|Wrap|Line Numbers
  1. typedef union
  2. {
  3.   unsigned char value;
  4.   struct 
  5.   {
  6.     unsigned char 
  7.             Bit0 : 1,
  8.             Bit1 : 1,
  9.             Bit2 : 1,
  10.             Bit3 : 1,
  11.             Bit4 : 1,
  12.             Bit5 : 1,
  13.             Bit6 : 1,
  14.             Bit7 : 1;
  15.   };
  16. } port;
  17.  
  18. port myPort;
  19.  
  20. myPort.value = 0xFF;
  21. myPort.Bit3 = true;
  22.  
I don't know what compiler you are using, so I can't tell you how you will make this union to point to a port, search for it into your compiler's guide. A pointer is likely an impossible way, pointers to ports are usually not allowed (at list for my compiler).

Finally if you need a second structure pointing at the same address, then declare this structure inside the union. But really, stick to the simple way at the top of my post.
May 31 '10 #3
melle
8
Ok, thx for the help so far...

Could someone please explain why it is necessary to please an unsigned char within the union ? I'm not quite sure, but does it has something to do with the port containing 8 outputs ? I kinda understand the meaning of a union, but not the practical use of it.

Second, when using typecasting to load p_struct2 with the adres of struct1. That is adres 0x00. I can't seem to understand the logic off it...

p_struct2 = (struct1*) &struct2

To me, it's seems like I'm loading the adres of struct2 into it's own pointer. And what am I exactly converting ? It alters the pointer type, but into what ?
Jun 1 '10 #4
donbock
2,426 Expert 2GB
C89 allows bit fields to be of type unsigned int, signed int, or int (where int has a very different meaning than it does for regular variables). C99 added the capability to also use type _Bool for bit fields. Anything else (including IO_BYTE or unsigned char) relies on nonstandard extensions. I don't know about C++.

The rules for how members are packed into a structure are implementation-dependent. The rules for bit fields are notoriously fickle. While these rules vary from one compiler to another, they are required to be stable and predictable for any particular compiler. Therefore, you will have to experiment with your compiler to discover how to get the bit fields to line up the way you want them -- and you can expect them to line up differently when you use a different compiler.

In my opinion, bit fields are never a good idea. Much better to do the bit manipulations explicitly so they are portable and reliable.
Jun 1 '10 #5
melle
8
@donbock

thx for your reply. Could you please give me a simple example on how to make bit manipulation portable and reliable ?
Jun 2 '10 #6
donbock
2,426 Expert 2GB
Suppose the width of your I/O port is the same size as long.
Suppose that the I/O port is read/write.
Suppose that you want to manipulate the least-significant bit.
Expand|Select|Wrap|Line Numbers
  1. static volatile unsigned long * const pPort = <some address>;
  2. static const unsigned long bit0 = 0x00000001uL;
  3.  
  4. // Read from a long-sized I/O port
  5. unsigned long readLongPort(volatile unsigned long *addr) {
  6.     unsigned long value;
  7.     value = *addr;
  8.     <fix endianness of 'value' if necessary>
  9.     return value;
  10.     }
  11.  
  12. // Write to a long-sized I/O port
  13. void writeLongPort(volatile unsigned long *addr, unsigned long value) {
  14.     <fix endianness of 'value' if necessary>
  15.     *addr = value;
  16.     }
  17.  
  18. // Set the bit
  19. writeLongPort(pPort, readLongPort(pPort) | bit0);
  20.  
  21. // Reset the bit
  22. writeLongPort(pPort, readLongPort(pPort) & ~bit0);
  23.  
  24. // Test if the bit is set
  25. if ((readLongPort(pPort) & bit0) != 0)
  26.     ...
  27.  
  28. // Test if the bit is reset
  29. if ((readLongPort(pPort) & bit0) == 0)
  30.     ...
You need to do a little more work for wider bit-fields.

I recommend you handle endian mismatches between your compiler and your I/O device in access functions as shown above rather than sprinkle endian-swap logic throughout your application.
Jun 2 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Lars Grøtteland | last post by:
Hello! Was thinking of having an array inside a asp page. I'm rather new to this. How do I begin. my asp page should work as follows: 1. One html page include mail adresses. We receive a lot...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
5
by: FKothe | last post by:
Hello together, the program below shows a behavior i do not understand. When compiled with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function test_it0 points to an invalid adress and...
7
by: Rich Claxton | last post by:
Hi we have a 3rd party in process com dll, which was written In C++ and our client is in c#. All is working fine apart from we get a callback from the DLL which contains an array of structures....
0
by: Jarod_24 | last post by:
I got a function that return the url of a image, the problem is that the image control on the .aspx page automaticaly adds the adress of itself to the adress Example: The page...
10
by: dbz | last post by:
Hello everyone. I have a query. Lets say that following is given:- struct struct1{ char *word; int n; } *p; QUERIES: What does the following refer to?
11
by: mwebel | last post by:
Hi, i had this problem before (posted here and solved it then) now i have the same problem but more complicated and general... basically i want to store the adress of a istream in a char* among...
1
by: Simon | last post by:
Dear reader, By printing an e-mail adress field (hyperlink field) it will be shown as: "xxxx@yyyy.nn#Mailto:xxxx@yyyy.nn#"
3
Sagittarius
by: Sagittarius | last post by:
Hi there. I have a problem concerning an UDP socket in C++ (Winsock). The next paragraphs is merely to explain the system I am working on. If U want to skip it, I have marked the question in...
10
by: bimbam | last post by:
Hi, I've been trying to allocate dynamicaly an array of pointers to structures. As it didn't work, I tried to reduce the problem as much as possible. This is what I have : #include...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.