我正在进行一个项目,我需要使用PIC12LF1552。我尝试运行的代码非常简单,包括读取RA5上的输入,然后设置RA2上的输出以点亮LED。 问题是PIC似乎没有读取RA5上的输入。如果我将PIC编程为闪烁LED而不读取任何输入,则它可以正常工作 用于编译的程序是MPLAB X 2.05,正在使用的程序员是Pickit3 我使用的代码是:
#include <xc.h>
#include "pic12lf1552.h"
#include <stdio.h>
#include <stdlib.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
#define LED PORTAbits.RA2
#define SWITCH PORTAbits.RA5
void MSDelay (unsigned int);
void main(void)
{
//Set up I/O pins
TRISAbits.TRISA2 = 0; //RA2 = LED output
TRISAbits.TRISA5 = 1; //RA5 = switch
ADCON1=0b00100;
// ADCON1 = ;
//int b;
// int i;
if(SWITCH == 0)
{
LED=1;
MSDelay(2000);
LED=0;
}
else
{
LED=0;
MSDelay(2000);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0; i<itime;i++);
for(j=0; j<165;j++);
}
答案 0 :(得分:0)
根据数据表http://www.alldatasheet.com/datasheet-pdf/pdf/504825/MICROCHIP/PIC12LF1552.html,第93页关于ANSELA
注册:
“ANSELA位默认为Analog 复位后的模式。使用任何引脚作为 数字通用或外围设备 输入,相应的ANSEL位 必须由用户软件初始化为“0”。“
如果您不打算使用模拟输入,可以添加ANSELA=0;
目前,输出有效,因为:“ANSELA位的状态对数字没有影响 输出功能。一个TRIS清零和ANSEL设置的引脚 仍将作为数字输出运行,...... “
再见,