<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to extract byte from word? in S12 / MagniV Microcontrollers</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129839#M1227</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Sujith&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are two ways. The portable and non-portable way.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;******************* Non-portable ********************&lt;BR /&gt;unsigned char *ptr = &amp;amp;usWord;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;send-routine(*ptr++);&lt;BR /&gt;send-routine(*ptr);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*****************************************************&lt;BR /&gt;This works but the order of the bytes is 'endian' dependent. Big-endian (like HC12) or little-endia (like 80186).&lt;BR /&gt;If usWord is 0x1234, the data sent will be 0x12, 0x34 when big-endian or 0x34, 0x12 when little endian.&lt;BR /&gt;Generally network ordering (as used by Internet etc.) corresponds to the big-endian ordering.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;******************* portable ************************&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;send-routine((unsigned char)(usWord&amp;gt;&amp;gt;8));&lt;BR /&gt;send-routine((unsigned char)(usWord));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*****************************************************&lt;BR /&gt;The portable version will give the same results as the non-portable when using the HC12. The portable case also specifically sends in network order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark Butcher&lt;BR /&gt;&lt;A href="http://www.mjbc.ch/"&gt;www.mjbc.ch&lt;/A&gt; / &lt;A href="http://www.uTasker.com/"&gt;www.uTasker.com&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 10 Sep 2006 20:42:26 GMT</pubDate>
    <dc:creator>mjbcswitzerland</dc:creator>
    <dc:date>2006-09-10T20:42:26Z</dc:date>
    <item>
      <title>How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129836#M1224</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am using C to programm the HCS12 and wondering how to extract a byte from a word so that I can send the data in two bytes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Sujith&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Sep 2006 17:52:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129836#M1224</guid>
      <dc:creator>pIx</dc:creator>
      <dc:date>2006-09-09T17:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129837#M1225</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Just to clarify, I have declared the variable holding the data as an unsigned short, therefore it has a 16 data in it.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Sep 2006 17:54:03 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129837#M1225</guid>
      <dc:creator>pIx</dc:creator>
      <dc:date>2006-09-09T17:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129838#M1226</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Ok.... 1 way I figured out to get the MSB is to right or left shift 8 times and then AND it with 0x0F and dump it into the register. Is there an equivalent of assembly LDB, DPB in C for codewarrior that will just extract the upper or lower byte?&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Sep 2006 18:56:00 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129838#M1226</guid>
      <dc:creator>pIx</dc:creator>
      <dc:date>2006-09-09T18:56:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129839#M1227</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Sujith&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are two ways. The portable and non-portable way.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;******************* Non-portable ********************&lt;BR /&gt;unsigned char *ptr = &amp;amp;usWord;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;send-routine(*ptr++);&lt;BR /&gt;send-routine(*ptr);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*****************************************************&lt;BR /&gt;This works but the order of the bytes is 'endian' dependent. Big-endian (like HC12) or little-endia (like 80186).&lt;BR /&gt;If usWord is 0x1234, the data sent will be 0x12, 0x34 when big-endian or 0x34, 0x12 when little endian.&lt;BR /&gt;Generally network ordering (as used by Internet etc.) corresponds to the big-endian ordering.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;******************* portable ************************&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;send-routine((unsigned char)(usWord&amp;gt;&amp;gt;8));&lt;BR /&gt;send-routine((unsigned char)(usWord));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*****************************************************&lt;BR /&gt;The portable version will give the same results as the non-portable when using the HC12. The portable case also specifically sends in network order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark Butcher&lt;BR /&gt;&lt;A href="http://www.mjbc.ch/"&gt;www.mjbc.ch&lt;/A&gt; / &lt;A href="http://www.uTasker.com/"&gt;www.uTasker.com&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 10 Sep 2006 20:42:26 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129839#M1227</guid>
      <dc:creator>mjbcswitzerland</dc:creator>
      <dc:date>2006-09-10T20:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129840#M1228</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;/*-------------------------------------------*/&lt;BR /&gt;/* If the word has hex value 1234 then */ &lt;BR /&gt;/*-------------------------------------------/*&lt;BR /&gt;unsigned short usWord=0x1234;&lt;BR /&gt;&lt;BR /&gt;/*-------------------------------------------*/&lt;BR /&gt;/* To get the upper char ,just And with 0xF0 */&lt;BR /&gt;/* and shift right 8 places.result is 0x12 */&lt;BR /&gt;/*-------------------------------------------*/&lt;BR /&gt;unsigned char upper = (unsigned char)((usWord &amp;amp; 0xF0) &amp;gt;&amp;gt; 8);&lt;BR /&gt;&lt;BR /&gt;/*-------------------------------------------*/&lt;BR /&gt;/* To get the lower char ,just And with 0xF */&lt;BR /&gt;/* thats it...result is 0x34 */&lt;BR /&gt;/*-------------------------------------------*/&lt;BR /&gt;unsigned char lower = (unsigned char)(usWord &amp;amp; 0x0F);&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 10 Sep 2006 21:31:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129840#M1228</guid>
      <dc:creator>kwik</dc:creator>
      <dc:date>2006-09-10T21:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129841#M1229</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi Kiwi&lt;BR /&gt;&lt;BR /&gt;Careful with the logical arithmetic.&lt;BR /&gt;You need to AND with 0xff00 and 0x00ff and not 0xf0 resp. 0x0f.&lt;BR /&gt;However the AND operations are also superfluous since the casting to unsigned char chops the 16 bit word to 8 bits and so it really doesn't matter whether the other bits have been set to zero or not.&lt;BR /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;&lt;BR /&gt;Mark&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 10 Sep 2006 21:39:17 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129841#M1229</guid>
      <dc:creator>mjbcswitzerland</dc:creator>
      <dc:date>2006-09-10T21:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129842#M1230</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Yes of course !! he he.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Sep 2006 04:33:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129842#M1230</guid>
      <dc:creator>kwik</dc:creator>
      <dc:date>2006-09-12T04:33:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129843#M1231</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;PRE&gt;/* Type defines */typedef long  DWORD;typedef int  WORD;typedef char BYTE;typedef unsigned long UDWORD;typedef unsigned int UWORD;typedef unsigned char UBYTE;typedef DWORD INT32;typedef WORD INT16;typedef BYTE    INT8;typedef UDWORD UINT32;typedef UWORD UINT16;typedef UBYTE UINT8;typedef BYTE BOOLEAN;typedef WORD STACK;/************************************************************************ Useful Macros Section ***********************************************************************//* Returns the low byte of the word  */#define LOBYTE(w)  ((BYTE)(w))/* Returns the high byte of the word  */#define HIBYTE(w)  ((BYTE)(((WORD)(w) &amp;gt;&amp;gt; 8) &amp;amp; 0xFF))/* Makes a word from \a low byte and \a high byte. */#define MAKEWORD(low, high) ((WORD)(((BYTE)(low)) | (((WORD)((BYTE)(high))) &amp;lt;&amp;lt; 8)))/* Returns the low word of the double word */#define LOWORD(l)  ((UINT16)(UINT32)(l))/* Returns the high word of the double word */#define HIWORD(l)  ((UINT16)((((UINT32)(l)) &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF))/* Returns the low signed word of the double word */#define LOSWORD(l)  ((WORD)(DWORD)(l))/* Returns the high signed word of the double word */#define HISWORD(l)  ((WORD)((((DWORD)(l)) &amp;gt;&amp;gt; 16) &amp;amp; 0xFFFF))/* Makes a double word from low word and a high word. */#define MAKELONG(low, high) ((UINT32)(((UINT16)(low)) | (((UINT32)((UINT16)(high))) &amp;lt;&amp;lt; 16)))&lt;/PRE&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;Example use with c code&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;WORD Test = 0xAB;&lt;/DIV&gt;&lt;DIV&gt;BYTE HiByte, LoByte;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;HiByte = HIBYTE(Test);&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;LoByte = LOBYTE(Test);&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Sep 2006 03:37:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129843#M1231</guid>
      <dc:creator>Technoman64</dc:creator>
      <dc:date>2006-09-15T03:37:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129844#M1232</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;DIV&gt;Hello.&lt;/DIV&gt;&lt;DIV&gt;I'm just wondering, wouldn't it be more efficient to use a union&amp;nbsp;between a word and a struct of&amp;nbsp;two bytes? The processor wouldn't then have to do the rotating and anding for the high byte. Or would the compiled code still result in the same process?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I don't remember exactly how to do it, but something like this perhaps:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;typedef union{&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; word Word;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; struct{&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; byte HIGHBYTE;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; byte LOWBYTE;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; } Bytes;&lt;/DIV&gt;&lt;DIV&gt;} myword;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;/Anders&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Sep 2006 13:53:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129844#M1232</guid>
      <dc:creator>Anders</dc:creator>
      <dc:date>2006-09-15T13:53:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129845#M1233</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;I have found that the compiler is very good at optimizing the code. I have seen register swapping instructions used quite often when inspecting the code generated by the compiler.&lt;BR /&gt;&lt;BR /&gt;You do have a good point with the struct though. A person could generate code usign differant methods and inspect the code to see which method is optimized the best.&lt;BR /&gt;&lt;BR /&gt;Just a thought...&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Sep 2006 17:32:28 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129845#M1233</guid>
      <dc:creator>Technoman64</dc:creator>
      <dc:date>2006-09-15T17:32:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract byte from word?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129846#M1234</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;BLOCKQUOTE&gt;&lt;DIV&gt;&lt;HR /&gt;Anders wrote:&lt;BR /&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I'm just wondering, wouldn't it be more efficient to use a union&amp;nbsp;between a word and a struct of&amp;nbsp;two bytes?&lt;BR /&gt;&lt;HR /&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;This is not recommended - especially if portability is required - as the order in which will the bytes will be arranged depends on the compiler.&amp;nbsp;It is safer&amp;nbsp;to use masks and shifts as suggested above.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Steve M.&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Sep 2006 15:55:17 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-extract-byte-from-word/m-p/129846#M1234</guid>
      <dc:creator>sjmelnikoff</dc:creator>
      <dc:date>2006-09-19T15:55:17Z</dc:date>
    </item>
  </channel>
</rss>

