# PIC12F675 GP4 doesn't work

I'm using a PIC12F675 for a project, and everything works fine except one thing. GP4 does not work as digital IO. I've looked at the configs and the code a lot, but couldn't find anything.

# Config:

#pragma config FOSC = INTRCCLK
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config BOREN = ON
#pragma config CP = OFF
#pragma config CPD = OFF


# Code:

#include <xc.h>
#include <math.h>
#include "config.h"
#define _XTAL_FREQ 4000000

void delay(unsigned int freq){
for(int i = 0; i < (int)freq; i++){
__delay_ms(1);
}
}

void dClock(unsigned int freq){
GPIO1 = 1;
delay(freq);
GPIO1 = 0;
delay(freq);
}

ANSEL = 0x11;
CMCON = 0x7;
VRCON = 0;
}

while(GO_nDONE);
}

void main(void) {
TRISIO1 = 0; //output
TRISIO2 = 0; //indication
TRISIO3 = 1; //mode
TRISIO4 = 0; //halt
TRISIO5 = 1; //pulse_button

char pressed = 0;
GPIO1 = 0;

while(1){
if(GPIO4 == 0){
if(GPIO3 == 0){
GPIO2 = 1;
dClock(freq);
}
else{
GPIO2 = 0;
if(GPIO5 == 1 && pressed == 0){
GPIO1 = 1;
__delay_ms(50);
GPIO1 = 0;
pressed = 1;
}
else if(GPIO5 == 0 && pressed == 1){
pressed = 0;
}
}

}
}
return;
}

• Doesn't #pragma config FOSC = INTRCCLK tell the PIC to output it's clock on GP4? Isn't there a different FOSC option you should be using? – brhans Jul 11 at 21:59
• @brhans Yes, I noticed that now! INTRCCLK makes the microcontroller use its internal clock, and connects it to GP4. However, INTRCIO does the same thing except it configs GP4 to be used as IO. Thank you for making me notice! – Ali Abo Shady Jul 11 at 23:53

Since the microcontroller you are using has only 8 pins, they necessarily must share functionality to provide the various features that the device is capable of.

I just wanted to provide a sort of "roadmap" to help explain how the configuration works.

Check out the pin function diagram on the datasheet page 2:

You'll notice that physical pin 3 has at least five functions: GP4, AN3, !T1G, OSC2, and CLKOUT. Sometimes you have to specify in the configuration which function the pin should have. It's definitely not always clear. I find it helpful to search the datasheet for references to the register or pin function I'm having issues with.

Here's an excerpt from page 52 on which GP4 was found:

Microcontrollers very often can operate using an internal oscillator if they have one, or from an external oscillator such as a crystal. Even further, they can connect their internal oscillator to a pin for clocking other devices. The configuration register here has three bits (FOSC) that determine how this gets set up.

If you look at the two modes listed with values 101 and 100, they both specify to use the internal oscillator, but one of the options connects GP4 to the clock, where the other maintains its function as GPIO.

The defined constants INTRCCLK and INTRCIO Spehro mentioned should reflect these values.

• Thank you so much, it worked! I found your explanation very helpful and I understand now how it works. – Ali Abo Shady Jul 11 at 23:54

You have configured the PIC to output the internal clock signal on GP4 using

#pragma config FOSC = INTRCCLK


Replace INTRCCLK with INTRCIO.

• Thank you so much, it worked!! I understand now how it works. – Ali Abo Shady Jul 11 at 23:53
• You're welcome. A suggestion- it's often better to try to reproduce the problem in the simplest possible program. In this case a simple loop copying GP4 to 1 would have shown the problem and avoided all the distractions in your full program. – Spehro Pefhany Jul 12 at 8:43
• Alright, got it. I'll do that next time. Thanks again. – Ali Abo Shady Jul 12 at 10:46