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

Problem with indexed variable

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



Joined: 25 Apr 2007
Posts: 14

View user's profile Send private message

Problem with indexed variable
PostPosted: Tue Jul 14, 2015 11:34 am     Reply with quote

Hi. I´m trying to make this code run on PIC16F1827, PCWH 4.099:
No warnings or errors during compilation ocurred.
This is the relevant code, beyond clock setting and fuses,
Code:

int i;
int Out[15];

#define Out15   PIN_A0
#define Out1   PIN_A1
#define Out2   PIN_A2
#define Out3   PIN_A3
#define Out4   PIN_A4
#define Solo_in   PIN_A5
#define Out5   PIN_A6
#define Out6   PIN_A7
#define Out7   PIN_B0
#define Out8   PIN_B1
#define Out9   PIN_B2
#define Out10   PIN_B3
#define Out11   PIN_B4
#define Out12   PIN_B5
#define Out13   PIN_B6
#define Out14   PIN_B7

main
{
    for (i=1;i<=10;++i)
      {
         output_high(Out[i]);
         delay_ms(500);
      }

    for (i=1;i<=10;++i)
      {
         output_low(Out[i]);
         delay_ms(500);
      }

}

I never get any output turning on. Can anyone help me to spot what i´m doing wrong?

Thanks a lot.
gaugeguy



Joined: 05 Apr 2011
Posts: 303

View user's profile Send private message

PostPosted: Tue Jul 14, 2015 12:21 pm     Reply with quote

Out[1] != Out1
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: Problem with indexed variable
PostPosted: Wed Jul 15, 2015 2:06 am     Reply with quote

To get your main() to work as written, you would need to define Out[] like this:
Code:

int16 Out[16] = { PIN_A0, PIN_A1, PIN_A2, PIN_A3, PIN_A4, PIN_A5, PIN_A6, PIN_A7, PIN_B0, PIN_B1, PIN_B2, PIN_B3, PIN_B4, PIN_B5, PIN_B6, PIN_B7 };


The array needs to be int16, as that is the (barely documented) type of the pin constants - int is too small. With the array as int, you are talking to the wrong pins; if any pins at all. There need to be sixteen elements as that's how many you define. The elements need to be initialised to the values. The array can be const if you aren't going to change it.

Using variables, even const variables, for the bit output calls is very slow and gives bulky code. Its much better to always use constants. One way of doing this is with a switch statement.
ricperez



Joined: 25 Apr 2007
Posts: 14

View user's profile Send private message

Thanks gaugeguy and RF_Developer
PostPosted: Wed Jul 15, 2015 10:03 am     Reply with quote

I appreciate your inputs.
As you can see I'm not a highly experienced developer, though am not a novice either.
Your suggestion fully make sense to me and I'll try it at once.
I managed to solve the issue by addressing the bit numbers which are consecutive to handle the outputs per 16F1827.h file (starting at 96 up to 111), which is not a very flexible nor elegant solution but did work.
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