Never seen this before!!


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Location
    London
    Posts
    251

    Default Never seen this before!!

    Code:
     
    movlw   -97
    addwf   TMR0
    I have never come across any negative number getting loaded into a variable.
    Is this the same as Loading 159-> (256-97=159) to Timer0 ?

  2. #2
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default Re: Never seen this before!!

    Quote Originally Posted by Megahertz View Post
    Code:
     
    movlw   -97
    addwf   TMR0
    I have never come across any negative number getting loaded into a variable.
    Is this the same as Loading 159-> (256-97=159) to Timer0 ?
    It sure is.

    You can look at the .LST file after you compile the program and find the line for "movlw -97". You will see that it will be something like:

    00009E 0E9F 00232 movlw -97

    00009E <-- Address of the the instruction

    0E9F <-- Actual instruction code

    If you look up the MOVLW code in the PIC datasheet, you will see the OP-CODE (most significant 8 bits of the 16 bit instruction) is $0E (%00001110). The literal is the 8 least significant bits, $9F in this case (which is 159).
    Last edited by SteveB; - 1st July 2012 at 23:08.

  3. #3
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default Re: Never seen this before!!

    Much appreciated your in-depth answer. Thanks Steve

  4. #4
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default Re: Never seen this before!!

    Quote Originally Posted by SteveB View Post
    Quote Originally Posted by Megahertz View Post
    Code:
     
    movlw   -97
    addwf   TMR0
    I have never come across any negative number getting loaded into a variable.
    Is this the same as Loading 159-> (256-97=159) to Timer0 ?
    It sure is.
    That's not exactly correct. Notice that the constant value is being added to the free running TMR0 register. In this case it seems the author is using Timer 0 to generate precise 100 cycle intervals. Of course this only works if you're not using the TMR0 prescaler since any write to TMR0 will also clear the prescaler. The -97 value accounts for one cycle for TMR0 overflow and the inherent two cycle delay you get after writing TMR0 before TMR0 starts counting again.

    Hope this information helps.

    Cheerful regards, Mike
    Last edited by Mike, K8LH; - 2nd July 2012 at 15:26.

  5. #5
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default Re: Never seen this before!!

    Good point. I was focused on the fact that the movlw had a negative value. Your explaination takes it to the next step as to why the value would be there in the first place. Nicely done!

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts