<?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: &amp;quot;#define&amp;quot; is not working with the MC9S12XS128 in S12 / MagniV Microcontrollers</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172537#M5866</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear acehigh99,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PORTA is a memory location.&amp;nbsp; When used as a rvalue (ie. on the right of an assignment) its contents are used, so the line:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;LAST_COIL_CTL = COIL_05_CTL;&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just copies the _contents_ of PORTA to&amp;nbsp; &lt;EM&gt;LAST_COIL_CTL.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code is doing something like the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;int i;&lt;/P&gt;&lt;P&gt;int j;&lt;/P&gt;&lt;P&gt;&amp;nbsp;......&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;j = i ;&lt;/P&gt;&lt;P&gt;j = 33;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but you are expecting i to change!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code will do what you want (for a different CPU but changes should be obvious):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;
#include &amp;lt;hidef.h&amp;gt; /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */

typedef unsigned char U8;

#define     COIL_05_CTL     PTAD
#define     COIL_05_PIN     (1&amp;lt;&amp;lt;5)

U8     *LAST_COIL_CTL;
U8     LAST_COIL_PIN;


void main(void) {

  EnableInterrupts; /* enable interrupts */
  /* include your code here */

LAST_COIL_CTL = &amp;amp;COIL_05_CTL;  // Assign coil 5
LAST_COIL_PIN = COIL_05_PIN;  // Assign coil 5 pin
*LAST_COIL_CTL &amp;amp;= (~LAST_COIL_PIN);  // Turn off pin 5 of PORT A
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Hope this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A further tip - It is sometimes useful to to look at the Pre-processed code to see how you macros expand.&amp;nbsp; You can do this by right-clicking and select Preprocess.&amp;nbsp; You can also look at the assembly language generated by selecting Disassemble on the same menu.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;bye&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Oct 2020 09:21:06 GMT</pubDate>
    <dc:creator>pgo</dc:creator>
    <dc:date>2020-10-29T09:21:06Z</dc:date>
    <item>
      <title>"#define" is not working with the MC9S12XS128</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172536#M5865</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a question on what I figured to be a rather simple bit of coding that is not working the way I would expect it to... I've assigned labels to some output pins on the MC9S12XS128 (port A, specifically) using #defines, and then tried to&lt;/P&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;#define&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COIL_05_CTL&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PORTA&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;#define &amp;nbsp; &amp;nbsp; COIL_05_PIN &amp;nbsp; &amp;nbsp; BIT5&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;ubyte &amp;nbsp; &amp;nbsp; LAST_COIL_CTL;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;ubyte &amp;nbsp; &amp;nbsp; LAST_COIL_PIN;&lt;BR /&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;case 5:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; LAST_COIL_CTL = COIL_05_CTL;&amp;nbsp; // Assign coil 5 control to last coil control&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; LAST_COIL_PIN = COIL_05_PIN;&amp;nbsp; // Assign coil 5 output pin to last coil pin&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;LAST_COIL_CTL &amp;amp;= (~LAST_COIL_PIN);&amp;nbsp; // Turn off pin 5 of PORT A - does not work&lt;BR /&gt;&lt;/EM&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This last command DOES NOT turn off pin 5 of PORT A; however, the following line of code DOES turn off the pin, as expected:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;COIL_05_CTL &amp;amp;= (~COIL_05_PIN);&amp;nbsp; // Turn off pin 5 of PORT A&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Any idea as to why the #define can't be assigned to simple ubyte variable and manipulated accordingly?&amp;nbsp; I also tryed (ubyte) castings, which did not help:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;P&gt;&lt;EM&gt;case 5:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; LAST_COIL_CTL = (ubyte)COIL_05_CTL;&amp;nbsp; // Assign coil 5 control to last coil control&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; LAST_COIL_PIN = (ubyte)COIL_05_PIN;&amp;nbsp; // Assign coil 5 output pin to last coil pin&lt;/EM&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Feb 2010 04:26:57 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172536#M5865</guid>
      <dc:creator>aceshigh99</dc:creator>
      <dc:date>2010-02-03T04:26:57Z</dc:date>
    </item>
    <item>
      <title>Re: "#define" is not working with the MC9S12XS128</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172537#M5866</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear acehigh99,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PORTA is a memory location.&amp;nbsp; When used as a rvalue (ie. on the right of an assignment) its contents are used, so the line:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;LAST_COIL_CTL = COIL_05_CTL;&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just copies the _contents_ of PORTA to&amp;nbsp; &lt;EM&gt;LAST_COIL_CTL.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code is doing something like the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;int i;&lt;/P&gt;&lt;P&gt;int j;&lt;/P&gt;&lt;P&gt;&amp;nbsp;......&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;j = i ;&lt;/P&gt;&lt;P&gt;j = 33;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but you are expecting i to change!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code will do what you want (for a different CPU but changes should be obvious):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;
#include &amp;lt;hidef.h&amp;gt; /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */

typedef unsigned char U8;

#define     COIL_05_CTL     PTAD
#define     COIL_05_PIN     (1&amp;lt;&amp;lt;5)

U8     *LAST_COIL_CTL;
U8     LAST_COIL_PIN;


void main(void) {

  EnableInterrupts; /* enable interrupts */
  /* include your code here */

LAST_COIL_CTL = &amp;amp;COIL_05_CTL;  // Assign coil 5
LAST_COIL_PIN = COIL_05_PIN;  // Assign coil 5 pin
*LAST_COIL_CTL &amp;amp;= (~LAST_COIL_PIN);  // Turn off pin 5 of PORT A
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Hope this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A further tip - It is sometimes useful to to look at the Pre-processed code to see how you macros expand.&amp;nbsp; You can do this by right-clicking and select Preprocess.&amp;nbsp; You can also look at the assembly language generated by selecting Disassemble on the same menu.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;bye&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:21:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172537#M5866</guid>
      <dc:creator>pgo</dc:creator>
      <dc:date>2020-10-29T09:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: "#define" is not working with the MC9S12XS128</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172538#M5867</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;The problem isn't syntax-related, it is your algorithm that doesn't make sense.&lt;BR /&gt;Change it to this:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;LAST_COIL_PIN = COIL_05_PIN; // Assign coil 5 output pin to last coil pin&lt;BR /&gt;&lt;BR /&gt;COIL_05_CTL &amp;amp;= ~LAST_COIL_PIN; // Turn off pin 5 of PORT A&lt;DIV class="message-edit-history"&gt;&lt;SPAN class="edit-author"&gt;Message Edited by Lundin on&lt;/SPAN&gt; &lt;SPAN class="local-date"&gt;2010-02-03&lt;/SPAN&gt; &lt;SPAN class="local-time"&gt;09:55 AM&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Feb 2010 16:54:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172538#M5867</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2010-02-03T16:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: "#define" is not working with the MC9S12XS128</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172539#M5868</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear acehigh99,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps I have misunderstood what you are trying to do.&amp;nbsp; I assumed that you wanted to be able to set up access to an arbitrary pin on an arbitrary port and then (later) access that pin.&amp;nbsp; If you are able to restrict to a single port then Lundin's suggestion is much superior.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;bye&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Feb 2010 08:00:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172539#M5868</guid>
      <dc:creator>pgo</dc:creator>
      <dc:date>2010-02-04T08:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: "#define" is not working with the MC9S12XS128</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172540#M5869</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your suggestion worked very well.&amp;nbsp; Just needed to reference the hardware by address rather than directly.&amp;nbsp; You were correct...thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 20 Feb 2010 02:41:13 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/quot-define-quot-is-not-working-with-the-MC9S12XS128/m-p/172540#M5869</guid>
      <dc:creator>aceshigh99</dc:creator>
      <dc:date>2010-02-20T02:41:13Z</dc:date>
    </item>
  </channel>
</rss>

