
In this project tutorial, we are going to make blink-led projects with a pic microcontroller. After you read this tutorial you can build a PIC microcontroller project with XC8 on MPLAB IDE. We are going to make 3 different blinking projects. These projects are basic. Just learn how to make a basic project with a PIC microcontroller. You can see below which projects we are going to make;
- Blinking LED with 1s delay,
- LED with button,
- Blinking LED with a button.
Before we start to learn, think about the purpose of this tutorial. After you learn these projects you can make PICs pins as output and input. So you can control LEDs, relays, drivers, LCDs, 7-segment displays, and so on. You can get data from, buttons, digital sensors, touch buttons, microcontrollers, and so on. You can see that when you learn to blink you will learn principles of control and get data from these devices. After that let’s get started.
Creating the MPLAB IDE Project
Firstly, you need to install MPLAM IDE and XC8 Compiler to your computer and buy PICkit3 or PICkit4 (you can program the PIC microcontroller with other tools it is your choice) if you don’t have it. After that follow the list below;
- Open MPLAB IDE and click
File->New Project
(shortcut is Ctrl+Shift+N) , - Select Standalone Project and click
Next >
, - Select
Mid-Range 8-bit MCUs (PIC10/12/16/MCP)
family,PIC16F628A
device or just write PIC16F628A in the device area and clickNext >
, - Select None and click
Next >
, - Select your tool (If you don’t have any tool, you can select Simulator and make a simulation on MPLAB IDE or you can make a Proteus simulation ) and click
Next >
, - Select XC8 compiler and click
Next >
, - Write your project name, select the project location and click Finish.

To write our code we need main.c file. To create it right-click to Source Files > New > main.c...
like the image above. After that write main to File Name and click the Finish button. On the left panel double-click Source files and double click main.c file to open it.

You are created your project. Now we must set configuration bits. Click Production on top of the menu and click Set Configuration Bits. The panel will appear in the below of window. We are going to use the internal oscillator of the PIC microcontroller. Set FOSC
to INTOSCCLK
and OFF
others and click generate like the image above. After the code is generated copy and paste all code to the top of the main.c file. Be sure that #include <xc.h>
code is not rewritten.

We are going to use this circuit (pictured above) for all of these projects. ICSP connector is used for uploading code to the microcontroller via PICkit3.
1. Blinking LED With 1s Delay
For blinking led; we need to make the pin as output and turn on and off this pin with making delay. In microcontrollers, we need a clock signal to run our operation. We can use external clocks or if the microcontroller has one, we can use internal clocks. PIC which we use has 4MHz
internal crystal. In configuration bits, we set FOSC it
as an internal oscillator. So we are going to use it. To use __delay_ms()
the function we must define _XTAL_FREQ 4000000
to say the frequency of the microcontroller. You can see how to make blink led with the below code.
Blink LED Code
/* * File: main.c * Author: radon * * Created on January 16, 2020, 2:14 PM */ // PIC16F628A Configuration Bit Settings // 'C' source line config statements // CONFIG #pragma config FOSC = INTOSCCLK // Oscillator Selection bits (INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN) #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 // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD) #pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled) #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming) #pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #define _XTAL_FREQ 4000000 #include <xc.h> void main(void) { // Use pins as digital CMCONbits.CM = 0b111; // Set RB0 as output TRISBbits.TRISB0 = 0; while(1) { // Set RB0 pin to 5V PORTBbits.RB0 = 1; // Wait 1 second __delay_ms(1000); // Set RB0 pin to 5V PORTBbits.RB0 = 0; // Wait 1 second __delay_ms(1000); } return; }
You can see how blinking led in the video below.
2. LED With Button
RA0 pin connected to a pull-down resistor. In this way, the pin has 0V input and after the button is pressed pin will change to 5V input. So we can check the RA0 pin to make the condition. If the RA0 pin is 0V then RB0 equals 0, else RB0 equals 1. You can see how we can do this in the code below. We just change the main function.
LED With Button Code
void main(void) { CMCONbits.CM = 0b111; TRISBbits.TRISB0 = 0; PORTBbits.RB0 = 0; // Set RA0 pin as input TRISAbits.TRISA0 = 1; while(1) { // Check RA0 pin if(PORTAbits.RA0 == 1) { PORTBbits.RB0 = 1; } else { PORTBbits.RB0 = 0; } } return; }
You can see in the video when we press the button the led is turned on and when we release led is turned off.
3. Blinking LED with a Button
Now we are going to make blink-led (n) times. For that, we define a new function called blink. This function will blink (n) times with (time) delay. For more information watch our YouTube video at the end of the post.
Blinking LED With a Button Code
#define T 1 void blink(int time, int num) { // Loop for blink num times for(int j = 0; j < num; j++) { PORTBbits.RB0 = 1; // Loop for delay for(int i = 0; i < time; i++) { __delay_ms(T); } // Loop for delay PORTBbits.RB0 = 0; for(int i = 0; i < time; i++) { __delay_ms(T); } } } void main(void) { CMCONbits.CM = 0b111; TRISBbits.TRISB0 = 0; PORTBbits.RB0 = 0; TRISAbits.TRISA0 = 1; while(1) { if(PORTAbits.RA0 == 1) { PORTBbits.RB0 = 1; // Blink LED 10 times with 100ms delay blink(100, 10); } else { PORTBbits.RB0 = 0; } } return; }
In this project tutorial, we learned how to make a project in PMLAB IDE, blink led, and check the input. This is the start of the PIC microcontroller journey. Be healthy and see you next time.
YouTube Video

Comments
Post a Comment