Tiny 13 ADC

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

I have a small problem
When I do a single ADC conversion I get the previous selected Cannel as result.
Does someone know why.
I am using a Tiny 13 @ 9.6 MC and this is the code that I use.

int get_adc(unsigned char cannel)
{
// 00 ADC0 (PB5)(pin 1)
// 01 ADC1 (PB2)(pin 7)
// 10 ADC2 (PB4)(pin 3)
// 11 ADC3 (PB3)(pin 2)

ADMUX  = ( 1 << ADLAR) | cannel ;  					// VCC used as analog reference.  ADC Left Adjust Result
ADCSRA = ( 1 << ADEN)  | ( 1 << ADSC ) | 0x06 ;		// ADC Enable, ADC Start Conversion ADC Prescaler Select Bits Division Factor 64
while ( ( ADCSRA & ( 1 << ADIF )) == 0 ); 			// Wait untill adc is ready 
return ADCW;
}


int main()
{
unsigned char w;
unsigned char c;
PORTB	= 0x00;
DDRB	= 0x01; //
MCUCR  = ( 1 << SE ) ;  //  Sleep Enable Idle
TCCR0B = ( 1 << CS01) | ( 1 << CS00) ; // clkI/O/64 (From prescaler)
TIMSK0 = ( 1 << TOIE0);

// The CKDIV8 Fuse determines the initial value of the CLKPS bits. 
CLKPR  = 0x80;
CLKPR  = 0x00; // Set devide by 1 Hi Speed 9.6 MC

MCUCR	= ( 1<< PUD) | (1 << SE ) | (1 << SM1) ;  // PULLUP DISABLE, Sleep Enable Power-down

for(;;)
{
	PutHellow();
	c= GetChar();
	switch(c)
	{	
		case 'A':
		PutHex16(get_adc(1));
		break;
		case 'B':
		PutHex16(get_adc(2));
		break;
		case 'C':
		PutHex16(get_adc(3));
		break;
		default:
		PutHex16(get_adc(1));
		PutHex16(get_adc(2));
		PutHex16(get_adc(3));
		break;
	}
}
return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
while ( ( ADCSRA & ( 1 << ADIF )) == 0 );          // Wait untill adc is ready 

You're checking for ADIF, but you never clear it. I suggest using ADSC instead:

while ( ADCSRA & ( 1 << ADSC )) /*EMPTY*/;         // Wait until conversion is done