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.

[SOLVED] Temperature sensor using pic18f452...require urgent help!!

Status
Not open for further replies.
huzeeigat,

Also, understand the while(1) loop.

Code:
while(1)
{
//......
//......
}

The condition for the above loop is ALWAYS true. So, everything within the braces/parentheses is always continuously executed.

If you use while(1) loop in the temperature routine, the controller will never exit from there.

Hope this helps.
Tahmid.
 

@Tahmid: thanks for the knowledge of the while loops...
I changed the code but...the circuit doesnt work as i want in proteus...
 

You should go slowly. Instead of making the existing code work, work slowly in parts. First, try to make a code for checking temperature and lighting a LED if it's below 25'C. Then move forwards.
 

@Arun: I referred PIC Microcontroller and Embedded Systems using assembly and c for pic 18 by mazidi...i got the conversion of temperatur and adc ka part from there..rest all the loops for comparing are written by me..

---------- Post added at 23:19 ---------- Previous post was at 23:18 ----------

@Arun: Can u make a code for me according to my requirements.. as i mentioned in the start of my thread..?
 

You can start with this code:

Code:
#include <p18f452.h>

#pragma config WDT=OFF //Turn watchdog timer off
#pragma config OSC=XT //For 4MHz oscillator

unsigned int temp1=102;
//25'C gives LM35 voltage of 0.25V, Reference voltage = 2.5V,
//    so 0.25V gives ADC reading of (0.25/2.5)*1023 = 102

unsigned int temp2=27;
unsigned int itime;
unsigned char l_byte,h_byte;
unsigned int bin_temp;

void temperature(void);
void delay(void);

void main()
{

	ADCON0=0X81;
	ADCON1=0XC5;
	
	TRISD=0;
	TRISAbits.TRISA0=1;
	TRISAbits.TRISA2=1;

while(1){
	temperature();
	if (bin_temp > temp1){ //If temperature > 25'C
		PORTDbits.RD3 = 1;
	}
	else{
		PORTDbits.RD3 = 0;
	}

}
}

void temperature(void)
{

delay();

ADCON0bits.GO=1;
while(ADCON0bits.DONE==1);

l_byte=ADRESL;
h_byte=ADRESH;

bin_temp=(h_byte << 8)|l_byte;
//Results direct result of conversion without processing
}

void delay(void)
{
		int i,j;
		for(i=0;i<=250;i++)
		for(j=0;j<=135;j++)
         {
          }
}

This checks if temperature is > 25'C or not. If temperature > 25'C, RD3=1, else RD3=0.

Using this code and the ideas in it, you should be able to write the rest.

Hope this helps.
Tahmid.
 
@Tahmid: That was of great help..the above code is running successfully..buddy..i will try with my conditions now and see if it works...:):):)
 

Try to use the if...else conditions instead of while loops, so that execution is repeated in the while(1) loop and not stuck in a particular while loop. You can see, this is what I've done in the above code. The rest should be simple enough. However, if you get stuck, I'll be glad to help you.
 
@Tahmid: i used the if else conditions and was successfully able to execute the program..thanks alot once again..:):)
 

Glad I could be of help.

For anyone else who might be coming to this thread for help (eg. via Google), here is a simple way to do the operation (and this is also probably what you did, huzeeigat):


Code:
#include <p18f452.h>

#pragma config WDT=OFF //Turn watchdog timer off
#pragma config OSC=XT //For 4MHz oscillator

unsigned int temp1=102;
//25'C gives LM35 voltage of 0.25V, Reference voltage = 2.5V,
//    so 0.25V gives ADC reading of (0.25/2.5)*1023 = 102

unsigned int temp2=110;
//27'C gives LM35 voltage of 0.27V, Reference voltage = 2.5V,
//    so 0.27V gives ADC reading of (0.27/2.5)*1023 = 110

unsigned int itime;
unsigned char l_byte,h_byte;
unsigned int bin_temp;

void temperature(void);
void delay(void);

void main()
{

	ADCON0=0X81;
	ADCON1=0XC5;
	
	TRISD=0;
	TRISAbits.TRISA0=1;
	TRISAbits.TRISA2=1;

while(1){
	temperature();
	if (bin_temp < temp1){ //If temperature < 25'C
		PORTDbits.RD3 = 1;
	}
	else{
		PORTDbits.RD3 = 0;
	}
	if (bin_temp > temp2){ //If temperature > 27'C
		PORTDbits. RD2 = 1;
	}
	else{
		PORTDbits.RD2 = 0;
	}
}
}

void temperature(void)
{

delay();

ADCON0bits.GO=1;
while(ADCON0bits.DONE==1);

l_byte=ADRESL;
h_byte=ADRESH;

bin_temp=(h_byte << 8)|l_byte;
//Results direct result of conversion without processing
}

void delay(void)
{
		int i,j;
		for(i=0;i<=250;i++)
		for(j=0;j<=135;j++)
         {
          }
}

If temperature < 25'C, RD3 = 1, if temperature > 27'C, RD2 = 1, if temperature is between 25'C and 27'C or equal to 25'C or 27'C, both RD3 = 0 and RD2 = 0.

The code logic is pretty simple.

Hope this helps.
Tahmid.
 
@Tahmid: Absolutely correct bro..!!!:)

---------- Post added at 23:58 ---------- Previous post was at 23:54 ----------

@Tahmid: I have question as the above program has a oscillator frequency of 4Mhz...Will i have a problem in hardware if i use a 10Mhz crystal for the pic...?

---------- Post added 19-03-12 at 00:02 ---------- Previous post was 18-03-12 at 23:58 ----------

please also explain me the line
bin_temp=(h_byte << 8)|l_byte; from the above program...
 

If you use 10MHz crystal, a small change needs to be made. Firstly, select HS as the oscillator type and not XT.

So, you change this:
Code:
#pragma config OSC=XT //For 4MHz oscillator
to
Code:
#pragma config OSC=HS

In general for 8MHz and above, use HS setting. For 1MHz to 8MHz, use XT. XT and HS are both for crystal oscillators/resonators.

Also, take care of the delay before ADC conversion start - the sampling delay. In this program, you don't need to change it. However, always take care about this time and ensure that it is greater than the minimum specified in the datasheet.

Hope this helps.
Tahmid.
 
@Tahmid: I have a last query..for this circuit do i need to connect it to amplifier and then give it to pic or is the circuit made by me correct?
 

Here, you don't need amplifier. The voltage is sufficient to be pretty accurately read by the PIC ADC. So, amplifier is not needed.

Hope this helps.
Tahmid.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top