I have a multiprogrammer board (PIC 16F877A) with the Switch board on PORTA and the LED board on PORT B. I want to turn on the LED assocated with the pressed switch. I have attempted several different programs without any success. The program below is the minimul that I thought should work. No luck. Any help would be appreciated.
STATUS EQU 003
TRISA EQU 085
TRISB EQU 086
PORTA EQU 005
PORTB EQU 006
ORG 000
goto 5
ORG 004
goto 005
ORG 005
bsf STATUS,5
movlw 0xFF
movwf TRISA
movlw 0x00
movwf TRISB
bcf STATUS,5
Here movf PORTA,0
movwf PORTB
goto Here
END
Jgriffin
Reading and writing to ports
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Hello I can see a few potential problems with the code.
Firstly which compiler are you using. And how is a hex number represented.
Eg. you used
But normally it would be something like this
Next there is this bit
Which I think would work a lot better like this. However I am not sure about it and this may well be the reason your programs arn't working.
The setup of the TRIS registers is fine.
The final loop probably needs a : after the label EG
Firstly which compiler are you using. And how is a hex number represented.
Eg. you used
Code: Select all
STATUS EQU 003
TRISA EQU 085
TRISB EQU 086
PORTA EQU 005
PORTB EQU 006
Code: Select all
STATUS EQU 0x03
TRISA EQU 0x85
TRISB EQU 0x86
PORTA EQU 0x05
PORTB EQU 0x06
Code: Select all
ORG 000
goto 5
ORG 004
goto 005
ORG 005
Code: Select all
ORG 0x00
goto start
start: ORG 0x04
The final loop probably needs a : after the label EG
Code: Select all
Here: movf PORTA,0
movwf PORTB
goto Here
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Steve
- Matrix Staff
- Posts: 3426
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Reading and writing to ports
Ben's right - this could be your problem (the default "radix" could be set to decimal rather than hex). MPASM used H'xx' to represent hex numbers, so you code should be similar to:jgriffin wrote:TRISB EQU 086
Code: Select all
TRISB EQU H'86'
Another thing to look at is whether the configuration settings for the chip are appropriate for the actual hardware used. For example: if you are using a crystal for the oscillator, are you specifying this correctly in the config word settings (i.e. XT or HS, depending on the osc frequency)?
Thanks
Thanks so much for the reply.
I am using MPLab 7.2 with the default radix set to HEX.
A little more background may help. I have the switches set on the Multiprogrammer board to FAST and XTAL. I have written the same program in Flowcode and it works fine. I also set PPP to HS, WDT off, LVP off. I have simulated the code in MPLab and the flow is ok.
I made a couple of suggested changes and added the configuration word from PPP. The code is below.
__CONFIG 0x3F39
STATUS EQU H'03'
TRISA EQU H'85'
TRISB EQU H'86'
PORTA EQU H'05'
PORTB EQU H'06'
ORG 00
goto Start
ORG 04
goto Start
ORG 05
Start: bsf STATUS,5
movlw 0xFF
movwf TRISA
movlw 0x00
movwf TRISB
bcf STATUS,5
Here: movf PORTA,0
movwf PORTB
goto Here
END
I am mixing hex designation and default radix but I don't think that matters. (Though not proper)
Still, no lights. I think I must be missing something simple in the setup.
Any ideas?
Thanks,
Jack
I am using MPLab 7.2 with the default radix set to HEX.
A little more background may help. I have the switches set on the Multiprogrammer board to FAST and XTAL. I have written the same program in Flowcode and it works fine. I also set PPP to HS, WDT off, LVP off. I have simulated the code in MPLab and the flow is ok.
I made a couple of suggested changes and added the configuration word from PPP. The code is below.
__CONFIG 0x3F39
STATUS EQU H'03'
TRISA EQU H'85'
TRISB EQU H'86'
PORTA EQU H'05'
PORTB EQU H'06'
ORG 00
goto Start
ORG 04
goto Start
ORG 05
Start: bsf STATUS,5
movlw 0xFF
movwf TRISA
movlw 0x00
movwf TRISB
bcf STATUS,5
Here: movf PORTA,0
movwf PORTB
goto Here
END
I am mixing hex designation and default radix but I don't think that matters. (Though not proper)
Still, no lights. I think I must be missing something simple in the setup.
Any ideas?
Thanks,
Jack
Got IT!!
Steve,
The minimum code below works . The problem was the A/D on Port A of the 877A. I noticed in several of the other post the reference to configuraing the A/D as digital I/O. Also, I used the Flowcode .asm file as a guide. By deleting code, I finally narrowered it down to the
movlw D'7' ;turn off A/D
movwf ADCON1
section of code.
If you see any major problems with the code, I would appreciate your comments.
Thanks,
Jack
STATUS EQU 0x03
PORTA EQU 0x05
PORTB EQU 0x06
TRISA EQU 0x85
TRISB EQU 0x86
ADCON1 EQU 0x9f
A EQU 0x20
ORG 0
goto Start
ORG 4
goto Start
Start
bsf STATUS, 5 ;switch to bank 1
bcf STATUS, 6
movlw 0xff ;make porta an input
movwf TRISA
movlw 0x00 ;make portb an output
movwf TRISB
movlw D'7' ;turn off A/D
movwf ADCON1
bcf STATUS, 5 ;switch to bank 0
bcf STATUS, 6
Here
movf PORTA, W ;read porta
movwf A
movf A, W ;save value in a
movwf PORTB ;write value to portb
goto Here ;loop
END
The minimum code below works . The problem was the A/D on Port A of the 877A. I noticed in several of the other post the reference to configuraing the A/D as digital I/O. Also, I used the Flowcode .asm file as a guide. By deleting code, I finally narrowered it down to the
movlw D'7' ;turn off A/D
movwf ADCON1
section of code.
If you see any major problems with the code, I would appreciate your comments.
Thanks,
Jack
STATUS EQU 0x03
PORTA EQU 0x05
PORTB EQU 0x06
TRISA EQU 0x85
TRISB EQU 0x86
ADCON1 EQU 0x9f
A EQU 0x20
ORG 0
goto Start
ORG 4
goto Start
Start
bsf STATUS, 5 ;switch to bank 1
bcf STATUS, 6
movlw 0xff ;make porta an input
movwf TRISA
movlw 0x00 ;make portb an output
movwf TRISB
movlw D'7' ;turn off A/D
movwf ADCON1
bcf STATUS, 5 ;switch to bank 0
bcf STATUS, 6
Here
movf PORTA, W ;read porta
movwf A
movf A, W ;save value in a
movwf PORTB ;write value to portb
goto Here ;loop
END