Defining an Array of constants

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
sujoy
Level 2
Level 2
10 replies posted 5 replies posted 5 sign-ins

Hi,

I have a program with PSOC5 where I have a multichannel ADC. I want to be able to configure the zero error and gain of each channel individually so that I can use the below code.

for(i=0; i<No_of_ADC_Channels;i++)

{     Actual_ADC_Reading [i]  = (Instant_ADC_Reading[i] + Zero_Error[i])*Gain_x100[i]/100;    }

------------------------------

And have a definition such that 

Zero_Error[0]=100;

Zero_Error[1]=30;

Zero_Error[2]=-70;

.... and so on.

I have been simply using variable arrays to store Zero_Error and Gain_x100 but I am sure that there is a better way to do this.

Obviously I can't use #define.

 

0 Likes
1 Solution
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

sujoy,

If you already know the 'constants' for each channel, you should use the following lines

#define No_of_ADC_Channels  16 //  Enter the # of channels here
const uint16 Zero_Error[No_of_ADC_Channels] = {100, 30, -70, ..., 128};
const uint16 Gain_x100[No_of_ADC_Channels] = {1, 1, 2, ..., 4};

The 'const' in the line defines that the array is FLASH.   Therefore the memory is only changeable at design-time.

A more unified method of coding is to incorporate both variables in a struct array.  Example:

#define No_of_ADC_Channels  16 //  Enter the # of channels here
/* Here is the struct definition that combines the two correction factors */
typedef struct
{
   const uint16 Gain_x100;
   const uint16 Zero_Error;
} gain_offset_s;

gain_offset_s ADC_corrections[No_of_ADC_Channels] = 
{ /* Gain_x100, Zero_Error */
  {          1, 100 },
  {          1, 30},
  {          2, -70},
  ...
  {          4, 128},
};

/* Example use in a function */
function()
{
uint16 gain;
uint16 zero;
...
   gain = ADC_corrections[channel].Gain_x100;
   zero = ADC_corrections[channel].Zero_Error;
...
}

If you need to calibrate each channel for every unit, then you need to at least store the data in RAM.   In this case, you should take the code lines above and remove the 'const'.   It will place the arrays in SRAM.  You can even use the = {...} values to be default values.

 I assume you have functions that can executed to calibrate the gain and offset of each channel.

Once you have SRAM array variables as you currently allocate, you may want to use the PSoC5 EEPROM to store these arrays.   That way if you lose power or get a watchdog reset, the offset and gain values can be reloaded into the SRAM arrays.

Len
"Engineering is an Art. The Art of Compromise."

View solution in original post

0 Likes
1 Reply