You are on page 1of 13

EXPERIMENT 4: Intermediate I/O Programming (Introduction to MikroC)

Introduce students to MikroC compiler for PIC.
Understand I/O pin assignments and learn to program the I/O port.
To familiarize with LCD Display and Keypad Input Output Interfacing
To utilize time delay in Input Output Interfacing

Pre-lab: Download mikroC PRO evaluation software and install in your personal
computer/laptop. Familiarize yourself with the compiler before coming to the lab.

Download link:


There are several C compilers on the market for the PIC18 series of microcontrollers. These
compilers have many similar features, and they can all be used to develop C-based high-level
programs for PIC18 microcontrollers. Some of the C compilers used most often in
commercial, industrial, and educational
PIC18 microcontroller applications are:

The popular and powerful mikroC, developed by MikroElektronika is easy to learn and comes
with rich resources, such as a large number of library functions and an integrated
development environment with a built-in simulator and an in-circuit debugger (e.g.,
mikroICD). A demo version of the compiler with a 2K program limit is available from

PICC18, another popular C compiler, was developed by Hi-Tech Software and is available in
two versions: standard and professional. A powerful simulator and an integrated development
environment (Hi-Tide) are provided by the company. PICC18 is supported by the PROTEUS
simulator ( which can be used to simulate PIC microcontrollerbased
systems. A limited-period demo version of this compiler is available on the developers web

C18 is a product of Microchip Inc. (web site: A limited-period demo

version as well as a limited functionality version of C18 with no time limit, are available from
the Microchip web site. C18 includes a simulator and supports hardware and software
development tools such as in-circuit emulators (e.g., ICE2000) and in-circuit debuggers (e.g.,

CCS has been developed by the Custom Computer Systems Inc. (web site: The company offers a limited-period demo version of their compiler.
CCS provides a large number of built-in functions and supports an in-circuit debugger (e.g.,
ICD-U40) which are very helpful in the development of PIC18 microcontrollerbased

In this lab, we are mainly concentrating on the use of the mikroC compiler.

EEEB371 E41
MikroC IDE

Figure 4.0 MikroC desktop

MikroC allows users to quickly develop and deploy complex applications:

Write C source code using the highly advanced Code Editor
Use the included mikroC libraries to dramatically spped up the development: data
acquisition, memory, displays, conversions, communications..
Monitor user 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...
Provide plenty of examples to expand, develop, and use as building bricks in projects.

Building Application in mikroC

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.

A. New Project
The easiest way to create a 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. Note: Make sure you
click Edit Project under Project tab to set the configurations as you as have set in MPLAB
under configuration settings.

EEEB371 E42
B. 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 the drop-down menu. To remove file(s) from your project, select
Project>Remove from Project from drop-down menu.
Note: For inclusion of header files, use the pre-processor directive #include.

C. Compilation
When the user have created the project and written the source code, you will want to compile
it. Select Project>Build from the drop-down menu, or click Build icon, or simply hit

Progress bar will appear to inform you about the status of compiling. If there are errors, you
will be notified in the Error Window. If no errors are encountered, mikroC will generate
output files as follows:
.hex Intel style hex records. Use this file to program PIC MCU.
.mcl Binary distribution of application that can be included in other projects.
.lst Overview of PIC memory allotment: instruction addresses, registers, routines,
.asm Human readable assembly with symbolic names, extracted from the List File.

Port Programming

Depending on the type of microcontroller used, PIC microcontroller input-output ports are
named as PORTA, PORTB, PORTC, and so on. Port pins can be in analog or digital mode. In
analog mode, ports are input only and a built-in analog-to-digital converter and multiplexer
circuits are used. In digital mode, a port pin can be configured as either input or output. The
TRIS registers control the port directions, and there are TRIS registers for each port, named as
TRISA, TRISB, TRISC, and so on. Clearing a TRIS register bit to 0 sets the corresponding
port bit to output mode. Similarly, setting a TRIS register bit to 1 sets the corresponding port
bit to input mode.

Ports can be accessed as a single 8-bit register, or individual bits of a port can be accessed. In
the following example, PORTB is configured as an output port and all its bits are set to a 1:

TRISB = 0; // Set PORTB as output

PORTB = 0xFF; // Set PORTB bits to 1

EEEB371 E43
Similarly, the following example shows how the 4 upper bits of PORTC can be set as input
and the 4 lower bits of PORTC can be set as output:

TRISC = 0xF0;

Bits of an input-output port can be accessed by specifying the required bit number. In the
following example, variable P2 is loaded with bit 2 of PORTB:

P2 = PORTB.F2;

All the bits of a port can be complemented by the statement:


LCD Display

The 2x16 character LCD offers character display for embedded system. It can be used to
display numerical information, text message and also special symbol. We can control a LCD
using either 8 pins (8-bit interface: RD0-RD7) or 4 pins (4-bit interface: RD4-RD7),
depending on the I/O pins that we have. For learning purposes, 4-bit interfacing using MikroC
library is recommended as it will be relatively easy. Figure 4.1 shows the schematic for LCD
connection in PTK40A Training Kit.

Figure 4.1 Connection of a 2x16 character LCD

EEEB371 E44
Controlling LCD

Before using the LCD for display purpose, the LCD has to be initialized by sending the
commands to initialize the LCD. After initializing the LCD, we can easily display any
character we like on the LCD by using mikroC LCD library. The mikroC PRO for PIC
provides library routines for communication with LCDs through the 4-bit interface.

Library Routines:

EEEB371 E45
EEEB371 E46
4x4 Keypad

Keypad is an array of switch. There will be 2 terminal pins connected each time a button is
pressed. For example; when button 1 is pressed, pin COL1 and ROW1 is connected.
Initially, there is no connection between rows and columns. The button connects it. The
keypads pins need to be pulled up or pulled down to avoid floating case. Pull up normally
connect to 5V and pull down is connect to ground. 4x4 Keypad pin can directly connect to
microcontroller or keypad decoder IC.

In this section, we are trying to scan the button (character) being pressed on keypad. The 4x4
keypad is a 16-way XY-Matrix hexadecimal keypad. It has eight connections where four pins
for row (ROW1-ROW4) and four pins for column (COL1-COL4). The internal connection for
the keypad is shown in Figure 4.2.

EEEB371 E47
Figure 4.2 Internal Connection of a 4x4 Keypad

From the schematic in Figure 4.2, the row pins are connected directly to PIC, and being
configured as writeable pins (Output from PIC). Column pins are being pulled high to 5V,
and being configured as readable pins (Input to PIC). To check which button is pressed, users
need to scan it column by column and row by row. Make rows as output and columns as input
as explained earlier. For example, set (5V, high logic) all rows by default. 1st scan, clear
(logic low) row 1 and scan column 1 to column 4 (1, 2, 3 and A) for low logic, this will
determine which button is pressed in row 1. If one of those buttons is pressed, record it and
jump out from the scanning loop and continue with the action required. If none button is
pressed in row 1, set it back to default (logic high) and clear row 2. Scan column 1 to column
4 again and this will scan button press of 4, 5, 6 and B. This process should be repeated
until all four rows are being completed scanned.

EEEB371 E48
Figure 4.3 Connection 4x4 keypad to microcontroller

EEEB371 E49

Part A: LCD Display

1. Write the source code in MikroC Compiler, build it and download the Hex file into the
microcontroller and run the program. Print out the source file and write down your




ADCON1=0x0F; //Configureallportswithanalogfunctionasdigital
CMCON=7; //Disablecomparators

Lcd_Init(); //InitializeLCDbasedonLCDmoduleconnections
Lcd_Cmd(_LCD_CURSOR_OFF); //Turncursoroff
Lcd_Out(1,3,text); //Displaytextatfirstrow,thirdcolumn


2. From Help menu in mikroC, click on the search tab and type lcd. From the example in the
help file, change the program so that the text will be displayed at the second row, third
column and the text display should shift left continuously. Adjust the time delay
accordingly so that the text can be visible while shifting left. Write down your
observation and print out the source file.

EEEB371 E410
PART B: Keypad

3. Complete and write the source code below in MikroC so that each keypad characters will
be displayed at the LCD display when the respective keypads are pressed.




voidgetkey() //afunctiontocheckwhichcolumnisactive

if(Button(&PORTB,0,1,0)) //ifRB0islow,column1isactive
elseif(Button(&PORTB,1,1,0)) //ifRB1islow,column2isactive
elseif(Button(&PORTB,2,1,0)) //ifRB2islow,column3isactive
elseif(Button(&PORTB,3,1,0)) //ifRB3islow,column4isactive

EEEB371 E411
intstat; //Assignstatus(stat)tointeger
char*key; //Assignkey(toholdkeypadvalues)tochar
TRISB=0xFF; //ConfigurePortBasinput
TRISD=0x00; //ConfigurePortDasoutput

ADCON1=0x0F; //Configureallportswithanalogfunctionasdigital
CMCON=7; //Disablecomparators

Lcd_Init(); //InitializeLCDbasedonLCDmoduleconnections
Lcd_Cmd(_LCD_CURSOR_OFF); //Turncursoroff
Lcd_Out(1,1,"KeypadPressed:"); //DisplayKeypadpressed:atrow1,column1

do //continuousdo..whileloop
stat=0; //Initializestatus(stat)to0

PORTD=0xEF; //scanningkeypadforfirstrow
getkey(); //andgettheinputofthekeypad

if(column==1) //ifcolumn1islow,itmeansthat
{ key="1"; //key1ispressed
Lcd_Out(1,16,key); //displayatrow1,column16atLCDdisplay
stat=1;} //asanindicatorthatakeyhasbeenpressed
elseif(column==2) //ifcolumn2islow,itmeansthat
{key="2"; //key2ispressed
Lcd_Out(1,16,key); //displayatrow1,column16atLCDdisplay
elseif(column==3) //ifcolumn3islow,itmeansthat
{key="3"; //key3ispressed
Lcd_Out(1,16,key); //displayatrow1,column16atLCDdisplay
elseif(column==4) //ifcolumn4islow,itmeansthat
{key="A"; //keyAispressed
Lcd_Out(1,16,key); //displayatrow1,column16atLCDdisplay

if(stat==1) //resetbackstatvalue
{delay_ms(100); //ashortdelaybeforecheckingforthenextinput

EEEB371 E412
PORTD=0xDF; //scanningkeypadforsecondrow
getkey(); //andgettheinputofthekeypad

if(column==1) //ifcolumn1islow,itmeansthat
{key="4"; //key4ispressed
Lcd_Out(1,16,key); //displayatrow1,column16atLCDdisplay
elseif(column==2) //ifcolumn2islow,itmeansthat
{key="5"; //key5ispressed
Lcd_Out(1,16,key); //displayatrow1,column16atLCDdisplay
elseif(column==3) //ifcolumn3islow,itmeansthat
{key="6"; //key6ispressed
Lcd_Out(1,16,key); //displayatrow1,column16atLCDdisplay
elseif(column==4) //ifcolumn4islow,itmeansthat
{key="B"; //keyBispressed
Lcd_Out(1,16,key); //displayatrow1,column16atLCDdisplay

if(stat==1) //resetbackstatvalue
{delay_ms(100); //ashortdelaybeforecheckingforthenextinput





4. Build the source code, download and run the application. Write down your observation.
Print out the source code.

5. Turn off the PTK40A power supply, rearrange the USB cable back into the Training Kit.
Exit from MPLAB and PICKit 2 programmer. Shutdown your PC and rearrange your
workstation before you leave.

EEEB371 E413