I am using PIC18F452 and MikroC.
My doubt is not specifically related to this controller.
I am trying to make a program, in which I have to continuously monitor the temperature. Controller do the calculations as normal work and Whenever there is a change by (+ or -)2degree (or 2 units) the controller must notify or do certain specified task and come back to normal work.
How do I do that. Please tell me the logic.
I thought of writing the program in timer interrupt and the timer will check for the change whenever the timer flag overflows. but that doesn't help as what if each time the timer overflows and temperature increase by 2 degree and decrease by 1 degree (in that case I controller will not detect it).
I thought of connecting the temperature input to INT1 but there also that change is not detectable.
It is something like whenever there is 2degree change it will take that as interrupt. I know that if it is in the ISR till then it will not detect any further change but even then I didn't found a way to do that.
I don't believe that the problem is primarly related to interrupts or would be solved using interrupts.
Temperatures are varying rather slowly compared to code execution time, even in a really fast heater zone. So all you need is a piece of code to be executed repeatedly, usually in the main loop. In some cases, a timer or ADC interrupt may trigger the code, but that doesn't mean that the code has to be executed in an interrupt context.
Yes, 2 degree change in temperature will take some time and only a few milli seconds is needed to check if current temperature is 2 degrees more than previous temperature value. ISR is not needed. You can just set a flag if temperature has risen 2 degrees or more and then set a flag and execute the code needed if flag is set. The thing is the prevVal should not be changed with new value unless the difference between currVal and prevVal is 2 or more degrees.
Void interrupt ()
{
if(TMR1IF_bit)
{
//set clear necessary flags etc.
if(T_present >= T_previous+2)
{
//do some some calculations etc. CAL1
// T_previous=T_current
}}}
void main()
{
// set flags, timer priorities, T1CON, INTCON etc.
//start timer
while(1)
{
//do something CAL2
}
}
In this what I am doing is, it will check for temperature is changed by 2units whenever the timer flag is raised, till then it will be working in main.
also the code in main (outside this condition) is also very large.
So if I do that than the time between checking the condition twice is very large. So the change between these two checks will not be detected.
[[ what I am doing is, I am saving all the temperature values and then trying to predict the next approximate change so the more faster the check is the more better it is.
Obviously, we cant overcome the hardware constraints but we can make it more accurate by programming in such a way that it will check the condition as many times and also the other calculations will run.]]