Programming microcontrollers in Windows, Linux and OS X using MPLAB-X

The fact that Microchip is building MPLAB X External link, a platform independent programming environment for microcontrollers, is no longer a secret. The final version is not available yet, but the current beta version (Beta 7.12) is mature enough for daily use. In this blog post, we show how to use MPLAB X in combination with the Dwengo library and the Dwengo board. We will show you how to install MPLAB X and how to build your first project.

Requirements

  1. One Dwengo board
  2. One Dwengo programmer
  3. Enclosed USB cable
  4. Internet connection

Installation of the software

Navigate to the MPLAB X installation page External link and check MPLAB X IDE Beta v7.12 (or higher) and MPLAB C18 Lite Compiler for PIC18 MCUs. Also select the correct operating system (windows, linux of mac os x), then click Download Now and install the software (the C18 compiler and the MPLAB X IDE) by following the instructions from Microchip.

The Dwengo library

In order to use the functionality available in the Dwengo library you need to install it by downloading and extracting the following zip file. Next, copy the lib file to the microchip/mplabc18/v3.40/lib folder and the h files to the microchip/mplabc18/v3.40/h folder. Note that the exact folder name may differ depending on the version and the operating system you use.

Your first project

Once the software is installed, we can start building our first project. Start MPLAB X and choose New Project in the File menu. A wizard will start, in which you have to take the following steps:

Choose the project type

MPLAB X project wizard


Select the device: PIC18F4550

PIC kiezen


Select the programmer: PicKit 2

PicKit 2


Select the compiler: C18

C18


Choose a project name

projectnaam

At this point you created a project, so you can start writing the code, but first you need to create a C file. You do this by right clicking Source Files in the right hand column and then choosing New > Empty File from the drop down menu. Now select to create a C file and the choose a name, e.g. helloMPLABX.c:

code

Blinking LEDs

You can now write the code in the C file you just made. If you want to make the LEDs of the Dwengo board blink, you can use the following code example. More code examples can be found in our tutorials.

  1. #include <dwengoBoard.h>
  2. #include <dwengoConfig.h>
  3. #include <dwengoDelay.h>
  4.  
  5. void main(void) {
  6. initBoard();
  7. while(TRUE) {
  8. LEDS = 255;
  9. delay_ms(250);
  10. LEDS = 0;
  11. delay_ms(250);
  12. }
  13. }

A new handy feature of MPLAB X compared to the old MPLAB is automatic code completion. For instance, when you try to include a header file you automatically get a list of all possible header files:

code-aanvulling

To complete the project, you only need to add the Dwengo library, dwengo.lib, to the project. You do this by right clicking Library Files in the right hand column and then adding dwengo.lib to your project using the Add Existing Item option in the drop down menu.

Dwengo lib

Testing

Now that the code is finished we can start testing it. In order to do this you first need to connect the Dwengo programmer to your computer using a USB cable. Also connect the programmer to the Dwengo board. Next, select Run Project in the Run menu. If all went well, MPLAB X will now compile your code and program the Dwengo board.

testen

Congratulations, you finished your first project in MPLAB X!

Problems with MPLAB X

At this point, MPLAB X is still in Beta, so it is possible that some problems occur while using it. When you are experiencing problems it's best take a look at the MPLAB X information page External link and the MPLAB X forum External link.

Problems with PicKit 2 in OS X

The support for PicKit 2 programmers in MPLAB X Beta is not optimal yet when using OS X Leopard. One solution for this is to use the Microchip PicKit 2 command line tool. Using this tool you can program the Dwengo board from the terminal using the following command after you compiled the code using MPLAB X: ./pk2cmd -PPIC18F4550 -F/path/to/your/hex/file/filename.hex -M

Syndicate content