Question on variable values in register(more than 255)

Discussion in 'Embedded Systems and Microcontrollers' started by eddy123456, Feb 19, 2008.

  1. eddy123456

    Thread Starter New Member

    Feb 4, 2008
    7
    0
    Hi guys,

    Ive found out that the only way to have a value larger than 255(for a variable) is by increasing the value of another variable every time the first variable overflow. By the way im using pic16f84 and im currently learning how to program in assembly language.

    Example, "counta" for values from 0-256 and "countb" is for overflow of "counta".

    This is an example code that i found which shows the calculation of numbers more than 256. The result will correspond to an address in a map.

    rpmhi=counter overflow for rpmlow.
    value of rpm : 74<(rpmhi+rpmlow)<600

    Code:
    Code ( (Unknown Language)):
    1.    
    2.         ; the formula to get the data stored in the map is as follows
    3.         ;
    4.         ;                (rpmhi+rpmlow count)  -  74
    5.         ;    131   -    ----- ----------------------
    6.         ;                           4
    7.         ;
    8. caladv  movlw d'74'          ; (rpmhi+rpmlow count) - 74
    9.         subwf rpmlo,F      ; rpmlo = rpmlo - 74
    10.         btfss STATUS,C
    11.         decf rpmhi,F
    12.         ;
    13.         ; divide result by 4
    14.         ;
    15.         bcf STATUS,C
    16.         rrf rpmhi,F
    17.         rrf rpmlo,F        ; / 2
    18.         ;
    19.         bcf STATUS,C
    20.         rrf rpmhi,F
    21.         rrf rpmlo,F        ; / 4
    22.         ;
    23.         movlw .131         ; 131 entries in map list
    24.         movwf math
    25.         ;
    26.         movf rpmlo,W
    27.         subwf math,W       ; W = 131 - result
    28.         ;
    29.         bcf PCLATH,0      ; be sure to go h'200'
    30.         bsf PCLATH,1      ; where is the map.
    31.         call map      ; read map
    32.         movwf rtdset       ; come back with new retard value
    33.  


    Im not sure how the codes work.Ive tried the logic, and it seems fine for numbers <256,

    example :

    say value of rpm : (rpmhi+rpmlow)=240

    where rpmhi=0 and rpmlow=d'240'

    ive done 2 different approach :
    a) using calculator =41.5
    b) using the logic operator (subwf,rrf) =41


    But when rpm value > 256, say (rpmhi+rpmlow)=496

    where rpmhi=1 and rpmlow=d'240'

    same approach as above:
    a) using calculator =105.5
    b) using the logic operator (subwf,rrf) =41

    or am i missing something here..:confused:
     
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Taking your example value:

    rpm = 240
    rpmhi = 0x00, rpmlo = 0xF0

    value = (0xF0 - 0x4A)/0x04
    value = 0x29


    rpm = 496
    rpmhi = 0x01, rpmlo = 0xF0

    after being subtracted by 0x4A, rpmlo would be 0xA6.
    after the first rrf, rpmhi would be 0x00, and the C would be 1.
    after the second rrf, rpmlo would be 0xD3.
    after the third rrf, rpmhi wold stay at 0x00, and the C would be 0.
    after the fourth rrf, rpmlo would be 0x69.

    so, the value would be 0x69, which is the right value of 105.

    The thing that you seemed to miss was that rrf is a rotate right through carry instruction. Which means that the C bit is inserted at MSB and the LSB is shifted to C.
     
  3. eddy123456

    Thread Starter New Member

    Feb 4, 2008
    7
    0
    oh! now i get it, C is used to divide the 8 bit binary as if it were a 16 bit :

    rrf 00000001 11110000

    is the same as

    bcf
    rrf 00000001
    rrf 11110000

    Thanks a lot!
     
Loading...