I am trying use one pin on PIC12F675 for button, another pin for LED. I wrote this code but button did not work. LED is still on. GPIO0 is connected to +LED, GPIO1 is connected to button, button is connected to ground. Please help. Thank you.
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#include <xc.h>
int main(void) {
ANSEL=0x00; // ANALOG SELECT REGISTER (digital)
CMCON=0x07; // COPMARATOR DISABLE
nGPPU=0; // GPIO pull-ups are enabled by individual port latch values
WPU1=1; // WEAK PULL-UP REGISTER
TRISIO0=0;
TRISIO1=1;
while(1) {
if(GPIO1)
GPIO0=0;
else
GPIO0=1;
}
return 0;
}
Edit 1: If I comment everything in while(1) except GPIO0=1, LED is on. If I comment everything in while(1) except GPIO0=0, LED is off. So probably hardware is OK. I think debounce is not necessary in this program. If in original program swap GPIO0=1; and GPIO0=0; then LED is off. PIC executes what is in else.
Edit 2: I now tried use pin5 for button and it works. What does it mean? Is my PIC damaged or there is a bug in code?
Edit 3: Now I add CMCON=0x07; but no change. Still did not working. If button is on GPIO5, it works fine. If button is on GPIO1, it do not works.