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

LM35DZ Temperature Sensor

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



Joined: 04 Dec 2007
Posts: 11

View user's profile Send private message

LM35DZ Temperature Sensor
PostPosted: Mon Dec 17, 2007 10:08 am     Reply with quote

i hope i could get some help here

Code:
#include <16f887.h>
#fuses HS,NOWDT,NOPUT,NOPROTECT,NODEBUG,BROWNOUT,NOLVP,NOCPD,NOWDT
#device adc=10
#use delay(clock=4000000)
#include "flex_lcd.c"


void main()
{
int16 adc_value;
float volts;

setup_comparator(NC_NC_NC_NC);
 


setup_adc_ports(sAN5);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(5);

lcd_init();

while(1)
{

  adc_value = read_adc();
volts = (adc_value*5)/1023;   
   printf(lcd_putc, "\f%3.3f", volts);
   delay_ms(500);}

}


i ran the code and the lcd didnt show the conversion.
vout of lm35dz to re0
&
gnd to re1

i checked and the lcd is working

i set up the connections based on the following link...
http://users.tpg.com.au/gramo/Site/lm35dz.htm

please advice. thanks !
Ttelmah
Guest







PostPosted: Mon Dec 17, 2007 10:47 am     Reply with quote

What did it show?.
There is an obvious problem in the arithmetic. Your line:
volts = (adc_value*5)/1023;

needs to be:

volts = (adc_value*5)/1023.0;

As written, if you have an ADC value of (say) 100, the code will calculate 5*100 (integer), and divide this by 1023 (integer), to get 0.

In C, the type used for arithmetic, is the 'highest' type for the components involved in the arithmetic. In this case, int_16. You need to force a component to be a _float_, to get the float result from float arithmetic (otherwise you will get the result of integer arithmetic, converted to a float). Adding a decimal point to the declaration of the divisor, forces this to be a float constant, and float arithmetic to be used here.

Best Wishes
xbt87



Joined: 04 Dec 2007
Posts: 11

View user's profile Send private message

PostPosted: Mon Dec 17, 2007 11:05 pm     Reply with quote

Thanks! Smile

erm but another problem occured..
the hyperterminal is not showing anything. with this code


Code:

#include <16f887.h>
#fuses HS,NOWDT,NOPUT,NOPROTECT,NODEBUG,BROWNOUT,NOLVP,NOCPD,NOWDT
//#fuses HS,NOWDT,NOPROTECT,NOLVP
#device adc=10
#use delay(clock=4000000)
#include <flex_lcd.c>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)



void main()
{

float voltage,tempdeg,  temp;

  lcd_init();      // always call this 1st
lcd_gotoxy(1,1);
  lcd_putc("volt:");
lcd_gotoxy(1,2);
   lcd_putc("temp:");

set_tris_e(0xff);
 setup_adc_ports(sAN5);
   setup_adc(adc_clock_div_32);
setup_comparator(NC_NC_NC_NC);



  do
   {
      set_adc_channel(5);  //re0/an5
      delay_us(500);        //delay
      temp = read_adc();    // starts the conversion/read the rsult

      voltage = (temp*5)/1023.0;
      tempdeg = 100*voltage;

  lcd_gotoxy(8,1);
     printf(lcd_putc,"%2.7f",voltage);
     lcd_gotoxy(8,2);
      printf(lcd_putc,"%2.7f",tempdeg);
      delay_ms(1000);
 
   }while (true);

[b]do
{

      getch();
       printf("%2.7f",voltage);
  delay_ms(1000);
       getch();
        printf("%2.7f",tempdeg);
   }while (true);[/b]

 
}


please help. thanks.
Ttelmah
Guest







PostPosted: Tue Dec 18, 2007 3:41 am     Reply with quote

Two things:

1) How is it ever going to send anything to hyperterminal?.
As shown, the code sits sending to the LCD. It'll never reach the second lot of code to send stuff to hyprterminal.....

2) Have you got a working serial connection?. If you are testing with the serial, prove this is working, before wondering if something else is wrong. A simple single line at the start of the main, sending something like '"Sytem alive", will prove you have working comms. You are using 'HS' for the oscillator, but have the crystal speed shown as 4MHz. HS is borderline at this speed (XT is recommended for 4MHz). Generally, unless you have a very low gain crystal, HS will overdrive it at 4MHz, and can result in the oscillator running significantly 'off frequency', which will affect serial comms. How is your serial wired?.

Best Wishes
xbt87



Joined: 04 Dec 2007
Posts: 11

View user's profile Send private message

PostPosted: Wed Dec 19, 2007 12:49 am     Reply with quote

Thanks for the tips!
it's working now Smile
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