Getting started & your first Program with MPLABX XC8

MPLABX with XC8 – Getting started & your first Program: This article explains that how to install MPLAB X along with XC8 compiler and also demonstrates that how to write and execute to our first program for pic16f877a microcontroller.

Check this complete list of pic microcontroller tutorials for beginners.
MPLABX with XC8 - Getting started & your first Program

  • Google for MPLAB X download and visit that microchip website.
  • Click on downloads and then downloads the MPLAB X IDE
  • And then scroll down and selects the XC8 compiler for download
  • Save both the downloaded files in a single folder
  • Now click on NPLAB X windows installer to start the installation. MPLAB X IDE installation starts.
  • Click on Next and then accept the agreement
  • And then leave the installation to default directory. Set up installs MPLAB X IDE. If you leave this stick mark, you will be directed to the download link of XC8 compiler as we have already downloaded XC8, take this.
  • Then click on Finish
  • Now start the XC8 compiler installation
  • Click on Next and then accept the agreement. Leave the installation to the default directory
  • Under compiler setting, you can tick all the options
  • Then click on Next
  • Now the XC8 compiler has been installed. And finish it.

I am using a big development board for this demo.development board for pic 2

In this board the port pins are extended for easy connection using jumper wires at RB7. The LED is connected using a jumper wire. An external adapter of 12 volt one amps is used to power the board. The PIC16f877a c is to be placed on the ZIF socket so that pin1 matches the handle. In these types of boards always pin one is near the handle.programmer 3

Port BRB 7 is connected to an LED using jumper wire. Match the ground pins of board with that of connector and that should match that of the development board. And shoot that VPP of picket tube goes to MCLR, Vdd goes to +5 volt, DAT to PGD, and CLK to PGC and then ground to ground. Now connect the picket to 2 USB port of PC. Now the setup is ready for fusing hex file and testing.hardware 10

        PICKIT2                    DEMO BOARD

      Vpp                         MCLR

Vdd                         +5v

    DAT                          PGD

  CLK                         PGC

  • Now open the MPLAB IDE the ID has 4 panes: FILE pane, EDITOR pane, NAVIGATION pane, and then the TASK pane to start a new project.
  • Click on file and then new project
  • Then select the microchip embedded standalone project.
  • Click on Next
  • Now you have to select the device PIC16f877a. (You can straightaway type in the device or you can from the drop-down, you can select the IC16f877a)
  • Click on Now you have to select the tool used for programming. Here I am using the PICKIT2
  • Click on
  • On next screen, you have to select the compiler as XC8. If XC8 is not seen here, you recheck the installation again. Then click on
  • And then provide a project name as LED blink. You can browse and store it on a wanted location.
  • Click on Finish to see the project files pane
  • To write you C code, right click on source files
  • Then select new see main file.
  • Provide a name to the C file
  • And then select the extension .C from the drop-down
  • Now click on You can see some code generated on the editor pane. Now delete this code.

(First step is to set the configuration bits)

  • Click on Window then PIC memory views then Configuration Bits on the task pane. You get the configuration bits
  • First we have to set the Fosc selected as HS as an external high FREQ. crystal is used on demo board. You can leave all other bits off
  • Then click on generate source code to output button at the bottom
  • Now you get the pragma configuration codes
  • Select all this configuration code generated and then copy it, paste it on to the editor pin. The first line of the config code is # include exceed H. This preprocessor macro tells the compiler to include the header file <xc.h>. Now the compiler will understand chip specific words like trice port etc. On top of this Include statement, you have to define the crystal value. We are using 20Mega. This value should match the value on the demo board otherwise, you will get delay function errors.getting started code 5

The first function is their main function, the entire program resides within this main function.

  • Void keyword in front of the main indicates that no return data is expected. The parentheses ( () )after main is for argument’s. Our data to pass to the function in this case, no data to pass so we can leave it blank or write void.
  • Next open braces ( { ) and then click on Enter, MPLAB X will provide automatically closed brace ( } ). We need to access 2 registers inside microcontroller to control any I/O pin tries and port registers tries controls whether a pin access input or output port register controls whether a pin is at level high or low. That is on or off. Price be is assigned all zeroes indicating all ports be pins or output 0 is for output and 1 indicates input. Port B is then initialized as all zeroes that is all pins are at low level. Note that all statements end up with a semicolon ( ; )
  • Now we start the never ending loop, this never-ending loop is created using while statement. The condition is always true i.e1, so the loop is executed repeatedly.
  • Inside the loop, we said the LSB bit or B7 as high. This is achieved by writing bs000001 that is only the LSB bit dismayed high.
  • Next we provide a delay of one second that is written as thousand milliseconds.
  • Note the usage of double underscore before delay and single underscore after the delay then we make RB7 bit low and again provide a delay that is port be all pins are made low and then we provide a delay again. The whole function repeats forever thus blinking the LED at pin RB7.

 ( If you have followed all the syntax correctly, yu get billed successful at the task pane)

  • Now click on Run and build main project. Run main project to load the hex file onto the target I see as the PICKIT2 is connected to ICSP of target demo board. The case file is automatically loaded. Then LCD had beenRB7 blinks. You can c programming target and then the hex file is loaded and executed. You can see the LED at pin not be 7 is blinking. It is on for one second and again off for another second know that pins are made high. So only the LED had RB7 blinks.
  • Now we see how to program the same code using bit masking technique. Define a constant say LED mask. A binary value we need to toggle RB7 bit so assign LED mask as binary 000001. A constant led Mask is defined using #define preprocessor of value o0b00000001. This is used to set the last bit. Inside the while loop, we use the OR operator to set the bit.
  • Port B we have to are with the LED mask and again assign back to port B. This will set the RB7 bit. In short, we can write port B or equals LED mask. After the delay, we have to clear the bit.
  • To clear the bit, we use exclusive or operator, that is port B is exclusive on with LED mask and again as into port B. This can be written as port B exclusive or equals LED mask. This will clear the last bit so to set a bit, you use the OR operator to clear the bit. You use the exclusive OR operator.
  • Now build and Run  the project to see the LED blinking. By changing the bits of LED mask, we can control any other bit of the port. By build and run, you can see LED at RB5 blinks.You shift the jumper wire from RB7 to RB5, you can see the LED blinking. Just by setting the bits on the LED mask constant, we achieve this.

Leave a Comment