0% found this document useful (0 votes)
45 views4 pages

Exp10 1

 Nano-Technology

Uploaded by

anamayank111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

Exp10 1

 Nano-Technology

Uploaded by

anamayank111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exp No.

10 Date: D D - M M - Y Y

TEMPERATURE SENSOR INTERFACING WITH AVR USING ADC


AIM

To interface Temperature sensor with AVR microcontroller using ATmega32 ADC and display on LCD

THEORY
To use ATmega32 ADC, the analog input need to be connected to PORTA pins which aremultiplexed
as ADC input pins (labeled as ADC0 to ADC7). LM35 Temperature sensor (Pin 2) is connected to
PORTA Pin 0(ADC0 pin) of ATmega32. PORTA is configured as an input port and PORTD and C as out
put ports.is configured as an output port. Data lines of LCD are connected to PORTD, and PORTC.6
and 7 are used as RS and E. RW is connected to ground.
LM35 series are precision integrated-circuit temperature sensors whose output voltage is linearly
proportional to the temperature. It outputs 10 mV for each degree of temperature.
The ADC has 10-bit resolution. So number of steps will be 1024. We use the internal 2.56 V
reference voltage as Vref, so the step size would be 2.56 V/1024 = 2.5 mV. LM35 sensorproduces 10
mV for each degree of temperature change and the ADC step size is 2.5 mV. So each degree of
temperature change corresponds to 4 ADC steps(10 mV/2.5 mV = 4).This makes the binary output
number for the ADC four times the real temperature. So the 10-bit output of the ADC is divided by 4
to get the real temperature. To divide the 10-bit output of the ADC by 4 we choose the left-justified
option and only read the ADCH register. It is same as shifting the result 2 bits right (division by 4 in
binary).

C Program

// Code for Interfacing LM 35 and LCD with ATmega32 AVR microcontroller

#define F_CPU 16000000UL /* Define CPU Frequency e.g. here 16MHz */


#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include inbuilt defined Delay header file */
#define LCD_DATA PORTD //PORT D as data line
#define LCD_RS 6 //of PORT C
#define LCD_EN 7 //of PORT C

//------function declaration---------
void lcd_command(char c);
void lcd_data(char d);
void lcd_puts(const char *s);

//------main function----------------
main()
{
unsigned int ADATA = 0; //storing digital output of ADC
char DIGITS [3]; //for holding string of digits
DDRC=0XFF; //PORT C as o/p line
DDRD=0XFF; //PORT D as o/p line
DDRA = 0; // PORTA as input.
_delay_ms(20);
ADMUX |=(1<<REFS0)|(1<<REFS1) |(1<<ADLAR);
//setting the reference of ADC as 2.56V,left justified data
ADCSRA |=(1<<ADEN)|(1<<ADATE)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2);
//enabling the ADC, setting free running mode, setting prescalar 128

//-------passing commands to lcd for initialization------


lcd_command(0X38); //2 lines, 5x8 matrix, 8-bit mode
lcd_command(0X80); // cursor to the beginning of the 1st line
lcd_command(0X0C); // Display on, cursor off
lcd_command(0X06); // Shift the cursor right
lcd_command(0X01); // Clear the display screen
_delay_us(100);
ADCSRA |=(1<<ADSC); //start conversion
while(1)
{
ADATA = ADCH; //devide adc data by four.(10mV/2.5mV=4)= ADCH
lcd_puts("ROOM TEMPERATURE"); // first line
lcd_command(0XC0); // cursor to the beginning of the 2nd line
lcd_puts("Degree C:"); // second line
itoa(ADATA,DIGITS,10); //binary data to digits in a string
lcd_puts (DIGITS); //display digits of temperature in LCD
lcd_command(0X80); //retuning to first line first column

_delay_us(5);
}
}

//-----------function lcd command---------


void lcd_command(char c)
{
LCD_DATA=c;
PORTC&=~(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}

//-----------function lcd data---------


void lcd_data(char d)
{
LCD_DATA=d;
PORTC|=(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}

//-----------function lcd puts---------


void lcd_puts(const char *s)
{
while(*s)
{
lcd_data(*s++);
_delay_us(5);
}
}

RESULT

Interfacing program for LM 35 with 16x2 LCD is written in embedded C, compiled, downloaded to target
board, and implemented.

You might also like