0
\$\begingroup\$

I am attempting to make a project wherein I have some LED's on pins GP2 and GP4 of a Microchip PIC12F683 microcontroller flashing in a simple pattern. The problem is that I cannot write to GP4 correctly, or that it at the very least won't respond to me writing to it. As far as I can tell, there's nothing wrong with my configuration registers, and when I tested the pin with a multimeter, it reads at about 2.5 volts. When I probed the pin with an oscilloscope, it seems to have a sine wave floating on the top. This might be interference of some kind, but to be honest, I have no idea. I'm using MPLAB X and a PICKIT 3 to program the controller.

TL;DR: GP4 doesn't work and I have no idea why.

My code:

/* * File: newmain.c * Author: Malachi * * Created on May 3, 2018, 4:38 PM */

#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 8000000
// PIC12F683 Configuration Bit Settings
// 'C' source line config statements

// CONFIG
#pragma config FOSC = EXTRCCLK  // Oscillator Selection bits (EXTRC oscillator: External RC on RA5/OSC1/CLKIN, CLKOUT function on RA4/OSC2/CLKOUT pin)
#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       // MCLR Pin Function Select bit (MCLR pin function is GP3)
#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)
#pragma config BOREN = ON       // Brown Out Detect (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>


/*
 * 
 */

int main(void) {
    OSCCON = 0b01110001;
    TRISIO = 0b00001000; // all bits are 0, setting them as outputs (except for GP3)
    //NOTE: WHEN YOU CHANGE GP3, BE SURE TO CHECK THE MCLR BIT AT THE TOP IDIOT
    CMCON0 = 0b00000111;
    ADCON0 = 0b00000000;
    ANSEL = 0b00000000;
    GPIO = 0b00000000;

    while(1) {
        GP2 = 1;
        GP5 = 1;
        __delay_ms(500);
        GP2 = 0;
        GP5 = 0;
        __delay_ms(500);
    }
}

For context, while I'm not necessarily new to writing code, I'm by no means experienced.

\$\endgroup\$
  • \$\begingroup\$ What frequency sine wave? \$\endgroup\$ – DoxyLover Dec 27 '18 at 23:02
  • \$\begingroup\$ My multimeter said it was about 2MHz, but it could change based on what was connected to that pin. For instance, when I only had the scope on it, the wave was a higher frequency, but when the multimeter was connected as well, it visibly changed lower in frequency. If I touched the pin (via a jumper), the frequency could go low enough to even hear on a speaker. \$\endgroup\$ – malachik Dec 27 '18 at 23:08
  • 1
    \$\begingroup\$ Read the end of your #pragma config FOSC line... \$\endgroup\$ – brhans Dec 28 '18 at 0:50
2
\$\begingroup\$

This chip has so many functions multiplexed on that one pin, that you must work hard to configure it as GP4 digital output.
It appears that your config line #pragma config FOSC = EXTRCCLK forces that GP4 pin to be a clock output, and disables digital output function. As @brhans has suggested, look at the compilers config output to verify. In the diagram below, CLKOUT appears to be derived from CONFIG:
I/O configuration
You have properly disabled Analog Input mode for the Analog-to-digital and CMCON1 seems proper. It might be wise to add:

CMCON1 = 0b00000000;
\$\endgroup\$
  • 1
    \$\begingroup\$ I can't say how many hours I've spent trying to get IO pins working on PICs that were set up as analogue by default ... \$\endgroup\$ – dmb Dec 28 '18 at 21:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.