This post provides the code to make an LED blink using PIC12F675 microcontroller. This code is written in C language using MPLAB with HI-TECH C compiler. This code is intended to be the first step in learning how to use PIC12F675 microcontroller in your projects. You can download this code from the 'Downloads' section at the bottom of this page.

Following figure shows the minimum circuit required to make an LED blink with PIC12F675.
Figure 1.   Circuit diagram for LED blinking using PIC12F675
In this figure, first thing to note is that no crystal oscillator is used with PIC12F675, because internal oscillator of 4MHz frequency is being used as the clock source here. GP3 is usually the master reset pin for PIC12F675 microcontroller. But to increase total number of pins available for other purposes, here I am not using GP3 pin as reset pin. So, to make this PIC12F675 microcontroller work, you only need to provide power on the pin 1 and pin 8 of this microcontroller.

Code

The code for making LED blink using PIC12F675 is shown below.
Figure 2.   Code for making LED blink using PIC12F675
Whenever you are writing code for PIC microcontrollers, then you have to include "htc.h" file in the code. After including "htc.h" file, configuration bits are being set in the code shown above. To understand the details of how these configuration bits are being programmed, you can read this post.

After the configuration bits, LED pin is being defined as the GP0 pin. You can replace GP0 with any other pin name if you want. After LED pin definition, CPU frequency is being defined. You have to define _XTAL_FREQ macro, if you want to use built in delay functions like __delay_us() and __delay_ms(). Here CPU frequency is defined to be 4MHz, which is the internal oscillator frequency[1].

In the main function, firstly ADC and comparator is turned off to make GP0 and other pins digital IO pins[2]. Using TRISIO register[3], we can set the direction of any pin i-e if it is an input or output. Also, using GPIO register you can make any pin high or low.

LED pin is being toggled in the while loop after every half second. In this way you can easily make LED blink using PIC12F675 microcontroller.

You can leave your comments in the comment section below.

Notes and References

[1]  Because every PIC microcontroller has an architecture which executes an instruction in 4 CPU cycles, when CPU frequency is defined to be 4MHz, then actual speed of this PIC microcontroller will be 1 MIPS (Million of instructions per second). 
[2]  ADC and comparator are by default turned on. So, we have to turn them off if we want to use GP0 pin as digital IO pin.
[3]  For details please refer to PIC12F675 datasheet.

Downloads

LED blinking code using PIC12F675 was compiled in MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. To download code and Proteus simulation click here.

Further Reading Suggestions

34

View comments

Loading