CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

using #define on multiple pins

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jameszum



Joined: 31 Jan 2012
Posts: 14

View user's profile Send private message

using #define on multiple pins
PostPosted: Tue Sep 27, 2016 1:30 pm     Reply with quote

Good Morning
I am working on a project where I have a number of control lines going off to a number of modules. It would be nice if I were able to put the control lines in n array such as

Code:
#define port_select[8]  {PIN_F13,PIN_B14,PIN_H6,PIN_F2,PIN_A14,PIN_A4,PIN_H14,PIN_D13};

is this possible?
coding would be something like
Code:
for i=0; i<9; i++) output_high(port_select[i]) ;

Thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 27, 2016 2:32 pm     Reply with quote

I don't have the PCD compiler, but I made a test program for an 18F PIC.
Code:

#include <18F8722.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)

int16 port_select[8] = {PIN_F3,PIN_B4,PIN_H6,PIN_F2,PIN_A5,PIN_A4,PIN_H4,PIN_D3};

//======================================
void main(void)
{
int8 i;

for(i=0; i<8; i++)
   {
    output_high(port_select[i]); 
   }

while(TRUE);
}


Now look at the for() loop in the .LST file:
Code:

.................... for(i=0; i<8; i++) 
0009A:  CLRF   14
0009C:  MOVF   14,W
0009E:  SUBLW  07
000A0:  BNC   00E2
....................    {
....................     output_high(port_select[i]);   
000A2:  BCF    FD8.0
000A4:  RLCF   14,W
000A6:  CLRF   03
000A8:  ADDLW  04
000AA:  MOVWF  FE9
000AC:  MOVLW  00
000AE:  ADDWFC 03,W
000B0:  MOVWF  FEA
000B2:  MOVFF  FEC,16
000B6:  MOVF   FED,F
000B8:  MOVFF  FEF,15
000BC:  MOVFF  15,17
000C0:  MOVLW  01
000C2:  MOVWF  18
000C4:  MOVLW  0F
000C6:  MOVWF  1A
000C8:  MOVLW  89
000CA:  MOVWF  19
000CC:  RCALL  0004
000CE:  MOVFF  15,17
000D2:  CLRF   18
000D4:  MOVLW  0F
000D6:  MOVWF  1A
000D8:  MOVLW  92
000DA:  MOVWF  19
000DC:  RCALL  0004
000DE:  INCF   14,F
000E0:  BRA    009C


That's not all of it. Here's the routine at address 0x00004 that it calls:
Code:
00004:  MOVF   17,W
00006:  ANDLW  07
00008:  MOVWF  00
0000A:  RRCF   17,W
0000C:  MOVWF  01
0000E:  RRCF   01,F
00010:  RRCF   01,F
00012:  MOVLW  1F
00014:  ANDWF  01,F
00016:  MOVF   01,W
00018:  ADDWF  19,W
0001A:  MOVWF  FE9
0001C:  MOVLW  00
0001E:  ADDWFC 1A,W
00020:  MOVWF  FEA
00022:  CLRF   01
00024:  INCF   01,F
00026:  INCF   00,F
00028:  BRA    002C
0002A:  RLCF   01,F
0002C:  DECFSZ 00,F
0002E:  BRA    002A
00030:  MOVF   18,F
00032:  BZ    003A
00034:  MOVF   01,W
00036:  IORWF  FEF,F
00038:  BRA    0040
0003A:  COMF   01,F
0003C:  MOVF   01,W
0003E:  ANDWF  FEF,F
00040:  RETURN 0

So indirect addressing of i/o pins is incredibly inefficient and time-consuming.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue Sep 27, 2016 2:35 pm     Reply with quote

No.

Just use a constant array.
Code:

const unsigned int16 port_select[8]
= {PIN_F13,PIN_B14,PIN_H6,PIN_F2,PIN_A14,PIN_A4,PIN_H14,PIN_D13};


#defines are not code or variables. They are 'text substitution macros' all done at compile time/ you can substitute in 'values', but not pick parts of them like this.

However the pin 'names', themselves are just #defines, and are really just numbers.

Also 'beware'. A 'output_high' using a variable, is a lot larger/slower than the output_high on a constant value. You'll probably find it is smaller and quicker to just have the 8 values hard coded.....

After posting, I see PCM_Programmer also pointed this out, while I was typing. Smile
jameszum



Joined: 31 Jan 2012
Posts: 14

View user's profile Send private message

PostPosted: Tue Sep 27, 2016 2:40 pm     Reply with quote

Thank you
jameszum



Joined: 31 Jan 2012
Posts: 14

View user's profile Send private message

PostPosted: Tue Sep 27, 2016 2:43 pm     Reply with quote

Thank You
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group