Altitude from lookup example PIC12F675
Here's a little code to calculate altitude from 0 to about 17,000 feet. I could not quite fit the full 85 value lookup2 table without getting some errors, so it is more of a 74 value lookup table. I may try a 74 value lookup table that covers the range to 36,000 feet later. Also will not work for Death Valley where the altitude goes to negative.
It is a pretty short program. I am using about 900 words of the PIC12F675 though, so not much space left over. Have not tested it too much, so if anyone finds issues, let me know.
Code:
DEFINE OSC 20
INCLUDE "DT_Analog.pbp" ; DT's 16-bit Analog Module
DEFINE ADC_BITS 10 '10 BIT A/D CONVERSION RESULT
DEFINE ADC_CLOCK 2 '
DEFINE ADC_SAMPLEUS 5 'SET SAMPLE TIME IN MICROSECONDS
CMCON = 7 'TURN COMPARATORS OFF
TRISIO = %000100 'Set GSIO 0 INPUTS, others to OUTPUT
ANSEL = %00100100 '
ADCON0.7 = 1 'Right Justify for 10-bit
ADCON0 = %10001001
ADbits = 14 ; set to 14-bit resolution
DEFINE DEBUG_REG GPIO
DEFINE DEBUG_BIT 0
DEFINE DEBUGIN_BIT 1
DEFINE DEBUG_BAUD 2400
DEFINE DEBUG_MODE 0
ALTresult var word 'lookup table output
tempALT var word 'base altitude of lookup from table
ADtemp var byte 'lookup table input
ALTdifference var word 'difference of two nearby altitudes
finalALT var word 'final calculated altitude in feet
;---------------------------------------------------------------------------
Main:
ADchan = 2 ; Do only AN2 channel
GOSUB GetADC ; Get A/D value
ADtemp = (ADvalue/100) - 60 ' get to table acceptable value for lookup
gosub lookmeup
tempALT = ALTresult 'save first result for use in math
ADtemp = ADtemp - 1 'run another lookup for nearby altitude for diff calc
gosub lookmeup
ALTdifference = ALTresult - tempALT 'get difference in nearby alt
finalALT = tempalt - (((ADvalue//100)* ALTdifference)/100)
'use difference to calculate slope & adjust altitude accordingly
debug "Alt= ",DEC finalALT," A/D= ",DEC ADvalue," "
pause 1000
GOTO Main:
End
lookmeup:
lookup2 ADtemp,[_
17646,17327,17011,16699,16389,16083,15780,15480,15183,14888,14597,14308,14022,_
13739,13458,13179,12903,12630,12359,12090,11823,11559,11297,11037,10779,10524,_
10270,10018,9769,9521,9275,9031,8789,8549,8310,8074,7838,7605,7373,7143,6915,_
6688,6463,6239,6017,5796,5577,5359,5143,4928,4715,4502,4292,4082,3874,3667,_
3462,3257,3054,2852,2652,2452,2254,2057,1861,1666,1472,1280,1088,898,708,520,_
332,146],ALTresult
return
A/D and the number of bits/levels
I don't see the problem with dividing by 1024?
The process is quantising a variable. Say our variable voltage lies in the range 0 to 10 Volts and we have one bit to use for the digital result of the quantising (2 to the power of 1 = 2, so we have two levels to play with):
A. The result 0 means that the voltage was in the range 0 to 5 Volts (not that it was zero).
B. The result 1 means the voltage was in the range 5 to 10 volts (not that it was 10 volts)
So each level is 10/2 volts wide. Not 10/1 volt wide.
The bit of cake after carving it up into portions has a slice 0 - it is not any smaller than any other slice.
This is not jibberish created by highly paid engineers it is simple maths.
I wonder how many devices you have launched on the world that are happily 'over' measuring analog quantities by assuming that each slice is bigger than it actualy is?
Kind regards to you all
Bill Legge
the problem ... without the solution.
Hi,
The problem is very simple : the servo signal frame length is too long ( or too short ??? ) and the ESC do not recognise it as a valid frame ...
The solution is not so simple : the total time of the frame must be 30 or 35 ms as a MAXIMUM and , say 17-18 ms as a minimum.
That's why servo works and not the ESC ...
Alain