On Mon, Mar 05, 2007 at 03:11:02AM +0100, Rikard Bosnjakovic wrote: > At first I thought I liked RISC, but now when I started to actually > code something (instead of just reading the book) I found it pretty > tricky. It's not RISC, it's just the PIC. > For example, in a mainloop I want a particular variable hold > the value 0, increase it every loop run and as soon as it turns 6 it's > supposed to reset. That is, I want it to keep the values 0-5. Counting up. Got it. > After 30 minutes I got it to work, but it's somehow backwards because > the subwf-instruction was a real caveat emptor (lots of warnings for > it in my book). Because it operates backwards, subtracting F from W, not the other way around. > Here's the code: > > > reinit > movlw d'6' > movwf segment > > mainloop > ; do lots of fancy code here > movlw d'42' > movlw d'19' > > decfsz segment > goto mainloop > > goto reinit The traditional countdown loop. What are the movlw's for? > The variable segment will keep the active number of the led-segment > which is (later on) supposed to be multiplexed into, so I guess it > doesn't matter if it goes backward or not. Not really. It easy to detect zero. So if the loop doesn't matter, simply count down. > But still, I did not managed to make a "forward" version of the code. > Anyone cares to give me a hint of how you could do it? Sure. Don't use subwf. The best instruction for comparing for equality (or inequality) is xorwf. It produces a zero (and sets the zero flag) when the values in W and F are equal. So something like ;---------------- reinit clrf segment mainloop ; lots of fancy code as before incf segment,F movlw d'6' ; I prefer hex personally. xorwf segment,W ; put the result in W so that segment is preserved btfss STATUS,Z ; skip next instruction if equal goto mainloop ; otherwise loop goto reinit ; start back at the beginning. ;---------------- This code is written off the top of my head and is untested. Note that it's longer than the countdown code. Hope this helps, BAJ -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist