Share | Tweet |
---|
In this project you will learn how to blink an led connected to the GPIO pins of PIC16F877A microcontroller using MPLAB IDE and PICKIT programmer. If you don't have access to your own hardware for this project, you can schedule this project on our company hardware and execute it remotely. If you don't have your own hardware for this project, you can schedule this project on our company hardware and execute it remotely. This project is also part of the systems software development online course that we provide and listed here. You can complete this project, get hands-on experience for software jobs and earn course completion certificate.
The required hardware and software for this project is as below:
A light-emitting diode (LED) is a two-lead semiconductor light source. It is a p–n junction diode that emits light when activated. When a suitable current is applied to the leads, electrons are able to recombine with electron holes within the device, releasing energy in the form of photons. Light-emitting diodes are used in applications as diverse as aviation lighting, automotive headlamps, advertising, general lighting, traffic signals, camera flashes, lighted wallpaper and medical devices.
I am using our company ETK01001 embedded trainer kit board for this project. You can use any PIC development board for this project. You can also schedule this project on our company hardware and execute the project remotely from anywhere.
The schematic diagram for this project is as shown below:
PIC16F877A is a 8-bit microcontroller. Download the datasheet of the microcontroller here. It has total 40 pins out of which 33 pins are used for GPIO. PIC16F877A has 5 ports namely PORTA, PORTB, PORTC, PORTD, PORTE. The GPIO pins are used interfacing different input/output devices to the PIC microcontroller.
PORTA - PORTE registers in PIC16F877A microcontroller have alternate functions which means when a pin in the register is used for alternate function it cannot be used for general purpose input/output functions. This is determined by the internal register settings configured by the user program. So to use as a led control pin we need to configure the pin connected to the led as GPIO output.
Register | Register Description |
---|---|
TRISX | Determines whether the PORT or pin is used for input or output. If TRISX bit=1, the corresponding port pin is set as input. If TRISX bit=0, the corresponding port pin is set as output. |
PORTX | The actual data that will be written on the PORT or pin. |
The PORTA - PORTE registers, their pins and alternate functions are defined in the following table.
Register | Pins | Alternate functions |
---|---|---|
PORTA | PA0 - PA5 | Analog to Digital Converter(ADC). |
PORTB | PB0 - PB7 | Interrupts. |
PORTC | PC0 - PC7 | PWM, UART, I2C. |
PORTD | PD0 - PD7 | Parallel slave port. |
PORTE | PE0 - PE2 | Analog to digital converter(ADC). |
Before starting on software development, let me introduce you some basic stuff on programming. To solve a problem you are creating a project/product. You need to develop source code for the project. To edit source code you need an editor or IDE. You need a compiler to compile your source code. We will use MPLAB X IDE for this project. I have downloaded and installed XC8 compiler which is detected by the MPLAB IDE and used to compile the source code. Follow the below steps to edit source code, compile source code, create binary for your project:
#include "xc.h"
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = ON // Data EEPROM Memory Code Protection bit (Data EEPROM code-protected)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 11059200 //11.0592MHZ
#define DELAY 100
void main()
{
TRISAbits.TRISA0 = 0;
while(1)
{
PORTAbits.RA0 = 1;
__delay_ms(DELAY);
PORTAbits.RA0 = 0;
__delay_ms(DELAY);
}
}
In the above program led.c we have configured RA0 pin as output by configuring TRISAbits.TRISA0 to 0. Led is an output device. As RA0 pin is configured as output we can connect an led to the RA0 pin. Then in an infinite while loop PORTA RA0 pin is set to 1 or 0 by configuring PORTAbits.RA0 to 1 or 0. A delay of 100ms is provided when led state changes between on and off.
When you install MPLAB X on your computer, you will get two shortcuts like MPLAB IDE and MPLAB IPE on your desktop. We used MPLAB IDE for editing source code, compiling the source code and creating binaries. Now we will use MPLAB IPE to load the binary on the Microcontroller. Following steps will help you to load binary on the microcontroller.
This is captured in the following snapshot
Once you load the binary in the microcontroller, you should observe that the led connected at the RA0 pin should blink with a delay of 100ms.
Share | Tweet |
---|