为什么这个程序在PORTB中工作但在PORTA(MPLAB XC8)中不起作用?

时间:2016-03-21 06:22:08

标签: pic mplab xc8

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

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

为什么我可以使用led如果我得到端口B而不是端口A?

最诚挚的问候。

1 个答案:

答案 0 :(得分:0)

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

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

LATB4 = X;

其中X为1或0