delkk0
delkk0

Reputation: 45

Reading pic input on PIC12LF1552

I'm working on a project and I need to use the PIC12LF1552. The code I'm trying to run is very simple, consists on reading the input on RA5 and then setting the output on RA2 to light an LED.
The problem is that it seems that the PIC is not reading the input on RA5. If I program the PIC to blink the LED without reading any input, it works correctly.
The program used to compile is MPLAB X 2.05, and the programmer being used is Pickit3.
The code that I'm using is this:

#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++);
}

Upvotes: 1

Views: 1275

Answers (1)

francis
francis

Reputation: 9817

According to the datasheet http://www.alldatasheet.com/datasheet-pdf/pdf/504825/MICROCHIP/PIC12LF1552.html, on page 93 about the ANSELA register :

"The ANSELA bits default to the Analog mode after Reset. To use any pins as digital general purpose or peripheral inputs, the corresponding ANSEL bits must be initialized to ‘0’ by user software."

If you don't plan to use analog inputs, you may add something like ANSELA=0;

for the moment, output works because : "The state of the ANSELA bits has no effect on digital output functions. A pin with TRIS clear and ANSEL set will still operate as a digital output,... "

Bye,

Upvotes: 0

Related Questions