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.

Does anyone know on how to initialize ADC on ATMEGA128 ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Visit site
Activity points
9,442
Hello guys,

Does anyone know on how to initialize ADC on ATMEGA128 ?
Any references will be appreciated,

thank you
 

I tried to init like this :
Code:
void adc_init()
{
	// enable ADC, select ADC clock = F_CPU / 128 (i.e. 125 kHz)

	ADCSRA = (1<<ADEN | 1<<ADPS2 | 1<<ADPS1 | 1<<ADPS0 );

	//Do a conversion to get rid of rubbish

	ADMUX=(1<<REFS0 | ADC_7);                  //Conversion on channel 7, thermistor input
	//Internal VCC Voltage Reference
	ADCSRA |= (1<<ADSC);                      //Start conversion
	loop_until_bit_is_clear(ADCSRA, ADSC);    //Wait for conversion complete
}


uint8_t read_adc(void)
{
	ADMUX=(1<<REFS0) | (1<<ADLAR) | ADC_0;                  // Conversion on channel 0, AVCC reference, 10 bit mode
	ADCSRA |= (1<<ADSC);                     // Start conversion
	loop_until_bit_is_clear (ADCSRA, ADSC);         // Wait for conversion complete
	return(ADCH);
}
.
.
main
{

	char volts[50];
	uint8_t  adc_result=0;
	double adcA=0.0;
 adc_result=read_adc();
			  adcA = (int)(adc_result*5.0)/1024.0;
			  if (adcA != 0)
			   {
				  sprintf(volts,"%0.2f",adcA);
				  lcd_string(volts);
				  _delay_ms(1000);
			   }
			   else
			   {
				     lcd_string("No Result!");
					 _delay_ms(2000); 
			   }
}

I got "?" in my LCD, do I miss something here ?

I connect to ADC like this :
adc.jpg

R1 = Trimpot 10 K
R2 = 4K7

Vin connected to PF0 or ADC0 on atmega 128,

anyone has ideas ?

thanks
 

My complete code, please correct me if I'm wrong,
How to change custom option on configuration on AVR Studio 6 ?

Thank you
Code:
/*
 * ADC_test.c
 *
 * Created: 24/05/2013 2:01:57 PM
 *  Author: Antonius
 */ 
//Mention Clock frequency here
#define _XTAL_FREQ 8000000
#define F_CPU 8000000UL  // 8 MHz

#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>


// structure to allow bit field operations, name conversions: PORTA.0 -> PORT_A.b0  PORTB.7 -> PORT_B.b7
typedef struct{ uint8_t b0:1;
	uint8_t b1:1;
	uint8_t b2:1;
	uint8_t b3:1;
	uint8_t b4:1;
	uint8_t b5:1;
	uint8_t b6:1;
uint8_t b7:1; } bits;

// define all the ports of your microcontroller, add more ports depending on the available mcu ports
#define PORT_D (* (volatile bits *) &PORTD)
#define PIN_D (* (volatile bits *) &PIND)
#define DDR_D (* (volatile bits *) &DDRD)

#define PORT_G (* (volatile bits *) &PORTG)
#define PIN_G (* (volatile bits *) &PING)
#define DDR_G (* (volatile bits *) &DDRG)

#define lcd_data_pin PORTA

#define en PORT_D.b0
#define rs PORT_D.b1
#define rw PORT_D.b2
#define relay1 PORT_D.b3
#define relay2 PORT_D.b4

// //ADC definitions
#define   ADC_0 0
#define   ADC_1 1
#define   ADC_2 2
#define   ADC_3 3
#define   ADC_4 4
#define   ADC_5 5
#define   ADC_6 6
#define   ADC_7 7

#define ADC_VREF_TYPE 0x00

void lcd_init();
void lcd_data(unsigned char data1);
void lcd_cmd(unsigned char cmd);
void lcd_control(unsigned char cmdordata);
void lcd_string(unsigned char *str);
void adc_init(void);
uint16_t read_adc(void);




void adc_init()
{
	// enable ADC, select ADC clock = F_CPU / 128 (i.e. 125 kHz)

	ADCSRA = (1<<ADEN | 1<<ADPS2 | 1<<ADPS1 | 1<<ADPS0 );

	//Do a conversion to get rid of rubbish

	ADMUX=(1<<REFS0 | ADC_0);                  //Conversion on channel 0, thermistor input
	//Internal VCC Voltage Reference
	ADCSRA |= (1<<ADSC);                      //Start conversion
	loop_until_bit_is_clear(ADCSRA, ADSC);    //Wait for conversion complete
}


uint16_t read_adc(void)
{
	ADMUX=(1<<REFS0) | (1<<ADLAR) | ADC_0;                  // Conversion on channel 0, AVCC reference, 10 bit mode
	ADCSRA |= (1<<ADSC);                     // Start conversion
	loop_until_bit_is_clear (ADCSRA, ADSC);         // Wait for conversion complete
	return(ADCH);
}




void lcd_init(){
	
	lcd_cmd(0x30);
	_delay_ms(10);
	lcd_cmd(0x38);
	_delay_ms(10);
	lcd_cmd(0x0F);
	_delay_ms(10);
	lcd_cmd(0x80);
	_delay_ms(10);

}

void lcd_data(unsigned char data1)
{
	
	
	lcd_data_pin = data1;// & 0x0F;
	en=1;
	rs=1;
	rw=0;
	_delay_ms(10);
	en=0;
	
}

void lcd_cmd(unsigned char cmd){
	lcd_data_pin = cmd ;
	
	en=1;
	rs=0;
	rw=0;
	_delay_ms(10);
	en=0;
	
}



void lcd_string(unsigned char *str){
	
	while(*str){
		lcd_data(*str++);
	}
	
}





int main(){
	char volts[50];
	int  adc_result;
	float adcA;
	
	DDR_D.b0 = 1;
	DDR_D.b1 = 1;
	DDR_D.b2 = 1;
	DDR_D.b3 = 1;
	DDR_D.b4 = 1;
	DDRA = 0xFF;
	
	lcd_init();
	adc_init();
	
	while(1){
		
			lcd_cmd(0x80);//put the cursor into the first row
			_delay_ms (10);
			lcd_cmd(0x01);//Clear display
			_delay_ms (10);
			lcd_string("ADC Value");
			lcd_cmd(0xC0);//goto second row
			//lcd_string("Value of PF0");
			_delay_ms(1000);
			//check the adc result begin
			adc_result=read_adc();
			adcA = (int)(adc_result*5.0)/1024.0;
			
			if (adcA != 0)
			{
				 //itoa(adcA,volts,5);
				sprintf(volts,"adc=%.3f",adcA);
				lcd_string(volts);
				_delay_ms(1000);
			}
			else
			{
				lcd_string("No Result!");
				_delay_ms(2000);
			}
			//check the adc result end

		

		
	}//end while
	
	return (0);
}//end main
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top