what is wrong with this assembly code? (pic18f4520)

Discussion in 'Embedded Systems and Microcontrollers' started by thar07, Sep 27, 2015.

  1. thar07

    Thread Starter Member

    Jan 3, 2015
    71
    0
    I wrote this program to light the LED when the switch moves, but it did not work in the protues.The program does not detect the input.Could you please tell why is that?

    Code (Text):
    1.  
    2.   #include <p18F4520.inc>
    3.   CONFIG OSC=HS , WDT=OFF , LVP=OFF
    4.  
    5. porta   equ 0xf80  ;port and tris adress
    6. trisa   equ 0xf92
    7. portb   equ 0xf81
    8. trisb   equ 0xf93
    9. portc   equ 0xf82
    10. trisc   equ 0xf94
    11. trisd   equ 0xf95
    12. portd   equ 0xf83
    13.  
    14.   org 0h
    15.   bsf trisb,3
    16.   bcf trisd,0
    17.  
    18. h: btfsc portb,3
    19.   BCF PORTD,0
    20.  
    21.   goto h
    22.   bsf portd,0
    23.   goto h
    24.  
    25.   end
    26.  
     
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    How come you are using 12 bit number to set the TRISB?
    TRISB equ 0x92.
    (oxf92)?
    Max.
     
  3. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,028
    He's not, Max. Those are register address definitions which actually are unnecessary with the .inc file IF the names are in upper case as they should be.

    The reason that the code does not work is that at line 21 it does a goto h which will keep anything below from being executed at all.

    Code (Microchip Assembler):
    1.  
    2.   #include <p18F4520.inc>
    3.   CONFIG OSC=HS , WDT=OFF , LVP=OFF
    4.  
    5.   ORG 0h
    6.  
    7.   bsf TRISB,3
    8.   bcf TRISD,0
    9. h:
    10.   btfsc PORTB,3    ; test the input once then decide what to do
    11.   bra    in_is_1
    12.  
    13.   BCF PORTD,0    ; output follows input
    14.   bra    h
    15.  
    16. in_is_1:
    17.   bsf PORTD,0
    18.   bra h
    19.   END
     
    Last edited: Sep 27, 2015
    thar07 likes this.
  4. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    Yup I overlooked it in haste!
    Max.
     
    JohnInTX likes this.
  5. thar07

    Thread Starter Member

    Jan 3, 2015
    71
    0
    Thank you very much
     
  6. thar07

    Thread Starter Member

    Jan 3, 2015
    71
    0
    The above program is works fine for the portc,7 pin but not for portb,3. why is that ?
    are there any configationtions to set to use portb as a input ?
     
  7. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,028
    Yes. You have to configure ADCON1. If you don't, the port will read '0' all the time.
    These things are well covered in the datasheet. That's the first place to go when designing and then debugging a program.

    You should look at the SUMMARY OF REGISTERS ASSOCIATED WITH PORTB (Table 10-4) and visit each of the registers listed. Same for ALL other ports.

    To head off your next problem, be sure to init ALL of the IO ports on the PIC not just the few you use. You also need to fully specify all _CONFIG for the target environment. Don't scrimp on that stuff .. it will bite you - hard.

    Good luck!
     
    thar07 likes this.
Loading...