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 / Interfacing LM35 and avr controller with use of internal ADC of ATMEGA32

Interfacing LM35 and avr controller with use of internal ADC of ATMEGA32

|

Microcontroller › AVR › Interfacing LM35 and avr controller with use of internal ADC of ATMEGA32

  • This topic has 5 replies, 4 voices, and was last updated 11 years, 11 months ago by abhijit.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • December 25, 2011 at 9:31 am #1466
    Dhruwank Vankawala
    Participant

    Hey, frds i m doing interfacing of LM35 with AVR controller(ATMEGA32). I got output on software bt still i dint get it on hardware following is the code for it……………can u plz help me…………….

     

    #include <avr/io.h>
    #include<util/delay.h>
    #include<math.h>
    #include<stdlib.h>
    #define LCD_PRT PORTC
    #define LCD_PIN PINC
    #define LCD_DDR DDRC
    #define LCD_RS 0
    #define LCD_RW 1
    #define LCD_EN 2

    void delay_us(int d)
    {
    _delay_us(d);
    }

    void delay_ms(int d)
    {
    _delay_ms(d);
    }

    void lcdcommand(unsigned char cmnd)
    {
    LCD_PRT = (LCD_PRT & 0x0F)| (cmnd & 0xF0);
    LCD_PRT &=~(1<<LCD_RS);
    LCD_PRT &=~(1<<LCD_RW);
    LCD_PRT |=(1<<LCD_EN);
    delay_us(1);
    LCD_PRT &=~(1<<LCD_EN);
    delay_us(20);
    LCD_PRT = (LCD_PRT & 0x0F) | (cmnd<<4);
    LCD_PRT |= (1<<LCD_EN);
    delay_us(1);
    LCD_PRT &=~ (1<<LCD_EN);
    }

    void lcddata(unsigned char data)
    {
    LCD_PRT = (LCD_PRT & 0x0F) | (data & 0xF0);
    LCD_PRT |= (1<<LCD_RS);
    LCD_PRT &=~ (1<<LCD_RW);
    LCD_PRT |= (1<<LCD_EN);
    delay_us(1);
    LCD_PRT &=~ (1<<LCD_EN);
    delay_us(20);
    LCD_PRT = (LCD_PRT & 0x0F) | (data << 4);
    LCD_PRT |= (1<<LCD_EN);
    delay_us(1);
    LCD_PRT &=~ (1<<LCD_EN);
    }

    void LCD_init()
    {
    LCD_DDR = 0xFF;
    LCD_PRT &=~(1<<LCD_EN);
    delay_us(2000);
    lcdcommand(0x33);
    delay_us(100);
    lcdcommand(0x32);
    delay_us(100);
    lcdcommand(0x28);
    delay_us(100);
    lcdcommand(0x0E);
    delay_us(100);
    lcdcommand(0x01);
    delay_us(2000);
    lcdcommand(0x06);
    delay_us(100);
    }

    unsigned int adc_conv(void)
    {
    DDRA=0;
    ADCSRA=0x87;
    ADMUX=0xE0;
    ADCSRA|=(1<<ADSC);
    while((ADCSRA&(1<<ADIF))==0);
    return ADCH;
    }

    void LCD_gotoxy(unsigned char x, unsigned char y)
    {
    unsigned char firstcharadr[]={0x80, 0x85};
    lcdcommand(firstcharadr[y-1] + x – 1);
    delay_us(100);
    }

    void LCD_print(char * str)
    {
    unsigned char i=0;
    while(str!=0)
    {
    lcddata(str
    );
    i++;
    }
    }

    int main(void)
    {
    unsigned char val,t;
    LCD_init();
    while(1)
    {
    LCD_gotoxy(1,1);
    LCD_print(“Temp “);
    LCD_gotoxy(1,2);
    val=adc_conv();
    t=round(val*0.48876);
    char buffer[10];
    itoa(val,buffer,10);
    LCD_print(buffer);
    delay_ms(1000);
    }
    return 0;
    }

    December 28, 2011 at 1:36 pm #6957
    Tuhin
    Participant
     hey, Dhruwank you just forget to diable jtag interface Port. I have modified your program a little and it is working on hardware and 

    wysiwyg_imageupload:3465:

    I have checked it. You have done a great job.
     
     
     
     
     
    #include <avr/io.h>
    #include<util/delay.h>
    #include<math.h>
    #include<stdlib.h>
    #define LCD_PRT PORTC
    #define LCD_PIN PINC
    #define LCD_DDR DDRC
    #define LCD_RS 0
    #define LCD_RW 1
    #define LCD_EN 2
    #define LS_NULL 0B00000000
     
     
    void delay_us(int d)
    {
    _delay_us(d);
    }
     
     
    void delay_ms(int d)
    {
    _delay_ms(d);
    }
     
    void lcdcommand(unsigned char cmnd)
    {
    LCD_PRT = (LCD_PRT & 0x0F)| (cmnd & 0xF0);
    LCD_PRT &=~(1<<LCD_RS);
    LCD_PRT &=~(1<<LCD_RW);
    LCD_PRT |=(1<<LCD_EN);
    delay_us(1);
    LCD_PRT &=~(1<<LCD_EN);
    delay_us(20);
    LCD_PRT = (LCD_PRT & 0x0F) | (cmnd<<4);
    LCD_PRT |= (1<<LCD_EN);
    delay_us(1);
    LCD_PRT &=~ (1<<LCD_EN);
    }
     
    void lcddata(unsigned char data)
    {
    LCD_PRT = (LCD_PRT & 0x0F) | (data & 0xF0);
    LCD_PRT |= (1<<LCD_RS);
    LCD_PRT &=~ (1<<LCD_RW);
    LCD_PRT |= (1<<LCD_EN);
    delay_us(1);
    LCD_PRT &=~ (1<<LCD_EN);
    delay_us(20);
    LCD_PRT = (LCD_PRT & 0x0F) | (data << 4);
    LCD_PRT |= (1<<LCD_EN);
    delay_us(1);
    LCD_PRT &=~ (1<<LCD_EN);
    }
     
     
    void LCD_init()
    {
    LCD_DDR = 0xFF;
    LCD_PRT &=~(1<<LCD_EN);
    delay_us(2000);
    lcdcommand(0x33);
    delay_us(100);
    lcdcommand(0x32);
    delay_us(100);
    lcdcommand(0x28);
    delay_us(100);
    lcdcommand(0x0E);
    delay_us(100);
    lcdcommand(0x01);
    delay_us(2000);
    lcdcommand(0x06);
    delay_us(100);
    }
     
    unsigned int adc_conv(void)
    {
    DDRA=0;
    ADCSRA=0x87;
    ADMUX=0xE0;
    ADCSRA|=(1<<ADSC);
    while((ADCSRA&(1<<ADIF))==0);
    return ADCH;
    }
     
    void LCD_gotoxy(unsigned char x, unsigned char y)
    {
    unsigned char firstcharadr[]={0x80, 0x85};
    lcdcommand(firstcharadr[y-1] + x – 1);
    delay_us(100);
    }
     
     
    void LCD_print(char * str)
    {
    unsigned char i=0;
    while(str!=0)
    {
    lcddata(str);
    i++;
    }
    }
     
     
     
     
    int main(void)
    {
    MCUCSR|= (1<<JTD);      //disaleb JTAG
    MCUCSR|= (1<<JTD); 
     
    unsigned char val,t;
    LCD_init(LS_NULL);
    LCD_gotoxy(1,1);
    LCD_print(“Temperature “);
    LCD_gotoxy(10,2);
    LCD_print(“*c”);
    while(1)
    {
     
    LCD_gotoxy(8,2);
    val=adc_conv();
    t=round(val*0.48876);
    char buffer[10];
    itoa(val,buffer,10);
    LCD_print(buffer);
     
     
    delay_ms(100);
    }
    return 0;
    }
     
    December 30, 2011 at 12:09 pm #6970
    Dhruwank Vankawala
    Participant

    i have tried the code that u have sent bt i dint work on hardware….

    i m using ATMEGA 32 development board provided by NEX ROBOTICS….

    Which development board u have used?????

    And tell me another manufacturer that provide ATMEGA 32 development board………

    January 1, 2012 at 4:22 pm #6976
    Tuhin
    Participant

    I connected it on BreadBoard. I have a developement board from Extreme Electronics But in my Developement board JTAG is already disabled by fuse byte settings from the time of buying. So I tried this on BreadBoard and it worked.

    January 2, 2012 at 8:31 am #6978
    jinay
    Participant

    well i assume that there’s no problem with your LCD CODE…so you are getting output on LCD but you have a problem in ADC CODE and Interfacing

     

    Interfacing is quite simple : Simply Connect LM35 to your ADC PIN

     

    CODE ERROR:

     

    I have found in your code that you are returning ADCH in adc_conv() function but my friend ADCH carries only upper bits of the resulting step as ATMEGA16/32 supports 10bit resolution ADC

     

    ADCH :- 9:8

    ADCL :-  7:0

     

    thus whole ADCL and ADCH combine gives the result of ADC

     

    and for that you only need to change ADC instead of ADCH

     

    where ADC register is 10 bit register and it combines ADCL and ADCH

     

    and don’t forget to change the return type of your function to unsigned short int adc_conv() for 10bit return

     

     

    Try this i am sure this will surely clears your problem….

     

    and still you find problem try to refer DATASHEET.. it will explain you very well…

    December 16, 2013 at 3:01 pm #10737
    abhijit
    Participant
    t=round(val/2);
    char buffer[10];
    d1=t/2;
    itoa(d1,buffer,10);
    LCD_print(buffer);
     
    only adjust this in main for temperature show
  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Unknown, to me, electric motor fitting November 13, 2025
  • Can a small solar panel safely trickle-charge old NiMH AA batteries? November 13, 2025
  • KiCad custom symbol definition correct approach November 13, 2025
  • Measuring controller current output with a meter November 13, 2025
  • restarting this Christmas project November 12, 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