Searching \ for '[PIC]: Some Basic Questions' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page:
Search entire site for: 'Some Basic Questions'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Some Basic Questions'
2003\03\02@114003 by Biswanath Dutta

picon face
I'm  essentially a beginner using PIC16F877 and trying to develop some code.
I have discovered the following and would someone please clear the
confusion --

1) incf, rlf, rrf type instructions do not seem to work properly on W. Is it
so ? Why ? Microchip Datasheet doesn't mention anything regarding this.

2) Why doesn't MPASM give error messages when the above instructions are
used on W

2) I entered the following code assembled and ran MPSIM
           incf  W,W
           incf   W,W
           incf   W,W

   While single stepping through , W is incremented on the first incf and
not on the subsequent two incf . WHY

Thanks in advance -- Biswanath Dutta

-- hint: To leave the PICList>

2003\03\02@121608 by Ian McLean

You need to re-evaluate your programming techniques a little for PIC
assembly.  W is not a file register, but the working register.  incf =
increment FILE register, etc.

The reason the behaviour you are witnessing with incf W, W is because it
incremented a FILE register pointed to by W, and placed the result in W.
The register pointed to by W was 0x00, because you cleared W.  File register
at 0x00 was zero to start with (no guarantees on that though !).  That is
why each incf W,W came back with the same result !

Get used to not performing these operations on W.  W is really just a go
between for file registers, and for setting flags in STATUS, such as Zero,
Carry, etc. (note that many file register operations set STATUS flags
anyway, so even this usage is really unnecessary except for code
optimization sometimes) - all the juicy things happen with file registers.
It's a bit of a PIC thing, just get used to moving W into, say, a temporary
register before performing bit shifting, increments, or decrements.  Really,
you should only be doing this on file registers to start with (i.e.
re-evaluation of technique here ;) ).  W is effected differently for many of
these operations.  Any static results you wish to keep should be in file
registers to start with.  The reasons for this are many, for example,
optimisation, and to keep each instruction to a single cycle.

1) Because the assembler allows W to point to a file register for these
operations.  I don't personally like this sort of syntax though, and think
it SHOULD report these as errors.

To recode 2) properly, you would write something like:

movwf   Temp
incf    Temp, F
incf    Temp, F
incf    Temp, W ; (last incf places result in W)

But really, you should be using a file register in the first place, not W -
and the result becomes even simpler, e.g.

clrf    Temp
incf    Temp, F
incf    Temp, F
incf    Temp, F

or optimized ...

movlw   0x03
addwf   Temp

Hope all this helps

{Original Message removed}

2003\03\02@123101 by Banjo Spam

picon face
Ian and Biswanath,

Ian's explanation is not *quite* correct.

INCF W,W will NOT increment the value *pointed to* by
W. Rather, this is equivalent to
INCF 0, W because 'W' in the assembly code is
translated to the number 0. Regardless of the value in
W (i.e. it does *not* matter that you cleared W prior
to this instruction), this instruction will retrieve
the value in register 0, and place the incremented
value into W.

The effect in your example, Biswanath, is the same as
Ian has indicated.
You are incrementing the value found in location 0 and
placing the result into W.

And Ian's comment "Get used to not performing these
operations on W." I couldn't agree more.

Good luck on your PIC-N-PATH!

--- Ian McLean <.....ianmcleanKILLspamspam@spam@OPTUSHOME.COM.AU> wrote:
{Quote hidden}

> {Original Message removed}

2003\03\02@123721 by Olin Lathrop

face picon face
> 1) incf, rlf, rrf type instructions do not seem to work properly on W.
> Is it so ? Why ?

These instructions only operate on "file registers", which does not
include W.  The result of these operations can be written back either to
the file register or to W, depending on the second operand (default is
write result back to the file register).  Unfortunately Microchip allows
the second operand to be either 0 or 1 to select W or the file register
instead of making this a special syntax.  They therefore define the
constants W and F in the include file to be 0 and 1 respectively.  This
means W and F are defined like any other symbols.  The instruction

   incf   w

is therefore perfectly legal syntax, although this increments the register
at location 0, not W.  Again, the W register is not mapped into the file
register address space.  Note that the instruction

   addlw  1

increments W by 1, although the effect on the flag bits is different.  The
INCF instruction only effects the Z flag, whereas the ADDLW instruction
also effects the C and DC flags.

> 2) I entered the following code assembled and ran MPSIM
>             clrw
>             incf  W,W
>             incf   W,W
>             incf   W,W
>     While single stepping through , W is incremented on the first incf
> and not on the subsequent two incf . WHY

Very good question.  W is cleared by the CLRW instruction.  The next
instruction increments data memory location 0 and writes the result into
W.  Location 0 is actually the INDF register, which is a special case.  It
is not a fixed register at all, but an alias for whatever location FSR and
the IRP bit of STATUS are pointing to.  This apparently was pointing to a
register that contained 0 (when FSR points to INDF it always reads 0).
The 0 value was incremented and written to W, thereby appearing to
increment W by 1.  The following instructions continue to do the same
thing.  Since INDF didn't get changed they still start with 0, increment
it to 1, and write it to W, thereby appearing to do nothing.

Note that the W is mapped into the RAM address space as "WREG" on the 18
family PICs.

Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014,

-- hint: To leave the PICList>

2003\03\02@125222 by

picon face
{Quote hidden}

If you look in the header files then you will see that W is a constant
defined to be 0, and is intended for use as the second argument to an
instruction which can have a destination of W (W=0) or the source register
(F=1). You are therefore writing the following instruction:
   incf 0,0

Looking in the datasheet at the definition of the incf instruction, you will
see that this means "take the value in register 0 in the current bank,
increment it and store the result in the working register (W)".

W is not a register that is addressable like other registers (if I remember
rightly for the processor you are using). On some processors there is a
register (normally named WREG) which is the working register.

To increment the working register, use ADDLW 1.

The fact that the first incf instruction that you tried did appear to
increment the working register was a coincidence - register 0 was read as 0.
It was still read as 0 for your second incf instruction, so W was still set
to 1 after the second incf.

MPASM does not give error messages because the instruction you wrote was
perfectly valid. It just wasn't doing what you thought it was doing.


-- hint: To leave the PICList>

2003\03\02@125633 by Ian McLean

Thanks for the correction, Banjo.  I forgot about this mapping ! Microchip
probably should not have mapped W to 0 for these operations, but forced
syntax like 'incf x, W' rather than allow 'incf x, 0'.  The syntax is too
confusing (it managed to confuse me in my explanation).  incf W, W should
not be legal syntax in my opinion.  It's nice to learn something at the same
time as teaching something - a real two way street ;)  I should be more
certain before giving this advise though.  Apologies to Biswanath for the
partially incorrect explanation.

{Original Message removed}

2003\03\03@002414 by jim barchuk

Hi Biswanath!

On Sun, 2 Mar 2003, Biswanath Dutta wrote:

> I'm  essentially a beginner using PIC16F877 and trying to develop some code.
> I have discovered the following and would someone please clear the
> confusion --

Take a look at the data sheet. Specifically Fig 1-2 on p6, Block Diagram.
Tape it on the wall in front of you for reference. This is a graphical
representation of the silicon itself. It doesn't have every niggly detail
about how one thing affects another, carries and flags and such, but is a
good overview of everything involved. It'll help you understand the
relationship between the hardware (ports and registers) and software
(instructions that tell the hardware where to move the bits and bytes
around.) Refer back to it often as you study the instruction set.

Have a :) day!


jim barchuk

-- hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

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