Introduction to MikroC Pro for PIC Compiler

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.

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

voidmain(){

TRISB.B0=0;// set direction of PORTB.B0 to be output

do{

LATB.B0=0;// Turn OFF LED on PORTB.0

Delay_ms(1000);// 1 second delay

LATB.B0=1;// Turn ON LED on PORTB.0

Delay_ms(1000);// 1 second delay

}while(1);// Endless loop

}

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.

Stay Update:

Search Here

Translate Website into your Language

Please support our work with a Donation

Writing tutorials and recording tutorials videos requires a lot of resources and time. If you found these tutorials helpful, please show your support and donate any amount you wish so that we can do even more.
Thanks