#include cblock 0x0C ones, tens, hund, thou, tenk lo, hi endc mov macro __x, __y movlw __x movwf __y endm mov 1, ones mov 2, tens mov 0, hund mov 7, thou mov 5, tenk call bcd2bin16 nop ;************************************************************************= ******* ; Input: unpacked BCD in tenk:thou:hund:tens:ones ;Output: 16 bit binary in hi:lo ; Size: 35 instructions ;Timing: 2+3+10+6+10+1+10+3+7+1+10+3+10+1+10+2+2=3D91 instruction cycles ; ; Notes: ;1) uses two stack levels ;2) if input is higher than 65535 output is meaningless ;3) algorithm synopsis: ; ;=BB dec2bin([1 10 100 1000 1e4], 16) ;ans =3D=20 ;0000000000000001 ;0000000000001010 ;0000000001100100 ;0000001111101000 ;0010011100010000 ; ;Or coded in three levels ('+' represents +1, '-' represents -1): ;00000000 0000000+ ones ;00000000 0000+0+0 tens ;00000000 0++00+00 hund ;00000+00 00-0+000 thou ;00+0+00- 000+0000 tenk ; ;bin =3D (((((((((((hund<<1)+ ; hund-thou+tenk*256)<<1)+ ; tenk)<<1)+ ; tens+thou+tenk*256)<<1)+ ; hund+thou*256)<<1)+ ; tens)<<1)+ ; ones-tenk*256 ; ;January 20, 2000 by Nikolai Golovchenko ;************************************************************************= ******* bcd2bin16 movf hund, w movwf lo clrf hi call shift1add movf thou, w subwf lo, f skpc decf hi, f movf tenk, w addwf hi, f call shift1add movf tens, w call shift1add movf tenk, w addwf hi, f movf thou, w call shift0add movf hund, w call shift1add movf thou, w addwf hi, f movf tens, w call shift1add movf ones, w call shift1add movf tenk, w subwf hi, f return shift1add clrc rlf lo, f rlf hi, f shift0add addwf lo, f skpnc incf hi, f return ;************************************************************************= ******* END =20 ---- Original Message ---- From: Philip Martin Sent: Friday, January 19, 2001 19:00:50 To: PICLIST@MITVMA.MIT.EDU Subj: [PIC]:Maths Problem > Many thanks to the two Tony's for your replies. Whilst the answers may = not > have directly helped solve the problem, they have certainly broadened m= y > understanding of maths on the PIC. But it was the final comment in Tony > Nixon's post that got me thinking, break the problem down into a manage= able > chunk. > So my train of thought went, if the distance is a fixed value (60 yards= ), > what would be the speed in MPH if it only took 1 second to travel this > distance. Answer, 122.75 mph. Now all I have to do is divide whatever t= ime I > get into this figure to convert it into MPH. Thus 122.75 / 12.63 second= s > would be 9.718 mph. > Route a) > 122.75 =3D 7A (msb) & 4B (lsb) / 12.63 =3D 0C (msb) & 3F (lsb) =3D 00 0= 9 (msb) . > 0C 14 (lsb) or 9.3092. > Route b) > 12275 =3D 2F (msb) & F3 (lsb) / 1263 =3D 04 (msb) & EF (lsb) =3D 00 09 = (msb) . 03 > 83 (lsb) or 9.908. > Question: > Would I be right in thinking that route b) is the more correct way to d= o it > and the error is due to the maths capability of the math routine? > I have got a routine to convert 16 or 24 bit to BCD and that works fine= . But > what I need is a routine to convert BCD to 16 bit, does anyone know the > location of such a routine or, how best to go about this. > TIA, > Philip Martin. -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body