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.

help for interfacing the lm35 with atmega8

Status
Not open for further replies.

achuth002

Junior Member level 3
Joined
Feb 4, 2013
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
hyderabad
Activity points
1,442
hi,
i was try to ineterfacing the lm35 with atmega8
plz help me
 

hi
achuth

first of all LM35 is sensor which gives only analog output. so you need to connect to PORTC.
if this is OK, then check the code level.
in ADC

there are 3 register in ATMEGA8 , AMUX,DATA,ADCSRA

ADCSRA is important ,7th bit is enable bit, it must be high using ADC

and final one check the Vref in hard ware according to your settings.


all are ok , if you does not get output, check LM35 sensor
 

    V

    Points: 2
    Helpful Answer Positive Rating
i am writing the code for interfacing the lm35
any wrong pl z tell me

Code:
#include <avr/io.h> 

int main (void) 
{ 
   DDRE |= (1 << 2); // Set LED1 as output 
   DDRG |= (1 << 0); // Set LED2 as output 

   ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz 

   ADMUX |= (1 << REFS0); // Set ADC reference to AVCC 
   ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading 

   // No MUX values needed to be changed to use ADC0 

   ADCSRA |= (1 << ADFR);  // Set ADC to Free-Running Mode 

   ADCSRA |= (1 << ADEN);  // Enable ADC 
   ADCSRA |= (1 << ADSC);  // Start A2D Conversions 

   for(;;)  // Loop Forever 
   { 
      // TODO: Test ADC Value and set LEDs 
   } 
}
 
Last edited by a moderator:

DDRE |= (1 << 2); // Set LED1 as output
DDRG |= (1 << 0); // Set LED2 as output

Download the data sheet !
mega 8 doesn't have a PORT E or PORT G
 

hi

you are not suppose to be any PIN as for ADC.

in atmega 8 portc is allotted for ADC


i saw your code. in that , i didint see any check condition for end of conversion of ADC

- - - Updated - - -

here

i give my ADC_Header file. just see and an idea to get

Code:
#define ADSC 6
#define ADIF 4

void adc_init (void);
unsigned char adc_read (unsigned char ch_select);

void adc_init (void)
{
    ADCSRA = 0x83;
}

unsigned char adc_read (unsigned char ch_select)
{
    unsigned char data;
    ADMUX = ch_select;
    
    ADCSRA.ADSC = 1;
    while (ADCSRA.ADIF == 0);
    data = ADCH;
    ADCSRA.ADSC = 0;
    return data;
}


in this code i got only 8 bit reading.
 

i am writing the interfacing program for lm35 with atmega8
any correction tell me,
and tell the better for simply wirte the programs
Code:
#include<stdio.h>
#include<string.h>

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

#include "uart.h"

void ADC_Init();
int read_adc();
unsigned int ADC_calculating();
unsigned int array[10];
unsigned int ADC_VALUE;
unsigned int value,temp; 

void main()
{
	
	_delay_ms(50);

	UART_Init(25);
    UART_TransmitString("Testing the room temperature:\r\n");

	DDRB = 0x01;
	PORTB= 0x01;
	
	DDRB = 0x00;
	PORTB= 0x00;

	ADC_Init();

	while(1)
	{
		if(bit_is_clear(PINB,0))
		{
			ADC_calculating();

			while(bit_is_clear(PINB,0));
		}
	}
}

void ADC_Init()
{
	ADCSRA = 0x00;

	ADMUX  = 0x40;
	ADCSRA = 0x86;
}

int read_adc()
{
    //select the ADC channel  
	ADMUX |= 0x01;
	
	//START THE ADC CONVERATION

	ADCSRA |= (1<<ADSC);

	//WAIT FOR CONVERATION TO COMPLETE

	while(!(ADCSRA) & (1<< ADIF));

	//CLEAR ADIF BY WRITING ONE TO IT

	ADCSRA |= 0x01;

	return(ADC);
}

unsigned int ADC_calculating()
{

	ADC_VALUE = read_adc();

	itoa(ADC_VALUE, array, 10);
	UART_TransmitString("ADC:");
	UART_TransmitString(array);
	UART_TransmitString("\r\n");

	value = ADC_VALUE * 4.8875;

	itoa(value, array, 10);
	UART_TransmitString("Ref:");
	UART_TransmitString(array);
	UART_TransmitString("\r\n");
	
	temp = value / 10;

	itoa(temp, array, 10);
	UART_TransmitString("temp:");
	UART_TransmitString(array);
	UART_TransmitString("\r\n");
	UART_TransmitString("\n");
}

- - - Updated - - -

the output of the program is
PHP:
Testing the room temperature:
ADC:0
Ref:0
temp:0

ADC:63
Ref:307
temp:30

ADC:62
Ref:303
temp:30

ADC:62
Ref:303
temp:30

ADC:62
Ref:303
temp:30

ADC:62
Ref:303
temp:30

ADC:62
Ref:303
temp:30

ADC:62
Ref:303
temp:30

ADC:63
Ref:307
temp:30

ADC:62
Ref:303
temp:30
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top