我正在做一个项目,我需要使用 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++);
}
最佳答案
根据数据表http://www.alldatasheet.com/datasheet-pdf/pdf/504825/MICROCHIP/PIC12LF1552.html ,关于 ANSELA
寄存器的第 93 页:
“ANSELA 位默认为模拟 复位后的模式。使用任何引脚作为 数字通用或外设 输入,相应的ANSEL位 必须由用户软件初始化为‘0’。”
如果您不打算使用模拟输入,您可以添加类似ANSELA=0;
目前,输出有效是因为:“ANSELA 位的状态对数字没有影响 输出函数。 TRIS 清除和 ANSEL 设置的引脚 仍将作为数字输出运行,... "
再见,
关于c - 读取 PIC12LF1552 上的图片输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165053/