indirect addressing in PIC MCU

Discussion in 'Embedded Systems and Microcontrollers' started by walid el masry, Feb 27, 2010.

  1. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    i have problem with indirect addressing in PIC16f877A, i wanna learn the indirect addressing so i saw the datasheet example and it was easy it clears memory from address 0x20 to 0x2f like this

    Code ( (Unknown Language)):
    1.  
    2. ORG 0x500
    3. BCF PCLATH,4
    4. BSF PCLATH,3 ;Select page 1
    5. ;(800h-FFFh)
    6. CALL SUB1_P1 ;Call subroutine in
    7. : ;page 1 (800h-FFFh)
    8. :
    9. ORG 0x900 ;page 1 (800h-FFFh)
    10. SUB1_P1
    11. : ;called subroutine
    12. ;page 1 (800h-FFFh)
    13. :
    14. RETURN ;return to
    15. ;Call subroutine
    16. ;in page 0
    17. ;(000h-7FFh)
    18.  
    so i decided to write my 1st program, i wanna save data indirectly and retrieve it indirectly and display it on portb the data is 0x01,0x02,0x04 which will blink leds on portb with time difference 0.5 sec between each display and before the whole process but

    unfortunately the code doesn't work correctly it blink RB5,RB1,RB0 unlike what i wrote(RB0,RB1,RRB2) that my code here


    Code ( (Unknown Language)):
    1.  
    2. ;------------------------------------------
    3. ;device configuration
    4. ;------------------------------------------
    5.     processor 16f877A
    6.     #include<p16F877a.inc>
    7.     __config _CP_OFF&_DEBUG_OFF&_WRT_OFF&_CPD_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC
    8. ;------------------------------------------
    9. ;variable decleration
    10. ;------------------------------------------
    11. cblock 0x24
    12. d1
    13. d2
    14. d3
    15. endc
    16. ;------------------------------------------
    17. ;device initialise
    18. ;------------------------------------------
    19.     org 0x00
    20.     bsf STATUS,RP0
    21.     movlw 0x00
    22.     movwf TRISB
    23.     bcf STATUS,RP0
    24.     movwf PORTB
    25. ;------------------------------------------
    26. ;load data in 0x20 ~ 0x22 with Fh,Eh,Dh
    27. ;------------------------------------------
    28. movlw 0x20
    29. movwf FSR
    30. movlw 0x01
    31. movwf INDF
    32. INCF FSR,F
    33. movlw 0x02
    34. movwf INDF
    35. INCF FSR,F
    36. movlw 0x04
    37. movwf INDF
    38. ;------------------------------------------
    39. call long_delay;wait 0.5 sec
    40. ;------------------------------------------
    41. ;read data from 0x20 ~ 0x22 and show it on PORTB for 0.5 sec for each address then stop
    42. ;------------------------------------------
    43. movlw 0x20
    44. movwf FSR
    45. next:
    46. movf INDF
    47. movwf PORTB
    48. call long_delay
    49. INCF FSR,F
    50. movlw 0x23
    51. xorwf FSR,W
    52. btfss STATUS,Z
    53. goto next;no
    54. ;------------------------------------------
    55. stop
    56. goto stop
    57. ;------------------------------------------
    58. ;0.5 sec subroutine
    59. ;------------------------------------------
    60. long_delay
    61.             ;499994 cycles
    62.     movlw    0x03
    63.     movwf    d1
    64.     movlw    0x18
    65.     movwf    d2
    66.     movlw    0x02
    67.     movwf    d3
    68. Delay_0
    69.     decfsz    d1, f
    70.     goto    $+2
    71.     decfsz    d2, f
    72.     goto    $+2
    73.     decfsz    d3, f
    74.     goto    Delay_0
    75.             ;2 cycles
    76.     goto    $+1
    77.             ;4 cycles (including call)
    78.     return
    79. ;------------------------------------------
    80. end
    81.  
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Is it this instruction?
    movf INDF
    the default would be F if you don't put ",W" on the end.
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    A nice little feature to neaten up the code is if you put:
    cblock 0x20
    ramtable:3 ;reserve 3 bytes
    .
    .

    and then anytime you do:
    movlw 0x20
    movwf FSR

    you can replace it with:
    movlw ramtable
    movwf FSR
     
  4. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    ooh what an idiot mistake yeah it was it i forgot to put "W" after "movf INDF" thnx " Markd77 " i'm about to use the indirect addressing in saving strings and retrieve them again when interfacing to LCD but there will be a problem that if i use too many strings the controller will still busy on reset in saving these strings in memory and may not response to outside untill it finishes, is there any way to save these strings in the mcu in flashing stage?
     
    Last edited: Feb 27, 2010
  5. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    i guess i will need to tell the compiler which byte i point to in ramtable because the work register is 8bit ?
     
    Last edited: Feb 27, 2010
  6. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It should work fine if you are still using incf FSR, F. I'm not sure what happens if you use it on PICs with more than 256 bytes of memory.
    If you want to store more you would have to use the EEPROM or a table in program memory using the retlw instruction.
    Have a look at AN556 on microchip site for the table read - it can get complicated.
     
  7. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    ok iam checking it now but by the way it is the 1st time i know that i can move register value to work register just by using "MOVLW" i used to do it like this
    movf f,w is there any difference between them or just the "movlw" is dealing with work register only unlike the "movf"
     
  8. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    In this case it is not using the contents of ramtable, it is using the address of ramtable, which is 0x20.
    It was probably easier to understand when variables were declared like:
    var equ 0x20
    var2 equ 0x21

    Any time you use a variable name in place of a literal it uses the address.
     
  9. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It's easy enough to read and maintain your code at the moment, but if you had 4 tables and you decided to change the sizes, this method could save a lot of time.
     
  10. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    yes yes i know that it uses the address of the ramtable but the problem is still exist ramtable addresses a 3byte data and the "FSR" even "INDF" is 1byte wide :)
    in my example ramtable will contain 0x010204 with address through 0x20,0x21,0x22 and i need to retrieve just 1 byte of 3 each time how could i point to it in code
     
  11. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    What happens in the cblock is that the number 0x20 is given the name "ramtable", the next 2 bytes are reserved but don't have names.
    Every time the compiler sees "ramtable" it just replaces it with the number 0x20.
    for example:
    movlw ramtable
    compiles to movlw 0x20

    and:
    incf ramtable, F
    compiles to incf 0x20, 1

    The PIC itself is expecting either a literal or a file address depending on the instruction.

    If you try it and watch what happens to the W reg when it executes the "movlw ramtable" instruction it should become clearer.
     
  12. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    yes it works but i thought that

    cblock 0x20
    example
    endc

    will take the address of 0x20 and name it "example" cause it is easier to deal with names unlike numbers so why when i wrote

    movlw example

    it moves the address value of 0x20 not the content of the address 0x20 which pointed to by the name "example"

    what is the reference you learned mcu from it ? :)
     
  13. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    check this it really helps but he said like me that example is name of the address of 0x20 unlike in realtime it holds the address it self like you said and i test :S

    http://tutor.al-williams.com/pic-inst.html

    he said

     
  14. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Sounds like you have got the hang of it. I think I probably picked it up from code examples on the web.
     
Loading...