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.

Problem with LM35 (tempreture sensor) and Atmega32

Status
Not open for further replies.

Bluestar88

Member level 3
Joined
Oct 17, 2014
Messages
59
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
447
I wanna measure the temperature of room. For this order I used a LM35 and Atmega32. But my circuit doesn't work correctly.It displays 499.5 C on it's LCD...Please help me...My code is here...
Code:
/****************************************************************
    Title                   : Thermometer with LM35
    Micro Contoroler        : ATMega32
    Clock frequency         : 1.000000 MHz
    Compiler                : Code Vision AVR
****************************************************************/

#include <mega32.h>
#include <delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <delay.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h>

typedef unsigned char byte;
flash byte char0[8]={
    0b10000111,
    0b10000101,
    0b10000111,
    0b10000000,
    0b10000000,
    0b10000000,
    0b10000000,
    0b10000000
};

void Configuration_LCD(void);
void define_char(byte flash *pc,byte);
void Configuration_ADC(void);
void Display_LCD_Start(void);
float Read_adc(unsigned char);
float Convert(float);
void Display_LCD_1(float);
void Display_LCD_2(unsigned int);

float Input_mV=0;
float Temp=0;

void main(void){
    Configuration_ADC();
    Configuration_LCD();
    define_char(char0,0); 
    Display_LCD_Start();
    while (1){
        Input_mV=Read_adc(0);
        Temp=Convert(Input_mV);
        Display_LCD_1(Temp);
        Display_LCD_2(Input_mV);
        delay_ms(300);                                        
    };
}

//********************************************************
void Configuration_LCD(void){
    lcd_init(16); lcd_clear();   
}

//********************************************************
void define_char(byte flash *pc,byte char_code){
    byte i,a;
    a=(char_code<<3) | 0x40;
    for (i=0; i<8; i++) lcd_write_byte(a++,*pc++);
}

//********************************************************
void Configuration_ADC(void){
    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;

    // ADC initialization
    // ADC Clock frequency: 125.000 kHz
    // ADC Voltage Reference: AVCC pin
    ADMUX=0x40 & 0xff;
    ADCSRA=0x83;
}

//********************************************************
void Display_LCD_Start(void){
    lcd_clear(); 
    lcd_gotoxy(0,0); lcd_putsf("LM35");
    lcd_gotoxy(0,1); lcd_putsf("Test");
    delay_ms(2000); lcd_clear(); delay_ms(200);
}

//********************************************************
float Read_adc(unsigned char adc_input){
    float x=0;
    ADMUX=adc_input | (0x40 & 0xff);
    // Delay needed for the stabilization of the ADC input voltage
    delay_us(10);
    // Start the AD conversion
    ADCSRA|=0x40;
    // Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);
    ADCSRA|=0x10;
    x=ADCW; x= x*4.8828125;
    return x;
}

//********************************************************
float Convert(float x){
     x=x/10;
     return x;
}

//********************************************************
void Display_LCD_1(float x){
    char buffer[16];
    lcd_gotoxy(0,0); 
    lcd_putsf("Temp: ");
    ftoa(x,1,buffer); lcd_puts(buffer); 
    lcd_putsf(" ");
    lcd_putchar(0);
    lcd_putsf("C");   
}

//********************************************************
void Display_LCD_2(unsigned int x){
    char buffer[16];
    lcd_gotoxy(0,1); 
    lcd_putsf("Input: ");
    ftoa(x,1,buffer); lcd_puts(buffer);
    lcd_putsf(" mV");
}

and my hardware is simple. I connected pin 10, 30 to VCC and 11, 31 to GND. Pin 40 to sensor. and PORT D to LCD....Please help me..I really confused...

- - - Updated - - -

It is such as a case that ADC doesnot work.....Please help me...

- - - Updated - - -

It is such as a case that ADC doesnot work.....Please help me...
 

It seems to be correct, I don't know what's problem...
 

Zip and post the code vision AVR project. I will test it in Proteus and also hardware.

ADC input pin configures as input and analog pin using DDRx ?
 
When you click Reply button it open a page and in that click Manage Attachments button and attach your Zip file and make the post.

Post the complete CodeVision AVR project files Zipped including the lcd.h file.
 
Last edited:

My problem was because of atmega32. When I replaced it, My problem solved.


Now, I want to turn on the relay by setting PinB.0 on atmega32 and a 330 ohm resistor and a BC237 transistor. In my code if temp<30, PinB.0=1 then causes to turn on the relay an if temp>30, PinB.0=0 then relay turn off. But I have a problem when tempreature is oscillating between 29.8 to 30.3 relay is turn off and turn off, consecutively. My code is here. Please help me.

Code:
/****************************************************************
    Title                   : Thermometer with LM35
    Last Updated            : 2013/04/19
    Micro Contoroler        : ATMega32
    Clock frequency         : 1.000000 MHz
    Compiler                : Code Vision AVR
****************************************************************/

#include <mega32.h>
#include <delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <delay.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h>

typedef unsigned char byte;
flash byte char0[8]={
    0b10000111,
    0b10000101,
    0b10000111,
    0b10000000,
    0b10000000,
    0b10000000,
    0b10000000,
    0b10000000
};

void Configuration_LCD(void);
void define_char(byte flash *pc,byte);
void Configuration_ADC(void);
void Display_LCD_Start(void);
float Read_adc(unsigned char);
float Convert(float);
void Display_LCD_1(float);
void Display_LCD_2(unsigned int);

float Input_mV=0;
float Temp=0;

void main(void){
       //PortA -> Output
    PORTB = 0x00;
    DDRB = 0x0f;
    Configuration_ADC();
    Configuration_LCD();
    define_char(char0,0); 
    Display_LCD_Start();
    while (1){
        Input_mV=Read_adc(0);
        Temp=Convert(Input_mV);
        Display_LCD_1(Temp);
        Display_LCD_2(Input_mV);   
        
[B]         if(Temp>30)
        {
         PORTB.0=1;  
         }
          if (Temp<30)
         {
         PORTB.0=0;   
         } 
         [/B]
        delay_ms(300);                                        
    };
}

//********************************************************
void Configuration_LCD(void){
    lcd_init(16); lcd_clear();   
}

//********************************************************
void define_char(byte flash *pc,byte char_code){
    byte i,a;
    a=(char_code<<3) | 0x40;
    for (i=0; i<8; i++) lcd_write_byte(a++,*pc++);
}

//********************************************************
void Configuration_ADC(void){
    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;

    // ADC initialization
    // ADC Clock frequency: 125.000 kHz
    // ADC Voltage Reference: AVCC pin
    ADMUX=0x40 & 0xff;
    ADCSRA=0x83;
}

//********************************************************
void Display_LCD_Start(void){
    lcd_clear(); 
    lcd_gotoxy(0,0); lcd_putsf("www.CSANCO.COM");
    lcd_gotoxy(0,1); lcd_putsf("LM35");
    delay_ms(2000); lcd_clear(); delay_ms(200);
}

//********************************************************
float Read_adc(unsigned char adc_input){
    float x=0;
    ADMUX=adc_input | (0x40 & 0xff);
    // Delay needed for the stabilization of the ADC input voltage
    delay_us(10);
    // Start the AD conversion
    ADCSRA|=0x40;
    // Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);
    ADCSRA|=0x10;
    x=ADCW; x= x*4.8828125;
    return x;
}

//********************************************************
float Convert(float x){
     x=x/10;
     return x;
}

//********************************************************
void Display_LCD_1(float x){
    char buffer[16];
    lcd_gotoxy(0,0); 
    lcd_putsf("Temp: ");
    ftoa(x,1,buffer); lcd_puts(buffer); 
    lcd_putsf(" ");
    lcd_putchar(0);
    lcd_putsf("C");   
}

//********************************************************
void Display_LCD_2(unsigned int x){
    char buffer[16];
    lcd_gotoxy(0,1); 
    lcd_putsf("Input: ");
    ftoa(x,1,buffer); lcd_puts(buffer);
    lcd_putsf(" mV");
}
 

Try this.


Code C - [expand]
1
2
3
4
5
6
7
8
if(Temp >= 30)
        {
         PORTB.0 = 1;  
         }
          if (Temp < 30)
         {
         PORTB.0 = 0;   
         }

 

hello


try to add hystereis behavior
so you get a dead band around 30.0°

Code:
// inital value;
hysteresis=0.1;  // less or more than 0.1 !



if(Temp >= 30+ hysteresis)
        {
         PORTB.0 = 1;  
         hysteresis=-0.1;
         }
          if (Temp < 30 - Hysteresis)
         {
         PORTB.0 = 0;
         hysteresis=+0.1;
         }
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top