ABL electronic PIC12, PIC18, PIC16 User Manual

mikroC
Develop your applications quickly and easily with the world's most intuitive C compiler for PIC Microcontrollers (families PIC12, PIC16, and PIC18).
Highly sophisticated IDE provides the power you need with the simplicity of a Windows based point-and-click environment.
With useful implemented tools, many practical code examples, broad set of built-in routines, and a comprehensive Help, mikroC makes a fast and reliable tool, which can satisfy needs of experi­enced engineers and beginners alike.
C Compiler for Microchip PIC microcontrollers
mikroElektronika
Development tools - Books - Compilers
www.mikroelektronika.co.yu
Users
manual
Making it simple
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
ii
MikroElektronika: Development tools - Books - Compilers
page
DISCLAIMER:
mikroC and this manual are owned by mikroElektronika and are protected by copyright law and international copyright treaty. Therefore, you should treat this manual like any other copy­righted material (e.g., a book). The manual and the compiler may not be copied, partially or as a whole without the written consent from the mikroEelktronika. The PDF-edition of the manual can be printed for private or local use, but not for distribution. Modifying the manual or the compiler is strictly prohibited.
HIGH RISK ACTIVITIES
The mikroC compiler is not fault-tolerant and is not designed, manufactured or intended for use or resale as on-line control equipment in hazardous environments requiring fail-safe per­formance, such as in the operation of nuclear facilities, aircraft navigation or communication systems, air traffic control, direct life support machines, or weapons systems, in which the fail­ure of the Software could lead directly to death, personal injury, or severe physical or envi­ronmental damage ("High Risk Activities"). mikroElektronika and its suppliers specifically dis­claim any express or implied warranty of fitness for High Risk Activities.
LICENSE AGREEMENT:
By using the mikroC compiler, you agree to the terms of this agreement. Only one person may use licensed version of mikroC compiler at a time. Copyright © mikroElektronika 2003 - 2005.
This manual covers mikroC version 2.1 and the related topics. Newer versions may contain changes without prior notice.
COMPILER BUG REPORTS:
The compiler has been carefully tested and debugged. It is, however, not possible to guarantee a 100 % error free product. If you would like to report a bug, please contact us at the address office@mikroelektronika.co.yu. Please include next information in your bug report:
- Your operating system
- Version of mikroC
- Code sample
- Description of a bug
CONTACT US:
mikroElektronika Voice: + 381 (11) 30 66 377, + 381 (11) 30 66 378 Fax: + 381 (11) 30 66 379 Web: www.mikroelektronika.co.yu E-mail: office@mikroelektronika.co.yu
Reader’s note
PIC, PICmicro and MPLAB is a Registered trademark of Microchip company. Windows is a Registered trademark of Microsoft Corp. All other trade and/or services marks are the property of the respective owners.
MikroElektronika: Development tools - Books - Compilers

Table of Contents

CHAPTER 1 mikroC IDE
CHAPTER 2 Building Applications
CHAPTER 3 mikroC Reference
CHAPTER 4 mikroC Libraries
mikroC User’s manual
CHAPTER 1: mikroC IDE 1
Quick Overview 1 Code Editor 3 Code Explorer 6 Debugger 7 Error Window 11 Statistics 12 Integrated Tools 15 Keyboard Shortcuts 19
CHAPTER 2: Building Applications 21
Projects 22 Source Files 23
Search Paths 23 Managing Source Files 24
Compilation 26
Output Files 26
Assembly View 26
Error Messages 27
CHAPTER 3: mikroC Language Reference 29
PIC Specifics 30 mikroC Specifics 32
ANSI Standard Issues 32 Predefined Globals and Constants 33 Accessing Individual Bits 33 Interrupts 34
Linker Directives 35 Lexical Elements 36 Tokens 38 Constants 39
Integer Constants 39
Floating Point Constants 41
Character Constants 42
String Constants 44
Enumeration Constants 45
Pointer Constants 45
Constant Expressions 45
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
iv
MikroElektronika: Development tools - Books - Compilers
page
Keywords 46 Identifiers 47 Punctuators 48 Objects and Lvalues 52 Scope and Visibility 54 Name Spaces 56 Duration 57 Types 59 Fundamental Types 60
Arithmetic Types 60
Enumeration Types 62
Void Type 64 Derived Types 65
Arrays 65
Pointers 68
Pointer Arithmetic 70
Structures 74
Unions 79
Bit Fields 80 Types Conversions 82
Standard Conversions 82
Explicit Typecasting 84 Declarations 85
Linkage 87
Storage Classes 89
Type Qualifiers 91
Typedef Specifier 92
asm Declaration 93
Initialization 94 Functions 95
Function Declaration 95
Function Prototypes 96
Function Definition 97
Function Calls 98 Operators 100
Precedence and Associativity 100
Arithmetic Operators 102
Relational Operators 104
Bitwise Operators 105
Logical Operators 107
Conditional Operator ? : 109
Assignment Operators 110
sizeof Operator 112
MikroElektronika: Development tools - Books - Compilers
v
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
Expressions 113 Statements 115
Labeled Statements 115
Expression Statements 116
Selection Statements 116
Iteration Statements 119
Jump Statements 122
Compound Statements (Blocks) 124 Preprocessor 125
Preprocessor Directives 125
Macros 126
File Inclusion 130
Preprocessor Operators 131
Conditional Compilation 132
CHAPTER 4: mikroC Libraries 135
Built-in Routines 136 Library Routines 138
ADC Library 139
CAN Library 141
CANSPI Library 153
Compact Flash Library 162
EEPROM Library 172
Ethernet Library 174
Flash Memory Library 186
I2C Library 188
Keypad Library 193
LCD Library (4-bit interface) 197
LCD8 Library (8-bit interface) 203
Graphic LCD Library 208
Manchester Code Library 219
Multi Media Card Library 224
OneWire Library 233
PS/2 Library 237
PWM Library 240
RS-485 Library 243
Secure Digital Library 249
Software I2C Library 254
Software SPI Library 258
Software UART Library 260
Sound Library 264
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
vi
MikroElektronika: Development tools - Books - Compilers
page
SPI Library 266
USART Library 271
USB HID Library 275
Util Library 280
ANSI C Ctype Library 281
ANSI C Math Library 285
ANSI C Stdlib Library 291
ANSI C String Library 295
Conversions Library 299
Trigonometry Library 303
MikroElektronika: Development tools - Books - Compilers
vii
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
viii
MikroElektronika: Development tools - Books - Compilers
page
CHAPTER
MikroElektronika: Development tools - Books - Compilers
1
mikroC IDE
mikroC is a powerful, feature rich development tool for PICmicros. It is designed to provide the customer with the easiest possible solution for developing applica­tions for embedded systems, without compromising performance or control.
PIC and C fit together well: PIC is the most popular 8-bit chip in the world, used in a wide variety of applications, and C, prized for its efficiency, is the natural choice for developing embedded systems. mikroC provides a successful match featuring highly advanced IDE, ANSI compliant compiler, broad set of hardware libraries, comprehensive documentation, and plenty of ready-to-run examples.

QUICK OVERVIEW

mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
2
MikroElektronika: Development tools - Books - Compilers
page
mikroC allows you to quickly develop and deploy complex applications:
- Write your C source code using the highly advanced Code Editor
- Use the included mikroC libraries to dramatically speed up the development:
data acquisition, memory, displays, conversions, communications…
- Monitor your program structure, variables, and functions in the Code Explorer.
Generate commented, human-readable assembly, and standard HEX compatible with all programmers.
- Inspect program flow and debug executable logic with the integrated Debugger.
Get detailed reports and graphs on code statistics, assembly listing, calling tree…
- We have provided plenty of examples for you to expand, develop, and use as
building bricks in your projects.
Code Explorer
Error Window
Watch Window
Code Editor
Breakpoints
Window
Code Assistant
MikroElektronika: Development tools - Books - Compilers
3
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
The Code Editor is an advanced text editor fashioned to satisfy the needs of pro­fessionals. General code editing is same as working with any standard text-editor, including familiar Copy, Paste, and Undo actions, common for Windows environ­ment.
Advanced Editor features include:
- Adjustable Syntax Highlighting
- Code Assistant
- Parameter Assistant
- Code Templates (Auto Complete)
- Auto Correct for common typos
- Bookmarks and Goto Line
You can customize these options from the Editor Settings dialog. To access the settings, choose Tools > Options from the drop-down menu, or click the Tools icon.

CODE EDITOR

Tools Icon.
Code Assistant [CTRL+SPACE]
If you type a first few letter of a word and then press CTRL+SPACE, all the valid identifiers matching the letters you typed will be prompted in a floating panel (see the image). Now you can keep typing to narrow the choice, or you can select one from the list using the keyboard arrows and Enter.
Parameter Assistant [CTRL+SHIFT+SPACE]
The Parameter Assistant will be automatically invoked when you open a parenthe­sis "(" or press CTRL+SHIFT+SPACE. If name of a valid function precedes the parenthesis, then the expected parameters will be prompted in a floating panel. As you type the actual parameter, the next expected parameter will become bold.
Code Template [CTR+J]
You can insert the Code Template by typing the name of the template (for instance, whileb), then press CTRL+J, and the Code Editor will automatically generate the code. Or you can click a button from the Code toolbar and select a template from the list.
You can add your own templates to the list. Just select Tools > Options from the drop-down menu, or click the Tools Icon from Settings Toolbar, and then select the Auto Complete Tab. Here you can enter the appropriate keyword, description, and code of your template.
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
4
MikroElektronika: Development tools - Books - Compilers
page
Auto Correct
The Auto Correct feature corrects common typing mistakes. To access the list of recognized typos, select Tools > Options from the drop-down menu, or click the Tools Icon, and then select the Auto Correct Tab. You can also add your own pref­erences to the list.
Comment/Uncomment
The Code Editor allows you to comment or uncomment selected block of code by a simple click of a mouse, using the Comment/Uncomment icons from the Code Toolbar.
Bookmarks
Bookmarks make navigation through large code easier.
CTRL+<number> : Go to a bookmark CTRL+SHIFT+<number> : Set a bookmark
Goto Line
Goto Line option makes navigation through large code easier. Select Search > Goto Line from the drop-down menu, or use the shortcut CTRL+G.
MikroElektronika: Development tools - Books - Compilers
5
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
Comment /
Uncomment Icon.
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
6
MikroElektronika: Development tools - Books - Compilers
page
The Code Explorer is placed to the left of the main window by default, and gives a clear view of every declared item in the source code. You can jump to a declara­tion of any item by clicking it, or by clicking the Find Declaration icon. To expand or collapse treeview in Code Explorer, use the Collapse/Expand All icon.
Also, two more tabs are available in Code Explorer. QHelp Tab lists all the avail­able built-in and library functions, for a quick reference. Double-clicking a routine in QHelp Tab opens the relevant Help topic. Keyboard Tab lists all the available keyboard shortcuts in mikroC.

CODE EXPLORER

Collapse/Expand
All Icon.
MikroElektronika: Development tools - Books - Compilers
7
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
The source-level Debugger is an integral component of mikroC development envi­ronment. It is designed to simulate operations of Microchip Technology's PICmicros and to assist users in debugging software written for these devices.
The Debugger simulates program flow and execution of instruction lines, but does not fully emulate PIC device behavior: it does not update timers, interrupt flags, etc.
After you have successfully compiled your project, you can run the Debugger by selecting Run > Debug from the drop-down menu, or by clicking the Debug Icon . Starting the Debugger makes more options available: Step Into, Step Over, Run to Cursor, etc. Line that is to be executed is color highlighted.
Debug [F9]
Start the Debugger.
Run/Pause Debugger [F6]
Run or pause the Debugger.
Step Into [F7]
Execute the current C (single– or multi–cycle) instruction, then halt. If the instruc­tion is a routine call, enter the routine and halt at the first instruction following the call.
Step Over [F8]
Execute the current C (single– or multi–cycle) instruction, then halt. If the instruc­tion is a routine call, skip it and halt at the first instruction following the call.
Step Out [Ctrl+F8]
Execute the current C (single– or multi–cycle) instruction, then halt. If the instruc­tion is within a routine, execute the instruction and halt at the first instruction fol­lowing the call.
Run to cursor [F4]
Executes all instructions between the current instruction and the cursor position.

DEBUGGER

Start Debugger
Step Into
Step Over
Step Out
Pause Debugger
Run to Cursor
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
8
MikroElektronika: Development tools - Books - Compilers
page
Toggle Breakpoint [F5]
Toggle breakpoint at current cursor position. To view all the breakpoints, select Run > View Breakpoints from the drop-down menu. Double clicking an item in window list locates the breakpoint.
Watch Window
Variables
The Watch Window allows you to monitor program items while running your pro­gram. It displays variables and special function registers of PIC MCU, their addresses and values. Values are updated as you go through the simulation.
Double clicking one of the items opens a window in which you can assign a new value to the selected variable or register and change number formatting.
Toggle
Breakpoint.
Stopwatch Window
The Stopwatch Window displays the current count of cycles/time since the last Debugger action. Stopwatch measures the execution time (number of cycles) from the moment the Debugger is started, and can be reset at any time. Delta represents the number of cycles between the previous instruction line (line where the Debugger action was performed) and the active instruction line (where the Debugger action landed).
Note: You can change the clock in the Stopwatch Window; this will recalculate values for the newly specified frequency. Changing the clock in the Stopwatch Window does not affect the actual project settings – it only provides a simulation.
MikroElektronika: Development tools - Books - Compilers
9
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
Call Stack Window
The Call Stack Window keeps track of depth and order of nested routine calls in program simulation. Check the Nested Calls Limitations for more information.
Note: Real scenarios may differ from the simulation, depending on runtime program parameters.
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
10
MikroElektronika: Development tools - Books - Compilers
page
MikroElektronika: Development tools - Books - Compilers
11
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
In case that errors were encountered during compiling, the compiler will report them and won't generate a hex file. The Error Window will be prompted at the bottom of the main window by default.
The Error Window is located under the message tab, and displays location and type of errors compiler has encountered. The compiler also reports warnings, but these do not affect the output; only errors can interefere with generation of hex.
Double click the message line in the Error Window to highlight the line where the error was encountered.
Consult the Error Messages for more information about errors recognized by the compiler.

ERROR WINDOW

mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
12
MikroElektronika: Development tools - Books - Compilers
page
After successful compilation, you can review statistics of your code. Select Project > View Statistics from the drop-down menu, or click the Statistics icon. There are six tab windows:
Memory Usage Window
Provides overview of RAM and ROM memory usage in form of histogram.
Procedures (Graph) Window
Displays functions in form of histogram, according to their memory allotment.

STATISTICS

Statistics Icon.
MikroElektronika: Development tools - Books - Compilers
13
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
Procedures (Locations) Window
Displays how functions are distributed in microcontroller’s memory.
Procedures (Details) Window
Displays complete call tree, along with details for each function:
size, start and end address, calling frequency, return type, etc.
RAM Window
Summarizes all GPR and SFR registers and their addresses. Also displays symbol­ic names of variables and their addresses.
ROM Window
Lists op-codes and their addresses in form of a human readable hex code.
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
14
MikroElektronika: Development tools - Books - Compilers
page
MikroElektronika: Development tools - Books - Compilers
15
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
USART Terminal
mikroC includes the USART (Universal Synchronous Asynchronous Receiver Transmitter) communication terminal for RS232 communication. You can launch it from the drop-down menu Tools > Terminal or by clicking the Terminal icon.
ASCII Chart
The ASCII Chart is a handy tool, particularly useful when working with LCD dis­play. You can launch it from the drop-down menu Tools > ASCII chart.

INTEGRATED TOOLS

7 Segment Display Decoder
The 7seg Display Decoder is a convenient visual panel which returns decimal/hex value for any viable combination you would like to display on 7seg. Click on the parts of 7 segment image to get the desired value in the edit boxes. You can launch it from the drop-down menu Tools > 7 Segment Display.
EEPROM Editor
EEPROM Editor allows you to easily manage EEPROM of PIC microcontroller.
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
16
MikroElektronika: Development tools - Books - Compilers
page
mikroBootloader
mikroBootloader can be used only with PICmicros that support flash write.
1. Load the PIC with the appropriate hex file using the conventional programming
techniques (e.g. for PIC16F877A use p16f877a.hex).
2. Start mikroBootloader from the drop-down menu Tools > Bootoader.
3. Click on Setup Port and select the COM port that will be used. Make sure that
BAUD is set to 9600 Kpbs.
4. Click on Open File and select the HEX file you would like to upload.
5. Since the bootcode in the PIC only gives the computer 4-5 sec to connect, you
should reset the PIC and then click on the Connect button within 4-5 seconds.
6. The last line in then history window should now read “Connected”.
7. To start the upload, just click on the Start Bootloader button.
8. Your program will written to the PIC flash. Bootloader will report an errors that
may occur.
9. Reset your PIC and start to execute.
The boot code gives the computer 5 seconds to get connected to it. If not, it starts running the existing user code. If there is a new user code to be downloaded, the boot code receives and writes the data into program memory.
The more common features a bootloader may have are listed below:
- Code at the Reset location.
- Code elsewhere in a small area of memory.
- Checks to see if the user wants new user code to be loaded.
- Starts execution of the user code if no new user code is to be loaded.
- Receives new user code via a communication channel if code is to be loaded.
- Programs the new user code into memory.
Integrating User Code and Boot Code
The boot code almost always uses the Reset location and some additional program memory. It is a simple piece of code that does not need to use interrupts; therefore, the user code can use the normal interrupt vector at 0x0004. The boot code must avoid using the interrupt vector, so it should have a program branch in the address range 0x0000 to 0x0003. The boot code must be programmed into memory using conventional programming techniques, and the configuration bits must be pro­grammed at this time. The boot code is unable to access the configuration bits, since they are not mapped into the program memory space.
MikroElektronika: Development tools - Books - Compilers
17
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
18
MikroElektronika: Development tools - Books - Compilers
page
Below is the complete list of keyboard shortcuts available in mikroC IDE. You can also view keyboard shortcuts in Code Explorer window, tab Keyboard.
IDE Shortcuts
F1 Help CTRL+N New Unit CTRL+O Open CTRL+F9 Compile CTRL+F11 Code Explorer on/off CTRL+SHIFT+F5 View breakpoints
Basic Editor shortcuts
F3 Find, Find Next CTRL+A Select All CTRL+C Copy CTRL+F Find CTRL+P Print CTRL+R Replace CTRL+S Save unit CTRL+SHIFT+S Save As CTRL+V Paste CTRL+X Cut CTRL+Y Redo CTRL+Z Undo
Advanced Editor shortcuts
CTRL+SPACE Code Assistant CTRL+SHIFT+SPACE Parameters Assistant CTRL+D Find declaration CTRL+G Goto line CTRL+J Insert Code Template CTRL+<number> Goto bookmark CTRL+SHIFT+<number> Set bookmark CTRL+SHIFT+I Indent selection CTRL+SHIFT+U Unindent selection CTRL+ALT+SELECT Select columns

KEYBOARD SHORTCUTS

Debugger Shortcuts
F4 Run to Cursor F5 Toggle breakpoint F6 Run/Pause Debugger F7 Step into F8 Step over F9 Debug CTRL+F2 Reset
MikroElektronika: Development tools - Books - Compilers
19
page
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
20
MikroElektronika: Development tools - Books - Compilers
page
CHAPTER
MikroElektronika: Development tools - Books - Compilers
2
Building Applications
Creating applications in mikroC is easy and intuitive. Project Wizard allows you to set up your project in just few clicks: name your application, select chip, set flags, and get going.
mikroC allows you to distribute your projects in as many files as you find appro­priate. You can then share your mikroCompiled Libraries (
.mcl files) with other
developers without disclosing the source code. The best part is that you can use
.mcl bundles created by mikroPascal or mikroBasic!
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
22
MikroElektronika: Development tools - Books - Compilers
page
mikroC organizes applications into projects, consisting of a single project file (extension .ppc) and one or more source files (extension .c). You can compile source files only if they are part of a project.
Project file carries the following information:
- project name and optional description,
- target device,
- device flags (config word) and device clock,
- list of project source files with paths.
New Project
The easiest way to create project is by means of New Project Wizard, drop-down menu Project > New Project. Just fill the dialog with desired values (project name and description, location, device, clock, config word) and mikroC will create the appropriate project file. Also, an empty source file named after the project will be created by default.
Editing Project
Later, you can change project settings from drop-down menu Project > Edit Project. You can rename the project, modify its description, change chip, clock, config word, etc. To delete a project, simply delete the folder in which the project file is stored.
Add/Remove Files from Project
Project can contain any number of source files (extension .c). The list of relevant source files is stored in the project file (extension .ppc). To add source file to your project, select Project > Add to Project from drop-down menu. Each added source file must be self-contained, i.e. it must have all the necessary definitions after preprocessing. To remove file(s) from your project, select Project > Remove from Project from drop-down menu.
Note: For inclusion of header files, use the preprocessor directive
#include.

PROJECTS

New Project.
Edit Project.
Add to Project.
Remove from
Project.
Loading...
+ 282 hidden pages