ADC interrupts

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Freaks,

I am using a M48 and gcc with AVRStudio and internal clock. I have an NTC thermistor from Sparkfun (spec. is attached). I have written this simple ADC code:


#include 
#include 
#include 

int main (void)
{
   
   DDRB = 0xFF;
  
   ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) | (1 << ADATE); // Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz

   ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
   ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading

   // No MUX values needed to be changed to use ADC0

   ADCSRA |= (1 << ADEN);  // Enable ADC

   ADCSRA |= (1 << ADIE);  // Enable ADC Interrupt
   sei();   // Enable Global Interrupts

   ADCSRA |= (1 << ADSC);  // Start A2D Conversions

   for(;;)  // Loop Forever
   {
   }
}

ISR(ADC_vect)
{
   if(ADCH < 180) //temperature is > 65 deg C
   {
    
      PORTB = 0x01; // Turn on LED2
   }
      
   else // LED normally off for room temperature
   {
      
      PORTB = 0x00; // Turn off LED2
   }
}

I am using the ADC channel 0 (PC0) in interrupt mode according to the tutorial. In the ISR, I am checking if the ADC value is less than 180(which translates to 65 deg C). I have calculated this value based on the NTC spec. and the temperature sensing tutorial by Guillem.(see attached Excel spreadsheet)

Also attached is a schematic. Basically, I am using a potential divider topology with ratiometric reference for my ADC.

Here is how I tried it: I put a heated soldering gun near my NTC and hoped to see the LED glow on PB0 but I did not see anything. I have already checked the NTC with a multimeter and the soldering gun and it works just fine. (resistance decreases with temp. increase)

I also tried to turn on LED in the else part of the ISR to check if the ISR was firing or not and I did NOT see the LED glow. This probably indicates that the ISR is not firing.

Why would that be? Is there something I am missing in ADC initialization?

Appreciate any help.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You forgot this (see the data sheet):
DIDR0 = (1 << ADC0D);

You should read the data sheet section 21.6.1 Analog Input Circuitry.

Quote:
The ADC is optimized for analog signals with an output impedance of approximately 10 kΩ or less. ....
There is an ADC tutorial (select ALL and search for "free run"):
https://www.avrfreaks.net/index.p...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok I tried that and it didn't work.

I also tried this:


ISR(ADC_vect)
{

 PORTB = 0x01;
}

And that did not work either. In my circuit, I do have a 10K in series with the ADC0 pin so I am satisfying the 10K impedance condition for the ADC input.

I think my ADC interrupt is not working. I guess I need to step back and check if the ADC is working without interrupts.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you sure your LED is good and not in reverse? Not that I have ever done this. Also since you have all portb as an ouput try turning on the whole port int the ISR and use your volt meter and see if you get a reading.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I checked the LED and it is in right.

I will try with PORTB = 0xFF but I am also going to try replacing the NTC by a 10K so I will get 2.5V going in to the ADC0 pin. This will be half the supply so I will be checking for 511 in my infinite loop.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok that has worked. The biasing resistor was wrong. Now when I hold the NTC with my fingers, the LED is OFF but when I remove my finger, then the LED glows.(detecting heat on my finger)
But now it's working with interrupts and my ISR is firing.
Thanks for your help,guys.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

another nice thing that i like to do is hysteresis with the LED.

simply replace the ISR code with

if (ADCH < 180) 		//temperature is > 65 deg C
{
	PORTB |= 0x01; 		// Turn on LED2 
}
else if (ADCH > 185)	// temp < ?? deg c
{
	PORTB &= 0xFE;		// Turn off LED2
}

this will stop the LED from flashing and doing crazy stuff when right at the 65 deg c mark :) not needed but may prove useful to you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, nedward.

Right now I think my NTC is very slow to respond to changes in temperature anyway. I will certainly try it if I get a faster NTC.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

my NTC is very slow

???

Why would that be? AFAIK the device will "instantly" show the characteristics that reflect the temperature that the device is at.

I don't do blowtorches, but in every one of my temperature measuring/controlling apps it is a slow-moving signal. Some mass (gas, liquid, solid) is changing temperature. There are varying degrees of coupling of the temperature sensor to this mass. And I guess it does take the thermistor a bit of time to reach the same temperature as its coupled mass. Not very long when properly coupled--it will change virtually just as fast as the mass.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It may be. I made that statement based on the fact that when I power my circuit on, I see the LED glow which it should when it is at room temperature (I have it coded that way). But when I hold the NTC with two fingers, the LED switches off temporarily till I have my fingers on it, which is correct too. When I remove my fingers from the NTC (so it is no longer monitoring what I think is my body temperature), it takes a while (about a second) for the LED to glow again. So when you mean coupled mass, is it (in this case) the atmosphere of the entire room?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
When I remove my fingers from the NTC (so it is no longer monitoring what I think is my body temperature), it takes a while (about a second) for the LED to glow again.

Of course it does. If you place a hot cup of coffee on the counter does it cool to room temperature instantaneously? The thermistor registers the temperature of the thermistor, not the temperature of what is around the thermistor.

Regards,
Steve A.

The Board helps those that help themselves.