PDA

View Full Version : LN(natural log)



actionplus
- 11th August 2004, 15:56
Is there anyway to use natural log in PICBasic Pro. I know that we can use Sin and Cos.

I am asking to much of PICBasic?:)

rpstroj
- 11th August 2004, 16:09
There's no way to perform natural log directly. When I had to perform the natural log function on the response from an A/D converter I calculated the natural log function for each A/D 3-bit point in an excel file and put it in a lookup table. Since the lookup table allows 255 entries (3-bits) this worked out.

If this method won't work you can use linear interpolation. Here's a website about it with many other helpful hints:

http://www.emesys.com/BS2math3.htm

actionplus
- 11th August 2004, 18:10
I am try to use a 100k thermistor to build a thermometer. And the curve is a relation to a LN formula.

I guess, I can still calculate each point or so and put it on an excel sheet.

The problem is that how do I corelate the A/D data from the PIC with a look-up table?

I have the thermistor connected to a 5V pwr supply and gnd. I get the changes is voltage as the temp differs.

I guess, I will feed this into A/D port of the PIC16F877 or maybe a 16F628.
How will I use the data and look-up table to do corelation?

Thanks

rpstroj
- 11th August 2004, 18:21
I used the look-up table for the exact same application, a thermistor. Here's what I did:

Use an Excel Spreadsheet to calculate what the tempurature is for each possible A/D count. If you're using 8-bits then that's great because you get 255 entries in the lookup table.

Use the lookup table with the A/D count as the index. In the actual table insert in order the tempurature entries you calculated in the spreadsheet.

So your code will look something like this:

Lookup adcount, [120, 119, 117, 117, ...]


If that's still not clear I can send you my spreadsheet and code when I did it.

And if you could help me out with my HSerout problem I'd appreciate it. See my other post.

actionplus
- 11th August 2004, 20:16
If you could send me spreadsheet, that will be great. If possible the lookup code that relates to that, so I can do the same to mine.

I have not used the Hserout command yet. I have used the Serout2 and Serin2 though. What is your application? Let me see if I can help you.

Thanks again

rpstroj
- 11th August 2004, 20:32
INCLUDE "modedefs.bas" 'Contains mode definitions for

DEFINE OSC 12 ' Define OSC 12Mhz for HS

DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

TRISA = %11111111
TRISB = %00000000

ADCON1=3


'Variables for LCD
clrSCR CON 12 ' Cntl-L: clear the display.
posCmd CON 16 ' Position the cursor.
ESC CON 27 ' Escape code.
noCurs CON 4 ' no cursor shown

Q0 VAR BYTE
adcount VAR BYTE
adjust VAR BYTE
LCD VAR PORTB.0

Pause 500

SerOut2 LCD, 16468, [clrSCR] 'Clear LCD and cursor to upper left
Pause 500

High PORTB.2

Loop:

ADCON0=%11000001
PauseUs 50
ADCON0.2 = 1 'start conversion
PauseUs 50
adcount=ADRES

'ADCIN 0, adcount
adjust=adcount-1
LookUp adjust,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,1,2,3,4,5,6,6,7,8,9,10,10,11,12,12,13, 14,14,15,16,16,17,18,18,19,20,20,21,21,22,22,23,24 ,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,3 2,33,33,34,34,35,35,36,36,37,37,37,38,38,39,39,40, 40,41,41,41,42,42,43,43,44,44,44,45,45,46,46,47,47 ,47,48,48,49,49,50,50,50,51,51,52,52,52,53,53,54,5 4,55,55,55,56,56,57,57,57,58,58,59,59,60,60,60,61, 61,62,62,63,63,63,64,64,65,65,66,66,67,67,67,68,68 ,69,69,70,70,71,71,72,72,73,73,73,74,74,75,75,76,7 6,77,77,78,78,79,80,80,81,81,82,82,83,83,84,85,85, 86,86,87,88,88,89,90,90,91,92,92,93,94,94,95,96,97 ,97,98,99,100,101,102,102,103,104,105,106,107,108, 109,110,112,113,114,115,117,118,120,121,123,125,12 6,128,130,133,135,138,141,144,147,152,157,163,170, 181,200,200],Q0
SerOut2 LCD, 16468, [clrSCR]
SerOut2 LCD, 16468, [posCmd, 87, DEC Q0, " C"]
SerOut2 LCD, 16468, [posCmd, 107, DEC adcount, " adcount"]

IF PORTB.2=1 AND Q0>100 Then
Low PORTB.2
EndIF

IF PORTB.2=0 AND Q0<50 Then
High PORTB.2
EndIF


GoTo Loop

End



It won't let me upload the spreadsheet.

rpstroj
- 11th August 2004, 20:36
Here's the spreadsheet. I just changed the extension to .txt so I could upload it. Before you open it change the extension from .txt to .xls then open it with Microsoft Excel. this isn't the exact same one that I used for the program you see, but it's the same idea. Let me know if you have any more questions.

anj
- 11th August 2004, 23:31
I rewrote the "by calc" section from the following site
http://www.emesys.com/BS2math3.htm
into a subroutine and ended up with the following
Result is the log value multiplied by 1000
Accuracy should be good enough for what you are doing.
If you want to know HOW it works, you need to read the text, i just rewrote it and it worked so i was happy.


LogNo VAR WORD ' Number to get log of

k VAR byte 'loop var
X Var WORD 'temp for processing
X1 var word 'temp for squaring
LgX var word 'lg ( base 2 ) of y the mantissa
cc var byte 'characteristic of log

Lg var word 'log base 2
Log var word 'log base 10
Ln var word 'log base e
'---------------------------------------------------
etc
etc
Logno = varX 'ie no to get result
gosub getlog
' now ref to Lg, Ln or Log as reqd

GetLog:
cc = NCD LogNo - 1
X = LogNo << ( 15 - cc )
LgX = 0
for k = 14 to 0 step -1
X1 = X**X
if X1.bit15 then
lgx = lgx | ( dcd k ) 'set the bit
X = X1
else
X = ( X1 << 1 ) + 1 'shift er over
endif
next k
lg = cc*1000 + ( lgx**20000/10 )
log = ( lg ** 19728 )
ln = ( lg ** 45426 )
return
---------------------

Andrew