+ Reply to Thread
Results 1 to 4 of 4

Thread: Understanding PI16F877 Timing

  1. #1
    dramrattan Good dramrattan Good
    Join Date
    Oct 2008
    Posts
    156

    Post Understanding PI16F877 Timing

    Hi,

    I'm trying to understand how to arrive at the timing values stated in the lecture unit attached. On page 6, there is a snippet which states that the delay should be 1ms however i think it should be 1us since it is a 4MHz clock. Here is the code:

    delay_1ms movlw 0xF9 ; 0xF9 = 249
    nop
    usec4 addlw 0xFF ; add -1, cannot use decrement, why?
    btfss STATUS,Z
    goto usec4
    return

    The value 249 is used because it needs to go through 256 cycles in order for one instruction to take place right?...The 249 + 2 (cycles for call) + 1 (cycle for loading of W) + 4 (cycles for loop) = 256 cycles

    Therefore, if all this is correct, could someone help me understand (which is also on page 6):

    This routine can be expanded through the use of a variable to allow times in multiples of 1 ms.
    For example
    ; routine is passes the amount of ms to delay in W
    ; it uses an additional register variable msec_count
    delay_ms movwf msec_count
    msecloop movlw 0xF8 ; 0xF8 = 248
    call usec4 ; 248 * 4 + 2 (call) = 994
    nop
    nop
    decfsz msec_count,F
    goto msecloop
    return

    How come decrement could be used in the second snippet and not the first?...The 248 * 4 + 2 = 994 is what is confusing me i think...because it is using that to generate the multiples of 1ms delays right?...but i don't understand how they arrive at that.

    Thanks for any help.
    Attached Files


  2. #2
    testerplus Newbie
    Join Date
    Apr 2009
    Location
    Russia, S-Pb
    Posts
    9

    Default

    (Tags CODE would be nice)

    Quote Originally Posted by dramrattan View Post
    Hi,
    usec4 addlw 0xFF ; add -1, cannot use decrement, why?
    There is no decrement instructions for WREG. You can decrease WREG by adding -1 (or 0xFF) only.

    call usec4 ; 248 * 4 + 2 (call) = 994
    Here is an error: 248*4 + 2(call) + 2(return) = 996

    decfsz msec_count,F
    ...
    How come decrement could be used in the second snippet and not the first?
    Instruction decfsz decrements a register (it can not to work with an accumulator WREG).

  3. #3
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,807

    Default

    Quote Originally Posted by testerplus View Post
    Here is an error: 248*4 + 2(call) + 2(return) = 996
    There is still an error as the last pass through usec4 takes 1 cycle less. So, it's 248*4-1 + 2(call) + 2(return) = 995.

    Also, another reason to use addlw 255 is that decrement doesn't set the zero flag.

    Mike.

  4. #4
    testerplus Newbie
    Join Date
    Apr 2009
    Location
    Russia, S-Pb
    Posts
    9

    Default

    Quote Originally Posted by Pommie View Post
    There is still an error as the last pass through usec4 takes 1 cycle less. So, it's 248*4-1 + 2(call) + 2(return) = 995.
    Of course!

+ Reply to Thread

Similar Threads

  1. Understanding an UHF circuit
    By bugmenot in forum Electronic Projects Design/Ideas/Reviews
    Replies: 0
    Latest: 8th September 2008, 01:04 AM
  2. Im understanding but....
    By JBizzle in forum Electronic Projects Design/Ideas/Reviews
    Replies: 2
    Latest: 16th April 2008, 12:10 PM
  3. understanding multiplier ckt
    By electronist in forum General Electronics Chat
    Replies: 3
    Latest: 4th May 2005, 03:46 AM
  4. understanding a delays
    By timothyjackson in forum Micro Controllers
    Replies: 11
    Latest: 13th January 2005, 08:23 AM
  5. Understanding the DM74LS47N
    By Valente in forum General Electronics Chat
    Replies: 5
    Latest: 23rd September 2004, 03:44 PM

Tags for this Thread