简体   繁体   English

PIC16F887 PORT无法与XC8 C编译器一起使用

[英]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. 我是很新,PIC编程,我试图用C(与Microchip的免费XC8编译MPLABX )做一个简单的“输入到输出”节目。
The problem I'm having is that the RA2, RA3 and RA5 input pins are just not working when programming in C. 我遇到的问题是,在C语言中进行编程时,RA2,RA3和RA5输入引脚不起作用。
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: 我尝试刻录到PIC上的程序如下:

#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. 如果将RA4设置为HIGH,我确实会在PORTB上获得输出,但是对于RA2,RA3或RA5则没有。 I believe this might be a problem with the configuration bits or something, but I'm not sure. 我相信这可能是配置位或其他问题,但是我不确定。

Hardware being used: 使用的硬件:

  • Microchip PIC16F887 微芯PIC16F887

  • FLEXiS Plus Board (sorry no english manual, but in page 8 and 9 there are board schematics) FLEXiS Plus板 (对不起,没有英文手册,但是在第8和9页中有板原理图)

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. 我认为问题在于这些特定的引脚也是ADC模块的模​​拟输入,您必须将它们配置为数字I / O才能使用它们。 Try setting ANSEL = 0x00 at the beginning of your program. 尝试在程序的开头设置ANSEL = 0x00

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. 您可以在数据手册中看到,POR上ANSEL的默认值为0xFF因此默认情况下所有模拟引脚都配置为模拟输入。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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