|
|
View previous topic :: View next topic |
Author |
Message |
ricperez
Joined: 25 Apr 2007 Posts: 14
|
Problem with indexed variable |
Posted: Tue Jul 14, 2015 11:34 am |
|
|
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
|
|
Posted: Tue Jul 14, 2015 12:21 pm |
|
|
Out[1] != Out1 |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: Problem with indexed variable |
Posted: Wed Jul 15, 2015 2:06 am |
|
|
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
|
Thanks gaugeguy and RF_Developer |
Posted: Wed Jul 15, 2015 10:03 am |
|
|
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.
|
|
|
|
|
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
|