Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

atmega 8+ analog to digital with proteus

Status
Not open for further replies.

mshh

Full Member level 6
Full Member level 6
Joined
May 30, 2010
Messages
349
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
usa
Visit site
Activity points
3,871
I want to make analoge to digital on atmega8 using avrstudio i wrote code but wheen i simulate on proteus the Leds are always high and didn't change
please help
Code:
#ifndef F_CPU
#define F_CPU 8000000UL // or whatever may be your frequency
#endif

#include <avr/io.h>
#include <util/delay.h>

void adc_init(void)
{
	ADCSRA=0x85; //ENABLE ADC, PRESCALER 128
	ADMUX=0x00; 		//PC0, AVcc AS REFERENCE VOLTAGE
}

uint16_t adc_read()
{
	adc_init;
	while((ADCSRA)&(1<<ADSC)); 	//WAIT UNTIL CONVERSION IS COMPLETE
	return(ADC); 		//RETURN ADC VALUE
}

int main(void)
{
	
	uint16_t adc_value;
    DDRD=0xff;
	
	while(1)
	{
				
         adc_init(); 		//INITIALIZE ADC
		adc_value=adc_read(0); 		//READ ADC VALUE FROM CHANNEL 0
	    PORTD=ADC;
		
	}
}
 

post your schematic

- - - Updated - - -

Code:
ADCSRA=0x85; //ENABLE ADC, PRESCALER 128

That doesn't set the prescaler to 128 but 32

Code:
ADMUX=0x00; 		//PC0, AVcc AS REFERENCE VOLTAGE

Also a misleading comment, this sets the Vref to the voltage connected to AREF

I don't see anywhere the needed code to start a conversation, you only initialize the ADC over and over again.
You need to use
Code:
 // Start the AD conversion
    ADCSRA |= 0x40;
 

Code:
#ifndef F_CPU
#define F_CPU 8000000UL // or whatever may be your frequency
#endif

#include <avr/io.h>
#include <util/delay.h>

void adc_init(void)
{
	ADCSRA=0x85; //ENABLE ADC, PRESCALER 32
	ADMUX=0x00; 		//PC0, AVref AS REFERENCE VOLTAGE
}

uint16_t adc_read()
{
	adc_init;
	while((ADCSRA)&(1<<ADSC)); 	//WAIT UNTIL CONVERSION IS COMPLETE
	return(ADC); 		//RETURN ADC VALUE
}

int main(void)
{
	
	uint16_t adc_value;
    DDRD=0xff;
	
	while(1)
	{
				
         adc_init(); 		//INITIALIZE ADC
		  // Start the AD conversion
		  ADCSRA |= 0x40;
		adc_value=adc_read(0); 		//READ ADC VALUE FROM CHANNEL 0
	    PORTD=ADC;
		
	}
}
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    113.6 KB · Views: 604

There is no point to call adc_init over and over again, just call it once at the start of main (out of the while loop) and you are done.

Also move DCSRA |= 0x40 inside adc_read

Your ADC result is stored in adc_value, why do you use PORTD=ADC ?

AVCC should also be connected to +5, that is the ADC supply
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top