Hysteresis in ADC

Discussion in 'Programmer's Corner' started by R!f@@, Apr 28, 2014.

  1. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,740
    759
    Any body know how to add Hysteresis to an ADC input.

    Code ( (Unknown Language)):
    1.  void Tempcheck(){
    2.  while(1){
    3.      Temp = ADC_Read(2);                   //Read ADC temperature.
    4.    if(temp>=1000){                          //This value determines the overtemp-
    5.      Vout_Rly = 0;                         // and if overtemp program remain-
    6.      Stby_Rly = 0;                         // loop. Switches off all Relay out-
    7.      Lcd_Out(1,1,txt4);                    // until PSU cools down. Flashes -
    8.      Lcd_Out(2,1,txt5);                    // warning msg in this loop
    9.      Delay_ms(500);
    10.      Lcd_Out(1,1,"                ");
    11.      Delay_ms(250);
    12.      Lcd_Out(1,1,txt4);
    13.      if(temp<=200);
    14.      break;
    15.  
    16.    }
    17.      Delay_ms(500);
    18.      Lcd_Cmd(_LCD_CLEAR);
    19.      Delay_ms(100);
    20.      Stby_Rly = 1;
    21.      break;
    22.   }
    23. }
    This my over temp loop.
    I think I still have issues.

    Loop is still breaking. I don't think break is applied correctly.

    I like to know how I can add hysteresis to ADC (2) value.
    I like remain in the loop until say ADC is below 200.

    The above code works but ADC is set at 1000.
    The part " if(temp<=200);" does not work.
     
  2. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    Code ( (Unknown Language)):
    1.  
    2.    if(temp>=1000) { //This value determines the overtemp-
    3.      if(temp<=200) { // Do Something}
    4.  
    5.    }
    6.  

    The condition less than or equal to 200 can never be met
    because it had to be greater than 1000 to enter the outer IF/THEN.

    You could use another WHILE.
    Code ( (Unknown Language)):
    1.  
    2. IF {temp>1000}
    3. WHILE {temp>200}
    4. // turn off relays, turn on fridge, etc.
    5. //bails out when temp is under 201.
    6. } // ENDWHILE
    7. } // ENDIF
    8.  
     
    R!f@@ likes this.
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Try:
    Code ( (Unknown Language)):
    1.  void Tempcheck(){
    2.  while(1){
    3.      temp = ADC_Read(2);                   //Read ADC temperature.
    4.    if(temp>=1000){                          //This value determines the overtemp-
    5.      Vout_Rly = 0;                         // and if overtemp program remain-
    6.      Stby_Rly = 0;                         // loop. Switches off all Relay out-
    7.      Lcd_Out(1,1,txt4);                    // until PSU cools down. Flashes -
    8.      Lcd_Out(2,1,txt5);                    // warning msg in this loop
    9.      Delay_ms(500);
    10.      Lcd_Out(1,1,"                ");
    11.      Delay_ms(250);
    12.      Lcd_Out(1,1,txt4);
    13.      while(temp>=200){
    14.           temp = ADC_Read(2);
    15.      }
    16.  
    17.    }
    18.      Delay_ms(500);
    19.      Lcd_Cmd(_LCD_CLEAR);
    20.      Delay_ms(100);
    21.      Stby_Rly = 1;
    22.   }
    23. }
    The breaks were making it leave the while loop...

    Actually, I'd suggest you use an interrupt to read the ADC and modify 'temp'...
     
    R!f@@ likes this.
  4. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,740
    759
    I was trying for 3 hours after posting. I posted when I got exhausted.
    After posting I thought of sleeping but this thing was going through my mind more than my madam next to me.
    So got up and tried and was able to fix it completely.

    I used flag bits for threshold . Now threshold can be set just by changing the ADC value and further more the break and one logic operator was wrong.

    I did it without interrupts.

    All the functions are working perfectly. With only one setback.

    I still need to figure out how to assign RAM to msg.

    will post about tht separately.
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You will Normally put constants like this in the ROM. Do waste your RAM on this
     
  6. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,740
    759
    Look for my new thread about this problem
     
Loading...