Hi Joe, Please tell us the end application. It looks like a fun project but I can't= =20 imagine what it's for and got curious while reading your post. Sorry to not have any good answer at this moment. /Ruben > Hi all, >=20 > Code challenge - >=20 > I've got a 5 x 5 LED matrix, which is driven by an 18F43K20 on > a one-LED-per-pin basis >=20 > The 18F43K20 is 40-pin, with digital I/O similar to the 16F877 and > 18F4520 etc. It's cheap enough and adequate for the job >=20 > For this project, >=20 > LEDs 1 - 7 are on LATB 1 -7 > LEDs 8 - 15 are on LATC 0 - 7 > LEDs 16 - 23 are on LATD 0 - 7 > LEDs 24 - 25 are on LATE 0 - 1 >=20 > PortA <5:0> and PortB,0 are used for various other functions >=20 > The PIC receives one-off requests from external sources (other PICs), > each uniquely numbered 1 to 25. Those requests are stored in a queue, > with the oldest at the head of the queue, ie a FIFO >=20 > The intention is to illuminate the oldest request more brightly than any > behind it in the queue. For example if a request from source 17 was the > first received and 4, 18, 10 and 6 were behind it in the queue, then > LED17 would be most brightly lit, LEDs 4, 18, 10 and 6 would be less > brightly lit and all others would be off. After 18's request has been > processed, 4 moves to the head of the queue, LED17 is turned off, > LED4 brightens and so on >=20 > I've got a PWM timer interrupt running, LEDs being turned on or > off in that interrupt >=20 > ----------------- > The code that I'd like to be more elegant is the conversion of a LED's > numerical value to its bit position within a 4-byte array. For example, > it could be done using shifts, as below, which would take over 300us > if all 25 LEDs needed to be lit >=20 > A routine like this, expanded to 32 bits wide, would be good >=20 > ;convert number (0-7) in temp to bit position in W >=20 > movlw .1 > btfsc temp,1 > movlw .4 > btfsc temp,0 > addwf wreg,w > btfsc temp,2 > swapf wreg,w >=20 > I suppose the number 1 - 25 (-1) could be divided by 8, with the > quotient being the byte and remainder being the bit of that byte, then > applying the routine above >=20 > Maybe that's close to an answer but I'd like to hear other solutions > or opinions >=20 > ----------------- >=20 > Brightness control is a minor complication. I've included an IRQ > counter so that one LED is turned on for longer or more often than > the rest. So perhaps the priority LED is turned off/on every alternate > interrupt, others in some fractional ratio, eg 1/2 or 1/3 of that >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > ;about 13us @ 64MHz > ;value in queue+0 >=20 > clrf latb ;all LEDs off > clrf latc > clrf latd > clrf late >=20 > clrf oldest+0 ;clear 'oldest' array > clrf oldest+1 > clrf oldest+2 > clrf oldest+3 >=20 > bsf oldest+0,1 ;LATB,1 >=20 > movff queue+0,q_shift ;first in queue (oldest request) > shift_q clrc > rlcf oldest+0 ;shift (value) times > rlcf oldest+1 > rlcf oldest+2 > rlcf oldest+3 > decfsz q_shift ;decrement counter > bra shift_q >=20 > For example if the LED to be turned on is number 21, then that > would be represented in oldest+x and port pins as (E7/MSb on > left, B0/LSb on right) >=20 > 00000000 00000000 00000000 00000010 shift '1' left to >=20 > 00000000 00100000 00000000 00000000 >=20 > Oldest+x could then be IORed with ports or shadow registers and > MOVed to the ports >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > TIA >=20 > Joe >=20 > * > * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D Ruben J=F6nsson AB Liros Electronic Box 9124, 200 39 Malm=F6, Sweden TEL INT +46 40142078 FAX INT +46 40947388 ruben@pp.sbbs.se =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .