View previous topic :: View next topic |
Author |
Message |
Valentinus
Joined: 21 Nov 2011 Posts: 6
|
Array index in function parameter |
Posted: Wed Dec 04, 2013 12:39 pm |
|
|
Hi everybody!
I would like to pass an integer type value via function's parameter and I would like to use it to get a long type value from a multidimensional array. This array declared global variable.
Unfortunately this is not working but if I using a constant value instead a variable, the function is working correctly.
This is working
Code: |
// "constans section"
#define T0CON_1ms 8
#define T0CON_10ms 8
#define T0CON_50ms 1
#define T0CON_100ms 2
#define T0CON_250ms 4
#define T0CON_500ms 5
#define T0CON_1s 6
//
#define T0TIME_1ms 0xEC78
#define T0TIME_10ms 0x3CB0
#define T0TIME_50ms 0x0BDC
#define T0TIME_100ms 0x0BDC
#define T0TIME_250ms 0x6769
#define T0TIME_500ms 0x6769
#define T0TIME_1s 0x6769
//
#define T0_1ms 0
#define T0_10ms 1
#define T0_50ms 2
#define T0_100ms 3
#define T0_250ms 4
#define T0_500ms 5
#define T0_1s 6
//
long T0_Parameters[7][2] = {
{T0CON_1ms, T0TIME_1ms},
{T0CON_10ms, T0TIME_10ms},
{T0CON_50ms, T0TIME_50ms},
{T0CON_100ms, T0TIME_100ms},
{T0CON_250ms, T0TIME_250ms},
{T0CON_500ms, T0TIME_500ms},
{T0CON_1s, T0TIME_1s}
};
void Setup_Timer0(void)
{
// set T0CON register
setup_timer_0(T0_Parameters[T0_1ms][0]);
// set TIMERH-TIMERL regiszters
set_timer0(T0_Parameters[T0_1ms][1]);
}
void main(void)
{
Setup_Timer0();
}
|
But if I try to use this code, the result is not as expected.
Code: |
// "constans section is the same as the previous"
int intIndex = T0_1ms;
//
void Setup_Timer0(int index)
{
// set T0CON register
setup_timer_0(T0_Parameters[index][0]);
// set TIMERH-TIMERL regiszters
set_timer0(T0_Parameters[index][1]);
}
//
void main(void)
{
Setup_Timer0(intIndex);
}
|
Anybody can help me? Thank you in advance and sorry for my bad english.
... I'm newbie. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Dec 04, 2013 1:07 pm |
|
|
Define "not as expected"
Is it not working at all?
Is it working but with a wrong crazy value?
Is the wrong value one of the possible values but not the right one?
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Dec 04, 2013 3:06 pm |
|
|
The standard debugging technique would insert a 'print variable' statement to visually confirm the correct values(s) are being transferred.
Once you're satisfied all is good, then delete the 'print variable' statement.
hth
jay |
|
|
Valentinus
Joined: 21 Nov 2011 Posts: 6
|
|
Posted: Wed Dec 04, 2013 3:37 pm |
|
|
Gabriel wrote: | Define "not as expected"
Is it not working at all?
Is it working but with a wrong crazy value?
Is the wrong value one of the possible values but not the right one?
G. |
The question is good. I forgot to write it.
The source codes are compiled successful but the values are incorrect. I'm tested it in PIC 18 Simulator IDE.
When I use the "constans" version the T0CON, TIMER0H and TIME0L registers are setting correctly. But if I use the "variable" version these registers are not sets correctly.
Perhaps the tester is not to able to test it properly... or something.
I had no time to test real-world environment.
I think I did something wrong but I don't know what. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Dec 04, 2013 3:51 pm |
|
|
PIC18?... thats a different compiler...
Simulator?... read the first sticky post about Proteus (a simulator)
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Valentinus
Joined: 21 Nov 2011 Posts: 6
|
|
Posted: Wed Dec 04, 2013 4:29 pm |
|
|
Gabriel wrote: | PIC18?... thats a different compiler...
Simulator?... read the first sticky post about Proteus (a simulator)
G. |
I wrote the code in CCS but tested in other simulator.
I just wanted to see the register values. This simulator is enough for me.
temtronic, thank you the idea, I'll try it. |
|
|
Valentinus
Joined: 21 Nov 2011 Posts: 6
|
|
Posted: Wed Dec 04, 2013 5:42 pm |
|
|
The problem is solved! It looks like that the simulator software includes one or more bugs. I tested the project in the MPLAB X's own simulator with printf function and looks like that the values are correct.
Thank you for help ;)
Best regards: Valentinus |
|
|
|