ADC on ATmega328 keeps running.

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

Looking for some pointers to why the ADC on a Atmega328 is in 'free running mode' although the ADATE bit is zero (disabled).

The code uses a timer to wake up every 0.25 a second and read a voltage on a adc input. The rest of the time is spent doing other stuff or sleeping.

The adc is working as expected except it will NOT stop running continiously reading NEW valves and waiting the uC up.

Even through the ADC is never started by setting ADSC to one it runs constantally.

ISR(ADC_vect, ISR_BLOCK) {
	 adc_sample.adc_val[adc_sample.adc_number] = ADCH;
	 adc_state |= (1<<ADC_COMP);
	 adc_state &= ~(1<<ADC_RUNNING);
}

ISR(TIMER1_COMPA_vect, ISR_BLOCK) {
	if (!(adc_state & (1<<ADC_RUNNING)))
	    adc_get(&adc_sample);
}

/*
 *
 */
static void adc_init() {
		
	ADMUX = (1 << REFS0) | (1 << ADLAR);					// Avcc = Avref, lefty
	ADCSRA = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) |	// adc clk = cpu clk / 128
				(1 << ADEN) | (1 << ADIE);					// enable a2d, enable interrupt
	DIDR0 = (1<<ADC0D) | (1<<ADC1D) | (1<<ADC2D);			// disable digital ports on adc inputs
	
	// ADCSRA &= ~(1<<ADATE); // STOP that ADC !!!!!!
	// ADCSRA |= (1 << ADSC); // start first conversion
}

static void adc_get(struct adc_data *adcd) {

    /* Trigger Capture */
	// ADCSRA |= (1<<ADSC);
	return;
}

The brain is hurting from reading the Atmega328[^p] data sheet so may times so any pointers would be great.

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

Perhaps show a bit more of your app. In particular for me, I'd like to see the mystery definitions (e.g. ADC_RUNNING) and the sleep code.

Are you perhaps using the noise-reduction mode? That will trigger a conversion when you go to sleep.

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
ADC_COMP, ADC_RUNNING

How are these defined?

Regards,
Steve A.

The Board helps those that help themselves.

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

theusch wrote:
Perhaps show a bit more of your app. In particular for me, I'd like to see the mystery definitions (e.g. ADC_RUNNING) and the sleep code.

Are you perhaps using the noise-reduction mode? That will trigger a conversion when you go to sleep.

I think your comments about noise-reuction mode has hit the nail on the head. I skipped over this section in the data sheet as was not using it. Now reading it I see that entering idle (like me) or ADC Noise Reduction mode automatically triggers the ADC start and there is no flag to disable it other than turning off the ADC before sleeping.

I've attached a simplied version of the code which is showing the problem. The ADC_RUNNING/ADC_COMP flags are just used to keep track where in the cycle we are.

Thanks, your a star! Will report back later then i get a chance to try it.

Attachment(s): 

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

Yeap, that fixed the problem. Most read the datasheet again and again and ....