built-in ADC


Using the built-in 10-bit ADC

Toolkit:AVR Development System

Location:/bipom/devtools/WinAVR/minimaxavrc/Examples/Labs/Lab05

Code Example


int main(void)
{
	unsigned char channel;
	float voltage;
	unsigned int val;
	int ndx;

	uart0Init(19200);

	adcInit();
	
	//	DIDR0  = 0x04;	//AD0..AD3 - analog inputs

	//	DIDR1  = 0x80;	//AD15 - analog input

	
	while(1)
	{
		for (channel = ADC_CHANNEL_0 ;channel <= ADC_CHANNEL_4; channel++)
		{
			for (ndx =0; ndx <8; ndx++)
				val = adcRead(channel);
				
			voltage = (((float)val) * VREF) 1023.0;
			uart0Printf ("\n\rCh[%u]=",channel);
			adc_print_voltage(voltage);
			uart0Printf ("(%04u) ",val);
		}		
		_delay_ms(1000);
		uart0Printf ("\n\r");
	}
}

void adc_print_voltage ( float voltage)
{
	unsigned long vInt;
	voltage = voltage * 100;
	vInt = (unsigned long) voltage;
	uart0Printf("%u",vInt100);
	uart0Printf(".");
	uart0Printf("%02u",vInt%100);
	uart0Printf("V");
}