CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

lm35 and pic16f877a problem. Help!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
raz



Joined: 22 Feb 2012
Posts: 22

View user's profile Send private message

lm35 and pic16f877a problem. Help!
PostPosted: Sun Jun 10, 2012 3:12 am     Reply with quote

Hi I'm working on a project to monitor temperature and display it on an lcd and control a fan and a heater, based on the room temperature compared with a reference temp. I'm using lm35, pic16f877a and 16x2 lcd.
The problem is when I connect the lm35 to Ra0 of the uc the voltage drops tp 0.18 and is constant even if increase the temperature near the lm35. The o/p of lm35 is varying but once it is connected to uc it drops to 0.18 and lcd displays constant 18 reading. What might be the problem ?
I'm connecting 0.1uf at the i/p and o/p of regulator.
Ps: The voltage at Vdd varies between 5, 4.98 and 4.6v.

Code:

#include <16F877A.H>
#device ADC=10
#fuses HS, NOWDT, NOPROTECT, NOPUT
#use delay(clock = 20000000)

#include "flex_lcd.c"


#define PwrLed Pin_b1
#define Cool Pin_b4
#define Heat Pin_b5

void InitializePorts(void);
void BlinkLed(int,int,int);
void adc(void);
void Lcd_display(void);

float Value,Temp,rtemp;

void main(void)
{
   
   rtemp=30;
   
  //Initialize ports
  InitializePorts();
 
  //Blink power led
  BlinkLed(PwrLed,3,3000);
  output_high(PwrLed);
   
  //Adc
  setup_adc_ports(RA0_ANALOG);//Ra0 set to analog
  setup_adc(ADC_CLOCK_DIV_8); 
  set_adc_channel(0);
     
 
  lcd_init();
  printf(lcd_putc,"Now Temp:");
  printf(lcd_putc,"\nRef Temp:");
   
 
  while(true)
  {
 
     
      Value=read_adc();//get ADC value
      Temp=(Value*0.5); //Convert ADC value to Temperature in Celcius
   
    lcd_gotoxy(11,1);
    printf(lcd_putc,"%g",Temp);
    lcd_gotoxy(11,2);
    printf(lcd_putc,"%g",rtemp);
      delay_ms(3000);
     
     
     
    // Compare temperatures to control appliances
     
     if(Temp>rtemp)  //Its hot
     {
        output_high(Cool); //Cool turn ac on
        output_low(Heat);
     }
     if(Temp<rtemp) //ITs cold turn heater
     {
        output_high(Heat);   //Heat turn heater on
        output_low(Cool);
     }
     if(Temp==rtemp)
     {
        output_low(Cool);
        output_low(Heat);
     }   
   
  }
}   
     
void InitializePorts(void)
{
  //define ports
  set_tris_a(0b000001);
  set_tris_b(0);
  set_tris_c(0);
  set_tris_e(0);
 
  //set ports
  output_a(0);
  output_b(0);
  output_c(0);
  output_e(0);
}
void BlinkLed(int whatPin ,int howManyTimes , int Time )
{
  int i=0;
  for(i=0;i<howManyTimes;i++)
  {
  output_High(whatPin);
  delay_ms(Time);
  output_low(whatPin);
  delay_ms(Time);
  }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19481

View user's profile Send private message

PostPosted: Sun Jun 10, 2012 4:00 am     Reply with quote

The problem is that you are driving A0 low.

As soon as you use 'output_a(0)', you tell CCS you want to perform byte wide output functions on porta, and this overrides the TRIS setting.

So:

Code:

//set ports
output_a(0);
output_b(0);
output_c(0);
output_e(0);
set_tris_a(0b000001); //Override the TRIS _after_ setting the port

The other TRIS settings are not needed - you are saying 'I want the pins to be outputs', with the output functions.

Other things wrong - ADC clock. DIV_8, is _not_ legal at 20MHz. Data sheet. Maximum frequency for DIV_8, is 5MHz. You need to use DIV_32.
Your accuracy is going to be awful, if your supply is varying by 8%. You need to look carefully at how this is generated and smoothed. Normally you'd want a significantly larger capacitor than 0.1uF at the regulator input and output. Typically perhaps 100uF at the input, 5uF at the output, and then 0.1uF _right close to the PIC_, and another right by the LCD. Depends though on the regulator. What is the input voltage?. Remember a regulator like a standard 7805, needs about 8.5v on it's input, before it can maintain 5v on the output. What else is on the supply?. How big are the current limiting resistors to the LED's?. What is the circuit to 'control the fan and heater'?.

Best Wishes
raz



Joined: 22 Feb 2012
Posts: 22

View user's profile Send private message

lm35
PostPosted: Sun Jun 10, 2012 10:56 am     Reply with quote

Thanks for your reply...
The outputs of the pic are connected to a 100R, with a led parallel with 1k resistor, connected to bc337 transistor that drives a relay to turn the fan and exactly the same for the heater. Thats all.
raz



Joined: 22 Feb 2012
Posts: 22

View user's profile Send private message

PostPosted: Sun Jun 10, 2012 10:58 am     Reply with quote

uh and I'm using a 9v battery and an adapter for the relays.
Ttelmah



Joined: 11 Mar 2010
Posts: 19481

View user's profile Send private message

PostPosted: Mon Jun 11, 2012 1:10 am     Reply with quote

One 'nasty', is I don't see any mention of a trap diode across the relay coil. Without this, when the relay switches _off_, the energy stored in the coil, has to go somewhere. It'll normally spike through the drive transistor, potentially damaging the PIC output.
The voltages that can appear across a coil when you release a drive, can be really surprising - this is the basis of the circuits used to drive flashguns off little batteries, easily generating 500v+, and your design _must_ try to avoid this....

Best Wishes
raz



Joined: 22 Feb 2012
Posts: 22

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 1:55 am     Reply with quote

Its workinggggg!!!!!!!!!! Thank you thank thank you ...Smile
Yeah I'm using the trap diodes but didn't mention as they are always connected to the relay.
Ttelmah



Joined: 11 Mar 2010
Posts: 19481

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 3:26 am     Reply with quote

Good.
One has to be a bit 'picky', since it is terrifying how many threads here have large sections where something very 'basic' has been assumed, but is not present. Classics are, current limiting resistors on LED's, pull up resistors on I2C, trap diodes on inductors, etc. etc.. Better to be 'sure'.
Glad it is working now.

Best Wishes
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Jun 27, 2012 11:18 pm     Reply with quote

I just posted my LM35 driver on the Code library... if its any help.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group