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 support@ccsinfo.com

Array index in function parameter

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



Joined: 21 Nov 2011
Posts: 6

View user's profile Send private message

Array index in function parameter
PostPosted: Wed Dec 04, 2013 12:39 pm     Reply with quote

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? Smile Thank you in advance and sorry for my bad english.

... I'm newbie. Smile
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Dec 04, 2013 1:07 pm     Reply with quote

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: 9161
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 04, 2013 3:06 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 04, 2013 3:37 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 04, 2013 3:51 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 04, 2013 4:29 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 04, 2013 5:42 pm     Reply with quote

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
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