Stupid question on define pin

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

Stupid question on define pin

2,448 Views
cicciounico
Contributor III

Hi,

I have a stupid question on define pin.

I want define the name of pin, example:

char SdaPin; 

SdaPin= PTCD_PTCD2;


but when I write:

SdaPin= 1;

this not work!!!

Sorry for stupid question, I don't understand!!!

Thanks

Labels (1)
Tags (1)
0 Kudos
6 Replies

687 Views
cicciounico
Contributor III

Thanks but...With variable it's impossible?

I used 3 pin for i2c (manual i2c not with dedicated function).

I have make generic Start, Stop,Write, Read. Before I use this function I set my port, example:

char SdaPin; 

SdaPin= PTCD_PTCD2;

Start();

Write();

Stop();

 

//other pin

SdaPin= PTCD_PTCD3;

Start();

Write();

Stop();

 

This is impossible if I must use a DEFINE.

I must re-write my function, one for each pin? Example:

Start_Pin2();

Write_Pin2();

Stop_Pin2();

and

Start_Pin3();

Write_Pin3();

Stop_Pin3();

 

Thanks again!!

0 Kudos

687 Views
stanish
NXP Employee
NXP Employee

Yes, it is possible to use it as a variable, but it's not as efficient as macro that generates just single bit set/clear instruction. On the other hand you can reuse the functions easily:

e.g.:

 

 

#define PIN_ON(x) *##x##.reg |= ##x##.mask
#define PIN_OFF(x) *##x##.reg &= ~##x##.mask

typedef struct {
 char *reg;    // poitner to the register
 char mask;    // bit mask
} tReg;

tReg SDA1 = {&PTADD, PTADD_PTADD0_MASK};   // define register address and particular bit(s)
tReg SDA2 = {&PTADD, PTADD_PTADD1_MASK};

void main(void)
{
  PIN_ON(SDA1);
  PIN_OFF(SDA1);

  PIN_ON(SDA2);
  PIN_OFF(SDA2);
}

Disassembly listing:

  21:  PIN_ON(SDA1);
  0001 c60002   [4]             LDA   SDA1:2
  0004 320000   [5]             LDHX  SDA1
  0007 fa       [3]             ORA   ,X
  0008 f7       [2]             STA   ,X
   22:  PIN_OFF(SDA1);
  0009 c60002   [4]             LDA   SDA1:2
  000c 43       [1]             COMA 
  000d 320000   [5]             LDHX  SDA1
  0010 f4       [3]             AND   ,X
  0011 f7       [2]             STA   ,X
   23: 
   24:  PIN_ON(SDA2);
  0012 c60002   [4]             LDA   SDA2:2
  0015 320000   [5]             LDHX  SDA2
  0018 fa       [3]             ORA   ,X
  0019 f7       [2]             STA   ,X
   25:  PIN_OFF(SDA2);
  001a c60002   [4]             LDA   SDA2:2
  001d 43       [1]             COMA 
  001e 320000   [5]             LDHX  SDA2
  0021 f4       [3]             AND   ,X
  0022 f7       [2]             STA   ,X

 

 Using register macros will generate more efficient code:


void main(void)
{
  PTADD_PTADD0 = 1;
  PTADD_PTADD0 = 0;

  PTADD_PTADD1 = 1;
  PTADD_PTADD1 = 0;
}

Disassembly listing:

  27:  PTADD_PTADD0 = 1;
  0001 1000     [5]             BSET  0,_PTADD
   28:  PTADD_PTADD0 = 0;
  0003 1100     [5]             BCLR  0,_PTADD
   29: 
   30:  PTADD_PTADD1 = 1;
  0005 1200     [5]             BSET  1,_PTADD
   31:  PTADD_PTADD1 = 0;
  0007 1300     [5]             BCLR  1,_PTADD

 

Stanish

 

 

 

 

 

0 Kudos

687 Views
Lundin
Senior Contributor IV

stanish wrote: Using register macros will generate more efficient code:


I believe there is an optimizer setting somewhere making bitwise operators equally efficient. They usually generate BSET / BCLR in my code... although I don't know how I managed to get it that way. They -should- generate equally efficient code, even when they are declared properly with "volatile".


You can't use the "register macros" in a C program anyway, since they don't follow standard C. Even if you find another non-standard compiler allowing 8-bit bitfields, the code would still not be portable, since bitfields aren't portable in the first place.

0 Kudos

687 Views
bigmac
Specialist III

Hello,

 

If you require that each function make use of a choice of SDA pins, you will need to pass this choice to  each of the functions.  Assuming that all the pins are allocated to PTCD, you might have something like the following -

 

#define SDA2  2

#define SDA3  3

 

// Bit manipulation macros:
#define  bset(n,reg)   (reg) |= (1 << (n))
#define  bclr(n,reg)   (reg) &= ~(1 << (n))

 

void Start( byte bitnum)

{

  bset( bitnum, PTCD);

  ...

  ...

  bclr( bitnum, PTCD);

}

 

void main( void)

{

  for ( ; ; );

    Start( SDA2);

    ...

 

    Start( SDA3);

    ...
 

  } 

}

 

Regards,

Mac

 

0 Kudos

687 Views
cicciounico
Contributor III
Thanks again!
0 Kudos

687 Views
bigmac
Specialist III

Hello,

 

Using the preprocessor macro -

 

#define  SdaPin  PTCD_PTCD2

 

should permit SdaPin = 1; to be used.

 

Whenever the symbol SdaPin is encountered, the preprocessor will substitute PTCD_PTCD2.  As it happens, this is also a macro (defined in the derivative header file), so a further substitution will actually take place.

 

Regards,

Mac

 

0 Kudos