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.

TEMPERATURE SENSOR CODE NEEDED USING PIC18F452

Status
Not open for further replies.

helloarloha

Newbie level 2
Newbie level 2
Joined
Oct 12, 2009
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
malaysia
Activity points
1,298
we are required to setup the PIC18F452 and enable the ADC module to read a temperature sensor uisng PIC...can anyone help to provide the code? thx in advance...
 

Hi,
What sensor you want to use and what is the highest limit of temperature? What should be the Language, Assembly or C or Mikrobasic?
 

it can be in IN MPLAB or MIcro C or CCS...I'm using LM 35 and the temperature range is not mentioned..but we can juz set it as maybe 10-50...

Added after 2 minutes:

if assembly is more preferable since i'm learning hard to it...if not micro C or CCS code also acceptable....thx...
 


Hi helloarloha,
Here I present a simple temperature sensor using PIC18F452 and LCD in mikroBASIC.

Code:
program thermometer452

dim LCD_RS as sbit at RB4_bit
    LCD_EN as sbit at RB5_bit
    LCD_D4 as sbit at RB0_bit
    LCD_D5 as sbit at RB1_bit
    LCD_D6 as sbit at RB2_bit
    LCD_D7 as sbit at RB3_bit

    LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB3_bit

dim ADCResult as longword
dim value as word[3]
dim vstring as string[3]

sub procedure GlobInit
    TRISB = 0
    PORTB = 0
    TRISA = 1
    ADCON1 = $4E
    LCD_Init
    LCD_Cmd(_LCD_CLEAR)
    LCD_Cmd(_LCD_CURSOR_OFF)
    LCD_Out(1, 1, "Temp:")
    LCD_Out(1, 15, "'C")
end sub

main:
     GlobInit
     while true
           ADCResult = (ADC_Read(0) * 500) >> 10
           value[0] = ADCResult div 100
           value[1] = (ADCResult div 10) mod 10
           value[2] = ADCResult mod 10
           vstring[0] = value[0] + 48
           vstring[1] = value[1] + 48
           vstring[2] = value[2] + 48
           LCD_Out(1, 10, vstring)
           delay_ms(50)
     wend
end.
 

Hi helloarloha,
Here is the equivalent source code in mikroC:

Code:
unsigned long ADCResult;
unsigned int value[3];
char vstring[3];

void GlobInit(void){
    TRISB = 0;
    PORTB = 0;
    TRISA = 1;
    ADCON1 = 0x4E;
    LCD_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0);
    LCD_Cmd(LCD_CLEAR);
    LCD_Cmd(LCD_CURSOR_OFF);
    LCD_Out(1, 1, "Temp:");
    LCD_Out(1, 15, "'C");
}

void main(){
    GlobInit();
    while (1){
          ADCResult = (ADC_Read(0) * 500) >> 10;
          value[0] = ADCResult / 100;
          value[1] = (ADCResult / 10) % 10;
          value[2] = ADCResult % 10;
          vstring[0] = value[0] + 48;
          vstring[1] = value[1] + 48;
          vstring[2] = value[2] + 48;
          LCD_Out(1, 10, vstring);
          delay_ms(50);
    }
}
I am not that good with C18, so I made the software in mikroC instead.
 

/*
*******************************************************************************
* PIC DIGITAL THERMOMETER USING A MICROCHIP MCP9700A ANALOG SENSOR
*******************************************************************************
*
* source code example for mikroC users
* feel free to use this code at your own risks
*
* target : PIC16F877A, 8 Mhz crystal
* HS clock, no watchdog.
*
* easyPIC4 settings :
* MCP9700A on DS1820 socket, see web page for more details.
*
* Author : Bruno Gavand, September 2007
* see more details on https://www.micro-examples.com/
*
*******************************************************************************
*/

/*
* LCD_printfix constants
*/
#define INT_RANGE 1000 // integer part : 3 digits
#define DEC_RANGE 10 // decimal part : 1 digit

/*
* this counter is incremented on each TIMER0 overflow
*/
unsigned int cntr ;

long temp ; // Temperature in Celcius * 10
int fahr ; // Temperature in Fahrenheit * 10

/*
* offset reference of the sensor : 0°C is 500 mV => 102.4
* since the sensor is factory calibrated, there is no need for adjustment
*/
int ref = 1024 ; // offset is multiplied by 10 to get tenth of degrees

/*
* LCD character definitions, generated by mikroC LCD Custom Character tool :
*/
const char characterC[] = {8,20,8,0,3,4,4,3}; // °C
const char characterF[] = {8,20,8,0,7,4,6,4}; // °F

/*
* print character pointed to by def at line pos_row column pos_char on LCD
*/
void CustomChar(const char *def, unsigned char n, char pos_row, char pos_char)
{
char i ;

LCD_Cmd(64 + n * 8) ;
for(i = 0 ; i<=7 ; i++)
{
LCD_Chr_Cp(def) ;
}
LCD_Cmd(LCD_RETURN_HOME) ;
LCD_Chr(pos_row, pos_char, n) ;
}

/*
* print v with fixed-size integer and decimal parts
*/
void LCD_printFix(unsigned int v)
{
unsigned int w ;
unsigned int d ;
unsigned char n ;
unsigned char blk = 1 ; // zero blanking

if(v >= 0)
{
LCD_Chr_Cp('+') ;
}
else
{
LCD_Chr_Cp('-') ;
}

v = abs(v) ;
w = v / DEC_RANGE ;
for(d = INT_RANGE / 10 ; d > 0 ; d /= 10)
{
n = (w / d) % 10 ;
if(n)
{
blk = 0 ;
}
if(blk)
{
LCD_Chr_Cp(' ') ;
}
else
{
LCD_Chr_Cp('0' + n) ;
}
}
LCD_Chr_Cp('.') ;
w = v % DEC_RANGE ;
for(d = DEC_RANGE / 10 ; d > 0 ; d /= 10)
{
LCD_Chr_Cp('0' + (w / d) % 10) ;
}
}

/*
* interrupt routine, called on each timer0 overflow
*/
void interrupt(void)
{
if(INTCON.T0IF) // timer 0 overflow ?
{
cntr++ ; // increment counter
INTCON.T0IF = 0 ; // done
}
}

/*
* program entry
*/
void main()
{
ADCON1 = 0x00 ; // set PORTA as analog input
TRISA = 0xff ; // set PORTA as inputs
TRISD = 0 ; // PORTD is output
LCD_Init(&PORTD) ; // Initialize LCD connected to PORTD
LCD_Cmd(Lcd_CLEAR) ; // Clear display
LCD_Cmd(Lcd_CURSOR_OFF) ; // Turn cursor off
LCD_Out(1, 1, "MCP9700A EXAMPLE"); // Print welcome message

OPTION_REG = 0x80 ; // start timer 0, no prescaler
INTCON = 0xA0 ; // allow timer 0 overflow interrupt

for(;;) // forever
{
if(cntr >= 4000) // if enough time since last sample
{
/*
* read the sensor
*/
temp = Adc_Read(7) * 10 - ref ; // read RE2 ADC, adjust to 0°C

/*
* get the result in celcius * 10
* sensor temperature coefficient is +10mV/°C
* ADC resolution is 5000/1024 = 4.88 mV so one ADC point is 0.488°C
*/
temp *= 488 ;
temp /= 1000 ;

fahr = ((9 * temp) / 5 ) + 320 ; // convert C degrees to F * 10

/*
* print temperature in °C on LCD
*/
LCD_Out(2, 1, "") ;
LCD_printFix(temp) ;
CustomChar(characterC, 0, 2, 7) ;

/*
* print temperature in °F on LCD
*/
LCD_Out(2, 10, "") ;
LCD_printFix(fahr) ;
CustomChar(characterF, 1, 2, 16) ;

cntr = 0 ; // clear counter
}
}
}
 

hi, I'm doing a project which uses PIC18F452 to read LM35DZ temperature..I don't know why the temperature value on my LCD is 0.00 'C..is there any coding I need to write to configure the sensor?
 

Hi sunahm,
You're probably not reading the LM35 properly, check your ADC initialization and reading part in the code.
If you can, posting the code would help.
Hope this helps.
Tahmid.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top