>On Wed, 10 May 2000, Jim Ham wrote:
>
>> The routine below works mostly OK - it seems to have about a 20% error. If
>> anyone sees the problem please drop me a note.
>
>Yeah the problem is that the PIC is getting bored taking such a long time
>calculating the square root and is forgetting the answer before the algorithm
>completes :). But seriously, if you need a square root routine I'd suggest
>looking at James' pic pages (what's the latest URL James?) or
>
>
http://www.dattalo.com/technical/software/pic/picsqrt.html
>
>I've haven't been paying attention to the thread so I'm not sure what your
>equation looks like.
>
>Scott
>
>>
>>
>> /***********************************************************************\
>> *
>> * return indicated airspeed (kts)(assume std density at sea level
>> * also assume 80mv FS in A2D
>> * returns airspeed in 10ths of knots
>> *
>> \***********************************************************************/
>> int SpdLookup( ulong raw )
>> {
>> /*
>> return (int)sqrt( raw * 200 )/10 ;
>> */
>>
>> byte iteration ;
>> unsigned long guess ;
>> unsigned long last_guess ;
>>
>> // 1 psi = 2.13e6 counts from A/D
>> // rho 2.378e-3 slugs/cu.ft - density of air at sea level
>> // p(psf) = 1/2 rho(slugs/cu.ft) V^2(ft/sec)
>> // p(psf) = raw / 14789 ;
>> // V(ft/sec) = sqrt( 2 * p /rho )
>> // 1 kt = 0.5925 ft/sec
>> // V(kts) = 0.5925*sqrt( 2 * p /rho )
>> // V(kts*100) = 59.25*sqrt( 2 * p /rho )
>> // V(kts*100) = sqrt( (59.25^2*2*raw) /(2.378e-3*14.789e3) )
>> raw *= 200ul ;
>> //***********************************************************************
>> //*
>> //* Take a square root - Newton's method
>> //*
>> //***********************************************************************
>> iteration = 20 ;
>> last_guess = guess = raw / 128 ;
>> do {
>> if (guess == 0l ) guess = 1l ;
>> guess += (raw / guess) ;
>> guess /= 2 ;
>> if (last_guess == guess) break ;
>> last_guess = guess ;
>> } while ( --iteration ) ;
>> return MakeInt(guess/10l) ;
>> }
>>