ADC not switching channels?

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include 
#include 
#include 
char volatile passes,GPLF,TF,cycles,TCharge;

int main()
{

char TS;	//TF=tappet flag, TS=tappet sensor, GPL=gearbox position loop flag

DDRD  |= 0x40;		//set PD6 as output (motor drive)
DDRB  |= 0x02;		//set PB1 as output (motor brake)

PORTD &= ~0x40;		//turns off PD6(motor drive)
PORTC &= ~0x40;		//turns off PC6(motor brake)
PORTC |= 0x07;		//pullup PC0,1,2
PORTD |= 0xBF;		//pullup PD0,1,2,3,4,5,7
PORTB |= 0x05;		//pullup PB0,2

ADCSRA |= 0x06;  	//ADC Prescalar set to 64 - 312.5kHz@20MHz 
ADMUX  |= 0x60;		//changes ADC to 8-bit resolution, read from ADCH, sets Vref to AVCC		
ADCSRA |= 0x80;  	//enable ADC


	while(1)		//main loop
	{	
		ADMUX |= 0x03;
		ADCSRA |= (1 <<ADSC);			//starts ADC conversion	
		while( ADCSRA&_BV(ADSC) );	//wait for conversion complete		
				if(ADCH > 132)				//sets value for Tappet Sensor
				{	
					TCharge = 1;
				}

				while(TCharge)
				{	//A
					if( !(PINC&_BV(PINC2)) )		//checks PINC2 value and turns on drive fet
					{								//and sets the GPLF flag
						PORTD |= (1 << 6);
						GPLF = 1;
					}

					ADMUX |= 0x04;					//PC4(BB detection), PC5(Tappet Position Sensor)
					while( GPLF == 1 || !(PINC&_BV(PINC2)) )	//start of cycle counting loop
					{	//B
						ADCSRA |= (1 <<ADSC);			//starts ADC conversion
						while( ADCSRA&_BV(ADSC) );	//wait for conversion complete		
							if(ADCH <= 10)				//sets value for Tappet Sensor
							{
								TS=1;	
							}		
							else
							{
				  				TS=0;	
							}

							if(TF != TS)				//increments cycles each time tappet sensor is true
							{
								if(TS)
									passes = passes + 1;
									TF=TS;
							}						

							if(passes == 3)				//this segment breaks the loop after 1 full gearbox cycle 
							{							//is completed unless PINC2 is still low, it also increments 
								passes=0;				//cycles and checks to see if GPLF flag should be set to 0
								cycles = cycles + 1;	//which breaks the main cycle loop
									if( PINC&_BV(PINC2) )
									{
										GPLF = 0;
									}
									if( cycles == 5)
									{
										TCharge = 0;
										cycles = 0;
									}
							}	
					}	//B

					if( !GPLF )							//this segment turns of the drive fet and turns on the break,
					{	//C									//waits for 10ms and then turns off the break.
						PORTD &= ~(1<< 6);
						PORTC |= (1 << 6);	
						_delay_ms(10);
						PORTC &= ~(1<< 6);
					}	//C

				}	//A
	}	
}

This is the program, which works absolutely perfectly if I comment out the following:

		ADMUX |= 0x03;
		ADCSRA |= (1 <<ADSC);			//starts ADC conversion	
		while( ADCSRA&_BV(ADSC) );	//wait for conversion complete		
				if(ADCH > 132)				//sets value for Tappet Sensor

Yet when I use that ADC channel, and the input reads high enough for TCharge to be one, the output stays high, which means its not switching channels when I write ADMUX |= 0x04;, I don't understand why this is happening, any ideas?

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

You always only set bits in ADMUX, never clear some. So after the first loop run ADMUX is fixed and don't change further.

Stefan Ernst