# what is wrong with this assembly code? (pic18f4520)

#### thar07

Joined Jan 3, 2015
71
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:
  #include <p18F4520.inc>
CONFIG OSC=HS , WDT=OFF , LVP=OFF

porta   equ 0xf80  ;port and tris adress
trisa   equ 0xf92
portb   equ 0xf81
trisb   equ 0xf93
portc   equ 0xf82
trisc   equ 0xf94
trisd   equ 0xf95
portd   equ 0xf83

org 0h
bsf trisb,3
bcf trisd,0

h: btfsc portb,3
BCF PORTD,0

goto h
bsf portd,0
goto h

end

Joined Jul 18, 2013
20,911
How come you are using 12 bit number to set the TRISB?
TRISB equ 0x92.
(oxf92)?
Max.

#### JohnInTX

Joined Jun 26, 2012
4,113
How come you are using 12 bit number to set the TRISB?
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:
  #include <p18F4520.inc>
CONFIG OSC=HS , WDT=OFF , LVP=OFF

ORG 0h

bsf TRISB,3
bcf TRISD,0
h:
btfsc PORTB,3    ; test the input once then decide what to do
bra    in_is_1

BCF PORTD,0    ; output follows input
bra    h

in_is_1:
bsf PORTD,0
bra h
END

Last edited:

Joined Jul 18, 2013
20,911
He's not, Max. Those are register address definitions which actually are unnecessary with the .inc file
Yup I overlooked it in haste!
Max.

#### thar07

Joined Jan 3, 2015
71
Thank you very much

#### thar07

Joined Jan 3, 2015
71
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 ?

#### JohnInTX

Joined Jun 26, 2012
4,113
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 ?
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!