Maker Pro
Maker Pro

help with my atmega16 ADC conversion

aishiqi

Jan 4, 2011
9
Joined
Jan 4, 2011
Messages
9
i want 10 bit precision of ADC with my code below.but the problem is that the result does not change Continuously while i change the voltage continuously with two Precision potentiometer.
do you konw what i mean??

for example :

now ADCL is 0x56.i turn the potentiometer slowly, but ADCL doesn't change immediately .i keep turning, suddenly ADCL changes to 0x60,then 0x62,then0x63,then0x71;it can't be 0x57,0x58,0x59,0x61...........


i hope someone can help me!! thanks a lot first .


this is my code:

#include<iom16v.h>

unsigned long angular,adth,adtl;
unsigned char count=0,adjok=0;
unsigned long speed=0;
unsigned long temp;
adinit()
{
ADMUX=0xc0;
ACSR=0x80;
ADCSRA=0xc8;
}
uinit()
{
UBRRL=51;
UCSRB|=0x88;
SREG|=0x80;

}

portinit()
{
DDRA=0;
PORTA=0x00;
}

delay(){
unsigned int i;
for(i=0;i<100;i++);
}
sendchar(char ch)
{
while(!(UCSRA&0x20));
UDR=ch;}

adjust()
{SREG&=0x7f;
speed=0;
angular=0;
count=0;
adjok=0;
SREG|=0x80;
}

#pragma interrupt_handler adcsvr:15
adcsvr()
{
adtl=ADCL;
adth=ADCH;

ADCSRA=0xc8;
//sendchar(adth);
sendchar(adtl);

angular=angular+((adth<<8)+adtl)*1000-speed;

if(adjok==0)
{
if(count<255) count++;
else
{adjok=1;
speed=angular/256;
}
}

}

main()
{
MCUCR=0x00;
uinit();
portinit();
adinit();
delay();
adjust();
while(1)
{
/*sendchar(adtl);
sendchar(angular>>24);
sendchar((angular>>16)&0xff);
sendchar((speed>>8)&0xff);
sendchar(speed&0xff);
delay();
*/


}

}
 

JoeyAVR

Aug 25, 2011
16
Joined
Aug 25, 2011
Messages
16
When debugging an ADC always start off by not doing any mathematical cleverness, just display the raw data. That way you can be sure what you're getting.

Very often ADC errors aren't errors - its the voltage you're measuring that isn't what you expect it to be!

What hardware are you using. Have you copied the example layouts and decoupling in the data sheet?

Is it possible that there is a jump in pot values due to dirt or construction? Measure the wiper voltage with a DVM at the same time and see if it really does jump.

Joey
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Do you have the pot connected across the power supply and the wiper connected to the ADC pin? (i.e. not connected to just one supply rail)

What is the resistance of the pot? (is it too high?)

+1 to the DMM and the raw data.
 

duke37

Jan 9, 2011
5,364
Joined
Jan 9, 2011
Messages
5,364
I was involved with a large data logger some time ago which had a similar problem. A large capacitor was charged up and the voltage measured as it slowly discharged. The measured value came down in steps with a resolution of about 8 bits.

The problem was digital interference into the analogue input. Make sure that the digital and analogue circuits have independant grounds.
 
Top