Skip to main content
Mohan1
Associate III
September 12, 2020
Solved

How to detect max value of real time data in an array

  • September 12, 2020
  • 8 replies
  • 5422 views

I have connected a sine wave of amplitude 3V with 1.5V shifted above the zero to 12-bit ADC channel by using dsPIC33FJ32MC204 controller and stored in an array. I want to detect the max value out of five samples because after the five samples my data is repeating, so please give me any suggestion on it. I am getting output as a zero.

0693W000003RTlMQAW.png

This topic has been closed for replies.
Best answer by sanju

If(I<arrayLenght)

{

} This case is to fill the array with adc values

When array will be filled completely then

If(I==arrayLength)

{

} This section will find the max value

Because until the array is not completed no need to check for max value​

​

The code you want to write will never fill the array completely because

Each time i will be 0​

​

You have take another variable for the loop to do by your way

​

​array[i] =adc_value;​

     

     for(j=0;j<8;j++)

  {

     if(array[j] > max)

       {

     max = array[j];

     }

sprintf(data1,"%.2f",max);

LCD_String_xy(1,1,data1);

max=0;

     

  }

i++;​

     if(i == 8)

    {

      i = 0;

    }

This will work​

8 replies

sanju
Associate III
September 12, 2020

#define arrayLength 5

unsigned int array[arrayLength];                                //Array values should be updated with ADC values.

unsigned int i, max = 0;

void main()

{

for(i=0;i<sizeof(array)/sizeof(array[0]);i++)

{

if(array[i] > max)

{

max = array[i];

}

}

//here after the loop completion you can get the maximum value in variable max

}

You can find max number in a array by this way.

Mohan1
Mohan1Author
Associate III
September 14, 2020

@sanju​ Thanks for your quick response sir and your logic is working properly and getting max value accurately but facing one problem that as data is real time ,so how can i take infinite data in array means how can i update array values with ADC values?

Thanks in advance.

sanju
Associate III
September 14, 2020

First of all, you can't store infinite data in array because RAM size of your device is 2 KB. so you have to define arrayLength

#define arrayLength 5(you have to set it first).

suppose you have this define it as 8

then code like this

#define arrayLength 8

unsigned int array[arrayLength];                                //Array values should be updated with ADC values.

unsigned int i=0, max = 0;

void main()

{

while(1)

{

if(i<arrayLength )

{

//Read adc value and assign in array

array[i] = adc_value;

i++;

}

if(i == arrayLength )

{

for(i=0;i<sizeof(array)/sizeof(array[0]);i++)

{

if(array[i] > max)

{

max = array[i];

}

}

//here after the loop completion you can get the maximum value in variable max

i=0;

//Read adc value and assign in array

array[i] = adc_value;

i++;

}

// do your other tasks here

//Put some delay if required between adc samples.

}

}