Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem with ADC in atmega8 (multimeter)

Status
Not open for further replies.

Kaminari

New Member
Hello.

I've got problem while measuring voltage.
First of all, ADC3 is working perfectly BUT ADC4 and 5 not.

I don't think it's a problem from circuit because I measured voltage on the pin and it was about 1.55V
Voltage ref = 2.73V so it should be about (result/1023) = 0.567. Yet it shows something like 0.005

When I disconnect anything it shows full 1. I think it's a problem with my code or it's optimization by compiler.
I don't think I could destroy ADC inside Atmega because it's basicly one with MUXED input.

THIS code works:
Code:
ADMUX = 192 | 3; //ADC3 with internal reference
		ADCSRA = ADCSRA | (1<<ADSC);	// Start ADC conversion
		while(!(ADCSRA & (1<<ADIF)))	// Wait for AD interrupt flag
			;		
		adResult = ((ADCL) | ((ADCH)<<8));	// Shift high and low to 10-bit	
		voltage = (0.0252113*adResult);

But not the others. Maybe MUX is not 3 2 1 0 but 0 1 2 3 ?
What is wrong here?

Here is my code:
Code:
#include <avr/io.h>
//#include <math.h>	// Needed for trunc function
#include <util/delay.h>
#include <stdlib.h> 
#include <D:\Archiwum\AVR\Projekty\Multi\HD44780.h>

/* Function Definitions */
void ioinit(void);

// SBI and CBI to set/clear bits
#define sbi(port_name, pin_number)   (port_name |= 1<<pin_number)
#define cbi(port_name, pin_number)   ((port_name) &= (uint8_t)~(1 << pin_number))

uint8_t DPStatus = 0x00;
volatile uint8_t resDP = 0;


int main()
{
	float adResult = 0;
	float voltage = 0;
	float current1 = 0;
	float current2 = 0;

	ioinit();

	LCD_Initalize();


	while(1)
	{	// Every loop starts with ADC measurement
		// measurement is processed and display updated
		ADMUX = 192 | 5; //ADC5 with internal reference
		ADCSRA = ADCSRA | (1<<ADSC);	// Start ADC conversion
		while(!(ADCSRA & (1<<ADIF)))	// Wait for AD interrupt flag
			;		
		adResult = ((ADCL) | ((ADCH)<<8));	// Shift high and low to 10-bit	
		current1 = (adResult/1023);

		
		ADMUX = 192 | 3; //ADC3 with internal reference
		ADCSRA = ADCSRA | (1<<ADSC);	// Start ADC conversion
		while(!(ADCSRA & (1<<ADIF)))	// Wait for AD interrupt flag
			;		
		adResult = ((ADCL) | ((ADCH)<<8));	// Shift high and low to 10-bit	
		voltage = (0.0252113*adResult);

		
		

		ADMUX = 192 | 4; //ADC4 with internal reference
		ADCSRA = ADCSRA | (1<<ADSC);	// Start ADC conversion
		while(!(ADCSRA & (1<<ADIF)))	// Wait for AD interrupt flag
			;		
		adResult = ((ADCL) | ((ADCH)<<8));	// Shift high and low to 10-bit	
		current2 = (adResult/1023);

		float mcurrent = (voltage - current2);

		LCD_GoTo(1,0);
		if (voltage <10) LCD_WriteText("0");
		char volt[5] = "     "; 
		dtostrf(voltage,3,3,volt);
		LCD_WriteText(volt);
		LCD_WriteText("V ");

		if (current1 <10) LCD_WriteText("0");
		char amp[5] = "     "; 
		dtostrf(current1,3,3,amp);
		LCD_WriteText(amp);
		LCD_WriteText("A ");

		char mamp[5] = "     ";   
		dtostrf(mcurrent,3,3,mamp);
  		LCD_GoTo(1,1);
		LCD_WriteText("Limit: ");
		LCD_WriteText(mamp);
		LCD_WriteText("A ");
		_delay_ms(50);


	}

	return 0;
}

void ioinit(void)
{

	DDRD = 0xFF;	// Set data direction for port D (A,B,...,G)
	PORTD = 0;	// Initialize all segments off

	DDRB = 0b00000111;	// Set data direction for digits and button (PB5)
	PORTB = 0;	// All digits off, button high

	DDRC = 0b00000000;	// PC0-2 are input, rest output

	/* Set up ADC */
	ADCSRA = (1<<ADEN);	// Enable ADC

}
 
microchip

microchip site wen i last looked had heaps of full working circuits and software for there line of products u could download there script for it then mod it for ur chip
 
I would put a little Capacitator at the Input Pins about 1..10nF should be enough.
After switching the A/D Input Multiplexer wait a little, bit before start A/D Conversation.
That should solve the Problem.

The 100nF Capacitator at the AREF Pin is included and AVCC has power?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top