Electronic – PIC12F675 GP4 doesn’t work

cembeddedpicprogramming

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

void InitADC(){
    ANSEL = 0x11;
    ADCON0 = 0b10000001;
    CMCON = 0x7;
    VRCON = 0;
}

unsigned int GetADCValue(){
    ADCON0 = 0b10000011;
    while(GO_nDONE);
    return (ADRESH << 8) + ADRESL;
}

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

    char pressed = 0;
    GPIO1 = 0;

    InitADC();

    while(1){
        if(GPIO4 == 0){
            if(GPIO3 == 0){
                GPIO2 = 1;
                unsigned int freq = GetADCValue();
                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;
}

Best Answer

Just to add to Spehro's correct answer:

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:

Datasheet pin diagram

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:

Datasheet FOSC specifics

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.