Forum: Mikrocontroller und Digitale Elektronik Problem using Noise Reduction Int for ADC Mega328P


von Josef M. (josefm)


Lesenswert?

Yes, I know just another entry with ADC but I did not find a hint
for my problem in the forum yet.

I have a Problem reading an adc value with ATMega328P in noise reduction
mode ( with a Interrupt Service routine ).
Because reading per polling the ADSC Bit is OK, I get the right value 
that way, I suppose the error has to do with the initialization of ADC 
in
my function EnableADCInt(... ).
The ISR is definitely not called !

adcValue in ISR is volatile in my header.

Where is the missing bit ?



By the way:
This forum is very helpful for me. I found already many important
hints.


Here the code:

void EnableADCInt(uint8_t channel)
{
    // Clear PRADC to startup ADC
    PRR &= ~(1<<PRADC);  // Power Reduction Register

    ADMUX = 0;
    ADMUX |= (1 << REFS1)  |  (1 << REFS0);
    ADMUX |=   channel;
    ADCSRA =  ( 1 << ADPS2)  |  ( 0 << ADPS1) | ( 1 << ADPS0) ;

  // Für Interrupt bei Free Running ?
  // ADCSRA |= ( 1 << ADATE);

  // Free running mode !?
  // ADCSRB = 0;

  // sleep Mode
    set_sleep_mode(SLEEP_MODE_ADC);

  // Bit 3 – ADIE: ADC Interrupt Enable
  // Bit 7 – ADEN: ADC Enable
  // Bit 6 – ADSC: ADC Start Conversion   but not for noise reduction ?
    ADCSRA |= (1 << ADEN)  |  (1 << ADIE);

  // enable Global Int
    sei();
}

void DisableADCInt()
{
  // disable ADC Int
  // Bit 3 – ADIE: ADC Interrupt Disable
  // Bit 7 – ADEN: ADC Disable
  // Bit 6 – ADSC: ADC Stop Conversion
  ADCSRA = 0;

  PRR |= (1<<PRADC);
}

// Oversampling to improve the value
int16_t GetADCTemp(uint8_t channel)
{
  EnableADCInt(channel);
  sleep_cpu();  // throw away first reading

  adcCount=0;
  adcValue=0;
  while( adcCount < 64 )
  {
     adcCount++;
     sleep_cpu();
  }

  DisableADCInt();

  int16_t  ticksresult = (adcValue >> 6);
  return ticksresult;
}

ISR(ADC_vect)
{
  adcValue =  ADC;   // low + high byte
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.