9
\$\begingroup\$

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; } 
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ Commented Jul 11, 2019 at 21:59
  • \$\begingroup\$ @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! \$\endgroup\$ Commented Jul 11, 2019 at 23:53

2 Answers 2

12
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thank you so much, it worked! I found your explanation very helpful and I understand now how it works. \$\endgroup\$ Commented Jul 11, 2019 at 23:54
13
\$\begingroup\$

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

#pragma config FOSC = INTRCCLK 

Replace INTRCCLK with INTRCIO.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Thank you so much, it worked!! I understand now how it works. \$\endgroup\$ Commented Jul 11, 2019 at 23:53
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 12, 2019 at 8:43
  • 2
    \$\begingroup\$ Alright, got it. I'll do that next time. Thanks again. \$\endgroup\$ Commented Jul 12, 2019 at 10:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.