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.

Help Needed reagarding PIC interfacing with LM35 code

Status
Not open for further replies.

imaasac

Newbie level 6
Joined
Nov 5, 2010
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,367
hi!!!
I am working on a project and i have no idea on pic programming. My project is to interface PIC with LM35 and when it gets beyond a certain temperature, the alarm should turn ON.

I have programmed somehow from internet but the problem is how to send signal to any of the output ports of micro controller so that from there the alarm should be generated after certain temperature.
Can anyone one help me understanding me the code for PIC.

Thanx

---------- Post added at 11:59 ---------- Previous post was at 11:57 ----------

sorry i forgot to mentione that i need to code in C and i am using 16F877A
 

post your code... it will be rectified.

---------- Post added at 12:42 ---------- Previous post was at 12:37 ----------

//==================inckude========================= ========
#include<pic.h>

//===============configuration====================== ========
__CONFIG (0x3F32);

//===============define IO port=============================
#define lcd PORTC
#define RS RA2
#define E RA5
#define CHANNEL0 0b10000001 // AN0
#define CHANNEL1 0b10001001 // AN1
#define buzzer RB5
#define fanA RB4
#define fanB RB3
#define ledA RB2
#define ledB RB1

//==============FUNCTION PTOTOTYPE=========================
void e_pulse(void);
void delay(unsigned short i);
void send_char(unsigned char data);
void send_config(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void dis_num(unsigned long data);
void increment(unsigned long data);
void read_adc(void);
unsigned short read_temp(void);

//====================MAIN========================== ======
unsigned short result;
unsigned short temp,tempA,tempB;

void main(void)
{
ADRESH=0; //clear A/D result
ADRESL=0; //clear A/D result

//setting ADCON1 Register
ADCON1=0b11000101; // A/D result right justified,
// configure RA2 and RA5 as digital I/O

TRISA=0b11011011; //configure PORTA I/O direction
TRISB=0b00000000; //configure PORTB as output
TRISC=0b00000000; //configure PORTC as output

PORTA=0;
PORTB=0;

while(1)
{
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //Lcd Return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //diplay on, cursor off and cursor blink off
send_config(0b00111000); //function set

lcd_goto(0); //cursor start from beginning

//display character on LCD
send_char(' ');
send_char('T');
send_char('E');
send_char('M');
send_char('P');
send_char('.');
send_char('A');
send_char('=');

lcd_goto(20); //cursor go to 2nd line of the LCD

//display character on LCD
send_char(' ');
send_char('T');
send_char('E');
send_char('M');
send_char('P');
send_char('.');
send_char('B');
send_char('=');

while(1) //infinity loop
{
//sensor A
ADCON0=CHANNEL0; //CHANNEL1=0b10001001
lcd_goto(;

read_adc();

temp=read_temp();
dis_num(temp/10);
send_char('.');
dis_num(temp%10);
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');

tempA=temp;

//sensor B
ADCON0=CHANNEL1; //CHANNEL0=0b10000001

lcd_goto(2;

read_adc();

temp=read_temp();
dis_num(temp/10);
send_char('.');
dis_num(temp%10);
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');

tempB=temp;

if((tempA>400)&&(tempB<350)) // *****************************************
{ // * LED A and Fan A activated only for *
ledA=1; // * temperature A greater than 40'C *
ledB=0; // * and temperature B less than 35'C *
fanA=1; // *****************************************
fanB=0;
buzzer=0;
}

else if((tempB>350)&&(tempA<400)) // *****************************************
{ // * LED B and Fan B activated only for *
ledA=0; // * temperature A less than 40'C and *
ledB=1; // * temperature B greater than 35'C *
fanA=0; // *****************************************
fanB=1;
buzzer=0;
}

else if((tempB>350)&&(tempA>400)) // ************************************************** ***
{ // * All LED A & LED B, Fan A & Fan B and Buzzer *
ledB=1; // * activated for temperature A greater than 40'C *
ledA=1; // * and temperature B greater than 35'C *
fanA=1; // ************************************************** ***
fanB=1;
buzzer=1;
}

else if((tempB<350)&&(tempA<400)) // ************************************************** ***
{ // * All LED A & LED B, Fan A & Fan B and Buzzer *
ledB=0; // * disactivated for temperature A less than 40'C *
ledA=0; // * and temperature B less than 35'C *
fanA=0; // ************************************************** ***
fanB=0;
buzzer=0;
}

delay(2000);

}

}

}



//==================subroutine LCD setting ==========================

void send_config(unsigned char data)
{
RS=0;
lcd=data;
delay(500);
e_pulse();
}

void e_pulse(void)
{
E=1;
delay(500);
E=0;
delay(500);
}

void send_char(unsigned char data)
{
RS=1;
lcd=data;
delay(500);
e_pulse();
}


void lcd_goto(unsigned char data)
{
if(data<16)
{
send_config(0x80+data);
}
else
{
data=data-20;
send_config(0xc0+data);
}
}


void lcd_clr(void)
{
RS=0;
send_config(0x01);
delay(600);
}


void dis_num(unsigned long data)
{
unsigned char hundred_thousand;
unsigned char ten_thousand;
unsigned char thousand;
unsigned char hundred;
unsigned char tenth;

hundred_thousand = data/100000;
data = data % 100000;
ten_thousand = data/10000;
data = data % 10000;
thousand = data / 1000;
data = data % 1000;
hundred = data / 100;
data = data % 100;
tenth = data / 10;
data = data % 10;

if(hundred_thousand>0)
{
send_char(hundred_thousand + 0x30); //0x30 added to become ASCII code
send_char(ten_thousand + 0x30);
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}

else if(ten_thousand>0)
{
send_char(ten_thousand + 0x30); //0x30 added to become ASCII code
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(thousand>0)
{
send_char(thousand + 0x30); //0x30 added to become ASCII code
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(hundred>0)
{
send_char(hundred + 0x30); //0x30 added to become ASCII code
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(tenth>0)
{
send_char(tenth + 0x30); //0x30 added to become ASCII code
send_char(data + 0x30);
}
else send_char(data + 0x30); //0x30 added to become ASCII code
}

void increment(unsigned long data)
{
unsigned short j;
for(j=10;j>0;j--)
{ lcd_goto(32);
data=data+1;
dis_num(data);
delay(10000);
}

}

//==================subroutine ADC=========================

void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1) //looping 2000 times for getting average value
{
ADGO = 1; //ADGO is the bit 2 of the ADCON0 register
while(ADGO==1); //ADC start, ADGO=0 after finish ADC progress
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC

result_temp+=result;
}
result = result_temp/2000; //getting average value

}

unsigned short read_temp(void)
{
unsigned short temp;
temp=result;
return temp;

}

//==================subroutine DELAY==========================
void delay(unsigned short i)
{
for(;i>0;i--);
}


after printing the temperature.. ADD your code to check and turn on the buzzer whenever required

---------- Post added at 12:44 ---------- Previous post was at 12:42 ----------

//==================inckude========================= ========
#include<pic.h>

//===============configuration====================== ========
__CONFIG (0x3F32);

//===============define IO port=============================
#define lcd PORTC
#define RS RA2
#define E RA5
#define CHANNEL0 0b10000001 // AN0
#define CHANNEL1 0b10001001 // AN1
#define buzzer RB5
#define fanA RB4
#define fanB RB3
#define ledA RB2
#define ledB RB1

//==============FUNCTION PTOTOTYPE=========================
void e_pulse(void);
void delay(unsigned short i);
void send_char(unsigned char data);
void send_config(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void dis_num(unsigned long data);
void increment(unsigned long data);
void read_adc(void);
unsigned short read_temp(void);

//====================MAIN========================== ======
unsigned short result;
unsigned short temp,tempA,tempB;

void main(void)
{
ADRESH=0; //clear A/D result
ADRESL=0; //clear A/D result

//setting ADCON1 Register
ADCON1=0b11000101; // A/D result right justified,
// configure RA2 and RA5 as digital I/O

TRISA=0b11011011; //configure PORTA I/O direction
TRISB=0b00000000; //configure PORTB as output
TRISC=0b00000000; //configure PORTC as output

PORTA=0;
PORTB=0;

while(1)
{
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //Lcd Return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //diplay on, cursor off and cursor blink off
send_config(0b00111000); //function set

lcd_goto(0); //cursor start from beginning

//display character on LCD
send_char(' ');
send_char('T');
send_char('E');
send_char('M');
send_char('P');
send_char('.');
send_char('A');
send_char('=');

lcd_goto(20); //cursor go to 2nd line of the LCD

//display character on LCD
send_char(' ');
send_char('T');
send_char('E');
send_char('M');
send_char('P');
send_char('.');
send_char('B');
send_char('=');

while(1) //infinity loop
{
//sensor A
ADCON0=CHANNEL0; //CHANNEL1=0b10001001
lcd_goto(;

read_adc();

temp=read_temp();
dis_num(temp/10);
send_char('.');
dis_num(temp%10);
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');

tempA=temp;

//sensor B
ADCON0=CHANNEL1; //CHANNEL0=0b10000001

lcd_goto(2;

read_adc();

temp=read_temp();
dis_num(temp/10);
send_char('.');
dis_num(temp%10);
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');

tempB=temp;

if((tempA>400)&&(tempB<350)) // *****************************************
{ // * LED A and Fan A activated only for *
ledA=1; // * temperature A greater than 40'C *
ledB=0; // * and temperature B less than 35'C *
fanA=1; // *****************************************
fanB=0;
buzzer=0;
}

else if((tempB>350)&&(tempA<400)) // *****************************************
{ // * LED B and Fan B activated only for *
ledA=0; // * temperature A less than 40'C and *
ledB=1; // * temperature B greater than 35'C *
fanA=0; // *****************************************
fanB=1;
buzzer=0;
}

else if((tempB>350)&&(tempA>400)) // ************************************************** ***
{ // * All LED A & LED B, Fan A & Fan B and Buzzer *
ledB=1; // * activated for temperature A greater than 40'C *
ledA=1; // * and temperature B greater than 35'C *
fanA=1; // ************************************************** ***
fanB=1;
buzzer=1;
}

else if((tempB<350)&&(tempA<400)) // ************************************************** ***
{ // * All LED A & LED B, Fan A & Fan B and Buzzer *
ledB=0; // * disactivated for temperature A less than 40'C *
ledA=0; // * and temperature B less than 35'C *
fanA=0; // ************************************************** ***
fanB=0;
buzzer=0;
}

delay(2000);

}

}

}



//==================subroutine LCD setting ==========================

void send_config(unsigned char data)
{
RS=0;
lcd=data;
delay(500);
e_pulse();
}

void e_pulse(void)
{
E=1;
delay(500);
E=0;
delay(500);
}

void send_char(unsigned char data)
{
RS=1;
lcd=data;
delay(500);
e_pulse();
}


void lcd_goto(unsigned char data)
{
if(data<16)
{
send_config(0x80+data);
}
else
{
data=data-20;
send_config(0xc0+data);
}
}


void lcd_clr(void)
{
RS=0;
send_config(0x01);
delay(600);
}


void dis_num(unsigned long data)
{
unsigned char hundred_thousand;
unsigned char ten_thousand;
unsigned char thousand;
unsigned char hundred;
unsigned char tenth;

hundred_thousand = data/100000;
data = data % 100000;
ten_thousand = data/10000;
data = data % 10000;
thousand = data / 1000;
data = data % 1000;
hundred = data / 100;
data = data % 100;
tenth = data / 10;
data = data % 10;

if(hundred_thousand>0)
{
send_char(hundred_thousand + 0x30); //0x30 added to become ASCII code
send_char(ten_thousand + 0x30);
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}

else if(ten_thousand>0)
{
send_char(ten_thousand + 0x30); //0x30 added to become ASCII code
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(thousand>0)
{
send_char(thousand + 0x30); //0x30 added to become ASCII code
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(hundred>0)
{
send_char(hundred + 0x30); //0x30 added to become ASCII code
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(tenth>0)
{
send_char(tenth + 0x30); //0x30 added to become ASCII code
send_char(data + 0x30);
}
else send_char(data + 0x30); //0x30 added to become ASCII code
}

void increment(unsigned long data)
{
unsigned short j;
for(j=10;j>0;j--)
{ lcd_goto(32);
data=data+1;
dis_num(data);
delay(10000);
}

}

//==================subroutine ADC=========================

void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1) //looping 2000 times for getting average value
{
ADGO = 1; //ADGO is the bit 2 of the ADCON0 register
while(ADGO==1); //ADC start, ADGO=0 after finish ADC progress
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC

result_temp+=result;
}
result = result_temp/2000; //getting average value

}

unsigned short read_temp(void)
{
unsigned short temp;
temp=result;
return temp;

}

//==================subroutine DELAY==========================
void delay(unsigned short i)
{
for(;i>0;i--);
}


after printing the temperature.. ADD your code to check and turn on the buzzer whenever required
 

hi!!!
I am working on a project and i have no idea on pic programming. My project is to interface PIC with LM35 and when it gets beyond a certain temperature, the alarm should turn ON.

I have programmed somehow from internet but the problem is how to send signal to any of the output ports of micro controller so that from there the alarm should be generated after certain temperature.
Can anyone one help me understanding me the code for PIC.

Thanx

---------- Post added at 11:59 ---------- Previous post was at 11:57 ----------

sorry i forgot to mentione that i need to code in C and i am using 16F877A

Pls don't ask about code, you should ask about algorithm.

How do u design PCB ? Have you ever working with ADC, LCD... with PIC ? Have u got any more ideas ? More detail question, more detail answer !
 

Pls don't ask about code, you should ask about algorithm.

How do u design PCB ? Have you ever working with ADC, LCD... with PIC ? Have u got any more ideas ? More detail question, more detail answer !

i agree with pk178....I suggest you not to work on any projects as you dont know basics of PIC... no programming knowledge of PIC.... project is not something you take from internet and copy paste it.....
its something you break your head and do it..... else there are many places where they do project and give you if you pay money to them

If its academic project then your approach is not fair
 

Pls don't ask about code, you should ask about algorithm.

How do u design PCB ? Have you ever working with ADC, LCD... with PIC ? Have u got any more ideas ? More detail question, more detail answer !

Thanx for your replies and showing interest. Look i got an academic project in a group and we dont know much about PIC, but we are keen to learn this contoller and do our project with it. Dear we will make on vero board.

Today i have worked a little on PIC-C compiler and i have completed till the if condition.
if(temperature>40)
//then how can I send signal to output port of Controller, so that the alarm goes ON.

and pls tell me how can i do it
 

Complete code is given to you.... all you need is to check the condition and change the status of port pin to which buzzer / alarm is connected as long as you want....
 

It is very simple.
which compiler you are using
if i will done it in ccs i use the following statements
Code:
if(temprature>40)
{
 output_high(PIN_D0);//if i want to use pin d0 of port d.
}
else
{
 output_low(PIN_D0);
}
 
If you are new in PIC microcontroller, you should work with some simple things, such as : Led blinking, led 7 segs, LCD, ADC,so on...

When you're familiar with those modules, you'll see that your project with LM35 is quite simple !

So pls don't ask about your project until you've already familiar with those modules.

Best regard !
 

It is very simple.
which compiler you are using
if i will done it in ccs i use the following statements

I am using PIC-C compiler. how can i do there? thanx
 

I haven't any idea about PIC-C compiler but the key is that when the temprature is above 40 you have to high the pin and if temprature is less than 40 then low the pin.connect the led at the pin with resistor and then ground it and see if the led is turn on and off while temprature is changing.

let try it
Code:
#bit portd0 = PORTD.0 //define it before the main loop
 
//then in the main loop
trisd0 = 0;//make the pin D0 as output
 
if(temprature>40)
{
 portd0 = 1;//high the pin
}
else
{
 portd0 = 0;//low the pin
}
 
Last edited:
Hi,
You have gotten a complete code, but I'll explain the thing a little bit.
You're sensing the input through ADC. ADC is 10-bit. Therefore the maximum value returned by the ADC will be (2^10)-1 = 1023. Therefore value will be from 0 to 1023. The power supply is (I assume) 5v. The value returned by the ADC is (Vin/Vout)*1023[remember that this is the maximum value). So, when input is 5v, your ADC returns 1023.
The LM35 outputs a voltage linearly, ie, 10mV/'C. So, 40'C outputs 400mV. So, value returned by the ADC is (0.4/5)*1023 = 81.
The alarm can be a buzzer driven by a transistor which is turned on by PIC. Let's assume the transistor is connected to PORTD0. So outputting a 1 on RD0[PORTD0] turns on the buzzer. Clearing the output to 0 turns the buzzer off.
If you need a short simple code, I can write one up for you using mikroC. If mikroC is okay for you, the code will be VERY small, less than 100 lines.

Hope this helps.
Tahmid.
 
@coolman: thanks dear... i will try it.
@tahmid: Thank U so much, i have already made out the formula. If you can do the code for me, i will try to know how acutally it works. Thank You dear
 

Hi,
Here's a simple sample code:
Code:
#define Buzzer RD0_bit //Buzzer at RD0 [PORTD bit 0]

unsigned int Temp;
unsigned long TripTemp;
const TripTempC = 40; //Temperature (in 'C). Buzzer sounds when temperature > this temp

void main() {
     TRISA = 0x01; //RAO(AN0) is input
     TRISD = 0; //ALL OUTPUT
     PORTD = 0; //CLEAR INITIAL STATES
     TripTemp = (TripTempC * 1023)/500; //Calculate ADC reference for trip voltage

     while (1){
           Temp = ADC_Read(0); //Read off AN0 (ADC CH0) and return value to Temp
           if (Temp > TripTemp){ //If temperature > Defined preset
              Buzzer = 1; //Turn on buzzer
           }
           else{ //If temperature < Defined preset
              Buzzer = 0; //Turn off buzzer
           }
     }
}
Hope this helps.
Tahmid.
 
Last edited:
@Tahmid: Thank U dear.. I will try this now..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top