TP N°1 - Prise en Main de MiKroC Pro For PIC
TP N°1 - Prise en Main de MiKroC Pro For PIC
I want to express my thanks to you for being interested in our products and for having
confidence in MikroElektronika.
The primary aim of our company is to design and produce high quality electronic products
and to constantly improve the performance thereof in order to better suit your needs.
Nebojsa Matic
General Manager
The Microchip name, logo and products names are trademarks of Microchip Technology in the U.S.A and other countries.
Page 2
Table of Contents
1. Introduction to mikroC PRO for PIC® . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 04
2. Hardware Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .05
3. Creating a New Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 06
Step 1 - Project Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 07
Step 2 - Add files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
Step 3 - Include Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Step 4 - Finishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
Blank new project created . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
4. Code Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5. Building the Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
6. Changing Project Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
7. What’s next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Page 3
1. Introduction to mikroC PRO for PIC®
mikroC PRO for PIC® organizes applications 01
into projects consisting of a single project file
(file with the .mcppi extension) and one or
more source files (files with the .c extension). 06
The mikroC PRO for PIC® compiler allows you 02
to manage several projects at a time. Source
files can be compiled only if they are part of
the project.
07
03
A project file contains:
• Project name and optional description; 05
• Target device in use;
• Device clock;
• List of the project source files;
• Binary files (*.mcl); and
04
• Other files.
08
PIC18F45K22
code. X2
RA5 RB1
RB1
RB0 LD6 LD2
8MHz SEC RE0 RB0 LED LED
R7 R3
RE1 VCC VCC-MCU 4K7 4K7
RB6 RB2
RE2 GND
LED blinking is just turning ON and OFF LEDs that C22 C21 VCC-MCU VCC RD7
LD7 LD3
22pF 22pF GND RD6 LED
are connected to desired PORT pins. In order to see
LED R4
R8
RA7 RD5 4K7 4K7
RB7 RB3
RA6 RD4
the example in action, it is necessary to connect RC0 RC7
DIP 40
connect the LEDs to PORTB only.
Page 5
3. Creating a New Project
The process of creating a new project is very
simple. Select the New Project option from
the Project menu as shown below. The New
Project Wizard window appears. It can also
be opened by clicking the New Project icon
from the Project toolbar.
Page 6
Step 1 - Project Settings
First thing we have to do is to specify the
general project information. This is done
by selecting the target microcontroller, it’s
operating clock frequency, and of course
- naming our project. This is an important
step, because compiler will adjust the
internal settings based on this information.
Default configuration is already suggested
to us at the begining. We will not change
the microcontroller, and we will leave the
default PIC18F45K22 as the choice for
this project.
Figure 3-2: You can specify project name, path, device and clock in the first step
Page 7
Step 1 - Project Settings
If you do not want to use the suggested path
for storing your new project, you can change
the destination folder. In order to do that,
01
follow a simple procedure:
Figure 3-3: Change the destination folder using Browse For Folder dialog
Page 8
Step 1 - Project Settings
Once we have selected the destination
project folder, let’s do the rest of the project
settings:
01
Figure 3-4: Enter project name and change device clock speed if necessary
03 Click the OK button to proceed.
Page 9
Step 2 - Add files
This step allows you to include additional files
that you need in your project: some headers
or source files that you already wrote, and
that you might need in further development.
Since we are building a simple application, we
won’t be adding any files at this moment.
01 Click Next.
01
Page 10
Step 3 - Include Libraries
Following step allows you to quickly set
whether you want to include all libraries in
your project, or not. Even if all libraries are
included, they will not consume any memory
unless they are explicitely used from within
01
your code. The main advantage of including
all libraries is that you will have over 500
functions available for use in your code
right away, and visible from Code Assistant
[CTRL+Space]. We will leave this in default
configuration:
Figure 3-6: Include all libraries in the project, which is a default configuration.
Page 11
Step 4 - Finishing
After all configuration is done, final step
allows you to do just a bit more.
02 Click Finish.
Page 12
Blank new project created
New project is finally created. A new source
file called “LedBlinking.c” is created and it
contains the void main() function, which
will hold the program. You may notice that
project is configured according to the settings
done in the New Project Wizard.
Page 13
4. Code Example
Time has come to do some coding. First thing
we need to do is to initialize the PORTB to act
LedBlinking.c - source code
as digital output. TRISB register, associated
1 void main() {
with PORTB, is used to set whether each pin
2 // set PORTB to be digital output
acts as input or output.
3 TRISB = 0;
// set PORTB to be digital output 4
TRISB = 0;
5 // Turn OFF LEDs on PORTB
6 LATB = 0;
LATB register is used instead of PORTB for
digital output. We can now initialize it with
7
8 while(1) {
logic zeros on every pin:
9 // Toggle LEDs on PORTB
// Turn OFF LEDs on PORTB 10 LATB = ~LATB;
LATB = 0;
11
12 // Delay 1000 ms
Finally, in a while() loop we will toggle the
13 Delay_ms(1000);
PORTB value, and put a 1000 ms delay, so
14 }
the blinking is not too fast.
15 }
while(1) { 16
// Toggle LEDs on PORTB
LATB = ~LATB;
17
18
// Delay 1000 ms
Delay_ms(1000);
} Figure 4-1: Complete source code of the PORTB LED blinking
Page 14
Figure 4-2: This is how the code looks
written in compiler code editor window
Page 15
5. Building the Source
When we are done writing our first
LedBlinking code, we can now build
the project and create a .HEX file
which can be loaded into our target
microcontroller, so we can test the
program on real hardware. “Building”
includes compilation, linking and
optimization which are done automatically. Build your code by clicking
on the icon in the main toolbar, or simply go to Build menu and
click Build [CTRL+F9]. Message window will report the details of the
building process (Figure 5-2). Compiler automatically creates necessary
output files. LedBlinking.hex (Figure 5-1) is among them. Figure 5-1: Listing of project files after building is done
Figure 5-2: After the successful compilation and linking, the message window should look something like this
Page 16
6. Changing Project Settings
If you need to change the target microcontroller or clock speed, you don’t have to go through the new project wizard all over again. This can be
done quickly in the Edit Project window. You can open it using Project->Edit Project [CTRL+SHIFT+E] menu option.
02
Community
If you want to find answers to your questions on many interesting topics we invite you to visit
our forum at http://www.mikroe.com/forum and browse through more than 150 thousand
posts. You are likely to find just the right information for you.
On the other hand, if you want to download more free projects and libraries, or share your own
Figure 7-1: Project explorer window
code, please visit the Libstock website http://www.libstock.com. With user profiles, you can
enables you to easily access provided
get to know other programmers, and subscribe to receive notifications on their code. examples and load them quickly
Page 18
DISCLAIMER
All the products owned by MikroElektronika are protected by copyright law and international copyright treaty. Therefore, this manual is to be treated as any other
copyright material. No part of this manual, including product and software described herein, may be reproduced, stored in a retrieval system, translated or transmit-
ted in any form or by any means, without the prior written permission of MikroElektronika. The manual PDF edition can be printed for private or local use, but not for
distribution. Any modification of this manual is prohibited.
MikroElektronika provides this manual ‘as is’ without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties or conditions
of merchantability or fitness for a particular purpose.
MikroElektronika shall assume no responsibility or liability for any errors, omissions and inaccuracies that may appear in this manual. In no event shall MikroElektronika,
its directors, officers, employees or distributors be liable for any indirect, specific, incidental or consequential damages (including damages for loss of business profits
and business information, business interruption or any other pecuniary loss) arising out of the use of this manual or product, even if MikroElektronika has been advised
of the possibility of such damages. MikroElektronika reserves the right to change information contained in this manual at any time without prior notice, if necessary.
The products of MikroElektronika are not fault – tolerant nor designed, manufactured or intended for use or resale as on – line control equipment in hazardous
environments requiring fail – safe performance, such as in the operation of nuclear facilities, aircraft navigation or communication systems, air traffic control, di-
rect life support machines or weapons systems in which the failure of Software could lead directly to death, personal injury or severe physical or environmental
damage (‘High Risk Activities’). MikroElektronika and its suppliers specifically disclaim any expressed or implied warranty of fitness for High Risk Activities.
TRADEMARKS
The MikroElektronika name and logo, the MikroElektronika logo, mikroC™, mikroBasic™, mikroPascal™, mikroProg™, EasyPIC™ v7, EasyPIC PRO™ v7, BigPIC6™ and
LV18F™ v6 are trademarks of MikroElektronika. All other trademarks mentioned herein are property of their respective companies.
All other product and corporate names appearing in this manual may or may not be registered trademarks or copyrights of their respective companies, and are only used
for identification or explanation and to the owners’ benefit, with no intent to infringe.
Page 19
If you want to learn more about our products, please
visit our website at www.mikroe.com. If you are
experiencing some problems with any of our products or
just need additional information, please place your ticket
at www.mikroe.com/esupport If you have any questions,
comments or business proposals, do not hesitate to
contact us at office@mikroe.com
Designed by
MikroElektronika,
January 2012.