【问题标题】:Why this program work in PORTB but Doesn't work in PORTA (MPLAB XC8)?为什么这个程序可以在 PORTB 中运行,但在 PORTA (MPLAB XC8) 中却不能运行?
【发布时间】:2016-03-21 06:22:08
【问题描述】:

这是一个在 XC8 (Microchip) 中打开/关闭 LED 的简单程序:

1) 此代码有效:

#include <xc.h> 

#define _XTAL_FREQ 4000000 

#pragma config FOSC = HS  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Disable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

 void main()
{
     TRISB0=1;
     TRISB4=0;

    if (RB0==1)
    {
        RB4 = 1;
    }
    else
    {
        RB4 = 0;
    }

} 

Switch Connected to portb RB0

2) 此代码不起作用:

#include <xc.h> // Librería XC8

#define _XTAL_FREQ 4000000 // Indicamos a que frecuencia de reloj esta funcionando el micro

// PIC16F648A Configuration Bit Settings
#pragma config FOSC = HS  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

 void main()
{
     TRISA0=1;
     TRISB4=0;

    if (RA0==1)
    {
        RB4 = 1;
    }
    else
    {
        RB4 = 0;
    }

}  

Switch Connected to port RA0

如果我得到端口 B 而不是端口 A,为什么我可以在 led 上?

最好的问候。

【问题讨论】:

  • 根据具体的 PIC 设备,PORTA 上通常有模拟输入,需要禁用模拟输入才能将引脚用作数字。因此,您需要ANSEL = 0; 之类的内容,寄存器的确切名称将取决于 PIC 设备。
  • 非常感谢兄弟。我只是添加 ANS0=0(来自 xc8 中的库)并且...它可以工作。

标签: pic mplab xc8


【解决方案1】:

应正确设置 ANSEL,否则 ADC 可能会覆盖它并使其成为上述注释的输入。

但是要设置 PORT 引脚输出,您通常 使用 LATCH 寄存器。 RB4 是您在引脚配置为数字输入时读取的内容。 LATB4 是输出锁存器。

LATB4 = X;

其中 X 为 1 或 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2015-11-10
    相关资源
    最近更新 更多