Reading and writing to ports

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
jgriffin
Posts: 22
Joined: Sat Jan 07, 2006 6:47 pm
Location: USA
Contact:

Reading and writing to ports

Post by jgriffin »

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

User avatar
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:

Post by Benj »

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

Code: Select all

STATUS EQU 003 
TRISA EQU 085 
TRISB EQU 086 
PORTA EQU 005 
PORTB EQU 006 
But normally it would be something like this

Code: Select all

STATUS EQU 0x03 
TRISA EQU 0x85 
TRISB EQU 0x86 
PORTA EQU 0x05 
PORTB EQU 0x06 
Next there is this bit

Code: Select all

ORG 000 
goto 5 
ORG 004 
goto 005 
ORG 005 
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.

Code: Select all

ORG 0x00 
goto start 
start: ORG 0x04 
The setup of the TRIS registers is fine.

The final loop probably needs a : after the label EG

Code: Select all

Here: movf PORTA,0 
movwf PORTB 
goto Here 

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Reading and writing to ports

Post by Steve »

jgriffin wrote:TRISB EQU 086
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:

Code: Select all

TRISB	EQU	H'86'
Your startup code is ok (the one with the ORG statements). Ben's suggestion is not quite right because ROM location 004 is the interrupt vector and the code should not start there (the 005 in your post is more appropriate).

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)?

jgriffin
Posts: 22
Joined: Sat Jan 07, 2006 6:47 pm
Location: USA
Contact:

Thanks

Post by jgriffin »

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

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

0x3F39 specifies a XT oscillator (which is good for crystal speeds up to 4Mhz or so). You may want a HS oscillator (0x3F3A).

jgriffin
Posts: 22
Joined: Sat Jan 07, 2006 6:47 pm
Location: USA
Contact:

Got IT!!

Post by jgriffin »

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

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Hi Jack,

Turning the ADC's to digital inputs should fix the problem - sorry I didn't spot it initially.

The code is fine, but it is not essential to save the value of porta to a variable before sending it to portb.

Post Reply