Hi Can someone please help me with the asm code for a 16-bit count down loop for a PIC16 processor. The following code does not count down properly. It exits the loop when both 0Ch and 0Dh are 1. movlw h'FF' movwf 0Ch movwf 0Dh loop decfsz 0Ch goto loop decfsz 0Dh goto loop return Thanks Mauritz
16 bit count down - PIC16
Started by ●July 10, 2004
Reply by ●July 10, 20042004-07-10
Reply by ●July 10, 20042004-07-10
Mauritz Geyser wrote:> Hi > > Can someone please help me with the asm code for a 16-bit count down loop > for a PIC16 processor. > > The following code does not count down properly. It exits the loop when both > 0Ch and 0Dh are 1. > > movlw h'FF' > movwf 0Ch > movwf 0Dh > loop decfsz 0Ch > goto loop > decfsz 0Dh > goto loop > return > > > Thanks > > Mauritz > >movlw h'FF' movwf 0Ch movwf 0Dh loop: // Value probed here movf 0Ch, f btfsc STATUS, Z goto turnaround decf 0Ch, f goto loop turnaround: movf 0Dh, f btfsc STATUS, Z goto exit movwf 0Ch decf 0Dh goto loop If you place code at the "Value probed here" marker, you'll see the values FFFF ... FF01 ... FF00 FEFF ... 0001 0000 exit But do you really need it ? If it's to iterate a fixed number of time without using the counter value, you just need to adjust the constant. Using incfsz might be more efficient. Just place in 0C and 0D (0x10000 - number of iter). clrf 0Ch clrf 0Dh loop: // Value probed here incfsz OCh goto loop incfsz 0Dh goto loop Sylvain