View previous topic :: View next topic |
Author |
Message |
gtx15
Joined: 19 May 2018 Posts: 27
|
constant or integer |
Posted: Mon Jul 27, 2020 7:54 am |
|
|
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: 19520
|
|
Posted: Mon Jul 27, 2020 8:18 am |
|
|
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
|
|
Posted: Mon Jul 27, 2020 8:53 am |
|
|
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: 9229 Location: Greensville,Ontario
|
|
Posted: Mon Jul 27, 2020 9:43 am |
|
|
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: 19520
|
|
Posted: Mon Jul 27, 2020 9:46 am |
|
|
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: 9229 Location: Greensville,Ontario
|
|
Posted: Tue Jul 28, 2020 5:50 am |
|
|
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 |
|
|
|