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

Assign pin names to an array

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



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

Assign pin names to an array
PostPosted: Fri May 29, 2020 2:06 pm     Reply with quote

Selamün Aleyküm (Hello)
There's a question I'm dealing with.
I name the pins as seen in the code.
AND I am throwing these names into a series.
However, I did not get any results.
If I solve this problem, I get rid of writing a 1000 line code. (Really 1000 lines. I tried)
How can I do it ?
Thank you
Code:

#DEFINE p1 pin_d7
#DEFINE p2 pin_d6
#DEFINE p3 pin_d5
#DEFINE p4 pin_d4
#DEFINE p5 pin_c1
#DEFINE p6 pin_c0
#DEFINE p7 pin_a6
#DEFINE p8 pin_a7

int1 array_p[8]={p1,p2,p3,p4,p5,p6,p7,p8};

for(int i=0;i<9;i++)
   {
   
   output_high(array_p[i]);
   delay_ms(100);
   }
gaugeguy



Joined: 05 Apr 2011
Posts: 303

View user's profile Send private message

PostPosted: Fri May 29, 2020 2:25 pm     Reply with quote

Your loop is trying to access 0-8 but your array only has 0-7, so that is a problem.

Here would be my solution to what you post, untested so it could have an error:

Code:

#DEFINE p1 pin_d7
#DEFINE p2 pin_d6
#DEFINE p3 pin_d5
#DEFINE p4 pin_d4
#DEFINE p5 pin_c1
#DEFINE p6 pin_c0
#DEFINE p7 pin_a6
#DEFINE p8 pin_a7

void PinHigh(int8 offset)
{
  if(offset < 8)
  {
  switch(offset)
  {
    case 0:
      output_high(p1);
      break;
    case 1:
      output_high(p2);
      break;
    case 2:
      output_high(p3);
      break;
    case 3:
      output_high(p4);
      break;
    case 4:
      output_high(p5);
      break;
    case 5:
      output_high(p6);
      break;
    case 6:
      output_high(p7);
      break;
    case 7:
      output_high(p8);
      break;
    }
  }
}

main()
{
 while(TRUE)
 {
  for(int8 i=0;i<8;i++)
  {
    PinHigh(i);
    delay_ms(100);
  }
 }
}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 29, 2020 2:47 pm     Reply with quote

What's your PIC ? It makes a difference here.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat May 30, 2020 12:15 am     Reply with quote

As PCM_Programmer says chip type matters. Pin names are always bigger
than an int1. If you look at the prototypes in the device header you will
see the size needed. So for example:

_bif void output_low(int16 pin);

So the pin name here needs an int16. Most chips do (all?). A few of the
smaller PIC10/12/16's can get away with an int8, but only a very few....

So:
Code:

#DEFINE p1 pin_d7
#DEFINE p2 pin_d6
#DEFINE p3 pin_d5
#DEFINE p4 pin_d4
#DEFINE p5 pin_c1
#DEFINE p6 pin_c0
#DEFINE p7 pin_a6
#DEFINE p8 pin_a7

const int16 array_p[8]={p1,p2,p3,p4,p5,p6,p7,p8};
//Note int16. Also unless you are changing them at run time
//why not use const?

for(int i=0;i<8;i++) //0...7 not 0...8
   {
   
   output_high(array_p[i]);
   delay_ms(100);
   }
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Sat May 30, 2020 1:23 pm     Reply with quote

Thank you gaugeguy, PCM programmer, Ttelmah.
Firstly, the proposal of 'gaugeguy' is correct. It must be for (i = 0; i <8; i ++). i wrote <9 by mistake.
However, I will develop this application in 32 pins. Therefore, the switch / case will extend the process. I have to do it with an array.

Secondly I use pic18F46k22.

Finally, using int16 instead of int1 solved the problem. Thank you.
Problem solved before 24 hours Elhandülillah.

Very Happy
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun May 31, 2020 2:03 am     Reply with quote

Good. Smile

If you think about it the variable has to hold the 'address' of the pin
(the actual number is 8* the physical register address, plus a 3bit 'bit
number'), not the 'bit' being returned from that pin. It then makes sense
that it has to be a lot bigger than an int1... Very Happy
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Sun May 31, 2020 4:12 am     Reply with quote

Yes. You are right. As I learn about them, the desire to do more projects is awakening. Very Happy
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