0
\$\begingroup\$

I'm using atmega32 µc with the internal 10-bit ADC. I connected my potentiometer to the ADC 1 and I'm using 5 V Voltage Reference.

using the following code, I did the ADC-config

void ADC_init(void)
{

    //Turn on the ADC
    ADCSRA |= 1<<ADEN;   // Turn On ADC (Enable ADC)

    //ADMUX  |= 1<<ADLAR ;  // Config shifting to the left (10-bit), otherwise it is right adjusted

    ADMUX  |= 1<<REFS0 ;  // Config VRef (external Vref in this case)
    ADMUX  |= 1<<MUX1 ;  // ADC-Channel 1

//  ADCSRA |= 1<<ADIE;   // Enable ADC Interrupt
    ADCSRA |= 3<<ADPS0;  // Assign ADC-prescaler 128

    ADCSRA |= 1<<ADSC;   //start the the measure conversion
}

Then in Main function I am calling the following function each 1000ms and send the data to the USART interface

void ADC_measure(void)
{
    _delay_ms(50);

    // if the ADC right adjusted
    theLow = ADCL;  // ADC Value Low
    _delay_ms(50);


    ADC_TenBit = ADCH<<8 | theLow  ;  // because of lift adjustment of ADC

    _delay_ms(50);

    sprintf( ADCBuffer, "The ADCValue is : %d \n", ADC_TenBit );

    _delay_ms(50);

    usart_pstr(ADCBuffer);

    _delay_ms(50);
    //start the the measure conversion
    ADCSRA |= 1<<ADSC;
}

The problem is : when I open the terminal , I see only the ADC Value = 255 or 248 increasing or decreasing the potentiometer value doesn't change the ADC value.

what can I do now?

\$\endgroup\$
  • \$\begingroup\$ You're using the pot as a voltage divider, right? \$\endgroup\$ – Ignacio Vazquez-Abrams Sep 7 '14 at 15:59
  • \$\begingroup\$ Why not use the ADC interrupt? I have no idea if your conversion is finished when you're reading the result register, because you're not even checking if it's done -- you're just delaying some arbitrary amount of time, and blindly reading the register. There are two ways I typically see ADC code implemented: using interrupts, or burning time in a while loop waiting for the "conversion finished" flag to be set. \$\endgroup\$ – Jay Carlson Sep 7 '14 at 16:18
2
\$\begingroup\$

You have incorrect multiplexer setting. To select channel 1, it should be

ADMUX  |= 1<<MUX0 ;  // ADC-Channel 1

Edit: Another thing is incorrect clock prescaler. To get \$f_{clk}/128\$:

ADCSRA |= 7<<ADPS0;  // Assign ADC-prescaler 128
\$\endgroup\$
  • \$\begingroup\$ thanks for your reply. but it still have the same problem \$\endgroup\$ – user3213767 Sep 7 '14 at 17:05
1
\$\begingroup\$

Try:

ADC_TenBit = (uint16_t)ADCH<<8 | theLow;

And you did declare ADC_TenBit uint16_t, right?

\$\endgroup\$
  • \$\begingroup\$ thanks for your reply. it works for the first measurement then again fails. the values are 255 or 290 or 245 ...just in this range. independent of the input Signal \$\endgroup\$ – user3213767 Sep 7 '14 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.