Configure PORTA (Assembley)

Discussion in 'Embedded Systems and Microcontrollers' started by Dawud Beale, Jan 17, 2013.

  1. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    When writing a program for a PIC in assembley, how do you configure PORTA as a digital input and use the stimulus to toggle the value of the pin RAO?

    I have set TRISA to 00000001 and set ANSELA to 00000000 but when I fire the stimulus (On MPLAB and the stimulus is set tot toggle) no bit changes on PORTA.

    Anyone know Assembely and MPLAB well?
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Dawud Beale likes this.
  3. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Ok I'll take a look, I'm using the 12f1822 but I am trying to get it to work on the simulator before I worry about actually downloading to a Microcontroller so I just want to get it to work in MPLAB first
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Is there something else that needs to be changed, like CMCON?
    Also you won't see the port change until the next instruction, so toggle the bit, and single step to see if that makes any difference.
    If not, post the code.
     
    Dawud Beale likes this.
  5. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    The PIC doesnt have a CMCON that I am aware of. According to the datasheet teh only registers related to PORTA are TRISA, LATA, ANSELA, WUA and INLVLA but I have only configured ANSELA and TRISA.

    I set the 0th bit to 1 on both.

    I haven't used BSR as I don't really understand what it does, but some tutorials state I need to use it. Could that be the problem?
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    In questions like this. Always post your code. As we will ask for it anyway
     
    Dawud Beale likes this.
  7. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    include C:\pathtomyheaderfile\as12f1822.h


    list p = 12f1822

    TRISA equ 008Ch


    ;*******Main body of program**************************************************


    movlw 0x01 ;
    movwf BSR ;

    movlw 0x01 ;
    movwf TRISA ;

    movlw 0x01 ;
    movwf ANSELA ;

    movlw 0x01 ;
    movlw 0x01 ;
    movlw 0x01 ;

    END

    I put the last 3 instructions in so I can try firing the input and then step through an instruction to see if it changed

    I have the correct path to the header file on the actual code
     
  8. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    I wanted to upload the header file aswell so you could simulate it in case you wanted to see how it runs but the forum won't upload .h files
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I am not sure I understand. Is the header file something you(or other) have made or is it from Microchip
     
  10. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    from microchip
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Does this actually build??? a dot-h file is for C programs. You want to include the dot-inc file for your PIC. Do search your microchip folder for 12F1822TEMP.ASM which is an outline of everything this PIC needs: every PIC has such a file and should be copied (leave the original intact for another project) and you can just start banging code into the area after "START."

    Do look up the RAM bank selections. You need to change banks for different registers.

    You also don't do any configuration word settings.

    Finally, you will need to set the ANSELA bit for RA0 to 0 and not 1 to disable the analog functionality and read the pin as anything as zero.
     
    Dawud Beale likes this.
  12. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    It builds ok and does configure the registers the way I tell it to, but nothing happens when I trigger the input stimulus

    what bank register to i need to set?

    why do u think PORTA doesnt change when I fire the input? I changed ANSELA
     
    Last edited: Jan 17, 2013
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    The stimulus will not create any changes unless the port is read. Try to make a program that mirror the output of one port to some other port. Also as a beginner you should go for a PIC with 28 or 40 pins. It is kind of wrong that small PIC is more simple to learn than the larger ones
     
  14. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    This is unfortunatelye the one we have been specified to use on the course.

    What do you mean by the port is read?
     
  15. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Changed ANSELA to what?

    As TRISA and ANSELA are in different banks it is impossible for your code to change them both: while TRISA is indeed in bank 1 ANSELA is in bank 3, and you never touch bank 3.

    Dawud, it is time to post your code again. The full .asm file, inside code tags please.

    You get code tags with the button with the # icon.

    BTW, using MPLAB sim before loading into a physical PIC is an excellent thing to do. Now let us help you get the other things straight to get the code to work.
     
  16. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    I just tried the code you posted and it doesn't assemble without errors... Could you benefit from some examples?

    Regards, Mike

    Code ( (Unknown Language)):
    1. ;******************************************************************
    2. ;                                                                 *
    3. ;   Filename:                                                     *
    4. ;     Author:                                                     *
    5. ;       Date:                                                     *
    6. ;                                                                 *
    7. ;   12F1822 Experiment                        *
    8. ;                                                                 *
    9. ;                                                                 *
    10. ;      MPLab: 8.84    (tabs=8)                                    *
    11. ;      MPAsm: 5.44                                                *
    12. ;                                                                 *
    13. ;******************************************************************
    14.  
    15.     #include <P12F1822.INC>
    16.     radix dec
    17.     errorlevel -302
    18.     list st=off
    19.  
    20.   __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _IESO_OFF & _FCMEN_OFF
    21.   __CONFIG _CONFIG2, _LVP_OFF & _PLLEN_OFF
    22. ;
    23. ; _PWRTE_OFF    default
    24. ; _CP_OFF   default
    25. ; _CPD_OFF  default
    26. ; _BOREN_ON default
    27. ; _CLKOUTEN_OFF default
    28. ;
    29. ; _WRT_OFF  default
    30. ; _STVREN_ON    default
    31. ; _BORV_LO      default
    32. ;
    33.  
    34. ;******************************************************************
    35. ;  variables                                                      *
    36. ;******************************************************************
    37.         cblock  0x20
    38.  
    39.         endc
    40.  
    41. ;******************************************************************
    42. ;  main init                                                      *
    43. ;******************************************************************
    44.     org 0x0000
    45. init
    46.     banksel OSCCON      ; bank 1              |B1
    47.     movlw   b'01111010' ;                 |B1
    48.     movwf   OSCCON      ; setup 16-MHz INTOSC         |B1
    49. waitfs  btfss   OSCSTAT,HFIOFS  ; stable? yes, skip, else     |B1
    50.     bra waitfs      ; branch and wait         |B1
    51. ;
    52. ;  configure port I/O
    53. ;
    54.     banksel ANSELA      ; bank 3              |B3
    55.     clrf    ANSELA      ; set digital I/O         |B3
    56.     banksel TRISA       ; bank 1              |B1
    57.     clrf    TRISA       ;                 |B1
    58.     bsf TRISA,TRISA5    ; GP?/RX must be set to input     |B1
    59.     banksel LATA        ; bank 2              |B2
    60.     clrf    LATA        ;                 |B2
    61.     movlb   0               ; bank 0                          |B0
    62.  
    63. ;******************************************************************
    64. ;  main loop                                                      *
    65. ;******************************************************************
    66.  
    67. loop
    68.         bra     loop            ;                                 |B0
    69.  
    70.  
    71. ;******************************************************************
    72.         end
    73.  
     
Loading...