Simple example program for the PIC12F675 microcontroller

The PIC12F675 is a very inexpensive 8-bit microcontroller from Microchip that’s available in an 8-pin DIP package (i.e. it’s suitable for breadboard use). I’ve had a small tube of these on the shelf for a few years, but I’ve only just got around to trying one out. In this post, I present a simple example circuit and program, which flashes an LED on one of the GPIO pins. The example circuit includes connections to the PICkit2 USB programmer that I’m using to program the device. The PICkit2 also supplies power to the circuit.

First, here’s the video of the example program running on the PIC12F675.

Example Code

This is the full C code for the flashing LED example. I built it using Microchip’s XC8 compiler (the compiler command line is shown in the opening comments).

//
// PIC12F675 example: blink an LED on pin GP5
// Written by Ted Burke - 18-2-2017
//
// To compile:
//
//    xc8 --chip=12F675 main.c
//

#include <xc.h>

#pragma config FOSC=INTRCIO,WDTE=OFF,MCLRE=OFF,BOREN=OFF

void main(void)
{
    TRISIO = 0b11011111; // Make pin GP5 a digital output
    
    while(1)
    {
        GP5 = 1;         // Set pin GP5 high
        _delay(500000);  // 0.5 second delay
        GP5 = 0;         // Set pin GP5 low
        _delay(500000);  // 0.5 second delay
    }
}

Example Circuit

pic12f675_circuit

Here’s my breadboard circuit:

20170217_162941

This entry was posted in Uncategorized and tagged , , , , , , , , , , , . Bookmark the permalink.

7 Responses to Simple example program for the PIC12F675 microcontroller

  1. larry con says:

    que chucha es TRISIO = 0b11011111??? tanta cosa para decir que sea de salida??? ni siquiera explicas

  2. Thomas says:

    Great tutorial… A silly question. If I just add the following code to your program
    while(1)
    {
    GP5 = 1;
    GP1 = 0;
    __delay_ms(500);
    GP5 = 0;
    GP1 = 1;
    __delay_ms(500);
    }
    I can blink two LEDs in alternance…
    but if I use GP0 (as below)
    while(1)
    {
    GP0 = 1;
    GP1 = 0;
    __delay_ms(500);
    GP0 = 0;
    GP1 = 1;
    __delay_ms(500);
    }
    …no leds are blinking anymore. Why would that be??

    many thanks for any comments you may have

    Thomas

    • batchloaf says:

      Hi Thomas,

      Short answer: I don’t know. Seems strange that GP1 is working in the first case but not in the second!

      Possible reason: Do you have anything else connected to GP0 other than the LED? Is the USB programmer still connected to the PIC? If so, pin 6 is used as ICSPCLK. I would assume ICSPCLK is a digital output on the PICkit2 (or whatever programmer you’re using) which would be a problem if you start using pin 6 as a digital output too (i.e. GP0) because if they disagree about the logic level you effectively have a short circuit.

      That’s just off the top of my head. Sorry for the delay getting back to you. I can’t find the time to keep up with all the comments!

      Ted

  3. Shafeek Bin Ashraf says:

    Hi, Thank you for the tutorial.
    i am using k150 programmer instead of pickit2, there is a problem i am facing. when i uploaded the program the GP5 seems to turned ON instead of blinking with 5 second delay.
    then i edited the program to find the error.
    while(1)
    {
    GP1 = 1;
    // Set pin GP1 high
    _delay(5000); // 0.5 second delay
    GP1 = 0;
    // Set pin GP1 low
    _delay(5000); // 0.5 second delay
    }
    Still the GP5 seems to be on instead of the GP1 to be blinking.
    Please help i am a total beginner in programming pic mcu.

  4. Do it this way:

    void main(void) {
    TRISIO=0;
    while(1)
    {
    GPIO=0x2;
    __delay_ms(500);
    GPIO=0x1;
    __delay_ms(500);

    }
    }
    and they will alternate.

  5. Gabriel says:

    Hello great tutorial. I want to extrapolate for making an electronic dice, with this MCU, how do I generate random numbers in this pic12f675?

Leave a comment