<?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>Keil Variable  declaration help</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/18180/keil-variable-declaration-help</link><description> Actually on the external interrupt from port,I want to read the timer 2 value into a variable.(TH2 and TL2). 
suppose I declare a variable 
unsigned int timer2_val; 
How can I move TL2 and TH2 into lower and upper nibble of timer2_val, so that I can</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Keil Variable  declaration help</title><link>https://community.arm.com/thread/96756?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2004 15:07:17 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e0a618b1-504e-4dd8-bd97-8f3030591c61</guid><dc:creator>Robert Wey</dc:creator><description>&lt;p&gt;&lt;i&gt;Well, but just remember that you should stop the timer before read these registers otherwise you may obtain wrong values. It is because reading of integer value is split into two reading of bytes (chars) due 8051 8-bit architecture. And if timer run then there is a chance that TL2 and TH2 registers change their values between these two readings.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Sage advice!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Keil Variable  declaration help</title><link>https://community.arm.com/thread/72889?ContentTypeID=1</link><pubDate>Fri, 26 Nov 2004 13:29:57 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:37ed6e28-271f-4f54-9378-7451542a594f</guid><dc:creator>Frodak Baksik</dc:creator><description>&lt;p&gt;The SFR keyword can&amp;#39;t be used within a struct or union.&lt;br /&gt;
&lt;br /&gt;
Nor can you do something like&lt;br /&gt;
&lt;pre&gt;
timer2_val = *(unsigned int*)&amp;TH2;
&lt;/pre&gt;
because TH2 must be a direct byte transfer otherwise the data will come out of idata, and it won&amp;#39;t compile.&lt;br /&gt;
&lt;br /&gt;
On the cypress FX2 TH2 and TL2 are at 0xcd and 0xcc, which can be declared as:&lt;br /&gt;
&lt;pre&gt;
sfr16 T2 = 0xcc;
&lt;/pre&gt;
This is straight out of the cx51 manual under sfr16, page 100.&lt;br /&gt;
&lt;br /&gt;
However, Timer 0 and Timer 1 are not as conveniant and this will work:&lt;br /&gt;
&lt;pre&gt;
union word_byte {
    unsigned int word;
    unsigned char byte[2];
};
    y.byte[0] = gTH1;
    y.byte[1] = gTL1;
    x = TIMER_1_RELOAD + y.word;
&lt;/pre&gt;
which will compile a little tighter than&lt;br /&gt;
&lt;pre&gt;
x = ((unsigned int)gTH1 &amp;lt;&amp;lt; 8 ) + gTL1 + TIMER_1_RELOAD;
&lt;/pre&gt;
&lt;br /&gt;
:-)&lt;br /&gt;
Frodak&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Keil Variable  declaration help</title><link>https://community.arm.com/thread/43185?ContentTypeID=1</link><pubDate>Fri, 26 Nov 2004 02:59:53 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:da3b86bf-84e4-42e8-88e3-4ab7e9454189</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;This is pure, standard &amp;#39;C&amp;#39; - nothing specifically to do with Keil.&lt;br /&gt;
&lt;br /&gt;
See this thread - the principles involved are the same:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.keil.com/forum/docs/thread5275.asp"&gt;http://www.keil.com/forum/docs/thread5275.asp&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Note that using pointers or unions &lt;i&gt;may&lt;/i&gt; be more efficient, but is non-portable - since it relies upon the specific compiler&amp;#39;s byte ordering.&lt;br /&gt;
Using shifts (or multiplications) is portable, but &lt;i&gt;may&lt;/i&gt; be less efficient.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Keil Variable  declaration help</title><link>https://community.arm.com/thread/87006?ContentTypeID=1</link><pubDate>Fri, 26 Nov 2004 02:47:28 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d2fd69c9-7f93-4cf8-8f3f-3881208dcaad</guid><dc:creator>Oleg Sergeev</dc:creator><description>&lt;p&gt;hi,&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;timer2_val = (TH2*256)+TL2
or
timer2_val = (TH2&amp;lt;&amp;lt;8)|TL2&lt;/pre&gt;
&lt;br /&gt;
Well, but just remember that you should stop the timer before read these registers otherwise you may obtain wrong values. It is because reading of integer value is split into two reading of bytes (chars) due 8051 8-bit architecture. And if timer run then there is a chance that TL2 and TH2 registers change their values between these two readings.&lt;br /&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Oleg&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Keil Variable  declaration help</title><link>https://community.arm.com/thread/43184?ContentTypeID=1</link><pubDate>Fri, 26 Nov 2004 02:42:07 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:41e93f49-c0b2-45a9-8c18-5375d005c40a</guid><dc:creator>clever dick</dc:creator><description>&lt;p&gt;timer2_val = (TH2*256)+TL2&lt;br /&gt;
or&lt;br /&gt;
timer2_val = (TH2&amp;lt;&amp;lt;8)|TL2&lt;br /&gt;
&lt;br /&gt;
You didn&amp;#39;t realy mean &amp;#39;Nibble&amp;#39; as that&amp;#39;s 4 bits.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>