Sequel to our last tutorial Introduction to PIC microcontrollers, In this tutorial we are going to learn the methods of programing PIC microcontroller. If you still remembered, in the last tutorial, we learnt basically what a PIC microcontroller is and the hardware and software required in other to program a PIC microcontroller. So I will assume you have installed the MPLABX IDE, the XC8 Compilers and also bought the PICKit-3 Programmer.
In learning how to program a PIC microcontroller, we will first, program a PIC to blink an LED using the PIC16F84A microcontroller. We will learn the basics of configuration registers.
Since we programming PIC16F84A, the very first thing to do is to download the PIC16F84A microcontroller’s datasheet. This is so because is always good to read and understand how a microcontroller works before programming it. More so, you should download the XC8 Compiler manuals we will be referencing it as we progresses.
Create a New Project in MPLAB-X:
Step 1: Launch the MPLAB-X IDE that we installed in the previous session (see interface below).
Step 2: Click on Files -> New Project, or use the shortcut Ctrl+Shift+N. The following POP-UP will show. From there select “Standalone Project” and click “Next”.
Step 3: Now you have to select the Microcontroller you want to program. So enter PIC16F84A over the Select Device drop-down section and then Click on “Next”.
Step 4: Here you are going to select the hardware tool you want to use to send the program into the PIC16F84. This would be PicKit3 for our project. Select PicKit3 and click on “next”.
Step 5: Here you will select the compiler that you want to use to compile the program you want to write. Select the XC8 Compiler and click next.
Step 6: Here you have to name your project and select the location where the project will be saved. I have named this Project as Blink_LED_With_PIC16F84A and saved it in the default user folder. You can name yours anything and save it in your preferred location. Your project will be saved as a folder with the Extension .X, which can be directly launched by MAPLB-X. Click Finish.
Step 7: Your project has been created. The left most window will show the project name, click on the project name to view the directories inside it. In order to start programming we need to add a C Main file, inside our Source file directory. To do this Right click on the source file and select New -> C Main File, as shown in below.
Step 8: The following dialog box appears. Enter a name for the C-file. I have named mine Blink_LED_With_PIC16F84A again. But you can name it anything. Then click Finish.
Step 9: Once the C main File is created, the IDE will open it as shown below, so we can now start programming.
Configuration Registers:
Before you start program any Microcontroller you have to know about its configuration registers. And the place to get this information is the microcontroller’s datasheet. Without setting the configuration registers of a microcontroller correctly, your program may be correct yet the device may not still function as required.
Configuration registers are set using a configuration pragma in the following forms:
#pragma config setting = state|value
#pragma config register = value
where setting is a configuration setting descriptor, e.g., WDT, and state is a textual description of the desired state, e.g., OFF. Consider the following examples.
#pragma config WDT = 1 //turn on watchdog timer
These configurations may look a bit strange now but as we progresses you will see how easy it will become using the MPLAP X Configuration Bits window but for now, let’s continue with the configurations manually.
Oscillator Configuration
The PIC16F87XA can be operated in four different oscillator modes. The user can program two configuration bits (FOSC1 and FOSC0) to select one of these four modes:
- LP Low-Power Crystal
- XT Crystal/Resonator
- HS High-Speed Crystal/Resonator
- RC Resistor/Capacitor
So, for our project, we are going to use a 10Mhz oscillator so we will select HS using the following code:
#pragma config FOSC = 10 //Oscillator selection bit (10 = HS)
Watchdog Timer (WDT) Bit Configuration
The Watchdog Timer is a free running, on-chip RC oscillator which does not require any external components. This RC oscillator is separate from the RC oscillator of the OSC1/CLKI pin. That means that the WDT will run even if the clock on the OSC1/CLKI and OSC2/CLKO pins of the device has been stopped. During normal operation, a WDT time-out generates a device Reset (Watchdog Timer Reset). The TO bit in the Status register will be cleared upon a Watchdog Timer time-out. If the timer is not cleared in our software coding then the microcontroller will reset upon every WDT timer overflow. The WDT can be permanently disabled by clearing configuration bit. We are not using WDT in our program so let us disable it by the following:
#pragma config WDTE = 0
Power-up timer (PWRT) Bit Configuration
The Power-up Timer (PWRT) provides a fixed delay of 72 ms (nominal) on power-up only. This design keeps the device in RESET while the power supply stabilizes. The Power-up Timer operates on an internal RC oscillator. The chip is kept in Reset as long as the PWRT is active. The PWRT’s time delay allows VDD to rise to an acceptable level. A configuration bit is provided to enable or disable the PWRT. We don’t need the PWRT so we turn it off with the following code:
#pragma config PWRTE = 1 //Power-Up timer enable bit (1 = Disable)
Code Protection (CP) bits Configuration
If this bit is turned on, once the Microcontroller is programmed, no one can retrieve our program from the microcontroller memory. But for now let’s just turn it OFF using the code below:
#pragma config CP = 1 // Code Protection bit (1 = Disabled)
If you did everything right your configuration section will look like these:
//Configurations #pragma config FOSC = 10 //Oscillator selection bit (10 = H) #pragma config PWRTE = 1 //Power-Up timer enable bit (1 = Disabd) #pragma config WDTE = 0 //Watchdog Timer Enable (0 = Disabd) #pragma config CP = 1 // Code Protection bit (1=Disabled)
Still moving on…
Now we are going to write the instructions that will make the PIC16F84A blink an LED so before then let’s look at its pin diagram.
As shown above PIC16F84A has 2 basic input/output ports. They are usually denoted by PORT A (R A) and PORT B (RB). These ports are used for input/output interfacing. “PORT A” is a bi-directional I/O port with 5 bits wide (RA-0 to RA-4). ”PORT B” is also a bi-directional I/O port and has 8 bit wide (RB-0 to RB-7).
The direction of the port is controlled by using TRIS(X) registers (TRIS A used to set the direction of PORT-A, TRIS B used to set the direction for PORTB). Setting a TRIS(X) bit ‘1’ will set the corresponding PORT(X) bit as input. Clearing a TRIS(X) bit ‘0’ will set the corresponding PORT(X) bit as output.
For our project let’s make the pin RB1 of PORT B as output so that our LED can be connected to it. See the complete program code below:
#pragma config FOSC = 10 //Oscilator selction bit (10 = H) #pragma config PWRTE = 1 //Power-Up timer enable bit (1 = Disabd) #pragma config WDTE = 0 //Watchdog Timer Enable (0 = Disabd) #pragma config CP = 1 // Code Protection bit (1=Disabled) #include <xc.h> #define _XTAL_FREQ 10000000 //Specify the crystal Oscilator frequency void main(void) { TRISB = 0; //Instruct the Microcontroller that the PORT B pins are used as Output. PORTB = 0; //Make all output of PORT B LOW while(1){ //Infinite While loop RB1 = 1; //LED ON __delay_ms(500); //Wait RB1 = 0; //LED OFF __delay_ms(500); //Wait //Repeat. }
}
First we have specified the external Crystal frequency using #define _XTAL_FREQ 10000000. Then in void main() function, we instructed our microcontroller that we are going to use the PORT B pins as an output (using TRISB=0X00;) pin. Next we bring all output pins of PORT B low (this will make the Led to be OFF at first). Then finally an infinite while loop is used so that the LED blinking goes on forever. In order to blink an LED we have to simply turn it ON and OFF with some delay in between. Once coding is complete, build the Project using Run -> Build Main Project command. This should compile your program. If everything is fine (As it should be) an output Console at the bottom of the screen will show a BUILD SUCCESSFUL message, as shown in the picture below.
If your build process was successful, a HEX file should be generated in a directory inside your project folder. Because I saved my project in the default location of MPLAx, the hex file is located in this location: C:/Users/iZUCHUKWU/MPLABXProjects/Blink_LED_With_PIC16F84A.X/dist/default/production/Blink_LED_With_PIC16F84A.X.production.hex. Yours may be different if you saved your oroject in a different location. The easiest way to locate it is to find the diectory like this: YourProjectName.X/dist/default/production. The file will be right inside.
Now, let us quickly simulate our code to be sure it does what we want: to blink an LED using PIC16F84A.
Simulating with PICSim
PICSim is a free PIC and Arduino Simulation software. Follow the steps below to sumulate the your program.
Step 1: Run PICSim from your computer. The PICSim interface opens, similar to the screenshot below.
Step 2: From the Board menu, select Mclab1 as shown below:
Step 3: then from the Microcontroller Menu, select PIC16F84A While at it, you can as well select 10 from the Clock (MHZ) drop down box to set the oscillator frequency.
Step 4: Now, you need to load the hex file generated when you compiled your program. So am sure you remembered the location where it was saved because we will need it now. Click on File -> Load Hex, a Choose File dialogue box will appear. Use the dialogue box to locate and open the hex file.
Step 5: You will notice the LED at pin RB1 blinking.
Simulating with Proteus
Unfortunately, Proteus isn’t free software but if you have it installed, we will use it to create schematic for this project (though you can use any software for drawing schematic). I can’t explain the processes of drawing schematic in with Proteus in this tutorial but the circuit will look similar to the one below:
Step 1: To simulate the circuit in Proteus, first, double click on the PIC microcontroller symbol. Then select the compiled hex file by clicking on the “Program file” menu.
Step 2: Then click on the play symbol on the bottom left corner of the software.
Step 3: Now, the LED-RED will be blinking.
Now you can pat yourself on the back. In this section, we have been able to write a program that will make a PIC16F84 microcontroller blink an LED and also simulated the PIC circuit with the program to confirm that it’s working.
In our next class we will learn how to build the real circuit (not simulation) using a PIC16F84A microcontroller.