Searching \ for '[PIC] First code' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'First code'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] First code'
2007\03\04@211103 by Rikard Bosnjakovic

picon face
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. 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.

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).

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 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.

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?


--
- Rikard.

2007\03\04@212609 by Byron A Jeff

face picon face
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.

{Quote hidden}

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

2007\03\04@223858 by Jinx

face picon face
> > 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.

Hate to do this, really feel awful, but SUBLW is the backwards one

temp = 50
movlw d10
subwf temp -> d40 in temp -> temp = temp - W

movlw d50
sublw d10 -> d216 (ie -40) in W -> W = 10 - 50 (literal - W)

I agree that xor is the better test for 0, however subwf still works

temp = 5
movlw   d06 ; limit to test against
subwf   temp,w ; -> result W = 5 - 6 = d255 (ie -1)

temp = 6
movlw   d06 ; limit to test against
subwf   temp,w ; -> result W = d0

2007\03\04@233842 by Byron A Jeff

face picon face
On Mon, Mar 05, 2007 at 04:38:29PM +1300, Jinx wrote:
> > > 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.
>
> Hate to do this, really feel awful, but SUBLW is the backwards one

No problem Jinx, I was talking off the top of my head without consulting
a datasheet.

{Quote hidden}

So it is six of one half dozen of the other. I guess I'm just partial to
xorwf.

BAJ

2007\03\05@002124 by Jinx

face picon face
> So it is six of one half dozen of the other. I guess I'm just partial
> to xorwf

Oh, me too. The sub way takes an extra instruction. In fact I use
the very same example you posted as a '6' test in a product

noclr    incf   revs
        movlw  0x06
        xorwf  revs,w        ;test for 6 counts (1 prop rev)
        btfss  zero          ;yes
        goto   exit          ;no

Rikard, because bytes can roll-over or roll-under through 00,
ADD and SUB are more closely related than they are in usual
maths. For example SUB 1 gets the same result as ADD 255

Here's a little 16-bit subtraction example using 'borrow' for you
to follow in MPLAB. xor can be used on multiple bytes to find
equality, but you will often want to get a numerical result too

; subtract number1 <hi:lo> from number2 <hi:lo>

        movf    number1_lo,w
        subwf   number2_lo,f
        movf    number1_hi,w
        btfss   status,0 ; carry
        incfsz  number1_hi,w
        subwf   number2_hi,f

You can find more on maths at

http://www.piclist.com/techref/microchip/math/index.htm


2007\03\05@065806 by Howard Winter

face
flavicon
picon face
BAJ,

On Sun, 4 Mar 2007 21:26:07 -0500, Byron A Jeff wrote:

{Quote hidden}

I think it's just worth pointing out that because the increment and test are together at the end of the loop, the mainloop "fancy code" will only be
executed for values of 0 to 5 - the test shortcircuits the 6.  Since the OP was using it to multplex 7-seg LEDs, I think this is not the intended result!  
Moving the increment before the fancy code and leaving the test afterwards would mean it was executed for values ot 1 to 6... still no good.

You really need a "greater than" test, but since we don't have a simple one to use here, it's easier to set the limit to 7 with a comment saying that
it's never executed with this value.

As for preferring hex, with a single digit the radix isn't important anyway!  :-)

Cheers,


Howard Winter
St.Albans, England


More... (looser matching)
- Last day of these posts
- In 2007 , 2008 only
- Today
- New search...