"#define" is not working with the MC9S12XS128

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

"#define" is not working with the MC9S12XS128

2,070 Views
aceshigh99
Contributor I

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

 

#define     COIL_05_CTL     PORTA

#define     COIL_05_PIN     BIT5

 

ubyte     LAST_COIL_CTL;

ubyte     LAST_COIL_PIN;

 

 

case 5:

     LAST_COIL_CTL = COIL_05_CTL;  // Assign coil 5 control to last coil control

     LAST_COIL_PIN = COIL_05_PIN;  // Assign coil 5 output pin to last coil pin

 

LAST_COIL_CTL &= (~LAST_COIL_PIN);  // Turn off pin 5 of PORT A - does not work

 

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:

 

 COIL_05_CTL &= (~COIL_05_PIN);  // Turn off pin 5 of PORT A

 

Any idea as to why the #define can't be assigned to simple ubyte variable and manipulated accordingly?  I also tryed (ubyte) castings, which did not help:

 

case 5:

     LAST_COIL_CTL = (ubyte)COIL_05_CTL;  // Assign coil 5 control to last coil control

     LAST_COIL_PIN = (ubyte)COIL_05_PIN;  // Assign coil 5 output pin to last coil pin

 

 

Thanks in advance.

Labels (1)
0 Kudos
Reply
4 Replies

1,237 Views
Lundin
Senior Contributor IV
The problem isn't syntax-related, it is your algorithm that doesn't make sense.
Change it to this:



LAST_COIL_PIN = COIL_05_PIN; // Assign coil 5 output pin to last coil pin

COIL_05_CTL &= ~LAST_COIL_PIN; // Turn off pin 5 of PORT A
Message Edited by Lundin on 2010-02-03 09:55 AM
0 Kudos
Reply

1,237 Views
pgo
Senior Contributor V

Dear acehigh99,

 

Perhaps I have misunderstood what you are trying to do.  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.  If you are able to restrict to a single port then Lundin's suggestion is much superior.

 

bye

0 Kudos
Reply

1,237 Views
aceshigh99
Contributor I

Your suggestion worked very well.  Just needed to reference the hardware by address rather than directly.  You were correct...thanks!

0 Kudos
Reply

1,237 Views
pgo
Senior Contributor V

Dear acehigh99,

 

PORTA is a memory location.  When used as a rvalue (ie. on the right of an assignment) its contents are used, so the line:

LAST_COIL_CTL = COIL_05_CTL; 

 

Just copies the _contents_ of PORTA to  LAST_COIL_CTL.

 

Your code is doing something like the following:

 

int i;

int j;

 ......

 

j = i ;

j = 33;

 

but you are expecting i to change!

 

 

The following code will do what you want (for a different CPU but changes should be obvious):

 

 

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */

typedef unsigned char U8;

#define     COIL_05_CTL     PTAD
#define     COIL_05_PIN     (1<<5)

U8     *LAST_COIL_CTL;
U8     LAST_COIL_PIN;


void main(void) {

  EnableInterrupts; /* enable interrupts */
  /* include your code here */

LAST_COIL_CTL = &COIL_05_CTL;  // Assign coil 5
LAST_COIL_PIN = COIL_05_PIN;  // Assign coil 5 pin
*LAST_COIL_CTL &= (~LAST_COIL_PIN);  // Turn off pin 5 of PORT A
}

 

 Hope this helps.

 

A further tip - It is sometimes useful to to look at the Pre-processed code to see how you macros expand.  You can do this by right-clicking and select Preprocess.  You can also look at the assembly language generated by selecting Disassemble on the same menu.

 

bye

 

0 Kudos
Reply