Issue in two integer array comparison

Discussion in 'Embedded Systems and Microcontrollers' started by ecaits, May 7, 2014.

  1. ecaits

    Thread Starter Member

    Jan 6, 2014
    52
    0
    Dear Sir,

    I have stored setpoint value in EEPROM memory in by keypad and I want to compare setpoint value with process value. Process value is stored in one array, say data[5]. I read the setpoint value from EEPROM and again stored in another array, say presetvalue[5]. I am using below logic to compare the both value but it is not working properly. I have used RC0 pin, which is connected to LED. When PV is higher than SV then RC0 pin will be high so LED should be glow.


    for(i=0;i<5;i++) // To read the setvalue from EEPROM
    {
    value=eeprom_read(i);
    presetvalue=value; // Stored setvalue in presetvalue[5] array
    }

    for(i=0;i<5;i++) // Comparison logic for setvalue and process value

    {
    if(data > presetvalue) // When PV>SV, RC0 pin high
    {
    RC0=1;
    i=5;
    }


    else if(data==presetvalue) // If equal then compare next bit (From MSB
    to LSB)
    { RC0=1; }


    else if(data < presetvalue) // When PV<SV, RC0 pin low
    {
    RC0=0;
    i=5;
    }
    }

    Plz suggest me where may be problem.

    If there is another good logic, then plz suggest me.

    Thanks in advance,
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    How are these values loaded? You may be having a big/little endian issue.

    In what way is it not working?
     
    Last edited: May 7, 2014
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    The way you set up the Comparison loop has it only check the zero element.

    If it is > or < then i=5 and you drop out. Only when they are equal do you go onto the next element.

    Is that OK or do you want to check all 5 elements?

    In the future please use the code tag button ( the # symbol) so your code looks more like this:

    Code ( (Unknown Language)):
    1. for(i=0;i<5;i++) // To read the setvalue from EEPROM
    2. {
    3.  value=eeprom_read(i);
    4.  presetvalue[i]=value; // Stored setvalue in presetvalue[5] array
    5. }
    6. for(i=0;i<5;i++) // Comparison logic for setvalue and process value
    7. {
    8.  if(data[i] > presetvalue[i]) // When PV>SV, RC0 pin high
    9.  {
    10.   RC0=1;
    11.   i=5;
    12.  }
    13.  else if(data[i]==presetvalue[i]) // If equal then compare next bit (From MSB to LSB)
    14.  { RC0=1; }
    15.  
    16.  else if(data[i] < presetvalue[i]) // When PV<SV, RC0 pin low
    17.  {
    18.   RC0=0;
    19.   i=5;
    20.  }
    21. } [/i][/i][/i][/i][/i][/i][/i]
     
Loading...