Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LM35 Code

Status
Not open for further replies.

AtomSoft

Well-Known Member
Hey guys i just got my LM35 in the mail and am glad to say its so much easier to use than the LM75 heh.. Here is a picture from ISIS.. It works well live also:

Below the picture is full source code for LCD, LM35 (ADC MANUALLY)

lm35-jpg.34560


https://atomsofttech.info/code/LM35_ADC.zip

OR

LM35_ADC.zip
 

Attachments

  • lm35.jpg
    lm35.jpg
    97.2 KB · Views: 11,309
Last edited:

AtomSoft

Well-Known Member
Tested again and might cause extra character to appear on LCD so here is new main.c file:

Code:
/* *****************************************************************************
;                                                                             *
;    Filename:                     				                              *
;    Date:                                         	                          *
;    File Version: 001                                                        *
;                                                                             *
;    Author:   Jason Lopez                                                    *
;    Company:  AtomSoft                                                       *
;                                                                             *
;***************************************************************************** */

#include <p18F4620.h>
#include <delays.h>
#include <string.h>
#include "lcd.h"

#pragma config WDT = OFF, LVP = OFF, OSC = HS, XINST = OFF


/************************************
Prototypes
*************************************/
void main(void);
unsigned char DECtoBCD(unsigned char DEC);
unsigned char DECtoASCII(unsigned char DEC,char part);
/************************************
Variables and Defines
*************************************/
    #define ADCPin TRISAbits.TRISA0 // RA0/AN0 TRIS (DATA DIRECTION REGISTER)

    unsigned int result; //Result Variable
    unsigned int DegC;   //Result in Celsius 
    unsigned int DegF;   //Result in Fahrenheit

    unsigned char lcdtmp[10];
/************************************
Main
*************************************/
void main(void){
    ADCPin = 1;                     //AN0 = Input
    ADCON0 = 0x00;                  //BIT 7:6 Always 00, BIT 5:2 is 0000 for Channel 0 (AN0),
                                    //BIT 1:0 is 00 (A/D Idle and Module is off) until we are done setting up
    ADCON1 = 0x0E;                  //BIT 7:6 Always 00, BIT 5 is 0 for VSS, BIT 4 is 0 for VDD
                                    //BIT 3:0 is 1110 for all digital expect AN0. We need it analog so we can convert it.
    ADCON2 = 0b10001101;            //BIT 7 is 0 for Left Justified, BIT 6 Always 0, BIT 5:3 001 for 2 TAD (A/D Acquisition Time)
                                    //BIT 2:0 is 100 for FOSC/4 for A/D Conversion Clock
    ADCON0bits.ADON = 1;            //Turn on the A/D module
    
    LCD_Init();
    DelayMS(100);

    LCD_LINE(1);
    LCD_STR((unsigned rom char*)"  AtomSoftTech  ");

    LCD_LINE(2);
    LCD_STR((unsigned rom char*)"Temp: ");

	while(1){
        ADCON0bits.GO = 1;          //Start the conversion
        while(ADCON0bits.DONE);     //Wait until it’s done
        result = ADRESH;            //Load ADRESH into result
        result <<= 8;               //Shift over Result 8 bits to the left
        result |= ADRESL;           //OR in our LOW byte to finish off out INT
        DegC = (result*5)/10;
        DegC--;

        DegF = DegC * 9;
        DegF/=5;
        DegF+=32;

        lcdtmp[0] = DECtoASCII(DegC,'h');
        lcdtmp[1] = DECtoASCII(DegC,'l');
        lcdtmp[2] = 'C';
        lcdtmp[3] = '-';
        lcdtmp[4] = DECtoASCII(DegF,'h');
        lcdtmp[5] = DECtoASCII(DegF,'l');
        lcdtmp[6] = 'F';
        lcdtmp[7] = lcdtmp[8]  = lcdtmp[9] = ' ';

        LCD_LINE(2);
        LCD_STR((unsigned rom char*)"Temp: ");
        LCD_STR2(lcdtmp);
        DelayMS(1000);

	}
}

unsigned char DECtoASCII(unsigned char DEC,char part){
unsigned char temp;
unsigned char myData;
    temp = DECtoBCD(DEC);

    if(part == 'l')
        myData = temp & 0x0F; 

    if(part == 'h') 
        myData = (temp >> 4) & 0x0F;
    
    myData += 0x30;
    return myData;
}
unsigned char DECtoBCD(unsigned char DEC){
unsigned char temph;
unsigned char templ;
unsigned char myData;
    templ = DEC % 10;
    temph = DEC / 10; 
    myData = (temph << 4) | templ;
    
    return myData; //Return myData
}
 

AtomSoft

Well-Known Member
Heh more update:

This code averages 10 readings all 10mS apart. So it takes 100ms to get 1 stable reading but heh its worth it...

Code:
/* *****************************************************************************
;                                                                             *
;    Filename:                     				                              *
;    Date:                                         	                          *
;    File Version: 001                                                        *
;                                                                             *
;    Author:   Jason Lopez                                                    *
;    Company:  AtomSoft                                                       *
;                                                                             *
;***************************************************************************** */

#include <p18F4620.h>
#include <delays.h>
#include <string.h>
#include "lcd.h"

#pragma config WDT = OFF, LVP = OFF, OSC = HS, XINST = OFF


/************************************
Prototypes
*************************************/
void main(void);
unsigned char DECtoBCD(unsigned char DEC);
unsigned char DECtoASCII(unsigned char DEC,char part);
/************************************
Variables and Defines
*************************************/
    #define ADCPin TRISAbits.TRISA0 // RA0/AN0 TRIS (DATA DIRECTION REGISTER)

    unsigned int result; //Result Variable
    unsigned int DegC;   //Result in Celsius 
    unsigned int DegF;   //Result in Fahrenheit

    unsigned char lcdtmp[10];
    unsigned char avg[10];

/************************************
Main
*************************************/
void main(void){
    unsigned char tmp;

    ADCPin = 1;                     //AN0 = Input
    ADCON0 = 0x00;                  //BIT 7:6 Always 00, BIT 5:2 is 0000 for Channel 0 (AN0),
                                    //BIT 1:0 is 00 (A/D Idle and Module is off) until we are done setting up
    ADCON1 = 0x0E;                  //BIT 7:6 Always 00, BIT 5 is 0 for VSS, BIT 4 is 0 for VDD
                                    //BIT 3:0 is 1110 for all digital expect AN0. We need it analog so we can convert it.
    ADCON2 = 0b10001101;            //BIT 7 is 0 for Left Justified, BIT 6 Always 0, BIT 5:3 001 for 2 TAD (A/D Acquisition Time)
                                    //BIT 2:0 is 100 for FOSC/4 for A/D Conversion Clock
    ADCON0bits.ADON = 1;            //Turn on the A/D module
    
    LCD_Init();
    DelayMS(100);

    LCD_LINE(1);
    LCD_STR((unsigned rom char*)"  AtomSoftTech  ");

    LCD_LINE(2);
    LCD_STR((unsigned rom char*)"Temp: ");

	while(1){
      for(tmp=0;tmp<10;tmp++){
        ADCON0bits.GO = 1;          //Start the conversion
        while(ADCON0bits.DONE);     //Wait until it’s done
        result = ADRESH;            //Load ADRESH into result
        result <<= 8;               //Shift over Result 8 bits to the left
        result |= ADRESL;           //OR in our LOW byte to finish off out INT
        avg[tmp] = result;  
        DelayMS(10);
      }       
        result = 0;
        for(tmp=0;tmp<10;tmp++){
            result += avg[tmp];
        }
        result /= 10;
        
        DegC = (result*5)/10;
        DegC--;

        DegF = DegC * 9;
        DegF/=5;
        DegF+=32;

        lcdtmp[0] = DECtoASCII(DegC,'h');
        lcdtmp[1] = DECtoASCII(DegC,'l');
        lcdtmp[2] = 'C';
        lcdtmp[3] = '-';
        lcdtmp[4] = DECtoASCII(DegF,'h');
        lcdtmp[5] = DECtoASCII(DegF,'l');
        lcdtmp[6] = 'F';
        lcdtmp[7] = lcdtmp[8]  = lcdtmp[9] = ' ';

        LCD_LINE(2);
        LCD_STR((unsigned rom char*)"Temp: ");
        LCD_STR2(lcdtmp);
        DelayMS(1000);
        DelayMS(1000);
	}
}

unsigned char DECtoASCII(unsigned char DEC,char part){
unsigned char temp;
unsigned char myData;
    temp = DECtoBCD(DEC);

    if(part == 'l')
        myData = temp & 0x0F; 

    if(part == 'h') 
        myData = (temp >> 4) & 0x0F;
    
    myData += 0x30;
    return myData;
}
unsigned char DECtoBCD(unsigned char DEC){
unsigned char temph;
unsigned char templ;
unsigned char myData;
    templ = DEC % 10;
    temph = DEC / 10; 
    myData = (temph << 4) | templ;
    
    return myData; //Return myData
}
 

AtomSoft

Well-Known Member
Here is the new code... Thanks for the tip :)

Code:
/* *****************************************************************************
;                                                                             *
;    Filename:                     				                              *
;    Date:                                         	                          *
;    File Version: 001                                                        *
;                                                                             *
;    Author:   Jason Lopez                                                    *
;    Company:  AtomSoft                                                       *
;                                                                             *
;***************************************************************************** */

#include <p18F4620.h>
#include <delays.h>
#include <string.h>
#include "lcd.h"

#pragma config WDT = OFF, LVP = OFF, OSC = HS, XINST = OFF


/************************************
Prototypes
*************************************/
void main(void);
unsigned char DECtoBCD(unsigned char DEC);
unsigned char DECtoASCII(unsigned char DEC,char part);
/************************************
Variables and Defines
*************************************/
    #define ADCPin TRISAbits.TRISA0 // RA0/AN0 TRIS (DATA DIRECTION REGISTER)

    unsigned int result; //Result Variable
    unsigned int DegC;   //Result in Celsius 
    unsigned int DegF;   //Result in Fahrenheit

    unsigned char lcdtmp[10];
    unsigned char avg[10];

/************************************
Main
*************************************/
void main(void){
    unsigned char tmp;

    ADCPin = 1;                     //AN0 = Input
    ADCON0 = 0x00;                  //BIT 7:6 Always 00, BIT 5:2 is 0000 for Channel 0 (AN0),
                                    //BIT 1:0 is 00 (A/D Idle and Module is off) until we are done setting up
    ADCON1 = 0x0E;                  //BIT 7:6 Always 00, BIT 5 is 0 for VSS, BIT 4 is 0 for VDD
                                    //BIT 3:0 is 1110 for all digital expect AN0. We need it analog so we can convert it.
    ADCON2 = 0b10001101;            //BIT 7 is 0 for Left Justified, BIT 6 Always 0, BIT 5:3 001 for 2 TAD (A/D Acquisition Time)
                                    //BIT 2:0 is 100 for FOSC/4 for A/D Conversion Clock
    ADCON0bits.ADON = 1;            //Turn on the A/D module
    
    LCD_Init();
    DelayMS(100);

    LCD_LINE(1);
    LCD_STR((unsigned rom char*)"  AtomSoftTech  ");

    LCD_LINE(2);
    LCD_STR((unsigned rom char*)"Temp: ");

	while(1){
      for(tmp=0;tmp<10;tmp++){
        ADCON0bits.GO = 1;          //Start the conversion
        while(ADCON0bits.DONE);     //Wait until it’s done
        result = ADRESH;            //Load ADRESH into result
        result <<= 8;               //Shift over Result 8 bits to the left
        result |= ADRESL;           //OR in our LOW byte to finish off out INT
        avg[tmp] = result;  
        DelayMS(10);
      }       
        result = 0;
        for(tmp=0;tmp<10;tmp++){
            result += avg[tmp];
        }
        result /= 10;
        
        DegC = (result*5)/10;
        DegC--;

        DegF = DegC * 9;
        DegF/=5;
        DegF+=32;

        lcdtmp[0] = DECtoASCII(DegF,'h');
        lcdtmp[1] = DECtoASCII(DegF,'l');
        lcdtmp[2] = 0xDF;
        lcdtmp[3] = 'F';
        lcdtmp[4] = ' ';
        lcdtmp[5] = DECtoASCII(DegC,'h');
        lcdtmp[6] = DECtoASCII(DegC,'l');
        lcdtmp[7] = 0xDF;
        lcdtmp[8] = 'C';
        lcdtmp[9] = ' ';

        LCD_LINE(2);
        LCD_STR((unsigned rom char*)"Temp: ");
        LCD_STR2(lcdtmp);
        DelayMS(1000);
        DelayMS(1000);
	}
}

unsigned char DECtoASCII(unsigned char DEC,char part){
unsigned char temp;
unsigned char myData;
    temp = DECtoBCD(DEC);

    if(part == 'l')
        myData = temp & 0x0F; 

    if(part == 'h') 
        myData = (temp >> 4) & 0x0F;
    
    myData += 0x30;
    return myData;
}
unsigned char DECtoBCD(unsigned char DEC){
unsigned char temph;
unsigned char templ;
unsigned char myData;
    templ = DEC % 10;
    temph = DEC / 10; 
    myData = (temph << 4) | templ;
    
    return myData; //Return myData
}
 

Noggin

Member
Heh more update:

This code averages 10 readings all 10mS apart. So it takes 100ms to get 1 stable reading but heh its worth it...

You wouldn't be able to read it that fast anyway :) But if it bothers you, you could use that digital_filter code I put in your other thread.

Edit: I just went and looked at your code a bit more closely. You have delays of several seconds in the code, I don't think you're worried about the 100mS you spend reading the ADC so nevermind.
 
Last edited:

ghostman11

Well-Known Member
nice one jason, i been studying your code while i learn, much of it goes over my head but i am starting to understand parts of it! is amazing how much and how quick i can learn on this site. i might to start taking back all the bad things ive said about the net :D
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top