Option A: Use a 32 bit phase accumulator that is properly aligned for your purposes, as follows: Top 6 bits - ignored Next 2 bits - highest bits you use as index Next 8 bits - remaining bits you use as index Next 8 bits - fraction bits Next 8 bits - more fraction bits This gives you 2 more fraction bits than you used to have, but you can choose to ignore them if you prefer. Now your task becomes simply: movff ACC2,TBLPTRL movf ACC3,W andlw 3 addwf high(LOOKUP_TABLE) movwf TBLPTRH Option B: Another alternative would be to leave your phase accumulator as is, but prescramble the stored waveform. Then you could: movff ACCH,TPLPTRL movlw high(LOOKUP_TABLE) >> 2 movwf TBLPTRH rlcf ACCM,W rlncf TBLPTRH,F rlcf WREG,W rlncf TBLPTRH,F Option C: You could use the 32-bit phase accumulator like option A, but only store one quadrant of your waveform (assumed to be a sin). You then use the two high order bits which represent the desired quadrant to adjust the index value and the value obtained from the table. Note that in this case ACCPTRH remains constant and is set outside the loop. movf ACC2,W btfsc ACC3,0 ; What quadrant? sublw 0 ; 1 or 3, so negate W (the index value) movwf TBLPTRL tblrd * ; Get value from table movf TABLAT,W btfsc ACC3,1 ; What quadrant? sublw 0 ; 2 or 3, so negate the waveform value btw: In your example you are only adding your frequency increment to ACCM and ACCH. This doesn't make much sense because ACCL will always be zero! Bob Ammerman RAm Systems ----- Original Message ----- From: "Ishaan Dalal" To: Sent: Monday, June 30, 2003 1:49 AM Subject: Fast 16-bit to 10-bit truncation? > This is for a phase accumulator in a DDS -- the lookup table is 1024 > entries, so the 24-bit accumulator needs to be truncated to 10-bits, or > truncating 16 bits of the high and middle byte to 10 bits. The PIC in > question is an 18F1320, and the table is aligned at 0x1C00. This means that > TBLPTRH and TBLPTRL need to be updated for each read, with 0x1C added to the > upper two bits of the 10-bit value for TBLPTRH, and the lower 8-bits of the > 10-bit value copied to TBLPTRL. My first effort is below, but I think there > must be a faster way to do it -- > > ACCLOOP > movlw FRQL //frequency increment low byte > addwf ACCM //increment accumulator low byte > movlw FRQH // freq incr high byte > addwfc ACCH //incr acc high byte > movff ACCH, SHDH //shadow acc high byte > rlncf SHDH > rlncf SHDH, W //rotated twice, two highest bits of 10-bit value now two > LSBs in W > andlw b'00000011' //leaves just those two bits > addlw 0x1C //add table offset > movwf TBLPTRH //TBLPTRH ready > rlncf ACCL, W > rlncf W > andlw b'00000011' > movwf SHDL //upper two bits of ACCL, which will be lower two bits of 10-bit > value readied in SHDL > rlncf SHDH, W > andlw b'11111100' //upper six bits (originally bits 5...0 of ACCH) > addwf SHDL, W > movwf TBLPTRL > > Would you guys know of standard/faster ways to do truncations to such > odd bit values? > > Thanks, > Ishaan > > -- > http://www.piclist.com hint: To leave the PICList > mailto:piclist-unsubscribe-request@mitvma.mit.edu > -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu