Hysteresis in ADC

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
Any body know how to add Hysteresis to an ADC input.

Rich (BB code):
 void Tempcheck(){
 while(1){
     Temp = ADC_Read(2);                   //Read ADC temperature.
   if(temp>=1000){                          //This value determines the overtemp-
     Vout_Rly = 0;                         // and if overtemp program remain-
     Stby_Rly = 0;                         // loop. Switches off all Relay out-
     Lcd_Out(1,1,txt4);                    // until PSU cools down. Flashes -
     Lcd_Out(2,1,txt5);                    // warning msg in this loop
     Delay_ms(500);
     Lcd_Out(1,1,"                ");
     Delay_ms(250);
     Lcd_Out(1,1,txt4);
     if(temp<=200);
     break;

   }
     Delay_ms(500);
     Lcd_Cmd(_LCD_CLEAR);
     Delay_ms(100);
     Stby_Rly = 1;
     break;
  }
}
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.
 

Art

Joined Sep 10, 2007
806
Rich (BB code):
   if(temp>=1000) { //This value determines the overtemp-
     if(temp<=200) { // Do Something}

   }

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.
Rich (BB code):
IF {temp>1000}
WHILE {temp>200}
// turn off relays, turn on fridge, etc.
//bails out when temp is under 201.
} // ENDWHILE
} // ENDIF
 

tshuck

Joined Oct 18, 2012
3,534
Try:
Rich (BB code):
 void Tempcheck(){
 while(1){
     temp = ADC_Read(2);                   //Read ADC temperature.
   if(temp>=1000){                          //This value determines the overtemp-
     Vout_Rly = 0;                         // and if overtemp program remain-
     Stby_Rly = 0;                         // loop. Switches off all Relay out-
     Lcd_Out(1,1,txt4);                    // until PSU cools down. Flashes -
     Lcd_Out(2,1,txt5);                    // warning msg in this loop
     Delay_ms(500);
     Lcd_Out(1,1,"                ");
     Delay_ms(250);
     Lcd_Out(1,1,txt4);
     while(temp>=200){
          temp = ADC_Read(2); 
     }

   }
     Delay_ms(500);
     Lcd_Cmd(_LCD_CLEAR);
     Delay_ms(100);
     Stby_Rly = 1;
  }
}
The breaks were making it leave the while loop...

Actually, I'd suggest you use an interrupt to read the ADC and modify 'temp'...
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
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.
 
Top