EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Error using Lm35 Interface with Atmega 16

Error using Lm35 Interface with Atmega 16

|

Microcontroller › AVR › Error using Lm35 Interface with Atmega 16

  • This topic has 4 replies, 5 voices, and was last updated 11 years, 4 months ago by harsh dhiman.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • August 5, 2013 at 7:26 pm #2575
    Hamza Khan
    Participant

    hey I am using the following code to interface LM35 with ATMEGA16 and displaying the result on LCD. 
     

    When the temprature is above 100 degree centigrade the output of ADC is fine but when the temperature falls below 99 degree centigrade the ADC outputs garbage value. 

     

    like for 150 degree to 100 it displays the same but for 99 to 10 degree centigrade it displays 990 degree to 100 and then from 9 to 0 centigrade the output is 900 degree centigrade to 100.

     

    here is the code tell me the mistake.

     

    #include<avr/io.h>
    #include<avr/delay.h>
    //#include<inttypes.h>
    //#include <stdint.h>
    #define rs PA0
    #define rw PA1
    #define en PA2
    void lcd_init();
    void ADC_init();
    unsigned int ADC_read(unsigned char);
    void dis_cmd(char);
    void dis_data(char);
    void lcdcmd(char);
    void lcddata(char);
     
    int main(void)
    {
    DDRA=0xEF;
    while (1)
    {
    ADC_init();
    unsigned char data0[]=”Temperature=”;
    unsigned int data1,adc_val,a;
    char int_buffer[10];
    lcd_init();
    LCD_print(data0);
    adc_val=ADC_read(3);
    data1=(adc_val/2.048);
    //a=(((308*5)*100)/1024);
    itoa(data1, int_buffer, 10);
     
    dis_cmd(0xC0);
     
    LCD_print(int_buffer);
    }
    }
     
    void lcd_init()     // Function for initialize
    {
    dis_cmd(0x02); // To initialize LCD in 4-bit mode.
    dis_cmd(0x28); // To initialize LCD in 2 lines, 5X7 dots in 4bit mode.
    dis_cmd(0x0F); // Blinking Cursor
    dis_cmd(0x06); // Entry mode
    dis_cmd(0x80); // This command gives the location of first letter  ** 80 + ‘x’ ** shifts the display ‘x’ units right.
    _delay_ms(500);
    }
    void dis_cmd(char cmd_value)
    {
    char cmd_value1;
    cmd_value1 = cmd_value & 0xF0; // Mask lower nibble because PA4-PA7 pins are used (Extracting Upper Four bits)
    // Masking means clearing bits
    // Upper nibble is sent first and then the lower nibble
    lcdcmd(cmd_value1); // Send to LCD
    cmd_value1 = ((cmd_value<<4) & 0xF0); // Shift 4-bit and mask (Extracting Upper Four Bits)
    lcdcmd(cmd_value1); // Send to LCD
    }
    void dis_data(char data_value)
    {
    char data_value1;
    data_value1=data_value & 0xF0;
    lcddata(data_value1);
    data_value1=((data_value<<4)&0xF0);
    lcddata(data_value1);
    }
    void lcdcmd(char cmdout)
    {
    //Commands on LCD is initialized by having a negative transition on the enable pin.
    // 1) RS=RW=0 & EN=1;
    // 2) RS=RW=0 & EN=0;
    PORTA=cmdout;
    PORTA&=~(1<<rs);
    PORTA&=~(1<<rw);
    PORTA|=(1<<en);
    _delay_ms(1);
    PORTA&=~(1<<en);
    }
    void lcddata(char dataout)
    {
    //Letters on LCD is displayed by having a negative transition on the enable pin.
    // 1) RS=RW=1 & EN=1;
    // 2) RS=RW=1 & EN=0;
    PORTA=dataout;
    PORTA|=(1<<rs);
    PORTA&=~(1<<rw);
    PORTA|=(1<<en);
    _delay_ms(1);
    PORTA&=~(1<<en);
    }
     
    void ADC_init()
    {
    ADMUX=(1<<REFS0);
    ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS0);
    }
    unsigned int ADC_read(unsigned char ch)
    {
    ch= ch & 0b00000111; // channel must be b/w 0 to 7
    ADMUX |= ch; // selecting channel
     
    ADCSRA|=(1<<ADSC); // start conversion
    while(!(ADCSRA & (1<<ADIF))); // waiting for ADIF, conversion complete
    ADCSRA|=(1<<ADIF); // clearing of ADIF, it is done by writing 1 to it
    return (ADC);
     
    }
    void LCD_print(char * str)
    {
    unsigned char i=0;
    while(str!=0)
    {
    dis_data(str);
    i++;
    }
    }
     
    September 17, 2013 at 8:32 am #10462
    mohammad amin
    Participant

    HI

     

    code software LM35

     

    #include <mega16.h>
    #include <stdio.h>
    #include <delay.h>
    #asm
       .equ __lcd_port=0x18 ;PORTB
    #endasm
    #include <lcd.h> 
    #define  Up        PIND.0
    #define  Down      PIND.1
    #define  Enter     PIND.2 
    #define  high_LED  PORTA.1
    unsigned char compare=0,set=0xff;
    float   temp; 
    //__________________________________________
    void display(){  
    char   lcd_buf[32];
    sprintf(lcd_buf,”Temp=%3.1fxdfCnHigh Temp=%i”,temp,compare);
    lcd_clear();
    lcd_gotoxy(0,0);
    lcd_puts(lcd_buf);
    lcd_gotoxy(13,1);
    if(high_LED==1) lcd_putsf(“on “);
    if(high_LED==0) lcd_putsf(“off”);
    } 
    //_________________________________________                        
    interrupt [ADC_INT] void adc_isr(void){
    unsigned int adc_data;
    adc_data=ADCW;
    temp=adc_data*2.56/1024; 
    temp=temp*100;
    if(temp>=set) high_LED=1;
    else high_LED=0;
    }
    //_________________________________________up
    void inc_set_temp(){
    if(compare<99) compare++;
    }
    //__________________________________________Down
    void dec_set_temp(){
    if(compare!=0) compare–;
    }             
    //__________________________________________
    void main(){
    PORTD=0XFF;
    DDRD=0X00;
    PORTA.1=0;
    DDRA.1=1; 
    ACSR=0XC0;
    SFIOR=0X00;
    ADMUX=0XC0;
    ADCSRA=0X8F;
    lcd_init(16);
    #asm(“sei”)
    ADCSRA=0XCF;
    while(1){
       display();
       delay_ms(250);
       ADCSRA=0XCF;
       if(Up==0)    inc_set_temp();
       if(Down==0)  dec_set_temp();
       if(Enter==0) set=compare;
     };
    }

    November 15, 2013 at 6:11 am #10609
    Ganesh Selvaraj
    Participant

    Hello Hamza Khan, I guess the problem is that the variable “data1” is declared as int type and so it doesn’t take decimals. Try changing it to float type.

    February 4, 2014 at 5:53 pm #10936
    Ashutosh Bhatt
    Participant

    the problem is sending and printing values on LCD. to display values in correct order use following function

    void display(unsigned char z)
      {
      unsigned int tmp1,tmp2,t,t1,i;
    unsigned char ASCII[3];
    tmp1 = (z & 0x0F);
    tmp2 = z>>4;
    tmp2 = tmp2*16;
    t = tmp1+tmp2;
    if(t>=100)
     {
    i=2;
    while(t>10)
    {
    t1=t%10;
    ASCII=t1+0x30;
    t=t/10;
    i–;
    }
    ASCII[0]=t+0x30;
     }
    else
     {
    t1=t%10;
    ASCII[2]=t1+0x30;
    t=t/10;
    ASCII[1]=t+0x30;
    ASCII[0]=0x30;
     }
    writedata(ASCII[0]);
    writedata(ASCII[1]);
    writedata(ASCII[2]);
      }
    March 6, 2014 at 3:29 pm #11214
    harsh dhiman
    Participant

    can u plz provide the circuit diagram . the code gets complied (Y)

     

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Wierd makita battery July 14, 2025
  • More fun with ws2812 this time XC8 and CLC July 14, 2025
  • I Wanna build a robot July 13, 2025
  • using a RTC in SF basic July 13, 2025
  • Is AI making embedded software developers more productive? July 13, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise