ADC interrupt

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

So I'm working on a big project using a atMega2560 but am trying to get the AVR ADC interrupt to work before I try to implement anything into my other project. For some reason I'm getting weird values and I can't figure out why. I am developing in AtmelStudio but wrote a mockup of what I am trying to do in the Arduino IDE just to get the code to initially work like a trial run. In the end I am going to have 7 thermistors hooked up to A0-A7. I will have the interrupt save the measured value and change the mux to read each value thermistor. Once all are read I will do a temp check of all 7 at once to check the temperatures are safe using a lookup table. Since temp will change slowly the process doesn't need to be fast. but currently in this code I'm just trying to read the A7 value while having the pin hooked up to a potentiometer. For some reason all I am seeing printed into the monitor are either a 1 or 0.  

 

here is the code:

 

#include <avr/interrupt.h>

 

volatile int x;
volatile bool newVal;

 

ISR(ADC_vect)
{
   x = ADC;
   newVal = true;
}

void setup() {
   Serial.begin(115200);
   
//   DDRF &= 0x00; //set A0-A7 to input
   
   // Set ADC reference to AVCC and mux to A7
   ADMUX |= (1 << REFS0) | (1 <<MUX2) | (1 << MUX3) | (1<<MUX0); 
 

   // Enable ADC, Enable interrupt, set ADC prescalar to 128, and Auto Triger Enable
   ADCSRA |= (1 << ADEN) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) | (1 << ADATE); 

 

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

   sei(); // enabe global interrupts
}

void loop() {
  if(newVal)
  {
    Serial.println(x);
    newVal = false;
  }
}

This topic has a solution.

Last Edited: Sat. Jul 11, 2015 - 12:39 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First hint - you posted in the UC3 forum while your question is about Mega2560.  A moderator will probably move it.

 

The common error is to not specify variables to be shared between ISR and main() as volatile, but you have done that.

 

I think to look at A7, you would want MUX0, MUX1, and MUX2 but you have MUX0, MUX2, and MUX3

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

Sorry about posting in the wrong forum. Thanks for your help Jim. That's super embarrassing that the problem was that I typed a 3 instead of a 1. That was driving me crazy for a more time than I feel comfortable admitting.  

Last Edited: Thu. Jul 9, 2015 - 09:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I read your code, it will do only one conversion and report the results.

 

IME the results of the first conversion should always be discarded.  (The datasheet is a bit more particular, talking about "changing the reference".)  You may well get a result quite a bit "off" on the first conversion right after setting up the ADC and picking a reference source.

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

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Topic locked