Copyright © mikroElektronika, 2012. All rights reserved.
TO OUR VALUED CUSTOMERS
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 AVR®, Atmega® names, logos and products names are trademarks of Atmel Inc. in the U.S.A and other countries.
Page 2
Table of Contents
1. Introduction to mikroC PRO for AVR® . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2. Hardware Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3. Creating a New Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1 - Project Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 2 - Add files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 3 - Include Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 4 - Finishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Blank new project created . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4. Code Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5. Building the Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6. Changing Project Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7. What’s next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
04
05
06
07
10
11
12
13
14
16
17
18
Page 3
1. Introduction to mikroC PRO for AVR
®
mikroC PRO for AVR®
organizes applica-
tions into projects consisting of a single
project file (file with the .mcpav
extension)
and one or more source files (files with the
.c extension). The mikroC PRO for AVR®
compiler allows you to manage several
projects at a time. Source files can be
compiled only if they are part of the project.
A project file contains:
• Project
name and optional description;
• Target
device in use;
• Device
clock;
• List of the project source files;
• Binary files (*.mcl); and
• Other files.
In this reference guide, we will create a
new project, write code, compile it and test
the results. The purpose of this project is
to make microcontroller PORTA LEDs blink,
which will be easy to test.
03
05
07
02
01
06
04
08
01
Main Toolbar
02
Code Explorer
03
Project Settings
Page 4
04
Messages
05
Code Editor
06
Image Preview
07
Project Manger
08
Library Manager
2. Hardware Connection
Let’s make a simple “Hello world” example for the
selected microcontroller. First thing embedded
programmers usually write is a simple
LED blinking
program. So, let’s do that in a few simple lines of C code.
LED blinking is just turning ON and OFF LEDs that are
connected to desired PORT pins. In order to see the
example in action, it is necessary to connect the target
microcontroller according to schematics shown on
Figure 2-1.
In the project we are about to write, we
will use only
PORTA,
so you should connect the LEDs
to PORTA only. Eight LEDs are more then enough for
demonstration.
PB0
PB1
PB2
PB3
PB4
PB5
PB6
PB7
RESET
VDD
GND
XTAL2
XTAL1
PD0
PD1
PD2
PD3
PD4
PD5
PD6
PA0
PA1
PA2
PA3
PA4
PA5
PA6
PA7
AREF
GND
AVDD
PC7
PC6
PC5
PC4
PC3
PC2
PC1
PC0
PD7
Figure 2-1:
Hardware connection schematics
VCC
LD4
PA0
PA1
PA2
PA3
PA4
PA5
PA6
PA7
PA4
LD5
PA5
LD6
PA6
LD7
PA7
LD0
PA0
LD1
PA1
LD2
PA2
LD3
PA3
R5
4K7
R6
4K7
R7
4K7
R8
4K7
R1
4K7
R2
4K7
R3
4K7
R4
4K7
C1
22pF
X1
8MHz
C2
22pF
Prior to creating a new project, it is necessary to do the following:
Step 1: Install the compiler
Install the mikroC PRO for AVR® compiler from the
Product DVD
or
download it from the MikroElektronika website:
http://www.mikroe.com/eng/products/view/228/mikroc-pro-for-avr/
Step 2: Start up the compiler
Double click on the compiler icon in the Start menu, or on your
desktop to Start up the mikroC PRO for AVR® compiler. The mikroC
PRO for AVR® IDE (Integrated Development Environment) will appear
on the screen. Now you are ready to start creating a new project.
Page 5
ATmega16