1
我正在開發一個項目,我需要使用PIC12LF1552。我試圖運行的代碼非常簡單,包括讀取RA5上的輸入,然後設置RA2上的輸出以點亮LED。
問題是,似乎PIC沒有讀取RA5上的輸入。如果我編程PIC使LED閃爍而不讀取任何輸入,它可以正常工作。
用於編譯的程序是MPLAB X 2.05,使用的程序員是Pickit3。
,我正在使用的代碼是這樣的:在PIC12LF1552上讀取PIC輸入
#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++);
}