<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>struct definition problem</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/33252/struct-definition-problem</link><description> Hi, 
 
I&amp;#39;m using KEIL uVision2 V2.20a, This definition doesn&amp;#39;t work, anyone know why ? 
 
Usually I&amp;#39;m using this to access each bit separatly in a byte, rather than using shifting ans masking. In my MPLAB C its working fine. But when using this declaration</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: struct definition problem</title><link>https://community.arm.com/thread/95420?ContentTypeID=1</link><pubDate>Mon, 24 Mar 2003 07:07:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3477dd91-4940-4396-8112-df6a9318caff</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;...is potentially difficult to port as C does not specify the order of the bit fields in memory&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
and has its own difficulties in C51, as Keil&amp;#39;s implementation can be somewhat unexpected!&lt;br /&gt;
&lt;br /&gt;
Search the knowledgebase (and this forum) as there are articles on the common pitfalls!&lt;br /&gt;
&lt;br /&gt;
Another reason to stick with the masks...?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: struct definition problem</title><link>https://community.arm.com/thread/71317?ContentTypeID=1</link><pubDate>Mon, 24 Mar 2003 06:23:38 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f9ac7115-4160-436d-9a5c-d948fdb6c165</guid><dc:creator>Graham Cole</dc:creator><description>&lt;p&gt;The bit field method would look something like this:&lt;br /&gt;
&lt;pre&gt;
struct structSplitByte
{
    unsigned char    b0:1;
    unsigned char    b1:1;
    unsigned char    b2:1;
    unsigned char    b3:1;
    unsigned char    b4:1;
    unsigned char    b5:1;
    unsigned char    b6:1;
    unsigned char    b7:1;
};

union unionSplit
{
    struct structSplitByte Split;
    char cValue;
};
&lt;/pre&gt;
This does give nice neat source code, but is potentially difficult to port as C does not specify the order of the bit fields in memory. Also, Keil is sometimes rather inefficient when accessing 1-bit fields.&lt;br /&gt;
&lt;br /&gt;
The most portable method is to use masks.&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: struct definition problem</title><link>https://community.arm.com/thread/39991?ContentTypeID=1</link><pubDate>Sun, 23 Mar 2003 18:14:31 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:af52dfa6-3020-4f9e-8e35-76d508df4718</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;&lt;i&gt;does anyone know a better and easy way to access the value of one bit in a byte ?&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Use a bit-field?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Jon&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: struct definition problem</title><link>https://community.arm.com/thread/39992?ContentTypeID=1</link><pubDate>Sun, 23 Mar 2003 16:13:18 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b1274400-0167-4d6e-9ab6-19997b6c4ae8</guid><dc:creator>Richard Collett</dc:creator><description>&lt;p&gt;&amp;gt;Usually I&amp;#39;m using this to access each bit&lt;br /&gt;
&amp;gt;separatly in a byte, rather than using&lt;br /&gt;
&amp;gt;shifting ans masking. In my MPLAB C its&lt;br /&gt;
&amp;gt;working fine. But when using this&lt;br /&gt;
&amp;gt;declaration in KEIL I get the error:&lt;br /&gt;
&lt;br /&gt;
As said by others, the &amp;quot;bit&amp;quot; directive is compiler specific.&lt;br /&gt;
&lt;br /&gt;
PIC processors can manipulate bits in every register, so naturally the PIC compiler allows you to define bit variables anywhere in memory.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: struct definition problem</title><link>https://community.arm.com/thread/71308?ContentTypeID=1</link><pubDate>Sun, 23 Mar 2003 15:47:54 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:54c6c9fb-ee47-4f74-bd36-d2156588db63</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;If the data you want to access doesn&amp;#39;t reside in the bit addressable memory (or an SFR), but is just an &amp;#39;ordinary&amp;#39; variable in data or xdata, then you&amp;#39;ll need to use those mechanisms instead of &amp;#39;bit&amp;#39; or &amp;#39;sbit&amp;#39;.&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
OR copy the data to a bit-addressable temporary location, and work with that?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: struct definition problem</title><link>https://community.arm.com/thread/39989?ContentTypeID=1</link><pubDate>Sat, 22 Mar 2003 13:40:14 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5bf411ab-f8c1-43c5-b99a-0fbb6ef067bd</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;&lt;br /&gt;
&amp;quot;bit&amp;quot; is not a standard C type.  Even if two different compilers both happen to choose that word as a keyword for an extension, they will most likely implement their extensions in different ways.  If you use non-standard extensions, then you&amp;#39;ll have to do a bit of work to port between compilers.&lt;br /&gt;
&lt;br /&gt;
With Keil C, the &amp;quot;bit&amp;quot; type is a way to declare an individual variable that resides in the bit-addressable memory region of the 8051.  Variables of &amp;quot;bit&amp;quot; cannot be members of a structure.  As the manual says for error 150, &amp;quot;A union-aggregate may not contain members of type bit&amp;quot;.  (Seems pretty clear to me, if blunt.)  Other limitations of the bit type are discussed in the manual in Chapter 3, &amp;quot;Bit Types&amp;quot;, page 98.&lt;br /&gt;
&lt;br /&gt;
If you want to access data as either a byte or bits, and want to use the 8051&amp;#39;s bit-addressable memory to hold that byte, you probably want to use the Keil &amp;quot;bdata&amp;quot; memory type and &amp;quot;sbit&amp;quot; extensions.  The &amp;quot;badata&amp;quot; memory qualifer locates variables in the bit-addressable memory.  The &amp;quot;sbit&amp;quot; type is a way to declare a bit that is part of an object in bdata.  So, one possible port of your code might look something like:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
char bdata cValue;
sbit cValueBit0 = cValue ^ 0;
sbit cValueBit1 = cValue ^ 1;
sbit cValueBit2 = cValue ^ 2;
sbit cValueBit3 = cValue ^ 3;
sbit cValueBit4 = cValue ^ 4;
sbit cValueBit5 = cValue ^ 5;
sbit cValueBit6 = cValue ^ 6;
sbit cValueBit7 = cValue ^ 7;
&lt;/pre&gt;
&lt;br /&gt;
The bdata and sbit types are discussed in the manual right after the bit types, in the &amp;quot;Bit-addressable Objects&amp;quot; section.  You&amp;#39;ll want to read carefully to note the bit ordering if you want to declare multi-byte words in bdata and access those bits.&lt;br /&gt;
&lt;br /&gt;
There&amp;#39;s also the standard ways in C to access bits in a byte, which have nothing to do with the 8051 architecture.  Bitwise &amp;quot;and&amp;quot; and &amp;quot;or&amp;quot; operations work just fine.  And you could use C bitfields, though those have their own portability problems.  If the data you want to access doesn&amp;#39;t reside in the bit addressable memory (or an SFR), but is just an &amp;quot;ordinary&amp;quot; variable in data or xdata, then you&amp;#39;ll need to use those mechanisms instead of &amp;quot;bit&amp;quot; or &amp;quot;sbit&amp;quot;.&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>