On Thu, Jun 26, 2003 at 07:17:36PM +0100, Sergio Masci wrote: > From: Daniel Serpell > > > > I'm not thinking on using lookup tables. The code will be: > > > > > > > That's exactly 121 cycles, and 121 program words. > > But seeing it writen, I have found an error (try to spot it!), > > so 7 more instructions are needed (with 128 cycles total). > > The code can be expanded for more than 8 bits. > > Unfortunatly I don't have time to go working through your code right now (which > is why I wrote the alogrithm in C in the first place). Perhaps you can show us > the corrected version? Well, I tought that finding it could be fun, don't be so serious... The problem is the state of the carry flag before each rrl accl,F The cbits fragment don't always clear carry at end, so en explicit inst. to clear carry have to be added before each rrl accl,F . As there are seven, it's seven extra instructions. I tried searching a cbits equivalent that didn't set's carry flag, but didn't succed. > Why do you not want to use a lookup table? This would speed things up. I will > certainly be having a closer look at what Scott has produced when I get a few > minutes. Because a lookup table takes an extra 256 bytes of program memory, and in the 14bit PIC's don't have much program memory to spare. But if you have, you can do: cbits MACRO data movlw HIGH(lookup_table) movwf PCLATH movf data,W movwf PCL ENDM "lookup_table" should be aligned to a 256 byte boundary, again to avoid 16bit arithmetic. The code is 7cicles each "cbits" macro instead of 12 cicles in my last example, so the total is 81 cycles. That's 47 cycles less than the other example, and 256+81=337 words of program memory. Daniel. PS: I reply to this type of questions because I think it's fun to play with little algorithms, but don't always have the time to write full explanations... -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads