Hello World - Blink LED on PIC16F18855
Check out this project's MPLAB Xpress Webpage to view the complete code example along with a detailed step by step write-up and description, or visit the PIC16F18855 Device Product Page for more information.
About this Code Example
In this project, an LED (Light Emitting Diode) is turned on by the PIC16F18855 microcontroller using the Xpress development board platform. This is a great starting point for getting your Xpress board running.
The PIC16F18855 GPIO pin RA0, connected to LED D2 on the Xpress board, is setup as an output to control the LED.
The project uses: PIC16F18855 Microcontroller
To follow along with these steps, MPLAB® Xpress should be open and the user should be logged in so that the MPLAB Code Configurator plug-in can be used. The setup is described in the steps of this training module. You should see a screen similar to the one shown here to start.
The MPLAB Xpress Development board should be connected to an available USB port on the host computer through a USB cable to the micro B connector on the board. Drivers should install successfully the first time the board is connected and may take a minute or so. The connection is shown in the picture. No other components are required.
Create a new project in MPLAB® Xpress for PIC16F188555 using the MPLAB Xpress Development Board. Instructions are below if this is your first project. Here is a video showing how to Create a Project in MPLAB® Xpress.
1. Launch the new project wizard
Launch the New Project Wizard using one of the following methods: From the Toolbar: Click on the New Project icon, or From the Menu: Select File ► New Project
The project wizard will walk you through the process and prompt you for all the required information. Your selections may be changed after the project has been created by modifying the project's properties.
2. Choose Project
a. Choose Microchip Embedded from the Categories column (center).
b. Choose Standalone Project from the Projects column (right).
c. Click the Next > button.
3. Select Device
a. Choose the device family from the Family drop-down box.
b. Choose your device's part number from the Device drop down box. You may also type in the part number directly, and the list will adjust to the characters you enter.
Or, start typing the part number in the device box using keypad and it will begin search for the matching part number; select the right one from the list
4. Select Project Name and Folder
Choose a Project Name. This will be your project's name that the IDE will create.
Note: The name cannot have any empty spaces!
Open the MPLAB® Code Configurator (MCC) using the MCC icon or under the Tools>Embedded>MPLAB Xpress Code Configurator menu of MPLAB Xpress.
A window will appear showing three steps. If you need the latest version of Java you can click on step one, otherwise click on the step two button to open the MPLAB® Xpress Code Configurator.
A file will automatically download in your browser similar to the picture shown. Click on that file to open it. This is a java application that will launch the MCC.
The process can take several seconds depending on your internet connection speed. Several windows may appear asking if you want to run the program. Also for if you are running the MCC tool for the first time it will ask you for a token which you will find in the pop up box in the Xpress IDE, you can copy and paste it to the token box. When the process is complete you will see a new screen appear, separate from the MPLAB Xpress IDE, that is the MCC control screen. In this screen you can select peripherals for your project, system setup for oscillator and other configuration settings, input and output selections for your device. When all of these are completed you can generate project code including a main.c file by clicking on the Generate button near the center top of the screen. All these generated files will be included in your MPLAB Xpress project.
There are three default Project Resources associated with any MPLAB® Xpress project as shown here. For this project we only need to setup the System Module and the Pin Manager. The Interrupt Manager will not be used so they can be left in their default state.
Let's start with the System Module settings. When the System Module selection is highlighted in the Project Resources area the System Module settings will appear in the center of the MCC screen.
For this project select the following:
a. Oscillator Select: HFINTOSC (1MHz)
b. HF Internal Clock: 4MHz
c. Clock Divider: 4
WDT Disabled, SWDTEN is ignored
In the Pin Manager section select the pin RA0 Output by clicking on the blue unlock button, turning it to a green locked symbol.
The Xpress Board has RA0 connected to the LED D2. By making RA0 an output, the pin will control the LED.
Under the Easy Setup tab of the Pin Module section, you will see the output box already properly checked for the RA0 pin. It also allows you to change the name of the pin from IO_RA0 to a custom name such as "led".
At this step also check the box "Start High". This will drive the RA0 output pin high and light the LED.
Click the Generate button in MCC to create the appropriate header and source files for this configuration.
Upon successful code generation the “Generation Completed” notification should appear. Select “OK” to close the window.
New MCC Generated header and source files should now be present in the Project window of the MPLAB Xpress IDE including a new main.c source file
Click on the main.c source file in the Project pane to open the file and scroll through the code to locate the //Add your application code comment inside of the while(1) loop.
This is highlighted to show you where you can modify the code to create a custom application. Fortunately, in this example we don't need to add any code to control the LED because the MCC already set the pin high in the Pin Manager setup of Step 5. We had it initialize the RA0 pin to** "Start High"**. This will keep it HIGH to light the LED. The initialization code generated by the MCC is in the pin_manager.c file.
Compile and download the project .hex file by clicking on the Make and Program Device button at the top of the MPLAB Xpress IDE.
Program the MPLAB Xpress board by dragging the project .hex file from the downloads section of the browser and dropping the file on to the Xpress drive.
The Programmer LED on the Xpress board should quickly flash from green to red and then back to green indicating that the .hex file was successfully programmed to the PIC16F18855.
The D2 LED will now be lit on the Xpress board.
The example shows how to control an I/O pin as an output to drive an LED on. The code shows how the MCC can generate initialization code so no additional code was required within the main.c file. Additional control of the LED can be performed by adding code to main.c under the while(1) loop. There are many I/O control macros established in the pin_manager.h file to make it easier to create that code.
This project can be the basis for building any project that requires an I/O pin to control a digital output signal. This can include various circuits such as transistor controlled relay or LED displays. This will form the basis of just about any microcontroller based project.
- Dec 4, 2018