0% found this document useful (0 votes)
92 views

lm35 PDF

This document contains code for an embedded system that uses an ADC to read temperature sensor values and display the temperature in Celsius and Fahrenheit on an LCD screen. It initializes the ADC and LCD, takes temperature readings in a loop, converts the values to Celsius and Fahrenheit, displays the values on the LCD, and includes functions to convert numbers to BCD and ASCII formats for display.
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)
92 views

lm35 PDF

This document contains code for an embedded system that uses an ADC to read temperature sensor values and display the temperature in Celsius and Fahrenheit on an LCD screen. It initializes the ADC and LCD, takes temperature readings in a loop, converts the values to Celsius and Fahrenheit, displays the values on the LCD, and includes functions to convert numbers to BCD and ASCII formats for display.
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/ 5

D:\Users\Jason\Pictures\blog\lm35\LM35_ADC\18f2525.

/* *****************************************************************************
; *
; 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 its 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;
1
D:\Users\Jason\Pictures\blog\lm35\LM35_ADC\18f2525.c

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


}

2
D:\Users\Jason\Pictures\blog\lm35\LM35_ADC\lcd.c

/* *****************************************************************************
; *
; 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 its 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
1
D:\Users\Jason\Pictures\blog\lm35\LM35_ADC\lcd.c

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


}

2
D:\Users\Jason\Pictures\blog\lm35\LM35_ADC\lcd.h

#ifndef __LCD_H
#define __LCD_H

#include <delays.h>
#include <p18cxxx.h>

#define ENDIAN LOWER //0 = 0:3 *** 1 = 4:7

#define LCD_PORT LATD //LCD DATA PORT


#define LCD_TRIS TRISD //LCD DATA DIRECTION PORT
#define LCD_DAT PORTD //LCD INPUT DATA VALUES

#define LCD_RS LATDbits.LATD4 //LCD Command/Data Control


#define LCD_E LATDbits.LATD5 //LCD Enable Line
//#define LCD_RW LATBbits.LATB6 //LCD Read/Write Control

#define LCD_TRIS_RS TRISDbits.TRISD4


#define LCD_TRIS_E TRISDbits.TRISD5
//#define LCD_TRIS_RW TRISBbits.TRISB4

#define LINE1 0x80


#define LINE2 0xC0
#define LINE3 0x94
#define LINE4 0xD4

#define CMD 0
#define TXT 1

#define LOWER 0
#define UPPER 1

#define LOWEND 0x08


#define UPPEND 0x80

#define LOWMSK 0x0F


#define UPPMSK 0xF0

#define DISPLAY 1
#define CURSOR 0

#define RIGHT 1
#define LEFT 0

#define MHz *1000000


#define MYFOSC 20MHz

void WaitLCDBusy(void);
void LCD_Init(void);
void LCD_DATA(unsigned char data,unsigned char type);
void LCD_NYB(unsigned char nyb);
void LCD_STR(unsigned rom char *text);
void LCD_LINE(char line);
void ShiftDisplay(unsigned char DC,unsigned char RL);
void DelayMS(unsigned int ms);
void LCD_CLEAR(unsigned char line);
void LCD_POS(unsigned char line);
void LCD_STR2(unsigned char *text);
extern unsigned char BF;
#endif

You might also like