Hi all, I'm taking something I wrote in
Parallax 's assembler, and now just starting to learn the
Micro chip MPASM , I find something a bit confusing.
1. Port direction control
2. port reading/writing.. ie: every interface to the world..
The problem arises because Parallex in their manual states that
this is the instruction.. MOV #literal !RA for example sets the directio
n for
the register, but in the Micro chip version, they are using the TRST com
mand
that is, according to the Micor chip test, not to be supported in the fu
ture.
Help..
Simple I/O/ guys/gals... an simple example program would be
great. Anything on the web that is very explicit?
I have all the ASM stuff from the microchip web page, and their handbook
etc.
Hi all, I'm taking something I wrote in
Parallax 's assembler, and now just starting to learn the
Micro chip MPASM , I find something a bit confusing.
1. Port direction control
2. port reading/writing.. ie: every interface to the world..
The problem arises because Parallex in their manual states that
this is the instruction.. MOV #literal !RA for example sets the
direction for
the register, but in the Micro chip version, they are using the TRST
command
that is, according to the Micor chip test, not to be supported in the
future.
Help..
Simple I/O/ guys/gals... an simple example program would be
great. Anything on the web that is very explicit?
I have all the ASM stuff from the microchip web page, and their handbook
etc.
> I'm taking something I wrote in Parallax 's assembler, and now just
> starting to learn the Micro chip MPASM , I find something a bit
> confusing.
>
> 1. Port direction control
> 2. port reading/writing.. ie: every interface to the world..
>
> The problem arises because Parallex in their manual states that
> this is the instruction.. MOV #literal !RA for example sets the
> direction for the register, but in the Micro chip version, they are
> using the TRST command that is, according to the Micor chip test,
> not to be supported in the future.
Brad:
You didn't say which PIC you're using, so I'll assume that it's one
of the 16C5x family.
Port Direction control on the '5x is accomplished via the TRIS
instruction, the syntax of which is "TRIS port", where "port" is
PORTA, PORTB, or (on the C55 or C57) PORTC.
TRIS uses the contents of the W-register to select the direction of
the port's I/O pins: 0's are outputs and 1's are inputs. Here's an
example:
MOVLW 00001111B ;Make RB4-7 outputs, RB0-3 inputs.
TRIS PORTB
To read the I/O pins, just use any instruction that reads from the
port's register. Here are two examples:
MOVF PORTB,W ;Copy the contents of PORTB to W.
BTFSS PORTB,3 ;Skip the next instruction if RB3 is hig
h.
To write to the I/O pins, use any instruction that writes to the
port's register. Here are another two examples:
MOVWF PORTB ;Copy the contents of the W-reg to PORTB
.
BSF PORTB,3 ;Pull RB3 high.
CLRF PORTB ;Pull all the PORTB outputs low.
Use of the TRIS command in 16Cxx (NOT 16C5x) parts is, as you
mentioned, discouraged by Microchip in their data books. However,
they have yet to produce a 16Cxx part that doesn't include the TRIS
instruction.
If you're using the 16C5x parts, on the other hand, you can ignore
the warning; the TRIS command is the only way to set the port
direction on the 16C5x.
-Andy
P.S. You can probably learn a lot by comparing the Parallax
"instructions" to the PIC opcodes to which Parallax's assembler
translates them. Last time I looked, Parallax included a translation
table in their PASM documentation.
> I'm taking something I wrote in Parallax 's assembler, and now just
> starting to learn the Micro chip MPASM , I find something a bit
> confusing.
>
> 1. Port direction control
> 2. port reading/writing.. ie: every interface to the world..
>
> The problem arises because Parallex in their manual states that
> this is the instruction.. MOV #literal !RA for example sets the
> direction for the register, but in the Micro chip version, they are
> using the TRST command that is, according to the Micor chip test,
> not to be supported in the future.
Brad:
You didn't say which PIC you're using, so I'll assume that it's one
of the 16C5x family.
Port Direction control on the '5x is accomplished via the TRIS
instruction, the syntax of which is "TRIS port", where "port" is
PORTA, PORTB, or (on the C55 or C57) PORTC.
TRIS uses the contents of the W-register to select the direction of
the port's I/O pins: 0's are outputs and 1's are inputs. Here's an
example:
MOVLW 00001111B ;Make RB4-7 outputs, RB0-3 inputs.
TRIS PORTB
To read the I/O pins, just use any instruction that reads from the
port's register. Here are two examples:
MOVF PORTB,W ;Copy the contents of PORTB to W.
BTFSS PORTB,3 ;Skip the next instruction if RB3 is
high.
To write to the I/O pins, use any instruction that writes to the
port's register. Here are another two examples:
MOVWF PORTB ;Copy the contents of the W-reg to
PORTB.
BSF PORTB,3 ;Pull RB3 high.
CLRF PORTB ;Pull all the PORTB outputs low.
Use of the TRIS command in 16Cxx (NOT 16C5x) parts is, as you
mentioned, discouraged by Microchip in their data books. However,
they have yet to produce a 16Cxx part that doesn't include the TRIS
instruction.
If you're using the 16C5x parts, on the other hand, you can ignore
the warning; the TRIS command is the only way to set the port
direction on the 16C5x.
-Andy
P.S. You can probably learn a lot by comparing the Parallax
"instructions" to the PIC opcodes to which Parallax's assembler
translates them. Last time I looked, Parallax included a translation
table in their PASM documentation.
Brad Mitchell wrote:
> The problem arises because Parallex in their manual states that
> this is the instruction.. MOV #literal !RA for example sets the
direction for
> the register, but in the Micro chip version, they are using the TRST
command
> that is, according to the Micor chip test, not to be supported in the
future.
>
> Help..
> Simple I/O/ guys/gals... an simple example program would be
> great. Anything on the web that is very explicit?
> I have all the ASM stuff from the microchip web page, and their
handbook etc.
It depends on which PICs you are using. For 16C5X parts the instruction
is TRIS <port address>. For these parts, the tristate (data direction)
registers are not directly addressable so there isno alternative to using
the TRIS instruction. The port addresses are: RA = 05h, RB = 06h and
RC = 07h (only the 16C57 has a port C). But the code is much more readable
if named constants are used instead of the actual port addresses. The
constants can be named in either an include file or at the beginning of the
source code.
Here's an example of setting up the I/O directions:
MOVLW B'0000' ;
TRIS RA ; set all port A pins to output
MOVLW B'11111111' ;
TRIS RB ; set all port B pins to input
MOVLW B'10101010' ;
TRIS RC ; set all port C pins to input and output
; alternately
As you can see, using binary radix to set I/O directions helps to make
the code more readable. 1's are inputs and 0's are outputs. This makes
things easy since ones and zeroes resemble I's and O's.
Here are some ways to read and write to the ports:
MOVLW B'1111' ;
MOVWF RA ; set all port A pins high
MOVF RB,W ; read port B to W register
BSF RC,2 ; set port C output pin 2 high
BCF RC,4 ; set port C output pin 4 low
BTFSS RC,1 ; if port C input pin 1 is high then skip
; next instruction
GOTO PIN1LO ;
BTFSC RC,3 ; if port C input pin 3 is low then skip
; next instruction
GOTO PIN3HI ;
For 16CXX parts ('6X, '7X, '8X) the tristate registers are directly
addressable. The TRIS instruction can be used in these parts, but
Microchip reccommends that this not be done for future compatibility.
Instead, just do a write directly to the appropriate tristate register.
Microchip's standard include files assign the constants TRISA, TRISB,
TRISC, TRISD and TRISE for these registers (not all 16CXX parts have
ports C, D, and E). These are all in register bank 1, so bit 5 (RP0)
of the STATUS register must be set in order to write them.
Here's an example of setting the port directions on a 16CXX part:
BSF STATUS,RP0 ; select register bank 1
MOVLW B'0000' ;
MOVWF TRISA ; set all port A pins to output
MOVLW B'11111111' ;
MOVWF TRISB ; set all port B pins to input
BCF STATUS,RP0 ; select register bank 0
Just having TRISA, TRISB, etc. set to bank 1 addresses (85h, 86h, etc.)
won't insure that the values are written to the right place. STATUS,RP0
must be set to write to any bank 1 register, and must be cleared before
writing to any bank 0 register.