Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MPASM - if equal to branch scenario

Status
Not open for further replies.
Hi,

My chip reads a parallel input, 8 bit, input ranging from 80 to 255. My circuit reads this and places it in a temporary file register.

Now what I want it to do is compare it to a decimal, if they match then it goes elsewhere and places a value into another file register in which the chip does other things with. I want the kind of process below

temp1 ; value read from input port.
does temp1 = 255 then goto elsewhere
if not then
does temp1 = 254 then goto elswhere2
if not then
etc, etc..

I'm not quite sure how I would do it, I thought somewhere along these lines but this seems to be long and drawn out,

movlw temp1
xorlw 255,W
btfsc STATUS,Z
goto nextbranch
goto elsewhere

nextbranch
movlw temp1
xorlw 254,W
btfsc STATUS,Z
goto next branch2
goto elswhere2
etc.. etc...

elsewhere
movlw d'104'
movwf delay
goto start


elsewhere2
movlw d'109'
movwf delay
goto start

Help would be appreciated,
Thanks
 
Have you found PicList yet? There are lots of mostly obvious time savers there, but some are really clever. Here's link to test for whether a number is in a range:


John
 
Assuming the value is in temp then,
Code:
    incf    temp,f
    btfsc  STATUS,Z
    goto   was255
    incf    temp,f
    btfsc  STATUS,Z
    goto   was254
 ........

Another way is to have a table of addresses. Search pic jump table for details.

Mike
 
How about.... (same as PICLIST example)...
Code:
        radix   dec
;
;
        movf    PORTB,W         ; get byte from PORTB             |00
        addlw   -(255+1)        ; range check                     |00
        addlw   (255-80)+1      ; range 80..255?                  |00
        skpc                    ; yes, skip, else                 |00
        goto    range_error     ;                                 |00
;
;  175 entries, one for each value 80 through 255
;
case
        addwf   PCL,F           ; affect a jump                   |00
        goto    case80          ;                                 |00
        goto    case81          ;                                 |00
        goto    case82          ;                                 |00
;       ~~~~
        goto    case255         ;                                 |00

The value left in WREG after a 'successful' range check is 0..175 which provides a convenient index for a table. There are some caveats associated with modifying PCL and setting up the table depending on the PIC family you're using.

Cheerful regards, Mike
 
Last edited:
Do all the "elsewheres" simply modify the delay? If so, you can use a lookup table to set the delay based on the input value, or better yet, find a formula for the delay which you can calculate.
 
Hi, thanks everyone for the replies.

I have put piclist in my favourites thanks to John.

Pommie that was the type of thing I were thinking of at first but I went for a table in the end, not covered this bit of programming yet but It worked out perfect.

Thanks Mike for pointing out using a table, I wasn't quite sure how they worked at first, a lot simpler than I thought. So my program is now working perfect.

NorthGuy, it is for a delay, but a little random so I couldn't use a formula unfortunately.
 
Hi Joe,

I misread your original question, which is not an infrequent occurrence for me to do. After re-reading it, I agree completely abut using a table. Glad you found the PicList link useful for other reasons.

What controller are you using? You may need to use high/low tables to cover the span of values + instructions that you need. A variant of that is to use a table within a table (aka double-table) described by Peter Anderson here: https://www.phanderson.com/PIC/16C84/mult_string.html

John
 
NorthGuy, it is for a delay, but a little random so I couldn't use a formula unfortunately.

You can use a lookup table. Start as Pommie suggested, then do:

Code:
  call case
  movwf delay
  goto do_something_else
 
case:
  addwf PCL,f
  retlw delay_for_80
  retlw delay_for_81
  .....
  retlw delay_for_255

Some PICs have brw instructions which is the same as "addwf PCL,f" but doesn't have any restrictions on the table location
 
You only need one branch instruction after the test for zero instruction -

Code:
;compare, jump if equal routine

     movlw     temp1       ;compare received data to 255
     xorlw     d'255',W
     btfsc     STATUS,Z    ;temp1 = 255?
     goto      elsewhere   ;yes

This code will jump elsewhere if the values are equal, else it will continue with normal code flow. You can also use a call instruction instead of a goto so that the code always returns back to comparing values if you wish.

However, with that many possible values I would seriously consider doing it via a lookup table.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top