Watch the Video Tutorial

The aim of this course is to teach you how to develop microcontroller based electronic systems using MikroC Pro for PIC Compiler.
MikroC Pro for PIC is a powerful, feature rich compiler fro PIC microcontrollers from Mikroelekronika. It is easy to learn and easy to use with a highly advanced integrated development environment (IDE), ANSI compliant compiler, broad set of easy to use hardware and software libraries, comprehensive documentation and plenty of ready to run examples.

MikroC Pro for PIC supports 8 bits PIC microcontroller devices from Microchip® like the PIC12, PIC16 and PIC18 microcontroller series. A fully functional demo version limited to 2Kb of output code size can be download for free fromMikroelekronika website.

For dsPIC30/33 and PIC24 devices from Microchip® (16 bits), MikroC Pro for dsPIC is used and for PIC32 devices from Microchip®, mikroC Pro for PIC32 is used. There is also mikroC Pro for AVR for AVR devices from Atmel®, mikroC Pro for 8051 for 8051 devices from Atmel® and Sillicon Labs® and mikroC Pro for ARM for ARM® Cortex-M0™, Cortex-M3™ and Cortex-M4™ devices.

 Starting a new Project

Let us create a simple project called “Introduction” to switch on and off an LED connected to PORTB.0 with 1 sec interval.

Assuming you have installed the latest version of mikroC Pro for PIC, you can start a new project by following these few steps below illustrated with screenshots. We are going to use PIC18F45k22 but it should be the same for the rest of microcontrollers.

Start mikroC Pro for PIC either form a shortcut on the desktop or from all programs. The easiest way to start a project, is by using New Project Wizard.
Go to File menu and click on New Project to start a new project wizard.

Or you can click on New Project Icon on the tool bar to start the New Project Wizard.

Select Standalone Project and click Next.

A New Project Wizard dialogue box will open. Click Next to start the wizard.

Step 1: Give your project a meaningful name in the project name textbox. In our case, we named: “Introduction”

Specify the location where your project will be saved by clicking on the browse button.

Select the PIC you are going to use from the Device Name drop-down list. In our case, we selected the PIC18F45K22 and lastly enter the oscillator frequency value in the Device Clock textbox. For our example, an 8MHz clock is going to be used. Click Next.

Step 2: Add project files to the project if they are available at this point. You can always add project files later using Project Manager. For now, we are not going to add any file. Click Next.

Step 3: Select “Include All” which is also the default to include all the libraries. For advanced users, you can also include none, so that you can select yourself the libraries you are going to use later on. Click Next.

Step 4: You can also tick the “Open Edit Project window to set Configuration bits” Checkbox to open the Edit Project box. If you don’t want to edit your configuration bits at this stage or if you want to use the default values, you can leave this checkbox un-ticked. Click Finish to end the wizard.

The Edit Project box allows you to set your configuration bits: Here you can choose which type of oscillator you are going to use: XT Oscillator, External RC Oscillator, Internal Oscillator block…

In our example, we selected “HS Oscillator” because we are going to use an external 8MHz crystal clock oscillator.

Here you can also choose, whether you are going to enable the MCLR pin or not, Watch Dog Timer, Brown-out and so on. The key here is to use only what you are going to need and disable the rest. Click OK when you are done.

This is all and now you can write your first code.

In this example, we are going to write a simple program to switch on and off an LED connected to PORTB.0 at an interval of 1 second:

Clock Settings  

The oscillator settings depend on frequency of the clock, the type of the clock source (crystal, RC…) and whether an internal or external clock is to be used.
Common oscillator settings are XT, HS, EC, RC and INT.
The XT and HS settings are for use with external crystal oscillators, XT is used for crystals 4 MHz and below, HS for 4 MHz and above. EC is for use with an external TTL/CMOS clock source. RC oscillator mode is for using an external RC resonator (usually 8 MHz and below) and INT specifies the processors internal oscillator if equipped.

This figure above shows how a crystal can be used as a clock input.

Please always check the PIC datasheet to get the accurate values of C1 and C2 of the oscillator. Higher capacitance increases the oscillator stability but also increases the start-up time.
The table below gives the recommended values.

In step 4 of Start a New Project Wizard, you can tick the “Open Edit Project window to set Configuration bits” Checkbox to open the Edit Project box, here you can set the clock settings.

Resetting a Microcontroller

When a microcontroller is reset, it starts the execution of the program from the beginning of the program (address 0x0000). There are times when a reset is required, this can be done automatically (when a microcontroller is in an endless loop to avoid freezing, very low voltage to avoid unexpected operation…) or even manually when a user wants to force to microcontroller to restart its program.

These following operations can reset the microcontroller:
–> POR (Power On Reset)
–> MCLR reset
–> WDT (Watchdog timer) reset
–> BOR (Brown-out reset)
–> Reset instruction
–> Stack full reset
–> Stack underflow reset
Usually the commonly used reset types are the POR and external reset using the MCLR pin.
The Power-On Reset is generated automatically when power supply voltage is applied to the chip.
The MCLR pin should always be connected to the supply voltage directly or preferably through a 10-K resistor.
To manually reset the microcontroller, the MCLR can be grounded, normally with a push button.
If a manual reset is not needed, the MCLR can be disabled in software.
This figure below shows a typical reset circuit.

  Building The Project

Now click on Build icon on the toolbar or press CTRL + F9 to build the project. If there is no error, the project will build successfully and generate an HEX file that can be loaded into the PIC.

You can download the full project files (MikroC project) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download Introduction MikroC project