Got a 16F877 with an LM35 connected on RA0 and an LCD connected via PortC and im using GC Graphical Basic
It seems no matter what I do I cannot make a converted temperature readout display to the LCD and it appears to be a prob with my maths, here is the code im using:
start:
CLS
Locate 0, 0
RawAnalog = ReadAD10(AN0)
test = RawAnalog * 0.48872
Print RawAnalog
Locate 1, 0
Print test
Wait 1 s
Goto start
The raw value from RA0 is displaying correctly hower the second value "test" is always 0, however if I multiply by a small whole number, say 10 for instance it works fine. Can anyone please shed some light on what im doing wrong as im currently tearing what little hair I have remaining out with frustration
Cheers
Tom
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
GCBasic doesn't know decimal variables , so your test value is false .
The ReadAD10 value is 1023 maxi , so you must simplify your calculations
0,48872 is 48.872 /1000 ~ 49/1000 , so you can do :
test= RawAnalog *49 ' maxi value for test is 49*1023=50127 this value doesn't exeed 65535 ( word variable)
test=test /100
Or , to have a better result , because I think you have AVref=5000mV , you can write
test= (RawAnalog *50)/1023)*10 '50*1023=51150 maxi is < 65535 !
Regards
GC
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
LM35 has a linear 10mV/°C scale factor , so , with a 10 bits resolution and a 5000mV ADVref , you can have directly the result by writing the precedent formula :
temperature=ReadAD10(AN0) *49
temperature=temperature /100
So ,for 100°C , at the LM35 ouput you only find 1000mV !
At 25°C , you'll have 250mV . With a 10 bits AD convertor and a 5000mV AVref , ReadAD10 'll give about 51
Wih this first formula :
temperature=(51*49)/100=2499
temperature=2499/100 =24,99 !!! ery near from 25
but GCBasic doesn't calculate decimales and 'll display 24 °C .
So you can find the most accurate value by typing :
temperature=readAD10 *49
temperature=temperature /100
if SysCalcTempX > 50 then 'It's the remainder of GCBasic after division
temperature=temperature +1
end if
print temperature
print " °C"
So , you 'll obtain a value nearing reality ! =25°C
regards
GC
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi All
Got a 16F877 with an LM35 connected on RA0 and an LCD connected via PortC and im using GC Graphical Basic
It seems no matter what I do I cannot make a converted temperature readout display to the LCD and it appears to be a prob with my maths, here is the code im using:
;Chip Settings
#chip 16F877,4
;Defines (Constants)
#define LCD_IO 4
#define LCD_RW PORTC.1
#define LCD_RS PORTC.0
#define LCD_Enable PORTC.2
#define LCD_DB4 PORTC.3
#define LCD_DB5 PORTC.4
#define LCD_DB6 PORTC.5
#define LCD_DB7 PORTC.6
;Variables
Dim RawAnalog As word
Dim test As word
start:
CLS
Locate 0, 0
RawAnalog = ReadAD10(AN0)
test = RawAnalog * 0.48872
Print RawAnalog
Locate 1, 0
Print test
Wait 1 s
Goto start
The raw value from RA0 is displaying correctly hower the second value "test" is always 0, however if I multiply by a small whole number, say 10 for instance it works fine. Can anyone please shed some light on what im doing wrong as im currently tearing what little hair I have remaining out with frustration
Cheers
Tom
Hi,
GCBasic doesn't know decimal variables , so your test value is false .
The ReadAD10 value is 1023 maxi , so you must simplify your calculations
0,48872 is 48.872 /1000 ~ 49/1000 , so you can do :
test= RawAnalog *49 ' maxi value for test is 49*1023=50127 this value doesn't exeed 65535 ( word variable)
test=test /100
Or , to have a better result , because I think you have AVref=5000mV , you can write
test= (RawAnalog *50)/1023)*10 '50*1023=51150 maxi is < 65535 !
Regards
GC
Thanks GC, is there a way that I can somehow convert the reading from the LM35 to just give me a DegC reading as a whole number?
Cheers
Tom
LM35 has a linear 10mV/°C scale factor , so , with a 10 bits resolution and a 5000mV ADVref , you can have directly the result by writing the precedent formula :
temperature=ReadAD10(AN0) *49
temperature=temperature /100
So ,for 100°C , at the LM35 ouput you only find 1000mV !
At 25°C , you'll have 250mV . With a 10 bits AD convertor and a 5000mV AVref , ReadAD10 'll give about 51
Wih this first formula :
temperature=(51*49)/100=2499
temperature=2499/100 =24,99 !!! ery near from 25
but GCBasic doesn't calculate decimales and 'll display 24 °C .
So you can find the most accurate value by typing :
temperature=readAD10 *49
temperature=temperature /100
if SysCalcTempX > 50 then 'It's the remainder of GCBasic after division
temperature=temperature +1
end if
print temperature
print " °C"
So , you 'll obtain a value nearing reality ! =25°C
regards
GC