In this article, we look at the steps of developing a simple XC8-based project; we will go through the steps in creating a source file using the MPLAB X IDE, then compiling the file. Additionally, we will show you how the program is loaded to the target microcontroller.
The program will turn the LED on whenever the button is pressed [push-button switch S1 connected RB0 and LED connected to port pin RD0]
Step 1
Double click the icon on your PC to start MPLAB X IDE. I presume you have already installed the MPLAB X IDE on your computer.
Step 2
Move the right-hand cursor down and click on icon Create New – under Projects, to create a new project, alternatively, you can click on File, and then click on create new project from the drop down menu.
Select the default Category: Microchip Embedded then select Projects: Standalone Project
Step 3
Click Next. Select the target microcontroller. Family: Advanced 8-bit MCUs (PIC18), and Device: PIC18F8722.
Step 4
Click Next. Select simulator as the tool.
Step 5
Click Next. Select compiler XC8 as illustrated below.
Step 6
Click Next. Name your project. In this example, we have name it Project-LED.
Step 7
Click Finish to create the required project files.
Step 8
Right click source Files on the left-hand window and select New โ main.c. Name the new source files as newmain (with extension .c)
Step 9
Click Finish. You should get a template C file as illustrated in the following figure:
Step 10
Modify the file by inserting the code below. The program turns on the LED connected to port pin RD0 whenever push-button switch S1 (connected to port pin RB0) is pressed.
/*
* File: newmain.c
* Author: zamulinj
*
* Created on December 26, 2023, 6:15 PM
* This program turns on an LED when a push-button switch is pressed.
* The LED is connected to port pin RD0 and the switch is connected to port
* pin RB0 of the microcontroller.
*/
#include <xc.h>
// Configuration: EXT reset, watch dog OFF, HS Oscillator
#pragma config MCLRE = ON, WDT = OFF, OSC = HS
// Define switch and LED connections
#define S1 PORTBbits.RB0
#define LED PORTDbits.RD0
// Define clock frequency
#define _XTAL_FREQ 10000000
//Start of main program
void main(void) {
TRISBbits.TRISB0 = 1; // Configure RB0 as input
TRISDbits.TRISD0 = 0; // Configure PORTD as outputs
MEMCONbits.EBDIS = 1; // Enable PORTD I/O functions
for (;;) { // Do forever
if (S1 == 0)
LED = 1;
else
LED = 0;
}
return;
}
More Details on the Program Above Code:
The #include <xc.h> statement at the start of the program identifies the microcontroller in use and calls the appropriate header files to include the processor specific definitions at the beginning of the program.
The configuration statement #pragma config MCLRE =ON, WDT = OFF, OSC = HS defines the processor configuration. In this case, master clear (reset) is enabled, watch dog timer is turned off, and the external high-speed crystal is chosen as the clock source.
The statement #define S1 PORTBbits.RB0 defines symbol S1 as port pin RB0. Likewise, the statement #define LED PORTDbits.RD0 defines symbol LED as port pin RD0 of the microcontroller.
The microcontroller clock frequency is then defined as 10 MHz.
At the start of the main program, port pin RB0 is configured as an input port. In the same way, RD0 is configured as an output port.
PORTD I/O functions are enabled by setting MEMCON bit EBDIS. You can find more details on PIC18F8722 data sheet.
The program enters an infinite loop where switch S1 is checked. Whenever the switch is pressed, that is, when S1 becomes 0, the LED is turned ON.
Step 11
To compile the program, right click on the project, on the drop down menu, click Build. The program should compile successfully and a message โLoading completedโ should be displayed as illustrated below:
To load the program to the target microcontroller on a development board, instead of selecting a simulator in Step 4, we would select for example PICKit 4 as the tool (hardware tool). The PICKit 4 programmer is connected to a development board such as PICDEM HPC Explorer Board. You then click Make and Program Device on the project (right click on the project to access it) to have the program loaded to the target microcontroller on the development board. On the board, the LED can be connected to RD0 and S1 to RB0 on the target microcontroller. The LED should turn ON when S1 is pressed.
Also read: MPLAB X IDE and MPLAB XC8 Compiler
Related Resource: Beginning C for Microcontrollers: Making Electronics Dance with Softwareย
Leave a Reply