如何使用 PIC 将端口设置为输入或输出?
How to set a port as input or output with PIC?
问题:无法理解如何将端口 A 和端口 B 设置为输入和输出
我正在使用一本书作为参考:第 19 页 - 简介(章节)- 书名:John Morton 第三版 - PIC 微控制器您的个人入门课程
根据我从书中了解到的内容,位编号从右到左,所以我应该读作端口 DCBA,这就是为什么:b'0010'
但是,第 18 页 上的这段内容确实令人困惑:
It moves the literal into the working register. Then the instruction
tris takes the number in the working register and uses it to select
which bits of the port are to act as inputs and which as outputs. A
binary 1 will correspond to an input and a 0 corresponds to an output.
再读一遍,我想知道每个端口是否有 4 位,我可以 select 其中有多少是输入,多少是输出?但是我以为一个端口只能输出或者输入..
拜托,有人能澄清一下吗?
__config _CP_OFF & _WDT_OFF & _XT_OSC
list P = 16F57;
include "C:\Program Files (x86)\Microchip\MPLABX\v3.40\mpasmx\p16f57.inc";
portA equ 05
portB equ 06
org 0 ; Starts at 0?
goto Start
Init
clrf portA ; Reset Port A and B States
clrf portB ;
movlw b'0010'; Set port B as output
tris portA;
movlw b'0010'; Set Port A as input
;0010 should mean -> ABCD port states?
tris portB;
retlw 0; return
Start
call Init;
Main
bsf portA,0;
goto Main;
END
一个单独的端口对应于其所有关联的引脚。例如,在 PIC16F57 上,您有引脚 RA0、RA1、RA2 和 RA3。这些引脚分别对应于 PORTA 位 0、1、2 和 3。所以,这就是实际发生的事情。
clrf portA
clrf portB
movlw b'0010' ;Set RA1 as input and RA0,RA2,RA3 as output
tris portA;
movlw b'0010' ;Set RB1 as input and RB0,RB2,RB3 as output
tris portB
需要注意的是,所有引脚在上电或复位时都被初始化为输入,而 PORTA 只是一个 4 位寄存器,而 PORTB 是 8 位。在这种情况下,最好显式声明该寄存器的所有位。
movlw b'00000010' ;Set RB1 as input all others as output.
tris portB
您必须确保阅读数据表以确定端口寄存器及其对应引脚的宽度。
TRISB=0xFF; //For PortB as a Input
TRISB=0x00; //For PortB as Output
问题:无法理解如何将端口 A 和端口 B 设置为输入和输出
我正在使用一本书作为参考:第 19 页 - 简介(章节)- 书名:John Morton 第三版 - PIC 微控制器您的个人入门课程
根据我从书中了解到的内容,位编号从右到左,所以我应该读作端口 DCBA,这就是为什么:b'0010'
但是,第 18 页 上的这段内容确实令人困惑:
It moves the literal into the working register. Then the instruction tris takes the number in the working register and uses it to select which bits of the port are to act as inputs and which as outputs. A binary 1 will correspond to an input and a 0 corresponds to an output.
再读一遍,我想知道每个端口是否有 4 位,我可以 select 其中有多少是输入,多少是输出?但是我以为一个端口只能输出或者输入..
拜托,有人能澄清一下吗?
__config _CP_OFF & _WDT_OFF & _XT_OSC
list P = 16F57;
include "C:\Program Files (x86)\Microchip\MPLABX\v3.40\mpasmx\p16f57.inc";
portA equ 05
portB equ 06
org 0 ; Starts at 0?
goto Start
Init
clrf portA ; Reset Port A and B States
clrf portB ;
movlw b'0010'; Set port B as output
tris portA;
movlw b'0010'; Set Port A as input
;0010 should mean -> ABCD port states?
tris portB;
retlw 0; return
Start
call Init;
Main
bsf portA,0;
goto Main;
END
一个单独的端口对应于其所有关联的引脚。例如,在 PIC16F57 上,您有引脚 RA0、RA1、RA2 和 RA3。这些引脚分别对应于 PORTA 位 0、1、2 和 3。所以,这就是实际发生的事情。
clrf portA
clrf portB
movlw b'0010' ;Set RA1 as input and RA0,RA2,RA3 as output
tris portA;
movlw b'0010' ;Set RB1 as input and RB0,RB2,RB3 as output
tris portB
需要注意的是,所有引脚在上电或复位时都被初始化为输入,而 PORTA 只是一个 4 位寄存器,而 PORTB 是 8 位。在这种情况下,最好显式声明该寄存器的所有位。
movlw b'00000010' ;Set RB1 as input all others as output.
tris portB
您必须确保阅读数据表以确定端口寄存器及其对应引脚的宽度。
TRISB=0xFF; //For PortB as a Input
TRISB=0x00; //For PortB as Output