简体   繁体   中英

PIC16F887 PORT won't work with XC8 C compiler

I'm pretty new to PIC programming and I'm trying to use C (compiled with Microchip's XC8 Free in MPLABX ) to make a simple "Input to Output" program.
The problem I'm having is that the RA2, RA3 and RA5 input pins are just not working when programming in C.
It's probably not a hardware problem, because when programming in Ladder those pins work just fine.
I've searched around on the internet for a while and couldn't find anyone with the same problem yet.

The program I'm trying to burn onto the PIC is as follows:

#define _XTAL_FREQ 20000000

#include <xc.h>

// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//END CONFIG

int main()
{
  TRISB = 0x00;
  TRISE = 0x00;
  TRISC = 0x00;
  TRISD = 0xFF;
  TRISA = \
          _TRISA_TRISA2_MASK |\
          _TRISA_TRISA3_MASK |\
          _TRISA_TRISA4_MASK |\
          _TRISA_TRISA5_MASK;
  PORTD = 0x00;
  PORTA = 0x00;
  PORTB = 0x00;
  PORTE = 0x00;
  PORTC = 0x00;

  while(1){
    PORTB = PORTA;
  }
  return 0;
}

I do get an output on PORTB if I set RA4 to HIGH, but not for RA2, RA3 nor RA5. I believe this might be a problem with the configuration bits or something, but I'm not sure.

Hardware being used:

  • Microchip PIC16F887

  • FLEXiS Plus Board (sorry no english manual, but in page 8 and 9 there are board schematics)

Software being used:

I think the problem is that those specific pins are also Analog Inputs for the ADC Module, you have to configure them to be digital I/O to use them. Try setting ANSEL = 0x00 at the beginning of your program.

You can see in the datasheet that the default value of ANSEL on POR is 0xFF so all analog pins are configured as analog inputs by default.

要将引脚用作模拟输入,应将ANSEL和TRISx寄存器中的相关位置1。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM