ADC bug? This may be silly

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

If this is a dumb error on my part, I apologize. I am varying the voltage to an pinput pin (PORT C bit 0) and when the ADC reads 0xff I light an LED. Big deal! Should work right?

It is very unreliable. Sometimes it works, sometimes it does not. Mostly the LED is ON and I cannot get it go off. Hmmm..... Can anyone spot the bug? I surely wish I could see it.

unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
ADCSRA|=0x40; // Start the AD conversion
while ((ADCSRA & 0x10)==0); // Wait for the AD conversion to complete
ADCSRA|=0x10;
return ADCH;
}

void main(void)
{
unsigned char i;
InitHardware(); //Init IO ports

for(;;)
{
i=read_adc(PINC.0); //ADC input PORTC pin 0

if(i == 0xff)
{
PORTB =0xff;
}
else
PORTB=~0xff;

}
}

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

I don't quite understand this part :

i=read_adc(PINC.0); //ADC input PORTC pin 0

You don't tell us the compiler you are using. But I think that PINC.0 is the digital value of pin 0 of the PORT C, which may value 0 or 1. If PC0 is analog channel 0, then you need to use :

i=read_adc(0);

.

Regards,
Alejandro.
http://www.ocam.cl

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

Thank you!!!!!!!!

You are right. I was using PINC.0 which reads the "digital" value. I changed the code to i=read_adc(0); It works now. Thanks for saving me time.