LM35, Protues and MikroC Pro

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Eng_Bandar
Posts: 65
Joined: 04 Jun 2010 01:13

LM35, Protues and MikroC Pro

#1 Post by Eng_Bandar » 18 Sep 2010 01:16

Hi all again ,

I did project in LM35 sensor and show temp in LCD in proteus every thing ok but problem only in values from 0 to 4 if I put LM35 equal 0 in LCD show 1 and if I put it 1 it show in LCD 2 Why?

and this is important part in code

Code: Select all

unsigned int adc,adc2;
char k[6];
void main() {
 ADCON1 = 0x80;
 TRISA = 0xFF;
.
.
.
adc = Adc_Read(2);
 adc2 = 0.245*adc*2;
 ByteToStr(adc2,k);

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: LM35, Protues and MikroC Pro

#2 Post by drdoug » 18 Sep 2010 02:42

I see two potential problems with the code posted.

You defined adc as integer which is fine and it will hold your 10 bit ADC reading.
Then you multiply by 0.245 which you will end up with a float value. I don't think the value will be stored properly in the integer adc. Also, you then convert 8 bits of the 16 bit value to a string (I think it converts the lower 8 bits but I am not 100% sure).
Maybe try IntToStr() instead of ByteToStr(). If you decide to use float values all the way through, you will want to use FloatToStr().

Also, to save a few lines of code 0.245*2 = 0.49

Code: Select all

unsigned int adc;
char k[15];
float adc2;
void main() {
ADCON1 = 0x80;
TRISA = 0xFF;
.
.
.
adc = Adc_Read(2);
adc2 = 0.49*adc;
FloatToStr(adc2,k);

Eng_Bandar
Posts: 65
Joined: 04 Jun 2010 01:13

Re: LM35, Protues and MikroC Pro

#3 Post by Eng_Bandar » 18 Sep 2010 03:12

Hi drdoug,

Same problem with your code .

Image

only with 0,1,2,3 and 4

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: LM35, Protues and MikroC Pro

#4 Post by drdoug » 18 Sep 2010 04:24

Please post all of your code. Maybe there is something in there.

Eng_Bandar
Posts: 65
Joined: 04 Jun 2010 01:13

Re: LM35, Protues and MikroC Pro

#5 Post by Eng_Bandar » 18 Sep 2010 14:11

Of course guy, take it

Code: Select all

// LCD module connections
sbit LCD_RS at RB0_bit;sbit LCD_EN at RB1_bit;sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;sbit LCD_D6 at RB4_bit;sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections
unsigned int adc;
char k[15];
float adc2;

void main() {
  ADCON1 = 0x80;              // Configure analog inputs and Vref
  TRISA  = 0xFF;              // PORTA is input
  TRISD=0;PORTD=0;
  Lcd_Init();
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  delay_ms(500);
  while(1) {
           adc = Adc_Read(2);
           adc2 = 0.49*adc;
           FloatToStr(adc2,k);
           lcd_out(2,5,k);
           delay_ms(1000);
  }}
Image

Image

Eng_Bandar
Posts: 65
Joined: 04 Jun 2010 01:13

Re: LM35, Protues and MikroC Pro

#6 Post by Eng_Bandar » 18 Sep 2010 14:35

And also how can I represent negative number

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: LM35, Protues and MikroC Pro

#7 Post by MARIO » 18 Sep 2010 15:17

Hi Eng_Bandar,

take care of Proteus.

Change the resolution of LM35 to 0.1 degrees C and of the voltmeter to millivolts an see that your aproximations are very satisfactory.

With 5.0 degrees I got 51.2 mV not 50.0 mv. What you see is not always you get! adc will be 10 and adc2 will be 4.9. And the remaining numbers should be because of float conversion.

And remember there is a quantization error of the analog converter also.

And there is a problem with LCD updating because with 5.0 deggres it must show exactly 4.9 not 4.969999... The .069999 is leftovers from previous display.
With the simulation not running, set the LM35 to 5.0 and run it to see what happens...or insert this command:

Code: Select all

           lcd_cmd(_LCD_CLEAR);   //add this line
           lcd_out(2,5,k);
           delay_ms(1000);
If you need more precision you should use Vref+ set to 1.50V and change the math, because now 1.50V (150 degrees) is really 1023 to the ADC.

BR.

EDIT: for negative numbers you should offset the output voltage by 0.55V, i.e., for 0 degrees the output should be 550mV.
You'll need some hardware to get this. AN-460 from National semiconductors is a good reference.

Eng_Bandar
Posts: 65
Joined: 04 Jun 2010 01:13

Re: LM35, Protues and MikroC Pro

#8 Post by Eng_Bandar » 24 Sep 2010 17:18

@MARIO
Thank you for your helping
I solved the problem by multiply adc by 0.5 and it came very well.But came new problem with large number like 37. If I put 36 show me 37 Why ? I think problem from math equation but I am not sure.
Also how can I approximate float number I mean if a=1.556, I want put a=1 only
without any number after point just integer number.

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: LM35, Protues and MikroC Pro

#9 Post by MARIO » 24 Sep 2010 18:32

Hi

Well, all the problem is to find the correct parameters. I'll not explain to you all the steps I reached this, but here goes what I did (for Proteus it worked almost fine):

Code: Select all

char k[15];
float adc, adc2;
/*unsigned long adc2;*/

void main() {
  ADCON1 = 0x80;              // Configure analog inputs and Vref
  TRISA  = 0xFF;              // PORTA is input
  TRISD=0;PORTD=0;
  Lcd_Init();
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  delay_ms(500);
  while(1) {
           adc = (498.1277*Adc_Read(2))/1023 + .0261434; //this equation got very good results when used with Proteus. In the real world it could be different.
           //adc2 = (15*adc)/1023;
           FloatToStr(adc,k);
           lcd_cmd(_LCD_CLEAR);   //add this line
           lcd_out(2,5,k);
           delay_ms(1000);
  }}
And about how to approximate float numbers you can use the functions from ANSI C Math library like ceil() and floor(). Read the help file about these functions.
And after that you should format the output string using the ANSI C String library or casting the float variable into an int variable (after rounding it, of course), and converting it to string, using WordToStr() or IntToStr() functions from Conversions library.
OBS.: Code above uses Vref+ set to 1.500V...... (pin RA3 = 1.5V)

BR.

Eng_Bandar
Posts: 65
Joined: 04 Jun 2010 01:13

Re: LM35, Protues and MikroC Pro

#10 Post by Eng_Bandar » 28 Sep 2010 04:22

Thank you MARIO so much for your effort.

your code didn't solve the problem. if you simulated it by proteus can you put it here (circuit) ?

I think best way for this in real.

Thank you guy again

shobhitkukreti
Posts: 52
Joined: 11 Feb 2006 17:35
Location: India

Re: LM35, Protues and MikroC Pro

#11 Post by shobhitkukreti » 28 Sep 2010 08:42

Well, please tell me how did you come with the figure of 0.245. The transfer function for Lm35 would be 10mv/'C. . You are dividing by thousand. Instead of float you could use the mod function and get the values after decimal. Isn't there an example in the example folder for such a design

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: LM35, Protues and MikroC Pro

#12 Post by slavisa.zlatanovic » 28 Sep 2010 12:10

Hi Eng_Bandar!

There is OneWire example distributed with the mikroC PRO for PIC compiler.
------>\Mikroelektronika\mikroC PRO for PIC\Examples\Development Systems\EASYPIC6\One Wire\...
I'm sure that it will be of some use to you.
Best regards
Slavisa

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: LM35, Protues and MikroC Pro

#13 Post by MARIO » 28 Sep 2010 13:47

Hi,
I'm sorry about previous post because I said I was using Vref+ and I wasn't. I was a little confused myself. The formula was for Vref = 5V.
It should work if you remove the voltage from pin RA3/AN3.
If you want to use Vref you should use the formula for adc2, in that code and comment adc, changing variable adc to adc2 into FloatToStr function.
Here goes what I did, already using Vref+ and rounding off the result:

Code: Select all

char k[15];
float adc;
unsigned int temp;

void main() {
  ADCON1 = 0x81;              // Configure analog inputs and Vref+
  TRISA  = 0xFF;              // PORTA is input
  TRISD=0;PORTD=0;
  Lcd_Init();
  Lcd_Cmd(_LCD_CURSOR_OFF);   // Cursor off
  delay_ms(500);
  while(1) {
           adc = 150.*Adc_Read(2)/1023;
           temp = (unsigned int)floor(adc);
           WordToStr(temp,k);
           lcd_cmd(_LCD_CLEAR);  
           lcd_out(2,5,k);
           delay_ms(100);
  }
}
and here is the schematic:
Attachments
lm35.PNG
lm35.PNG (22.4 KiB) Viewed 12565 times

Fatamy
Posts: 1
Joined: 10 May 2011 20:40

Re: LM35, Protues and MikroC Pro

#14 Post by Fatamy » 10 May 2011 20:46

i have the same project and i did not use the Vref and when i want to use it i don'tknow how..?
can any one help me ..? about same project
because i have problem to read from analog signal.. ?

hishoegypt
Posts: 3
Joined: 25 Nov 2011 05:44

Re: LM35, Protues and MikroC Pro

#15 Post by hishoegypt » 25 Nov 2011 05:52

could someone tell me please, how to read negative temperature????

Post Reply

Return to “mikroC PRO for PIC General”