ADC0 and ADC1 are not working

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

Hello, I need to use 2 ADC inputs, but when I see the Serial monitor, The value of the ADC0 is represented in the variable of the ADC1.  

void casos(){
	

	g++;
	
	switch(g) {
		

		case 1:
		ADMUX= 0B01000000;		//MISMO ADMUX DEL LAB2
		ADCSRA= 0B10000111;		//MISMO ADCSRA DEL LAB2
		ADCSRB= 0;				//MISMO ADCSRB DEL LAB2
		DIDR0 |=(1<<ADC0D);		//DEFINE EL PIN ADC0 COMO ENTRADA PARA LM35
		
		
		ADCSRA|=(1<<ADSC);

		ValorTemperatura=ADC;
		Temperatura_Serial=ADC;
			
		envia_cadena_usart(array);//mensaje a enviar al ordenador
			
			
		char string1[8];

		itoa(Temperatura_Serial, string1, 10);  //convert integer to string, radix=10
		envia_cadena_usart(string1);
	
		
		break;

		case 2:
		
	
		ADMUX= 0B01000001;		//MISMO ADMUX DEL LAB2
		ADCSRA= 0B10000111;		//MISMO ADCSRA DEL LAB2
		ADCSRB= 0;				//MISMO ADCSRB DEL LAB2
		DIDR1 |=(1<<ADC1D);		//DEFINE EL PIN ADC1 COMO ENTRADA PARA SENSOR HUMEDAD TIERRA
		
		ADCSRA|=(1<<ADSC);
		
		ValorHumedad=ADC;
		Humedad_Serial=ADC;
				
		envia_cadena_usart(array2);
				
		char string2[8];
		itoa(Humedad_Serial, string2, 10);  //convert integer to string, radix=10
		envia_cadena_usart(string2);
		
		condiciones();
		

		break;
	

		
		g=0;

		
	}
}

I have this routine, but the ADC1 is not working. Any solution?

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

You never wait for the ADC to finish! You need to poll the ADSC bit to see when the adc has finished.

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

Kartman wrote:
You never wait for the ADC to finish! You need to poll the ADSC bit to see when the adc has finished.

 

		case 2:
		
		if(ADSC==1)
		{
	
		ADMUX= 0B01000001;		//MISMO ADMUX DEL LAB2
		ADCSRA= 0B10000111;		//MISMO ADCSRA DEL LAB2
		ADCSRB= 0;				//MISMO ADCSRB DEL LAB2
		DIDR1 |=(1<<ADC1D);		//DEFINE EL PIN ADC1 COMO ENTRADA PARA SENSOR HUMEDAD TIERRA
		
		ADCSRA|=(1<<ADSC);
		
		ValorHumedad=ADC;
		Humedad_Serial=ADC;
				
		envia_cadena_usart(array2);
				
		char string2[8];
		itoa(Humedad_Serial, string2, 10);  //convert integer to string, radix=10
		envia_cadena_usart(string2);
		
		condiciones();
		}
		

		break;

Is it correct?

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

Kartman wrote:
You never wait for the ADC to finish! You need to poll the ADSC bit to see when the adc has finished.

Or this?

 

void casos(){
	

	g++;
	
	switch(g) {
		

		case 1:
		ADMUX= 0B01000000;		//MISMO ADMUX DEL LAB2
		ADCSRA= 0B10000111;		//MISMO ADCSRA DEL LAB2
		ADCSRB= 0;				//MISMO ADCSRB DEL LAB2
		DIDR0 |=(1<<ADC0D);		//DEFINE EL PIN ADC0 COMO ENTRADA PARA LM35
		
		
		ADCSRA|=(1<<ADSC);
		
		while (ADCSRA&(1<<ADSC))
		{
				ValorTemperatura=ADC;
				Temperatura_Serial=ADC;
				
				envia_cadena_usart(array);//mensaje a enviar al ordenador
				
				
				char string1[8];

				itoa(Temperatura_Serial, string1, 10);  //convert integer to string, radix=10
				envia_cadena_usart(string1);
		}

	
	
		
		break;

		case 2:
		
		
	
		ADMUX= 0B01000001;		//MISMO ADMUX DEL LAB2
		ADCSRA= 0B10000111;		//MISMO ADCSRA DEL LAB2
		ADCSRB= 0;				//MISMO ADCSRB DEL LAB2
		DIDR1 |=(1<<ADC1D);		//DEFINE EL PIN ADC1 COMO ENTRADA PARA SENSOR HUMEDAD TIERRA
		
		ADCSRA|=(1<<ADSC);
		while (ADCSRA&(1<<ADSC))
		{
			ValorHumedad=ADC;
			Humedad_Serial=ADC;
			
			envia_cadena_usart(array2);
			
			char string2[8];
			itoa(Humedad_Serial, string2, 10);  //convert integer to string, radix=10
			envia_cadena_usart(string2);
			
			condiciones();
			
			
		}
		
		

		break;
	

		
		g=0;

		
	}
}

 

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

You can keep on guessing or you can Google the solution in 10 seconds.

Here's what I found:

uint16_t adc_read(uint8_t ch)
{
  // select the corresponding channel 0~7
  // ANDing with ’7′ will always keep the value
  // of ‘ch’ between 0 and 7
  ch &= 0b00000111;  // AND operation with 7
  ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing
 
  // start single convertion
  // write ’1′ to ADSC
  ADCSRA |= (1<<ADSC);
 
  // wait for conversion to complete
  // ADSC becomes ’0′ again
  // till then, run loop continuously
  while(ADCSRA & (1<<ADSC));
 
  return (ADC);
}

Modify to suit your requirements.

 

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

Kartman wrote:

You can keep on guessing or you can Google the solution in 10 seconds.

Here's what I found:

uint16_t adc_read(uint8_t ch)
{
  // select the corresponding channel 0~7
  // ANDing with ’7′ will always keep the value
  // of ‘ch’ between 0 and 7
  ch &= 0b00000111;  // AND operation with 7
  ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing
 
  // start single convertion
  // write ’1′ to ADSC
  ADCSRA |= (1<<ADSC);
 
  // wait for conversion to complete
  // ADSC becomes ’0′ again
  // till then, run loop continuously
  while(ADCSRA & (1<<ADSC));
 
  return (ADC);
}

Modify to suit your requirements.

 

 

Thanks.