ivp wrote: > I've got a 5 x 5 LED matrix, which is driven by an 18F43K20 on > a one-LED-per-pin basis I assume the PIC is neither RAM, nor code, nor cycles limited in this application? If so, optimize for maintainability. If the customer is already asking for a few different brightnesses, I'd be nervous hard coding just those few. Sooner or later the specs will change. I think (knee jerk warning) I'd make the underlying mechanism with a byte per LED. This byte holds a brightness value. You certainly don't need to use all 256 brightnesses, and that would make the blink rate too slow anyway. Maybe brightnesses 0-8 would be good enough. LEDs don't have to b= e separately turned off, just set their brightness values to 0. The interrupt routine handles the conversion from brightness to the on/off decision every PWM slice interrupt. Let's say you want to flash the LEDs a= t 500Hz and use 9 brightness levels (0-8). That would mean the interrupt rat= e is 4KHz. At 10MHz instuction rate that's every 2500 instructions, which should leave a decent amount of the processor to the foreground code. The interrupt routine maintains a 0-7 counter that is incremented every interrupt. Any LED with a brightness value exceeding the counter is lit that interrupt. I'd probably deal with the mapping of LED number to port pin in a semi-automated way. You really want this mapping to be in only one place i= n the source code. I'd give each of the LED pins regular names in the /OUTBI= T directive. Then I'd use preprocessor math and looping to write the specifi= c instructions for each of the 25 LEDs as one unrolled loop. That code will be ugly, but you won't be maintaining it directly. You only maintain the single copy that gets unrolled automatically by the preprocessor loop. Eac= h loop iteration fetches the brightness value for that LED, compares it to th= e current interrupt count, then sets or clears the associated LAT bit accordingly. The preprocessor (unlike the MPASM macro processor) can do real string manipulations to build new symbols. It would loop over the LED number (0-24) and build the symbol name for the LAT bit accordingly. The brightness values would likely be in a regular array, so you load FSR0 once then use POSTINC0 to fetch each successive brightness value. Don't forget to save/restore FSR0 in the interrupt routine if you use this method. If you're tight on program memory this is not the best way. Otherwise, it is nicely maintainable, the odd mappings in only one place, and about as fast as possible. The foreground code then just stuffs brightness values into the brightness locations for the LEDs as desired. It doesn't and shouldn't be dealing wit= h the arbitrary pin mappings. ******************************************************************** Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products (978) 742-9014. Gold level PIC consultants since 2000. --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .