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

constant or integer

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



Joined: 19 May 2018
Posts: 27

View user's profile Send private message

constant or integer
PostPosted: Mon Jul 27, 2020 7:54 am     Reply with quote

Code:

int voltages[] = {0x52,0x61,0x86,0xC1};

// Is this the correct way to declare this ?

FOR (x=0;x < 4; ++x ) { 
   volts = voltages[x] ;
     
   a=read_adc();
}

It's simplified down, but volts is an 8-bit port and the For next loop changes value on that port. hould voltages be declared as integer or constant ?

Thank You.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Jul 27, 2020 8:18 am     Reply with quote

const is not a declaration. It is a modifier to a declaration.
You can't declare a variable as a 'const', it has to be declared as a
'int const' (or const int'). If you are ever going to change the value,
then it can't be a const int. If however this is a set of 'fixed' values,
then declaring these as 'const int', means that RAM won't be used for
these values.

What you post has no sign of you actually putting voltages into the
array. I'd have expected to see something like:
Code:

int voltages[4];

for (x=0;x < 4; ++x )
{
    set_adc_channel(x);
    delay_us(20);
    voltages[x]=read_adc();
}


Which would then result in the voltages array being loaded with the
four readings from adc_channel 0 to 3. Would make a lot more sense...
gtx15



Joined: 19 May 2018
Posts: 27

View user's profile Send private message

PostPosted: Mon Jul 27, 2020 8:53 am     Reply with quote

The program actually works. I probably simplified it too much.
Code:

int voltages[] = {0x52,0x61,0x86,0xC1};   // for switching port b outputs
int results[4]

  FOR (x=0;x < 4; ++x ) {   // voltage changes on the pins.
   volts = voltages[x] ;        // volts is port b
    a=read_adc();
    results[x] =a;
}

So int voltages never change. Should "const int voltagess[] = {0x52,0x61,0x86,0xC1}"
be used instead to free up ram?
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jul 27, 2020 9:43 am     Reply with quote

comment
If you need to save 4 bytes of RAM, you've really got a serious issue !!

While you could play tricks and store as ROM data, the overhead of reading them and then sending to the portb could create a larger program.

I'd suggest a larger 'family' member of the PIC you're using, if possible, to get more RAM.

This. below saves those 4 bytes of RAM though..

Now if these 4 bytes are never going to change,
then something like ....
Code:

volts=0x52;   //set portb for 1st selection
results[0]=read_adc;  //get adc and store
volts=0x61;
results[1]=read_adc;

2 more to code.....

Without knowing more about what is needed, it's hard to comment further.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Jul 27, 2020 9:46 am     Reply with quote

Yes.

So in fact then these are output values fed to the port. However in this
case, why copy the value into 'volts'?.
Just use the value directly from the array.
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jul 28, 2020 5:50 am     Reply with quote

I'm curious to know what the design of the project is.
You configure PORTB, read ADC 4x.
So I'm wondering what is attached to PORTB and why the specific bit patterns.

Jay
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