Question

i am using a PICkit2 Low Pin Count Demo board with a 16f690 chip. i have been able to write simple code to turn the LEDs on and off, but upon trying to utilize the push button to change the state of the LEDs, have been unable to do so. My code is below, along with a link to the schematic for the device.

#include <htc.h>

__CONFIG(MCLRE_OFF &  BOREN_OFF & PWRTE_OFF & IESO_OFF & CP_OFF & FCMEN_ON);

void main()
{
    //initialization
    TRISAbits.RA3 = 1;      //make button (RA3) input
    TRISCbits.RC0 = 0;      //make LED (RC0) output

    for(;;)                 // loop forever
    {
        //set RC0 if RA3 is low (button pressed), else clear RC0
        if(PORTAbits.RA3)
        {
            RC0 = 1;
        }
        else
        {
            RC0 = 0;
        }
    }
}

Demo Board User’s Guide

Was it helpful?

Solution

RA3 is also /MCLR, Table 3 of the datasheet says that the pullup on this pin is only activated with external /MCLR configuration. If there is no pullup, you won't get changes. Does the pin read continously high or low?

I'd avoid using RA3, at least while debugging, and if you have to use it in release code, swap the pin for another one when debugging.

Another common problem with PORTA is that it is by default an analog input port. ANSEL bits are set, and need to be cleared to be used as a digital input, but RA3 is not mapped to an analogue input.

OTHER TIPS

I have been having the same problem and found the following.

In the programmer settings for the MPLAB IDE tick the "3-State on Release from Reset". I have also changed the included header file from "htc.h" to "pic.h", the button now works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top