0% found this document useful (0 votes)
9 views155 pages

Book (EN)

This document outlines the usage and sharing restrictions for a book intended for a Microprocessors and Programming course at Yıldız Technical University, emphasizing privacy, copyright, and unauthorized distribution. It includes a preface detailing the book's content, which consists of theoretical information, experiments, and practical applications related to microprocessors and programming. The document also provides essential information on number systems, logic gates, and various electronic components used in the course.

Uploaded by

enese41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views155 pages

Book (EN)

This document outlines the usage and sharing restrictions for a book intended for a Microprocessors and Programming course at Yıldız Technical University, emphasizing privacy, copyright, and unauthorized distribution. It includes a preface detailing the book's content, which consists of theoretical information, experiments, and practical applications related to microprocessors and programming. The document also provides essential information on number systems, logic gates, and various electronic components used in the course.

Uploaded by

enese41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 155

Warning: About Using and Sharing This Book

This book is given specifically to you and should only be used


within the context of the relevant course. Please pay attention
to the following rules:

Privacy: This book and its contents should only be used by you.
Do not lend or copy the book to others.

Copyright: The content of the book is protected by copyright.


Unauthorized copying or sharing may result in legal
consequences.

Unauthorized Distribution: Distributing or sharing the


content of the book to others constitutes copyright infringement.

If you do not comply with these rules, disciplinary measures


may be taken and legal liability may arise. Please use the book
only within the scope of the relevant course and do not share it
with others.
MKT3811: Microprocessors and Programming

PREFACE
This book has been meticulously prepared for the
Microprocessors and Programming course, one of the 5th-
semester courses offered by the Mechatronics Engineering at
Yıldız Technical University. The book comprises nine
experiments supplemented with essential information required
for their successful execution.
In the experiments, Microchip integrated circuits were utilized
in the RISC structure, which features Harvard architecture
capable of data and commands through separate channels and
executing almost every command in a single cycle.
The book contains experiments written in both assembly and C
programming languages. While becoming proficient in
assembly code may require more time than C, it significantly
contributes to a deeper understanding of basic instruction sets,
processor units, memory addresses, integrated inputs and
outputs, and fundamental computer architecture.
We sincerely hope that this book serves as an invaluable
educational resource, providing practical examples that enhance
the understanding of the theoretical concepts covered in the
course.

i
MKT3811: Microprocessors and Programming

ii
MKT3811: Microprocessors and Programming

TABLE OF CONTENTS
PREFACE .............................................................................................................i
TABLE OF CONTENTS.................................................................................... iii
1. BASIC INFORMATION ................................................................................. 1
1.1. Theoretical Information ........................................................................... 1
1.1.1. Binary, Hexadecimal and Decimal Conversions .............................. 1
1.1.2. Logic Gates and Logic Circuits ....................................................... 6
1.2. Electronic Components ............................................................................ 9
1.2.1. PIC16F887....................................................................................... 9
1.2.2. Fixed Resistors .............................................................................. 19
1.2.3. Variable Resistors .......................................................................... 22
1.2.4. Capacitors ...................................................................................... 22
1.2.5. LED ............................................................................................... 25
1.2.6. Button ............................................................................................ 27
1.2.7. Crystal ........................................................................................... 29
1.2.8. Keypad........................................................................................... 31
1.2.9. LCD Screen ................................................................................... 32
1.2.10. 7 – Segment Display .................................................................... 35
1.3. Basic Circuit Building Information........................................................ 37
1.4. Used Programs ....................................................................................... 42
1.4.1. CCS C ............................................................................................ 42
1.4.2. MPLAB IDE .................................................................................. 44
1.4.3. MPLAB IPE .................................................................................. 50
1.4.4. Proteus ISIS ................................................................................... 58
1.5. Equivalent Microcontrollers .................................................................. 65
1.5.1. Differences Between 16F887 and 16F877A .................................. 65
1.5.2. Differences Between 16F877 and 16F877A .................................. 65
1.5.3. Configuration Settings for Equivalent Microcontrollers ................ 66
1.6. 16F88X Configuration Settings ............................................................. 72
1.6.1. CONFIG1: Configuration Word Register 1 ................................... 72
1.6.2. CONFIG2: Configuration Word Register 2 ................................... 76
2. A LIST OF MATERIALS ............................................................................. 77
3. EXPERIMENTS ............................................................................................ 78
3.1. Experiment 1: Digital I/O ...................................................................... 78
3.1.1. Preliminary Study .......................................................................... 78
3.1.2. A list of materials .......................................................................... 78
3.1.3. Purpose of Experiment .................................................................. 79
3.1.4. Experimental Procedure ................................................................. 79
3.2. Experiment 2: Counter ........................................................................... 86
3.2.1. Preliminary Study .......................................................................... 86
3.2.2. A list of materials .......................................................................... 86

iii
MKT3811: Microprocessors and Programming

3.2.3. Purpose of Experiment .................................................................. 87


3.2.4. Experimental Procedure ................................................................. 87
3.3. Experiment 3: Timer & Interrupt ........................................................... 93
3.3.1. Preliminary Study .......................................................................... 93
3.3.2. A list of materials .......................................................................... 93
3.3.3. Purpose of Experiment .................................................................. 94
3.3.4. Experimental Procedure ................................................................. 94
3.4. Experiment 4: LCD Display ................................................................ 100
3.4.1. Preliminary Study ........................................................................ 100
3.4.2. A list of materials ........................................................................ 100
3.4.3. Purpose of Experiment ................................................................ 101
3.4.4. Experimental Procedure ............................................................... 101
3.5. Experiment 5: Keypad ......................................................................... 107
3.5.1. Preliminary Study ........................................................................ 107
3.5.2. A list of materials ........................................................................ 107
3.5.3. Purpose of Experiment ................................................................ 108
3.5.4. Experimental Procedure ............................................................... 108
3.6. Experiment 6: ADC ............................................................................. 117
3.6.1. Preliminary Study ........................................................................ 117
3.6.2. A list of materials ........................................................................ 117
3.6.3. Purpose of Experiment ................................................................ 118
3.6.4. Experimental Procedure ............................................................... 118
3.7. Experiment 7: PWM ............................................................................ 124
3.7.1. Preliminary Study ........................................................................ 124
3.7.2. A list of materials ........................................................................ 124
3.7.3. Purpose of Experiment ................................................................ 125
3.7.4. Experimental Procedure ............................................................... 125
3.8. Experiment 8: Serial Communication .................................................. 132
3.8.1. Preliminary Study ........................................................................ 132
3.8.2. A list of materials ........................................................................ 132
3.8.3. Purpose of Experiment ................................................................ 133
3.8.4. Experimental Procedure ............................................................... 133
3.9. Experiment 9: 7-Segment Display ....................................................... 140
3.9.1. Preliminary Study ........................................................................ 140
3.9.2. A list of materials ........................................................................ 140
3.9.3. Purpose of Experiment ................................................................ 141
3.9.4. Experimental Procedure ............................................................... 141
REFERENCES ................................................................................................. 145

iv
MKT3811: Microprocessors and Programming

1. BASIC INFORMATION
1.1. Theoretical Information
1.1.1. Binary, Hexadecimal and Decimal Conversions
Due to the different needs arising over time, various
number systems have been developed. The decimal system,
which consists of the ten digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, is
the number system that is commonly used on a daily basis.
However, the binary system, which consists of only two digits
(0 and 1), is more appropriate for computers, as their circuits
can only represent open or closed states. 0 represents the closed
state in the binary system, while 1 represents the on state.
Hexadecimal numbers, also referred to as hex, have a base of
16. Because the binary representation of large numbers is
cumbersome, hexadecimal numbers are primarily employed to
abbreviate the binary system. In CCS C programming,
hexadecimal numbers must be preceded by 0x to distinguish
them from variable names. For instance, 14 is a decimal number
in C, but 0x14 represents a hexadecimal number (which is
equivalent to 20 in decimal). Understanding the interconversion
of these number systems is critical in avoiding such
complexities.
When converting a decimal number to a binary number,
division should be carried out until the quotient is less than 2.
Then, the remainder should be written from left to right in order,
starting from the last quotient to the first quotient. An example
transformation is given in Figure 1.

1
MKT3811: Microprocessors and Programming

Figure 1: Converting decimal to binary

When converting a binary number to a decimal number,


the numbers obtained are multiplied by the corresponding place
values, starting from the right to the left, starting from zero, and
increasing by one up to the left. The results are summed and the
decimal number corresponding to the binary number is found.
Figure 2 provides an example of this transformation.

Figure 2: Converting binary to decimal

When converting a decimal number to a hexadecimal


number, division is done until the quotient is less than 16, and
the remainder is written in order from left to right, starting from
the last quotient to the first quotient. An example transformation
is given in Figure 3.

2
MKT3811: Microprocessors and Programming

Figure 3: Converting decimal to hexadecimal

Figure 3 illustrates that decimal numbers ranging from


0 to 9 are represented by the digits 0 through 9, while numbers
ranging from 10 to 15 are represented by letters. The relation
between binary, decimal and hexadecimal systems is given in
Table 1. This table provides great convenience especially for
hexadecimal system conversions.
Table 1: Relationship between binary, decimal and hexadecimal
systems

Decimal Hexadecimal Binary


0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111

3
MKT3811: Microprocessors and Programming

When converting a hexadecimal number to a decimal


number, the resulting numbers are multiplied by the
corresponding place values, starting from the right to the left,
starting from zero to the sixteenth, and increasing by one to the
left. The results are summed up and the decimal number
corresponding to the hexadecimal number is found. An example
transformation is given in Figure 4.

Figure 4: Converting hexadecimal to decimal

When converting from hexadecimal to binary, one can


obtain the binary equivalent of each digit from Table 1 and add
them together side by side, in the same order as the original
hexadecimal number. An example transformation is given in
Figure 5.

Figure 5: Converting hexadecimal to binary

4
MKT3811: Microprocessors and Programming

When converting a binary number to a hexadecimal


number, the digits are divided into groups of four from right to
left, and each group of four has its hexadecimal equivalent from
Table 1. Then, the hexadecimal equivalent of the binary number
is obtained by writing them side by side in the same order.
While the number is divided into groups of four from right to
left, if there are less than 4 digits left at the far left, the digits in
front of the last group can be accepted as zero since they are
ineffective elements. An example transformation is given in
Figure 6.

Figure 6: Converting binary to hexadecimal

5
MKT3811: Microprocessors and Programming

1.1.2. Logic Gates and Logic Circuits


A logic gate is a device that acts as a building block for
digital circuits. They perform logical functions fundamental to
digital circuits. Most electronic devices we use today will have
some sort of logic gate built into them. For example, logic gates
can be used in technologies such as smartphones, tablets, or
memory devices.
In a circuit, logic gates will make decisions based on a
combination of digital signals from their inputs. Most logic
gates have two inputs and one output. Logic gates are based on
Boolean algebra. At any given moment, each terminal is in one
of two conditions, false or true. False represents 0, true
represents 1. Depending on the type of logic gate used and the
combination of inputs, the output will be different. The outputs
that correspond to the inputs of the most fundamental logic
gates are listed in Table 2.
Table 2: Outputs that basic logic gates respond to inputs
Input Input Output Output Output Output Output Output
A B (AND) (OR) (XOR) (NAND) (NOR) (XNOR)
0 0 0 0 0 1 1 1
0 1 0 1 1 1 0 0
1 0 0 1 1 1 0 0
1 1 1 1 0 0 0 1

In addition to the gates given in Table 2, the NOT gate


is an electronic circuit that produces an inverted version of the
input at its output. An apostrophe such as B = A' = 𝐴̅ or a bar
above A is used to indicate the NOT operation.

6
MKT3811: Microprocessors and Programming

AND gate is an electronic circuit that only outputs high


(1) when all its inputs are high. To show the AND operation, a
point in the form of C = A.B is used, where A and B are input
and C is output.
OR gate is an electronic circuit that gives a high output
if one or more of its inputs are high. To show the OR operation,
a plus is used as C = A+B, where A and B are input and C is
output.
XOR gate is an electronic circuit that gives a low output
if both inputs are the same, and a high output if they are
different. To illustrate the XOR operation, a circled plus of C =
A⊕B is used, with A and B as input and C as output.
NAND gate is an electronic circuit that gives a high
output if one or more of its inputs are low. Since the NAND
operation basically consists of AND and NOT operations, C =
̅̅̅̅̅
𝐴. 𝐵 with A and B as input and C as output, a dot and a bar
above the product are used to show the NAND operation.
NOR gate is an electronic circuit that gives a low output
if one or more of its inputs are high. Since the NOR operation
basically consists of OR and NOT operations, a bar is used in
the form of C = ̅̅̅̅̅̅̅̅
𝐴 + 𝐵 plus and above the sum, with A and B as
input and C as output to show the NOR operation.
XNOR gate is an electronic circuit that gives a high
output if both inputs are the same, and a low output if they are
different. Since the XNOR operation basically consists of XOR
and NOT operations, a plus circled as C = ̅̅̅̅̅̅̅̅̅
𝐴 ⊕ 𝐵 and a bar
above the function are used to represent the XNOR operation,
with A and B being the input, C being the output.

7
MKT3811: Microprocessors and Programming

Figure 7 provides representations of the basic logic


gates summarized by Table 2, plus the NOT gate.

Figure 7: Representations of basic logic gates

Most digital logic circuits are based on CMOS


(Complementary Metal Oxide Semiconductor) and TTL
(Transistor-Transistor Logic) technology structures. TTLs are
made up of bipolar type transistors while CMOSs are integrated
circuits made up of field effect transistors. Both of these
structures can be used to create logic functions. For example,
the creation of a NOT function with CMOS and TTL structures
is given in Figure 8.

8
MKT3811: Microprocessors and Programming

Figure 8: Creating the NOT function with CMOS (left) and TTL (right)
structures

1.2. Electronic Components


1.2.1. PIC16F887
A microcontroller is a compact integrated circuit
designed to manage a specific operation in an embedded
system. A typical microcontroller contains a processor,
memory, and input/output peripherals on a single chip.
Microcontrollers are found in vehicles, robots, office machines,
medical devices, mobile radio transceivers, vending machines,
and home appliances. Essentially, they are simple miniature
personal computers designed to control small features of a
larger component without a complex front-end operating
system. A large number of microcontrollers with various
functions and designs are produced by the Microchip company.
The produced microcontrollers can be named in different ways
according to their maximum application frequencies and CPU
bit widths. Some model families that are frequently used in the
market are given in Table 3.

9
MKT3811: Microprocessors and Programming

Table 3: Some PIC model families on the market

Maximum CPU bit


Model family application width
frequency (Mhz) (bits)
PIC10FXX 8 8
PIC12CXXX 20 8
PIC14000 20 8
PIC16C5XX 20 8
PIC16C7XX 20 8
PIC16F62X 20 8
PIC16F8X 20 8
PIC16F8XX 20 8
PIC16C9XX 20 8
PIC17CXXX 33 8
PIC18FXXX 40 8
PIC24F 16 16
PIC24H 40 16
dsPIC30 DSC 30 16
dsPIC33 DSC 40 16

Each PIC family listed in Table 3 has unique


characteristics. These families are divided into various models
based on size of instruction size, operating speed, number of
input/output ports and memory types. The PIC16F887 model
preferred in the book is one of Microchip's most popular
products. It contains most components that modern
microcontrollers normally have. With its low price, wide range
of applications, high quality and easy availability, it is used in

10
MKT3811: Microprocessors and Programming

the control of different processes in the industry, machine


control devices, measurement of different values, etc. It is an
ideal solution in applications. Table 4 provides some of its key
characteristics.
Table 4: Main features of 16F887

Features
Program Memory Type Flash
Program Memory Size (KB) 14
CPU Speed (MIPS/DMIPS) 5
Data EEPROM (bytes) 256
Capture/Comparison/PWM (CCP) 1
Maximum 8 Bit Digital Timers 2
Number of ADCs 1
ADC Channels 14
Maximum ADC Resolution (bits) 10
Number of Comparators 2
Temperature Range Minimum -40
Temperature Range Maximum 125
Operating Voltage Maximum (V) 5.5
Operating Voltage Minimum (V) 2
Number of Pins 40
Low Power None

11
MKT3811: Microprocessors and Programming

PIC16F887 has 40 pins. The pin configuration of the


model is given in Figure 9. As shown in Figure 9, most of the
pins have more than one function. For example, the identifier
RA3/AN3/VREF+/C1IN+ for pin five contains the functions of
A/D channel 3 input, A/D positive voltage reference input, and
comparator C1 positive input. These different pin functions
cannot be used simultaneously, but can be modified at any time
during operation.

Figure 9: PIC16F887 Pin Configuration

All pin functions of the model are listed and explained


in Table 5.

12
MKT3811: Microprocessors and Programming

Table 5: 16F887 Pinout description

Input Output
Name Function Description
Type Type
RA0/ RA0 TTL CMOS General purpose I/O.
AN0/ AN0 AN — A/D Channel 0 input.
ULP ULPW
WU/C AN — Ultra Low-Power Wake-up input.
U
12IN0
C12IN0
- AN — Comparator C1 or C2 negative input.
-
RA1/ RA1 TTL CMOS General purpose I/O.
AN1/ AN1 AN — A/D Channel 1 input.
C12I C12IN1
N1- AN — Comparator C1 or C2 negative input.
-
RA2/ RA2 TTL CMOS General purpose I/O.
AN2/ AN2 AN — A/D Channel 2.
VREF-
VREF- AN — A/D Negative Voltage Reference input.
/CVRE
F/C2I CVREF — AN Comparator Voltage Reference output.
N+ C2IN+ AN — Comparator C2 positive input.
RA3/ RA3 TTL CMOS General purpose I/O.
AN3/ AN3 AN — A/D Channel 3.
VREF+
/C1IN VREF+ AN — A/D Positive Voltage Reference input.
+ C1IN+ AN — Comparator C1 positive input.
RA4/ RA4 TTL CMOS General purpose I/O.
T0CK
T0CKI ST — Timer0 clock input.
I/C1O
UT C1OUT — CMOS Comparator C1 output.
RA5/ RA5 TTL CMOS General purpose I/O.
AN4/ AN4 AN — A/D Channel 4.
SS/C2 SS ST — Slave Select input.
OUT C2OUT — CMOS Comparator C2 output.
RA6/ RA6 TTL CMOS General purpose I/O.
OSC2 OSC2 — XTAL Crystal/Resonator.
/CLK CLKO
OUT — CMOS FOSC/4 output.
UT
RA7/ RA7 TTL CMOS General purpose I/O.
OSC1 OSC1 XTAL — Crystal/Resonator.
/CLKI External clock input/RC oscillator
N CLKIN ST —
connection.

13
MKT3811: Microprocessors and Programming

General purpose I/O. Individually


RB0/ RB0 TTL CMOS controlled interrupt-on-change.
AN12 Individually enabled pull-up.
/INT AN12 AN — A/D Channel 12.
INT ST — External interrupt.
General purpose I/O. Individually
RB1/ RB1 TTL CMOS controlled interrupt-on-change.
AN10 Individually enabled pull-up.
/C12I AN10 AN — A/D Channel 10.
N3- C12IN3
AN — Comparator C1 or C2 negative input.
-
General purpose I/O. Individually
RB2/ RB2 TTL CMOS controlled interrupt-on-change.
AN8 Individually enabled pull-up.
AN8 AN — A/D Channel 8.
General purpose I/O. Individually
RB3 TTL CMOS controlled interrupt-on-change.
RB3/ Individually enabled pull-up.
AN9/
AN9 AN — A/D Channel 9.
PGM/
C12I Low-voltage ICSP™ Programming
PGM ST —
N2- enable pin.
C12IN2
AN — Comparator C1 or C2 negative input.
-
General purpose I/O. Individually
RB4/ RB4 TTL CMOS controlled interrupt-on-change.
AN11 Individually enabled pull-up.
AN11 AN — A/D Channel 11.
General purpose I/O. Individually
RB5/ RB5 TTL CMOS controlled interrupt-on-change.
AN13 Individually enabled pull-up.
/T1G AN13 AN — A/D Channel 13.
T1G ST — Timer1 Gate input.
General purpose I/O. Individually
RB6/I RB6 TTL CMOS controlled interrupt-on-change.
CSPC Individually enabled pull-up.
LK ICSPC
ST — Serial Programming Clock.
LK
RB7/I General purpose I/O. Individually
CSPD RB7 TTL CMOS controlled interrupt-on-change.
AT Individually enabled pull-up.

14
MKT3811: Microprocessors and Programming

ICSPD
ST TTL ICSP™ Data I/O.
AT
RC0/ RC0 ST CMOS General purpose I/O.
T1OS
T1OSO — XTAL Timer1 oscillator output.
O/T1
CKI T1CKI ST — Timer1 clock input.
RC1/ RC1 ST CMOS General purpose I/O.
T1OS
T1OSI XTAL — Timer1 oscillator input.
I/CCP
2 CCP2 ST CMOS Capture/Compare/PWM2.

RC2/P RC2 ST CMOS General purpose I/O.


1A/C P1A ST CMOS PWM output.
CP1 CCP1 — CMOS Capture/Compare/PWM1.

RC3/S RC3 ST CMOS General purpose I/O.


CK/S SCK ST CMOS SPI clock.
CL SCL ST OD I2C™ clock.

RC4/S RC4 ST CMOS General purpose I/O.


DI/SD SDI ST — SPI data input.
A SDA ST OD I2C data input/output.
RC5/S RC5 ST CMOS General purpose I/O.
DO SDO — CMOS SPI data output.

RC6/ RC6 ST CMOS General purpose I/O.


TX/C TX — CMOS EUSART asynchronous transmit.
K CK ST CMOS EUSART synchronous clock.

RC7/ RC7 ST CMOS General purpose I/O.


RX/D RX ST — EUSART asynchronous input.
T DT ST CMOS EUSART synchronous data.
RD0 RD0 TTL CMOS General purpose I/O.
RD1 RD1 TTL CMOS General purpose I/O.
RD2 RD2 TTL CMOS General purpose I/O.
RD3 RD3 TTL CMOS General purpose I/O.
RD4 RD4 TTL CMOS General purpose I/O.
RD5/ RD5 TTL CMOS General purpose I/O.
P1B P1B — CMOS PWM output.
RD6/ RD6 TTL CMOS General purpose I/O.
P1C P1C — CMOS PWM output.
RD7/ RD7 TTL CMOS General purpose I/O.

15
MKT3811: Microprocessors and Programming

P1D P1D AN — PWM output.


RE0/ RE0 TTL CMOS General purpose I/O.
AN5 AN5 AN — A/D Channel 5.
RE1/ RE1 ST CMOS General purpose I/O.
AN6 AN6 AN — A/D Channel 6.
RE2/ RE2 TTL CMOS General purpose I/O.
AN7 AN7 AN — A/D Channel 7.
RE3/ RE3 TTL — General purpose input.
MCLR MCLR ST — Master Clear with internal pull-up.
/VPP VPP HV — Programming voltage.
VSS VSS Power — Ground reference.
VDD VDD Power — Positive supply.

Legend:

AN = Analog input or output CMOS = CMOS compatible input or output

TTL = TTL compatible input ST = Schmitt trigger input with CMOS levels

HV = High voltage XTAL = Crystal

OD = Open drain

In any experiment utilizing the PIC16F887, certain


fundamental connections must be established. These
connections can be classified as supply, oscillator and reset
connections. Supply leads for PIC16F887; VSS (negative feed)
is applied to one of pins 12 or 31, VDD (positive feed) is applied
to one of pins 11 or 32. Supply voltages can be between 2.0 V -
5.5 V. The PIC16F887 generates the clock signal using an
internal or external oscillator. The frequency value can be
increased up to 8 MHz in the use of internal oscillator and up to
20 MHz in the use of external oscillator. The PIC uses the clock
signal to process the commands in its memory and It should be
noted the instruction clock is 1/4 of the oscillator frequency.

16
MKT3811: Microprocessors and Programming

The oscillator pins for the PIC16F887 are pins 13 (OSC1) and
14 (OSC2). In order to obtain the appropriate oscillator
frequency, 22 pF capacitors can be connected to pins 13 and 14.
The reset connection is used to prevent the microcontroller from
resetting itself. Pin 1 of PIC16F887 is active in logic 0 state.
Therefore, as long as this pin is logic – 0, the microcontroller
will reset itself. Connecting this pin to 5 V with a 1 kΩ resistor
will bring the pin to the logic - 1 level, that is, it will prevent the
microcontroller from resetting itself. The three basic
connections mentioned are shown in Figure 10.

Figure 10: PIC16F887 Basic Connections

17
MKT3811: Microprocessors and Programming

The amount of current drawn by a circuit element connected


to a microcontroller is important to consider because exceeding
the maximum current rating of the microcontroller can cause
permanent damage to the microcontroller and potentially other
components in the circuit. Each input/output (I/O) pin of a
microcontroller has a maximum current rating, and when a
circuit element connected to an I/O pin draws more current than
the maximum rating, the excess current can cause the pin to
overheat, resulting in permanent damage to the microcontroller.
To avoid damaging the microcontroller, it is important to limit
the current draw of each circuit element, either by choosing
components with lower current requirements, adding current
limiting resistors, or using external drivers to handle higher
current loads. The absolute maximum ratings of the PIC18F887
microcontroller are as follows:

• Maximum current out of VSS pin: 300 mA,


• Maximum current into VDD pin: 250 mA,
• Maximum output current sunk by any I/O pin: 25 mA,
• Maximum output current sourced by any I/O pin: 25
mA,
• Maximum current sunk by PORTA, PORTB and
PORTE (combined): 200 mA,
• Maximum current sourced by PORTA, PORTB and
PORTE (combined): 200 mA,
• Maximum current sunk by PORTC and PORTD
(combined): 200 mA,
• Maximum current sourced by PORTC and PORTD
(combined): 200 mA.

18
MKT3811: Microprocessors and Programming

1.2.2. Fixed Resistors


Resistors are passive electrical components that create
resistance in the flow of electric current. They can be found in
most electrical networks and electronic circuits. Resistance is
measured in ohms (Ω). An ohm is the value that occurs when
one ampere (A) of current flows through a resistor with a one
volt (V) drop across its terminals. The current is proportional to
the voltage across the terminals. This ratio is represented by
Ohm's law:
𝑉
𝑅=
𝐼
The accepted symbol of resistance in Europe is a
rectangle as shown in Figure 11 (left). In Figure 11 (right), a
fixed resistance, which is accepted in the USA and represented
as a zigzag line, is given.

Figure 11: European representation of resistance (left), US representation


of resistance (right)

The value of a resistor can be determined with the help


of a multimeter, numerical data written on the resistor, or color
bands. The numerical equivalents of the color codes on the
resistor are given in Table 6, and the reading of the color bands
for an example resistor is given in Figure 12.

19
MKT3811: Microprocessors and Programming

Table 6: Color codes of resistors

Color Number Multiplier Tolerance


Black 0 100 -
Brown 1 101 %1
2
Red 2 10 %2
Orange 3 103 -
Yellow 4 104 -
5
Green 5 10 % 0.5
Blue 6 106 % 0.25
Purple 7 107 % 0.1
Grey 8 108 % 0.05
White 9 109 -
−1
Gold - 10 %5

Silver - 10−2 % 10

Figure 12: Reading a 4-tape resistor

20
MKT3811: Microprocessors and Programming

Resistors are connected to electronic circuits in series


or parallel according to their intended use. In line with these
connection types, the equivalent resistance calculations are as
follows. The series-parallel connection of two 560 kΩ resistors
and their calculation of the equivalent resistance are shown in
Figure 13.

Resistors in Series Resistors in Parallel


R1
R1 R2

R2
−1
1 1
𝑅𝑒𝑞 = 𝑅1 + 𝑅2 + ⋯ 𝑅𝑒𝑞 = ( + + ⋯)
𝑅1 𝑅2

Figure 13: Connecting resistors in series (left) and parallel (right)

21
MKT3811: Microprocessors and Programming

1.2.3. Variable Resistors


Variable resistors refer to components that can change
their resistance values through various interventions. Examples
of such components include potentiometers, rheostats, and
trimpots. A potentiometer, specifically, is a three-terminal
variable resistor that can manually adjust the resistance to
control current flow and act as an adjustable voltage divider.
The connection diagram of the potentiometer is given in Figure
14.

Figure 14: Connection diagram of potentiometer

1.2.4. Capacitors
Capacitors are passive electronic components
consisting of two or more conductive materials separated by an
insulating material. A capacitor, like a small rechargeable
battery, is an element that creates a potential difference between
its plates and has the capacity to store energy in the form of an
electric charge. There are many different types of capacitors,
from very small capacitor beads used in resonant circuits to
large power factor correction capacitors. However, they are

22
MKT3811: Microprocessors and Programming

basically all for charge storage purposes. Capacitors can be


filtered, rectified, depending on their properties such as storing
electrical energy, discharging the stored energy through the
circuit in a fast way in case of a short circuit, preventing direct
current from flowing in circuits and allowing alternating current
to pass, and causing a difference between the phases of input-
output signals. They are frequently used in electronic circuits
for many purposes such as isolation and energy storage.
Capacitors are divided into polarized and non-polarized. Figure
15 provides a demonstration of polarized and non-polarized
capacitors.

Figure 15: Non-polarized (left) and polarized (right) capacitor symbols

In theoretical calculations and circuit diagrams, the


value of the capacitors denoted by the letter C is expressed in
farad (F) unit and defined as 1 F, 1 C/V. This unit is called
capacitance because it indicates the capacity of the capacitor to
store electrical charges. Therefore, the charge (Q) on the
capacitor is directly proportional to the voltage and capacitance
value of that capacitor:
𝑄=𝐶𝑥𝑉
Depending on their intended use, capacitors are
connected to electronic circuits in series or parallel (Figure 16).
Equivalent capacitance calculations are given below in line with
these connection ways.

23
MKT3811: Microprocessors and Programming

Series Connected Capacitors Parallel Connected Capacitors


...
C1 C2 Cn
...
C1 C2 Cn
...
An equal amount of charge falls The voltage value across each
on each capacitor. capacitor is equal.
𝑄1 = 𝑄2 = ⋯ = 𝑄𝑛 𝑉1 = 𝑉2 = ⋯ = 𝑉𝑛
The equivalent capacitance of The equivalent capacitance of
the circuit is calculated by the the circuit is calculated by the
following equation. following equation.
1 1 1 −1
𝐶𝑒𝑞 = ( + + ⋯ + ) 𝐶𝑒𝑞 = 𝐶1 + 𝐶2 + ⋯ + 𝐶𝑛
𝐶1 𝐶2 𝐶𝑛

Figure 16: Series (left) and parallel (right) connection of capacitors

24
MKT3811: Microprocessors and Programming

1.2.5. LED
A light-emitting diode (LED) is a semiconductor device
that emits light when an electric current is passed through it.
Depending on the semiconductor material used and the amount
of doping, an LED will emit a colored light of a specific spectral
wavelength. In addition, LEDs allow current to flow in the
forward direction and block the current in the reverse direction.
The LED symbol is given in Figure 17.

Figure 17: LED symbol

The LED is powered by a simple LED circuit with a


series current-limiting resistor. Since LEDs have different
characteristics according to their colors, different limiting
resistors should be selected according to the LED color. A list
of limiting resistors calculated according to LED color and
supply voltage is given in Table 7. Also, the minimum
resistance value that can be employed can be expressed simply
as:

𝑆𝑢𝑝𝑝𝑙𝑦 𝑣𝑜𝑙𝑡𝑎𝑔𝑒 − 𝐶𝑎𝑡𝑎𝑙𝑜𝑔 𝑣𝑜𝑙𝑡𝑎𝑔𝑒 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 𝐿𝐸𝐷


𝑀𝑖𝑛𝑖𝑚𝑢𝑚 𝑟𝑒𝑠𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑣𝑎𝑙𝑢𝑒 =
𝐶𝑎𝑡𝑎𝑙𝑜𝑔 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 𝐿𝐸𝐷

25
MKT3811: Microprocessors and Programming

Table 7: Calculated limiting resistors based on LED color and supply


voltage

Supply Forward Desired Calculated Rounded


Voltage Color Voltage Current Resistance Resistance
(V) (V) (mA) (Ω) (Ω)
Red,
Yellow or
3 1.8 25 48 51
Yellow-
Green
Blue,
Green,
4.5 3.3 25 48 51
White or
UV
Blue,
Green,
5 3.3 25 68 68
White or
UV
Red,
Yellow or
5 1.8 25 128 150
Yellow-
Green

The circuit diagram to be established for a red LED


driven with 5V supply voltage is given in Figure 18.

26
MKT3811: Microprocessors and Programming

Figure 18: LED wiring diagram

1.2.6. Button
A button is a mechanical device used to control (close
– open) the electrical circuit. Alternatively, it can provide an
input for the user interface of an equipment or initiate/terminate
a specific function. They can be manufactured in a variety of
shapes, sizes and configurations depending on design
requirements. Some button types that are frequently used in the
market are given in Figure 19.

27
MKT3811: Microprocessors and Programming

Figure 19: Some types of buttons

Digital logic circuits have three logic states: logic – 1,


logic – 0, and high impedance. A high impedance condition
occurs when the pin is not pulled to a logic high or low level but
is instead left floating. A good example of this is a
microcontroller input pin that is not connected; such a pin is
neither high nor low. Therefore, the microcontroller may
interpret the input value as either logic high or logic low
unpredictably. This dilemma can be solved with the help of
resistors called pull-up and pull-down. The pull-down
connection will bring the digital input pin to the logic - 1 level
if the button is pressed, whereas the pull - up connection will
pull the digital input pin to the logic - 0 level if the button is
pressed. The connections are as shown in Figure 20.

28
MKT3811: Microprocessors and Programming

Figure 20: Pull-up (left) and pull-down (right) connections

1.2.7. Crystal
Crystal oscillators work on the principle of the inverse
piezoelectric effect. Although other substances such as
Rochelle salt and tourmaline exhibit a piezoelectric effect, these
oscillators are usually made of quartz crystal because quartz is
inexpensive, naturally occurring, and mechanically strong
compared to others. The crystal in crystal oscillators is precisely
cut and positioned between two metallic plates, as illustrated in
Figure 21a. In reality, the crystal behaves like a series RLC
circuit (Figure 21b) formed by the resistor, capacitor, and coil
components.

29
MKT3811: Microprocessors and Programming

Figure 21: (a) Crystal representation, (b) Crystal equivalent circuit

Microcontrollers require a clock signal to execute the


commands stored in their memory. The crystals can be used for
the generation of the clock signal. In this case, they are used
with lentil capacitors, as given in Figure 22.

Figure 22: The crystal circuit used to generate the clock signal

30
MKT3811: Microprocessors and Programming

1.2.8. Keypad
The keypad serves as an input device to read and
interpret the user's keystrokes. The 4x4 keypad consists of 4
rows and 4 columns. Keys are placed between rows and
columns. When a key is pressed, a connection is established
between the corresponding row and column. To read the value
of the key, the columns are pulled to the logic -1 level. Once the
corresponding column is powered, the rows are read similar to
a button. This allows for the determination of which row was
pressed in the given column. A diagram illustrating the
connection for the 4x4 keypad is provided in Figure 23.

Figure 23: 4x4 keypad wiring diagram

31
MKT3811: Microprocessors and Programming

1.2.9. LCD Screen


LCD (Liquid Crystal Display) is a type of flat panel
display that uses liquid crystals in its primary mode of
operation. LCDs consume significantly less power than LED
and gas screen displays because they operate on the principle of
blocking light rather than diffusing it. In LCDs, an image is
produced using backlighting while LED displays emit light.
The most common commercially available LCDs are 1-
line, 2-line, or 4-line LCDs that have only 1 controller and
support up to 80 characters. Most 1-controller LCDs typically
have 14 pins, while 2-controller LCDs have 16 pins (both have
two extra pins for backlight LED connections). Table 8
provides pin descriptions for the LM016L model LCD, which
consists of 2 rows and 16 columns, and Figure 24 provides the
pin configuration.

32
MKT3811: Microprocessors and Programming

Table 8: LM016 pin descriptions

Pin Number Pin Name Description

1 VSS Ground reference (GND)

2 VDD (VCC) Supply (+5V)

3 V0 (VEE) Contrast adjustment

4 RS 0 = Command input, 1 = Data input

0 = write to LCD module, 1 = read


5 RW
from LCD module

6 E (EN) Enable signal

7 D0 Bus line 0

8 D1 Bus line 1

9 D2 Bus line 2

10 D3 Bus line 3

11 D4 Bus line 4

12 D5 Bus line 5

13 D6 Bus line 6

14 D7 Bus line 7

15 A LCD backlight – Anode

16 K LCD backlight – Cathode

33
MKT3811: Microprocessors and Programming

Figure 24: LCD pin configuration

The anode and cathode pins of the LM016L LCD are


connected to the backlight. LED is used for backlighting in
LCD. To prevent the LED from burning out and to limit the
current, it is advisable to insert a 330 Ω resistor between the
cathode pin and the ground line. Figure 25 demonstrates this
setup.

Figure 25: LCD anode and cathode connection

34
MKT3811: Microprocessors and Programming

1.2.10. 7 – Segment Display


A 7-segment display is a type of display that comprises
seven LEDs arranged in a rectangular shape. Each LED in the
display is referred to as a segment as it forms a part of a numeric
digit, either decimal or hexadecimal, that is displayed when
illuminated. Sometimes an additional 8th LED is used within the
same package, allowing a decimal point to be displayed when
two or more 7-segment displays are connected together to
display numbers greater than nine.
Labels a through g are assigned to each of the seven
LEDs on the display. The LED pins are connected together and
wired with a common pin. The type of the 7-segment display is
determined based on the common pin connection, which can
either be a common cathode or a common anode. In the
common cathode type, all the cathode connections of the LED
segments are grounded. Individual segments are illuminated by
applying a logic-1 signal to individual anode terminals (a–g). In
the common anode type, all the anode connections of the LED
segments are combined with logic – 1. Individual segments are
illuminated by applying a logic 0 signal to individual cathode
terminals (a–g). The structures of these two different types of
segments are depicted in Figure 26.

Figure 26: Common cathode (left) and common anode (right) 7 – segment

35
MKT3811: Microprocessors and Programming

In the experiment, 7-segment common cathode type


was preferred. The connection diagram for the common cathode
segment can be found in Figure 27, where the digital pins of the
microcontroller are represented by pins B0-B6. It is important
to note that current limiting resistors are necessary for each
segment since they are essentially LEDs. To limit the current
flowing through the segment, 330 Ω resistors are recommended.

Figure 27: Common cathode 7 – segment wiring diagram

The connection diagram given in Figure 27 was created


using Proteus software. The real-life structure of the common
cathode 7 - segment in the library is shown in Figure 28.

36
MKT3811: Microprocessors and Programming

Figure 28: Real life structure (left) of common cathode 7 - segment found in
the Proteus library (right)

1.3. Basic Circuit Building Information


The breadboard is a common tool for temporarily
connecting circuit elements without soldering. During the
prototype and testing phases, it is primarily utilized for circuit
design. It composed of interconnected lines. The red and blue
lines on the right and left sides of the board are voltage lines. A
positive (power) connection is made on the red line, and a
negative (ground) connection is made on the blue line. Other
components of the circuit can draw power from these
conductors. The breadboard's middle section consists of
vertically interconnected 5-hole groups, whereas the horizontal
groups are not interconnected. Figure 29 depicts the connected
and unconnected lines on a breadboard, with the top two
interconnected horizontal lines serving as the power input lines.
Notice that this power input line is cut in the middle and there
is no connection between the two lines.

37
MKT3811: Microprocessors and Programming

Figure 29: Connected and unconnected lines on the breadboard

38
MKT3811: Microprocessors and Programming

In Figure 30, it is seen that a sample capacitor is


connected to the circuit correctly and incorrectly. In the
incorrect connection (on the right), both legs of the capacitor
are on the vertical line, resulting in short circuit. In the correct
connection (on the left), the legs of the capacitor are placed on
separate lines.

Figure 30: Correct (left) and incorrect (right) connection of a capacitor to


the circuit

To use multi-pin devices without creating a short


circuit, the breadboard's center can be utilized. For instance, the
integrated circuits and buttons can be connected to the
breadboard as shown in Figure 31. In this case, the legs of the
multi-pin devices on both sides will be on separate lines.

Figure 31: Breadboard connection of multi-pin circuit elements

39
MKT3811: Microprocessors and Programming

Male-to-male jumper cables can be utilized to connect


breadboard components. USB alligator cable delivering 5 V can
be preferred for breadboard power supply. In Figure 32, the
associated cables are depicted visually.

Figure 32: Male to male jumper cable (top) and USB crocodile cable
(bottom)

Figure 33 depicts a simple LED on/off circuit


comprised of wires and power sources. As previously
mentioned, this circuit can be constructed using jumper cables
for component-to-component connections and USB alligator
cable for power supply.

40
MKT3811: Microprocessors and Programming

Figure 33: Not pressed (top) and pressed (bottom) conditions for LED on-off
circuit

41
MKT3811: Microprocessors and Programming

1.4. Used Programs


1.4.1. CCS C
CCS C is a compiler software developed by Custom
Computer Services Inc. This program facilitates the
programming of Microchip PIC controllers using the C
programming language. Figure 34 depicts the principal toolbar
of the software.

Figure 34: The main toolbar of the CCS C program

To initiate a new study in the CCS C program, the user


must specify the filename and location after completing the
steps depicted in Figure 35, and then click the Save button.

Figure 35: Starting new project in CCS C program

42
MKT3811: Microprocessors and Programming

Once the writing the code on the active page have been
finalized, it can be can be compiled using the Compile option,
as shown in Figure 36.

Figure 36: Coding interface and compilation of code in CCS C program

Figure 37 depicts the result of the compilation. A "Build


Successful" notification within this box confirms that the code
has been compiled successfully. Additionally, the directory
where the output files have been generated is provided in the
same dialogue box.

Figure 37: Warning box resulting from compilation

43
MKT3811: Microprocessors and Programming

CCS C code produces various outputs, which are all listed in


Figure 38. This book utilizes only two of these files: the file
with the “.hex” extension in the upper left corner and the file
with the “.c” extension in the lower left corner. The user-written
programs in the C language are stored in files with the extension
".c" and it can be opened with the CCS C program for
reorganization of the code. C program code is transformed into
a hexadecimal object file format, which is intelligible to the
microprocessor, and saved in files with the extension ".hex".

Figure 38: File output resulting from compilation

1.4.2. MPLAB IDE


MPLAB IDE is a special free integrated development
environment for the development of embedded applications on
PIC and dsPIC microcontrollers and has been developed by
Microchip Technology. MPLAB IDE enables PIC controllers
from Microchip to be programmed in different languages such
as C or assembly. Figure 39 shows the main toolbar of the
program.

44
MKT3811: Microprocessors and Programming

Figure 39: The main toolbar of the MPLAB IDE program

In order to start a new study in the MPLAB IDE


program, the steps given in Figure 40-45 must be followed.

Figure 40: Starting a new sketch in MPLAB IDE – Step 1

Figure 41: Starting a new sketch in MPLAB IDE – Step 2

45
MKT3811: Microprocessors and Programming

Figure 42: Starting a new sketch in MPLAB IDE – Step 3

Figure 43: Starting a new sketch in MPLAB IDE – Step 4

46
MKT3811: Microprocessors and Programming

Figure 44: Starting a new sketch in MPLAB IDE – Step 5

Figure 45: Starting a new sketch in MPLAB IDE – Step 6

47
MKT3811: Microprocessors and Programming

Within the book, the MPLAB IDE will be used to write


assembly code. Therefore, after performing the operations
given in Figure 46 under the Projects box, a new assembly
window should be opened by performing the operations given
in Figure 47.

Figure 46: Starting assembly sketch in MPLAB IDE – Step 1

48
MKT3811: Microprocessors and Programming

Figure 47: Starting assembly sketch in MPLAB IDE – Step 2

When the codes written on the opened page are


completed, the codes can be compiled by pressing the Clean
and Build Main Project button as shown in Figure 48.

Figure 48: Compiling the code in MPLAB IDE

49
MKT3811: Microprocessors and Programming

Figure 49 gives the box showing the compilation result.


While the Build Successful warning in the opened box indicates
that the code was compiled successfully, the directory where the
output of the code with .hex extension was created is also
included.

Figure 49: Warning box resulting from compilation

1.4.3. MPLAB IPE


MPLAB IPE is a software application that provides a
simple interface for embedding a .hex file created with the help
of different compilers into the microcontroller. In order for this
software to communicate with the microcontroller,
programmers called PICkit are used. PICkit is a programmer
family for PIC microcontrollers made by Microchip
Technology. They can be used to program microcontrollers,
debug or program EEPROM. Figure 50 shows the PICkit3
programmer and its components. At the top is the adapter called
zif where the microcontroller is placed. In order to start the
programming process, the microcontroller is placed in the
adapter, the necessary connections are made and the PICkit3 is
connected to the computer with the help of a USB cable.

50
MKT3811: Microprocessors and Programming

Figure 50: PICkit3 programmer and components

Figure 51 displays the login screen of MPLAB IPE


when the PICkit3 is connected to the computer via a USB cable.
To initiate the programming process, the Connect button should
be pressed after the selections illustrated in Figure 51 have been
made. At this point, the warning on the screen is closed by
selecting OK button.

51
MKT3811: Microprocessors and Programming

Figure 51: MPLAB IPE start screen

The warning box of MPLAB IPE is shown in Figure


52. After the warning about the VDD voltage is received, the
steps between Figure 53 and Figure 56 should be followed to
set the VDD voltage.

52
MKT3811: Microprocessors and Programming

Figure 52: MPLAB IPE warning box

Figure 53: Adjusting VDD voltage – Step 1

Figure 54: Adjusting VDD voltage – Step 2

53
MKT3811: Microprocessors and Programming

Figure 55: Adjusting VDD voltage – Step 3

Figure 56: Adjusting VDD voltage – Step 4

54
MKT3811: Microprocessors and Programming

After the VDD voltage is adjusted and the Operate


button is pressed, the .hex file to be embedded in the
microcontroller should be selected as given in Figure 57 and the
Program button should be pressed. At this time, the warning on
the screen is closed with the OK button.

Figure 57: Embedding the .hex file into the microcontroller

At this stage, an error may be received as given in


Figure 58. In such a case, VDD voltage should be readjusted as
given in Figure 59. VDD voltage is set to 4.5 volts as indicated
in the error.

55
MKT3811: Microprocessors and Programming

Figure 58: Error occurred while sending the .hex file to the microcontroller

56
MKT3811: Microprocessors and Programming

Figure 59: Readjustment of VDD voltage

After re-adjusting the VDD voltage, the .hex file can be


reloaded by returning to the programming interface with the
Operate button. If the result of the installation is Programming
Complete, it means that the code is embedded in the
microcontroller without error. The warning box after the
download is given in Figure 60.

57
MKT3811: Microprocessors and Programming

Figure 60: MPLAB IPE warning box

1.4.4. Proteus ISIS


Proteus is a software developed by Labcenter
Electronics for simulation, design and drawing of electronic
circuits. There are numerous benefits to simulating circuits on
Proteus before physically building them. One of the significant
benefits is that designing circuits on Proteus takes less time
compared to the practical construction of circuits. Additionally,
identifying and rectifying connection issues in a practical circuit
can be a time-consuming process, whereas in simulation, errors
can be quickly identified and corrected. ISIS and ARES are the
two primary components of Proteus. ARES is used for
designing the printed circuit board (PCB) layout of circuits,
while ISIS is used for designing, drawing, and simulating
various circuits. Simulation-wise, the book focuses on the ISIS
component primarily. Figure 61 depicts the primary toolbox on
the Proteus ISIS homepage.

Figure 61: Proteus main toolbox

58
MKT3811: Microprocessors and Programming

Steps between Figure 62 and Figure 67 must be


performed to create a new project in Proteus ISIS.

Figure 62: Creating a new project in Proteus ISIS – Step 1

Figure 63: Creating a new project in Proteus ISIS – Step 2

59
MKT3811: Microprocessors and Programming

Figure 64: Creating a new project in Proteus ISIS – Step 3

Figure 65: Creating a new project in Proteus ISIS – Step 4

60
MKT3811: Microprocessors and Programming

Figure 66: Creating a new project in Proteus ISIS – Step 5

Figure 67: Creating a new project in Proteus ISIS – Step 6

61
MKT3811: Microprocessors and Programming

After the project is created, the component tree is


displayed on the left-hand side of the screen. The Component
Mode segment of the component tree is depicted in Figure 68.
This section allows the selection of circuit elements to be
utilized for the project.

Figure 68: Component Mode tab

Figure 69 shows the Terminals Mode part in the


component tree. Terminal elements such as ground, input and
output can be selected from this section.

Figure 69: Terminals Mode tab

62
MKT3811: Microprocessors and Programming

Figure 70 shows the Generator Mode part in the


component tree. Various power sources such as DC, sine and
pulse can be selected from this section.

Figure 70: Generator Mode tab

Figure 71 shows the Virtual Instruments Mode part in


the component tree. Various measuring instruments such as
oscilloscope and multimeter can be selected from this section.

Figure 71: Virtual Instruments Mode tab

63
MKT3811: Microprocessors and Programming

In Figure 72, simulation images of the code, which is


given as an example in the 1.4.1. and 1.4.2. sections, and which
is used to periodically flash the LED connected to the 0th pin of
the B port of the microcontroller, are given.

Figure 72: Proteus simulation of LED on (bottom) – off (top) code

64
MKT3811: Microprocessors and Programming

1.5. Equivalent Microcontrollers


1.5.1. Differences Between 16F887 and 16F877A
The 16F887 is a more up-to-date processor than the
16F877A, but it is pin compatible with previous versions. Some
advantages of 16F887 over 16F877A are as follows;
• Added internal oscillator (8MHz)
• The number of I/O pins has increased by 3
• Number of analogue channels increased from 8 to 14
For more detailed information, the document “PIC16F87XA to
PIC16F88X Migration” on the microchip website can be
examined.

1.5.2. Differences Between 16F877 and 16F877A


16F877A is the advanced version of the 16F877.
Although they have some advantages in terms of memory and
hardware, they are not different processors in practice. Major
improvements are as follows;
The 16F877A is considered to be an updated iteration
of the 16F877 microcontroller. They are essentially similar
processors in practice. The significant improvements of the
16F877A over the 16F877 are as follows:
• EEPROM access speed and lifetime are higher
• The number of analog comparators has increased by 1
• Added ICSP (serial programming on the circuit)
support

65
MKT3811: Microprocessors and Programming

• The minimum operating voltage decreased by 5mV to


3.65V
For more detailed information, the document
“PIC16F87X → PIC16F87XA Migration” on the microchip
website can be examined. The visuals of the relevant models are
given in Figure 73.

Figure 73: Models 16F877, 16F877A and 16F887

1.5.3. Configuration Settings for Equivalent


Microcontrollers
For the applications covered in this book, the
PIC16F887 microcontroller model has been selected as the
primary option. However, the PIC16F877 and PIC16F877A
models, which share the same pin layout, can also be used.

66
MKT3811: Microprocessors and Programming

Relevant configuration changes that need to be made are


explained in detail in this section.
Unlike other sub-models, the PIC16F887 model can
function with both an external oscillator and an internal
oscillator up to 8 MHz. Figure 74 displays Proteus diagrams of
models PIC16F887 and PIC16F877A for a simple LED on-off
circuit. It can be observed that the two circuits serve the same
function, but the 16F887 model does not require an external
oscillator circuit, whereas the 16F877A model does require one.

Figure 74: Setting up the LED on – off circuit with PIC16F887 (top) and
PIC16F877A (bottom)

67
MKT3811: Microprocessors and Programming

Configuration changes are necessary in both Proteus


and other programs to use microcontroller models other than the
PIC16F887.
Figure 75 illustrates the configuration of CCS C code
used to flash an LED connected to the B0 pin of the PIC16F887
and PIC16F877A microcontrollers every 500 milliseconds.
Using the OSCCON command, the internal oscillator frequency
of the 16F887 model can be modified. The internal oscillator
will be set to the default value of 4 MHz if this command is
omitted. However, because the 16F877A model lacks an
internal oscillator, its external oscillator must be defined in the
software using the #fuses XT command. External oscillators are
preferred over internal oscillators in 16F887 circuits to ensure
that the book's applications can be run on equivalent
microcontrollers. If an internal oscillator is desired to be utilized
in CCS C applications, the changes illustrated in Figure 75
should be implemented.

68
MKT3811: Microprocessors and Programming

Figure 75: Writing the LED on – off code for PIC16F887 (top) and
PIC16F877A (bottom)

In the book, as in CCS C applications, an external


oscillator is preferred in assembly applications. However,
Experiment 1 demonstrated the use of both internal and external
oscillators with the PIC16F887 microcontroller. Therefore, if an
internal oscillator is desired for assembly applications, the
configuration changes made in Experiment 1 can be used as a
reference. The internal oscillator frequency of the 16F887
model can be adjusted using OSCCON register in assembly
language, similar to C language. If this command is not written,
the internal oscillator will default to 4 MHz. In addition, if
equivalent microcontrollers using external oscillator circuit
such as 16F877A are to be preferred in assembly applications,
the configuration settings in Figure 76 can be used.

69
MKT3811: Microprocessors and Programming

Figure 76: Configuration settings for the 16F877A assembly application

To use equivalent microcontrollers, it is necessary to


apply the configuration changes shown in Figure 77 and Figure
78 to MPLAB IDE and MPLAB IPE, respectively.
Furthermore, the CONFIG section should be modified
depending on the oscillator type (external/internal) used in
MPLAB IDE. Experiment 1 and Figure 76 provide a clear
example of this modification.

Figure 77: MPLAB IDE configuration change to use equivalent models

70
MKT3811: Microprocessors and Programming

Figure 78: MPLAB IPE configuration change to use equivalent models

71
MKT3811: Microprocessors and Programming

1.6. 16F88X Configuration Settings


Configuration settings for the 16F887 microcontroller
are established through registers called config1 and config2. It
is worth noting that the selection of an external or internal
oscillator is determined by these configuration settings.
A thorough understanding of the config1 and config2
bits' significance in the first three experiments' assembly codes
can be gained by reviewing the subsequent section.

1.6.1. CONFIG1: Configuration Word Register 1

bit 15-14 Read as '1'

bit 13 ̅̅̅̅̅̅̅̅̅̅
𝐃𝐄𝐁𝐔𝐆: In-Circuit Debugger Mode bit
1 = In-Circuit Debugger disabled,
RB6/ICSPCLK and RB7/ICSPDAT are
general purpose I/O pins
0 = In-Circuit Debugger enabled,
RB6/ICSPCLK and RB7/ICSPDAT are
dedicated to the debugger
bit 12 LVP: Low Voltage Programming Enable bit
1 = RB3/PGM pin has PGM function, low
voltage programming enabled
0 = RB3 pin is digital I/O, HV on ̅̅̅̅̅̅̅̅
MCLR must
be used for programming

72
MKT3811: Microprocessors and Programming

bit 11 FCMEN: Fail-Safe Clock Monitor Enabled


bit
1= Fail-Safe Clock Monitor is enabled
0 = Fail-Safe Clock Monitor is disabled
bit 10 IESO: Internal External Switchover bit
1 = Internal/External Switchover mode is
enabled
0 = Internal/External Switchover mode is
disabled

bit 9-8 BOREN1:0: Brown-out Reset Selection bits


11 = BOR enabled
10 = BOR enabled during operation and
disabled in Sleep
01 = BOR controlled by SBOREN bit of the
PCON register
00 = BOR disabled
bit 7 ̅̅̅̅̅̅
𝐂𝐏𝐃: Data Code Protection bit
1 = Data memory code protection is disabled
0 = Data memory code protection is enabled

bit 6 ̅̅̅̅: Code Protection bit


𝐂𝐏
1 = Program memory code protection is
disabled

73
MKT3811: Microprocessors and Programming

0 = Program memory code protection is


enabled

bit 5 ̅̅̅̅̅̅̅̅ pin function select bit


MCLRE: RE3/MCLR
̅̅̅̅̅̅̅̅ pin function is MCLR
1 = RE3/MCLR ̅̅̅̅̅̅̅̅

̅̅̅̅̅̅̅̅ pin function is digital input,


0 = RE3/MCLR
̅̅̅̅̅̅̅̅ internally tied to VDD
MCLR

bit 4 ̅̅̅̅̅̅̅̅̅̅̅: Power-up Timer Enable bit


𝐏𝐖𝐑𝐓𝐄
1 = PWRT disabled
0 = PWRT enabled
bit 3 WDTE: Watchdog Timer Enable bit
1 = WDT enabled
0 = WDT disabled and can be enabled by
SWDTEN bit of the WDTCON register

74
MKT3811: Microprocessors and Programming

bit 2-0 FOSC2:0: Oscillator Selection bits


111 = RC oscillator: CLKOUT function on
RA6/OSC2/CLKOUT pin, RC on
RA7/OSC1/CLKIN

110 = RCIO oscillator: I/O function on


RA6/OSC2/CLKOUT pin, RC on
RA7/OSC1/CLKIN
101 = INTOSC oscillator: CLKOUT function
on RA6/OSC2/CLKOUT pin, I/O function on
RA7/OSC1/CLKIN

100 = INTOSCIO oscillator: I/O function on


RA6/OSC2/CLKOUT pin, I/O function on
RA7/OSC1/CLKIN
011 = EC: I/O function on
RA6/OSC2/CLKOUT pin, CLKIN on
RA7/OSC1/CLKIN
010 = HS oscillator: High-speed
crystal/resonator on RA6/OSC2/CLKOUT and
RA7/OSC1/CLKIN
001 = XT oscillator: Crystal/resonator on
RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN

000 = LP oscillator: Low-power crystal on


RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN

75
MKT3811: Microprocessors and Programming

1.6.2. CONFIG2: Configuration Word Register 2

bit 15-11 Read as '1'

bit 10-9 WRT1:0: Flash Program Memory Self


Write Enable bits
00 = 0000h to 0FFFh write protected, 1000h
to 1FFFh may be modified by EECON control
01 = 0000h to 07FFh write protected, 0800h
to 1FFFh may be modified by EECON control
10 = 0000h to 00FFh write protected, 0100h
to 1FFFh may be modified by EECON control
11 = Write protection off
bit 8 BOR4V: Brown-out Reset Selection bit
0 = Brown-out Reset set to 2.1V
1 = Brown-out Reset set to 4.0V
bit 7-0 Read as '1'

76
MKT3811: Microprocessors and Programming

2. A LIST OF MATERIALS
The book features nine assembly and C code
applications that can be implemented using the materials listed
in Table 9.
Table 9: A list of materials

Material Name Number


PIC16F887 2
LM016L LCD Display 1
22 pF Capacitor 4
1 kΩ Resistor 2
10 kΩ Resistor 4
330 Ω Resistor 7
10 kΩ Potentiometer 3
LED 2
Buzzer 1
Button 1
4x4 Keypad 1
Common Cathode 7-Segment 1
4 MHz Crystal 2
5 V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

77
MKT3811: Microprocessors and Programming

3. EXPERIMENTS
3.1. Experiment 1: Digital I/O
3.1.1. Preliminary Study
For the preliminary preparation of the experiment, it is
recommended to review the basic circuit building information
provided in Section 1.3, the microcontroller pin configuration
described in Section 1.2.1, and the necessary connections for
the microcontroller, including the crystal connections outlined
in Section 1.2.7, LED connections and limiting resistor
calculations discussed in Section 1.2.5 as well as the pull-up and
pull-down resistor connections for buttons in section 1.2.6.
Also, please refer the last part of Section 1.2.1 to determine the
amount of the maximum current can be sourced from and sinked
in the PIC18F887.

3.1.2. A list of materials


The list of materials required for the experiment is
presented in Table 10.
Table 10: A list of materials

Material Name Number


PIC16F887 1
1 kΩ Resistor 1
330 Ω Resistor 1
10 kΩ Resistor 1
LED 1
Button 1
22 pF Capacitor (external oscillator circuit) 2
4 MHz Crystal (external oscillator circuit) 1
5 V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

78
MKT3811: Microprocessors and Programming

3.1.3. Purpose of Experiment


The objective of the experiment is to define input-
output pins in assembly language and perform an LED on-off
application utilizing a button. It also includes presenting the
configuration settings for using internal and external oscillators
in the PIC16F887 microcontroller. Therefore, the experiment
was conducted using both the internal oscillator of the
PIC16F887 and an external oscillator.

3.1.4. Experimental Procedure


The experiment can be simulated via the Proteus
software. The Proteus diagram of the experiment is given in
Figure 79. Try the assembly code is as follows:

79
MKT3811: Microprocessors and Programming

LIST p = 16F887
; Controller model introduced
#INCLUDE <p16F887.inc>
; Introduction of MCU library
__config _CONFIG1,b'1110000011100100'
__config _CONFIG2,b'1111100011111111'
; MCU configuration setting
ORG 0x00 ;
Reset vector
BSF STATUS,5
; BANK1 selected
MOVLW B'00000001'
; Binary value assigned to W register
MOVWF TRISD
; Value in W assigned to TRISD register
BCF STATUS,5
; BANK0 selected
MAIN
BTFSC PORTD,0
; Process subcommand if D0=1
; If D0=0 skip subcommand continue from next command
GOTO LEDON
GOTO LEDOFF
LEDON
BSF PORTD,1
; D1 -> Led high
GOTO MAIN
; Endless loop
LEDOFF
BCF PORTD,1
; D1 -> Led low
GOTO MAIN
; Endless loop
END

80
MKT3811: Microprocessors and Programming

Figure 79: Proteus diagram of the circuit

If the circuit is to be physically built, the power


connection of the microcontroller as shown in Figure 80 must
be added in addition to the Proteus diagram.

Figure 80: Additional connections of real-life circuit

The experiment can also be carried out by installing an


external oscillator circuit. In such a case, the circuit diagram in
Proteus will be as shown in Figure 81 and the assembly code
must be configured as follows:

81
MKT3811: Microprocessors and Programming

LIST p = 16F887
; Controller model introduced
#INCLUDE <p16F887.inc>
; Introduction of MCU library

__config _CONFIG1,b'1110000011100001'
__config _CONFIG2,b'1111100011111111'
; MCU configuration setting

ORG 0x00
; Reset vector
BSF STATUS,5
; BANK1 selected
MOVLW B'00000001'
; W = 00000001
MOVWF TRISD
; PortD = 00000001
BCF STATUS,5
; BANK0 selected

MAIN
BTFSC PORTD,0
; Process subcommand if D0=1
; Skip subcommand if D0=0
GOTO LEDON
GOTO LEDOFF

LEDON
BSF PORTD,1
; D1 -> Led high
GOTO MAIN
; Endless loop
LEDOFF
BCF PORTD,1
; D1 -> Led low
GOTO MAIN
; Endless loop

END

82
MKT3811: Microprocessors and Programming

Figure 81: Proteus diagram of the circuit

83
MKT3811: Microprocessors and Programming

To build the circuit in real life, it is essential to establish


the power connection of the microcontroller, as depicted in
Figure 80 in addition to the Proteus diagram.
Notes…………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

84
MKT3811: Microprocessors and Programming

…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

85
MKT3811: Microprocessors and Programming

3.2. Experiment 2: Counter


3.2.1. Preliminary Study
For the preliminary preparation of the experiment, the
basic circuit building information given in Section 1.3, the
microcontroller pin configuration given in Section 1.2.1 and the
basic connections of the microcontroller, the crystal
connections given in Section 1.2.7 and the pull-up and the pull-
ups connections given in Section 1.2.6 can be reviewed.

3.2.2. A list of materials


The materials to be used in the experiment are shown in
Table 11.
Table 11: A list of materials

Material Name Number


PIC16F887 1
1 kΩ Resistor 1
330 Ω Resistor 1
10 kΩ Resistor 1
Buzzer 1
Button 1
22 pF Capacitor 2
4 MHz Crystal 1
5 V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

86
MKT3811: Microprocessors and Programming

3.2.3. Purpose of Experiment


The experiment aims to create delay loops with
assembly instructions.

3.2.4. Experimental Procedure


The experiment can be simulated using Proteus
software. Figure 82 shows the Proteus diagram of the
experiment. The assembly code for the experiment is provided
below:

87
MKT3811: Microprocessors and Programming

LIST P=16F887
INCLUDE "P16F887.INC"

__config _CONFIG1,b'1110000011100001'
__config _CONFIG2,b'1111100011111111'

ORG H'00'

SAYAC EQU H'20'


SAYAC1 EQU H'21'
SAYAC2 EQU H'22'

BSF STATUS,5
; Bank1 selected
MOVLW B'00000001'
; W=00000001
MOVWF TRISD
; PortD pin 0 is set as input

BCF STATUS,5
; Bank0 selected
CLRF PORTD

MAIN
BTFSS PORTD,0
GOTO MAIN
GOTO BUZZER

BUZZER
BSF PORTD,1
; Activate pin D1
CALL DELAY_2_SEC
; wait 2 seconds
BCF PORTD,1
; Disable pin D1
CALL DELAY_2_SEC
; wait 2 seconds
BTFSC PORTD,0
GOTO BUZZER
GOTO MAIN

88
MKT3811: Microprocessors and Programming

DELAY_ONE_MS
MOVLW D'250'
; count 250 cycles
MOVWF SAYAC

LOOP_ONE_MS
; ~1 millisecond
NOP
DECFSZ SAYAC,F
GOTO LOOP_ONE_MS
RETURN

DELAY_250_MS
MOVLW D'250'
MOVWF SAYAC1
LOOP_250_MS
; ~250 millisecond
CALL DELAY_ONE_MS
DECFSZ SAYAC1,F
GOTO LOOP_250_MS
RETURN

DELAY_2_SEC
; ~2 second
MOVLW D'8'
MOVWF SAYAC2
LOOP_2_SEC
CALL DELAY_250_MS
DECFSZ SAYAC2,F
GOTO LOOP_2_SEC
RETURN

END

89
MKT3811: Microprocessors and Programming

Figure 82: Proteus diagram of the circuit

90
MKT3811: Microprocessors and Programming

If the circuit is to be built in real life, the connection


given in Figure 80 must be made in addition to the Proteus
diagram. This connection is the power connection of the
microcontroller.
Notes…………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

91
MKT3811: Microprocessors and Programming

…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

92
MKT3811: Microprocessors and Programming

3.3. Experiment 3: Timer & Interrupt


3.3.1. Preliminary Study
For the preliminary preparation of the experiment, the
basic circuit building information given in Section 1.3,
microcontroller pin configuration given in Section 1.2.1, crystal
connections given in Section 1.2.7 and LED connections given
in Section 1.2.5 can be reviewed. Also, please refer the last part
of Section 1.2.1 to determine the amount of the maximum
current can be sourced from and sinked in the PIC18F887.

3.3.2. A list of materials


The materials to be used in the experiment are shown in
Table 12.
Table 12: A list of materials

Material Name Number


PIC16F887 1
1 kΩ Resistor 1
330 Ω Resistor 1
LED 1
22 pF Capacitor 2
4 MHz Crystal 1
5 V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

93
MKT3811: Microprocessors and Programming

3.3.3. Purpose of Experiment


The experiment aims to demonstrate the usage of the
interrupt and timer with assembly instructions.

3.3.4. Experimental Procedure


The experiment can be simulated using Proteus
software. The Proteus diagram of the experiment is given in
Figure 83. Try the assembly code is as follows:

94
MKT3811: Microprocessors and Programming

LIST P=16F887
INCLUDE "P16F887.INC"

__config _CONFIG1,b'1110000011100001'
__config _CONFIG2,b'1111100011111111'

ORG 0x00
GOTO MAIN

ORG 0x04
GOTO TMR0_INT

MAIN
; All inputs/outputs will be assigned digitally
BANKSEL ANSEL
CLRF ANSEL
CLRF ANSELH

; Selecting Bank1
BCF STATUS,6
BSF STATUS,5

MOVLW B'11010111'
; Timer0 presecaler (1/256)
MOVWF OPTION_REG
MOVLW B'10100000'
; GIE and TMR0IE active
MOVWF INTCON
CLRF TRISB
; PortB pins are assigned as outputs
BCF STATUS,5
; Bank0 selected
CLRF PORTB
; PortB outputs are disabled

LOOP
GOTO LOOP
; Endless loop

95
MKT3811: Microprocessors and Programming

TMR0_INT
BCF INTCON,2
; Clear TMR0 overflow interrupt flag bit
MOVLW H'01'
XORWF PORTB,F
; Toggle LED
CLRF TMR0
; Clear timer0
RETFIE
; Return from interrupt subcode
END

96
MKT3811: Microprocessors and Programming

Figure 83: Proteus diagram of the circuit

97
MKT3811: Microprocessors and Programming

If the circuit is to be built in real life, the connection


given in Figure 80 must be made in addition to the Proteus
diagram. This connection is the power connection of the
microcontroller.
Notes…………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

98
MKT3811: Microprocessors and Programming

…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

99
MKT3811: Microprocessors and Programming

3.4. Experiment 4: LCD Display


3.4.1. Preliminary Study
For the preliminary preparation of the experiment, the
basic circuit setup information given in Section 1.3,
microcontroller pin configuration given in Section 1.2.1, crystal
connections given in Section 1.2.7, LCD pin configuration
given in Section 1.2.9 and the potentiometer connection
diagram given in Section 1.2.3 can be reviewed.

3.4.2. A list of materials


The materials to be used in the experiment are shown in
Table 13.
Table 13: A list of materials

Material Name Number


PIC16F887 1
LM016L LCD Display 1
22 pF Capacitor 2
1 kΩ Resistor 1
330 Ω Resistor 1
10 kΩ Potentiometer 1
4 MHz Crystal 1
5 V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

100
MKT3811: Microprocessors and Programming

3.4.3. Purpose of Experiment


The experiment aims to introduce various types of
variables in the C programming language, demonstrate how to
display their values on an LCD screen, and teach the commands
used for controlling the display.

3.4.4. Experimental Procedure


The experiment can be simulated using Proteus
software. The Proteus diagram of the experiment is given in
Figure 84. Try the CCS C code is as follows:

101
MKT3811: Microprocessors and Programming

#include <16F887.h> // The header file of 16F887 is


introduced
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
#define use_portb_lcd TRUE // Specified that LCD is
connected to Port B
#include <lcd.c> // LCD library introduced

int x; // Variable of type INT is defined


float y; // Variable of type FLOAT is defined
char z; // Variable of type CHAR is defined

void main ()
{
set_tris_b(0b00000000); // Port B is completely set
to output
lcd_init(); // This command should be written before
calling the LCD functions
x = 10; // value assigned to variable x
y = 32.6; // value assigned to variable y

while(1) // Infinite loop


{ // NOTE: Model LM016L is in 2x16 format!
// /n command: Used to skip lines and the cursor
comes to the beginning of the line
// /f command: Moves the cursor to (1,1) and clears
the screen
lcd_cursor_on(TRUE); // This command makes the
blinking cursor appear on the screen
lcd_gotoxy(12,1); // Cursor moved to 12th column, 1st
row
printf(lcd_putc,"CCS C"); // put "CCS C" at (12,1)
delay_ms(1000); // Wait 1 second
printf(lcd_putc,"\n Compiler"); // Skip the line and
write "Compiler"
delay_ms(1000); // Wait 1 second
lcd_gotoxy(1,2); // Cursor moved to 1st column, 2nd
row
printf(lcd_putc,"\fX VALUE=%d",x); // Screen cleared,
x value printed

102
MKT3811: Microprocessors and Programming

delay_ms(1000); // wait 1 second


printf(lcd_putc,"\f\nY VALUE=%f",y); // The screen is
cleared and the line is skipped delay_ms(1000); //
wait 1 second
printf(lcd_putc,"\fMKT3811"); // LCD cleared,
"MKT3811" printed
delay_ms(1000); // Wait 1 second
z = lcd_getc(3,1); // // The character written in the
(3,1) position is taken and
printf(lcd_putc,"\f%c",z); // Character printed
instead of %c
delay_ms(2000); // Wait 2 second
}
}

103
MKT3811: Microprocessors and Programming

Figure 84: Proteus diagram of the circuit

104
MKT3811: Microprocessors and Programming

If the circuit is to be built in real life, the connections


given in Figure 85 must be made in addition to the Proteus
diagram. These connections are the power connection of the
microcontroller and the backlight connection of the LCD.

Figure 85: Additional connections of real-life circuit

Notes…………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

105
MKT3811: Microprocessors and Programming

…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

106
MKT3811: Microprocessors and Programming

3.5. Experiment 5: Keypad


3.5.1. Preliminary Study
For the preliminary preparation of the experiment, the
basic circuit setup information given in Section 1.3, the
microcontroller pin configuration given in Section 1.2.1, the
crystal connections given in Section 1.2.7, the LCD pin
configuration given in Section 1.2.9, potentiometer wiring
diagram given in Section 1.2.3 and the keypad wiring diagram
given in Section 1.2.8 can be reviewed.

3.5.2. A list of materials


The materials to be used in the experiment are shown in
Table 14.
Table 14: A list of materials

Material Name Number


PIC16F887 1
LM016L LCD Display 1
22pF Capacitor 2
1kΩ Resistor 1
330Ω Resistor 1
10kΩ Potentiometer 1
4MHz Crystal 1
4x4 Keypad 1
10kΩ Resistor 4
5V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

107
MKT3811: Microprocessors and Programming

3.5.3. Purpose of Experiment


The experiment aims to teach the working logic of the
keypad and how to use it with the microcontroller.

3.5.4. Experimental Procedure


The experiment can be simulated via the Proteus
software. The Proteus diagram of the experiment is given in
Figure 86. Try the CCS C code is as follows:

108
MKT3811: Microprocessors and Programming

#include <16F887.h> // The header file of 16F887 is


introduced
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
#define use_portb_lcd TRUE // Specified that LCD is
connected to Port B
#include <lcd.c> // LCD library introduced
void main()
{
lcd_init(); // This command should be written before
calling the LCD functions
delay_ms(100); // wait 100ms
set_tris_c(0x11110000); // Input and output assigned
output_c(0x00); // Port C outputs set to logic 0
int number = 0; // Variable defined to keep button
value as int
char sign = ' '; // Variable defined to keep button
value as char
while(1) // Infinite loop
{
// The 1st column is scanned and if there is a
value read from the buttons, it is
output_high(PIN_C0); // The first column buttons
are enabled for reading
if (input(PIN_C4)) {number = 7; sign = '7';};
if (input(PIN_C5)) {number = 4; sign = '4';};
if (input(PIN_C6)) {number = 1; sign = '1';};
if (input(PIN_C7)) {sign = 'C';};
output_low(PIN_C0); // The first column buttons
are disabled for reading
delay_ms(10); // wait 10 ms
// The 2nd column is scanned and if there is a
value read from the buttons, it is
output_high(PIN_C1); // Second column buttons
enabled for reading
if (input(PIN_C4)) {number = 8; sign = '8';};
if (input(PIN_C5)) {number = 5; sign = '5';};
if (input(PIN_C6)) {number = 2; sign = '2';};
if (input(PIN_C7)) {number = 0; sign = '0';};

109
MKT3811: Microprocessors and Programming

output_low(PIN_C1); // The second column buttons


are disabled for reading
delay_ms(10); // wait 10 ms
// The 3rd column is scanned and if there is a
value read from the buttons, it is
output_high(PIN_C2); // The third column buttons
are enabled for reading
if (input(PIN_C4)) {number = 9; sign = '9';};
if (input(PIN_C5)) {number = 6; sign = '6';};
if (input(PIN_C6)) {number = 3; sign = '3';};
if (input(PIN_C7)) {sign = '=';};
output_low(PIN_C2); // The third column buttons
are disabled for reading
delay_ms(10); // wait 10 ms
// The 4th column is scanned and if there is a
value read from the buttons, it is
output_high(PIN_C3); // The fourth column buttons
are enabled for reading
if (input(PIN_C4)) {sign = '/';};
if (input(PIN_C5)) {sign = 'X';};
if (input(PIN_C6)) {sign = '-';};
if (input(PIN_C7)) {sign = '+';};
output_low(PIN_C3); // The fourth column buttons
are disabled for reading
delay_ms(10); // wait 10 ms
// Current button values are printed on the LCD:
lcd_putc("\f"); // LCD cleared
printf(lcd_putc, "%d int", number); // Int
printf(lcd_putc, "\n%c char", sign); // Char
delay_ms(100); // wait 100ms
}
}

110
MKT3811: Microprocessors and Programming

Figure 86: Proteus diagram of the circuit

111
MKT3811: Microprocessors and Programming

If the circuit is to be built in real life, the connections


given in Figure 85 must be made in addition to the Proteus
diagram. These connections are the power connection of the
microcontroller and the backlight connection of the LCD. In
addition, since the 4x4 keypad to be used in the experiment is
different from the one used in Proteus, the simulation given in
Figure 87 should be used and the CCS C code should be
arranged as follows:

112
MKT3811: Microprocessors and Programming

#include <16F887.h> // The header file of 16F887 is


introduced
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
#define use_portb_lcd TRUE // Specified that LCD is
connected to Port B
#include <lcd.c> // LCD library introduced
void main()
{
lcd_init(); // This command should be written before
calling the LCD functions
delay_ms(100); // Wait 100ms
set_tris_c(0x11110000); // Input and output assigned
output_c(0x00); // Port C outputs set to logic 0
int number = 0; // Variable defined to keep button
value as int
char sign = ' '; // Variable defined to keep button
value as char
while(1) // Infinite loop
{
// The 1st column is scanned and if there is a
value read from the buttons, it is
output_high(PIN_C0); // The first column buttons
are enabled for reading
if (input(PIN_C4)) {number = 1; sign = '1';};
if (input(PIN_C5)) {number = 4; sign = '4';};
if (input(PIN_C6)) {number = 7; sign = '7';};
if (input(PIN_C7)) {sign = '*';};
output_low(PIN_C0); // The first column buttons
are disabled for reading
delay_ms(10); // Wait 10 ms
// The 2nd column is scanned and if there is a
value read from the buttons, it is
output_high(PIN_C1); // Second column buttons
enabled for reading
if (input(PIN_C4)) {number = 2; sign = '2';};
if (input(PIN_C5)) {number = 5; sign = '5';};
if (input(PIN_C6)) {number = 8; sign = '8';};
if (input(PIN_C7)) {number = 0; sign = '0';};

113
MKT3811: Microprocessors and Programming

output_low(PIN_C1); // The second column buttons


are disabled for reading
delay_ms(10); // Wait 10 ms
// The 3rd column is scanned and if there is a
value read from the buttons, it is
output_high(PIN_C2); // The third column buttons
are enabled for reading
if (input(PIN_C4)) {number = 3; sign = '3';};
if (input(PIN_C5)) {number = 6; sign = '6';};
if (input(PIN_C6)) {number = 9; sign = '9';};
if (input(PIN_C7)) {sign = '#';};
output_low(PIN_C2); // The third column buttons
are disabled for reading
delay_ms(10); // Wait 10 ms
// The 4th column is scanned and if there is a
value read from the buttons, it is
output_high(PIN_C3); // The fourth column buttons
are enabled for reading
if (input(PIN_C4)) {sign = 'A';};
if (input(PIN_C5)) {sign = 'B';};
if (input(PIN_C6)) {sign = 'C';};
if (input(PIN_C7)) {sign = 'D';};
output_low(PIN_C3); // The fourth column buttons
are disabled for reading
delay_ms(10); // Wait 10 ms
// Current button values are printed on the LCD:
lcd_putc("\f"); // LCD cleared
printf(lcd_putc, "%d int", number); // Int
printf(lcd_putc, "\n%c char", sign); // Char
delay_ms(100); // wait 100ms
}
}

114
MKT3811: Microprocessors and Programming

Figure 87: Keypad to be used in real life and equivalent keypad in Proteus

Notes…………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

115
MKT3811: Microprocessors and Programming

…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

116
MKT3811: Microprocessors and Programming

3.6. Experiment 6: ADC


3.6.1. Preliminary Study
For the preliminary preparation of the experiment, the
basic circuit setup information given in Section 1.3,
microcontroller pin configuration given in Section 1.2.1, crystal
connections given in Section 1.2.7, LCD pin configuration
given in Section 1.2.9 and the potentiometer connection
diagram given in Section 1.2.3 can be reviewed.

3.6.2. A list of materials


The materials to be used in the experiment are shown in
Table 15.
Table 15: A list of materials

Material Name Number


PIC16F887 1
LM016L LCD Display 1
22 pF Capacitor 2
1 kΩ Resistor 1
330 Ω Resistor 1
10 kΩ Potentiometer 3
4 MHz Crystal 1
5 V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

117
MKT3811: Microprocessors and Programming

3.6.3. Purpose of Experiment


The experiment aims to teach process of the converting
analog signal to digital signal (ADC).

3.6.4. Experimental Procedure


The experiment can be simulated via the Proteus
software. The Proteus diagram of the experiment is given in
Figure 88. Try the CCS C code is as follows:

118
MKT3811: Microprocessors and Programming

#include <16F887.h> // The header file of 16F887 is


introduced
#device adc = 8 // 8 bit ADC will be used
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
#define use_portb_lcd TRUE // Specified that LCD is
connected to Port B
#include <lcd.c> // LCD library introduced
void main()
{
byte result; // The analog value variable to be read
is set
lcd_init(); // This command should be written before
calling the LCD functions
delay_ms(10); // wait 10 ms
setup_adc_ports(sAN0); // AN0 pin is set as analog
input
setup_adc(ADC_CLOCK_DIV_32); // ADC clock frequency
set to Fosc / 32
set_adc_channel(0); // ADC operation will be applied
to the signal on channel AN0
delay_us(10); // Wait 10 us
while(1) // Infinite loop
{
result = read_adc(); // ADC is read and assigned
to result variable
printf(LCD_PUTC,"\fValue=%u",result); // Print
result variable to LCD
delay_ms(200); // Wait 200 ms
}
}

The above code was written for an ADC operation only.


If two ADC operations are desired to be done at the same time,
the CCS C code should be arranged as follows:

119
MKT3811: Microprocessors and Programming

#include <16F887.h> // The header file of 16F887 is


introduced
#device adc = 10 // 10-bit ADC will be used
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
#define use_portb_lcd TRUE // Specified that LCD is
connected to Port B
#include <lcd.c> // LCD library introduced
void main()
{
unsigned int16 result_1; // The analog value variable
to read from AN0 is set
unsigned int16 result_2; // The analog value variable
to read from AN1 is set
lcd_init(); // This command should be written before
calling the LCD functions
delay_ms(10); // wait 10 ms
setup_adc_ports(sAN0|sAN1); // AN0 and AN1 pins are
set as analog inputs
setup_adc(ADC_CLOCK_DIV_32); // ADC clock frequency
set to Fosc / 32
while(1) // Infinite loop
{
set_adc_channel(0); // ADC operation will be
applied to the signal on channel AN0
delay_us(10); // wait 10 us
result_1 = read_adc(); // ADC is read and assigned
to the variable result_1
printf(LCD_PUTC,"\fValue1=%lu",result_1); // The
variable result_1 is printed on the
set_adc_channel(1); // ADC operation will be
applied to the signal on channel AN1
delay_us(10); // wait 10 us
result_2 = read_adc(); // ADC is read and assigned
to the variable result_2
printf(LCD_PUTC,"\nValue2=%lu",result_2); // The
variable result_2 is printed on the delay_ms(200);
// wait 200 ms
}}

120
MKT3811: Microprocessors and Programming

Figure 88: Proteus diagram of the circuit

121
MKT3811: Microprocessors and Programming

If the circuit is to be built in real life, the connections


given in Figure 85 must be made in addition to the Proteus
diagram. These connections are the power connection of the
microcontroller and the backlight connection of the LCD.
Notes…………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

122
MKT3811: Microprocessors and Programming

…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

123
MKT3811: Microprocessors and Programming

3.7. Experiment 7: PWM


3.7.1. Preliminary Study
For the preliminary preparation of the experiment, the
basic circuit setup information given in Section 1.3, the
microcontroller pin configuration given in Section 1.2.1, the
crystal connections given in Section 1.2.7, the LCD pin
configuration given in Section 1.2.9, the potentiometer
connection diagram given in Section 1.2.3 and LED
connections and limiting resistor calculations given in Section
1.2.5 can be reviewed. Also, please refer the last part of Section
1.2.1 to determine the amount of the maximum current can be
sourced from and sinked in the PIC18F887.

3.7.2. A list of materials


The materials to be used in the experiment are shown in
Table 16.
Table 16: A list of materials

Material Name Number


PIC16F887 1
LM016L LCD Display 1
22pF Capacitor 2
1kΩ Resistor 1
330Ω Resistor 3
10kΩ Potentiometer 3
LED 2
4MHz Crystal 1
5V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

124
MKT3811: Microprocessors and Programming

3.7.3. Purpose of Experiment


The experiment aims to generate a Pulse Width
Modulation (PWM) signal using a microcontroller.

3.7.4. Experimental Procedure


The experiment can be simulated via the Proteus
software. The Proteus diagram of the experiment is given in
Figure 89. Try the CCS C code is as follows:

125
MKT3811: Microprocessors and Programming

#include <16F887.h> // The header file of 16F887 is


introduced
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
#define use_portb_lcd TRUE // Specified that LCD is
connected to Port B
#include <lcd.c> // LCD library introduced
// Function defined to change PWM1 value:
void pwm1_set(int16 mypwm);
void main()
{
lcd_init(); // This command should be written before
calling the LCD functions
delay_ms(10); // Wait 10 ms
set_tris_c(0x00); // All C port pins are set as
output
setup_ccp1(CCP_PWM); // CCP1 PWM mode activated
setup_timer_2(T2_DIV_BY_16, 255, 1); // Timer2
settings done
while(1) // Infinite loop
{
// Assigning different PWM1 values:
pwm1_set(200);
pwm1_set(400);
pwm1_set(600);
pwm1_set(800);
pwm1_set(1000);
}
}
// Function defined to change PWM1 value:
void pwm1_set(int16 mypwm)
{
set_pwm1_duty(mypwm); // PWM duty cycle time set to
"mypwm"
printf(LCD_PUTC,"\fPWM1=%lu",mypwm); // "mypwm" value
is printed to LCD
delay_ms(1000); // Wait 1 second
}

126
MKT3811: Microprocessors and Programming

The preceding code is intended to generate a PWM signal


with varying duty cycles at one-second intervals. The following
code enables the duty cycles of two distinct PWM signals to be
adjusted using potentiometers:

#include <16F887.h> // The header file of 16F887 is


introduced
#device adc = 10 // 10-bit ADC will be used
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
#define use_portb_lcd TRUE // Specified that LCD is
connected to Port B
#include <lcd.c> // LCD library introduced
void main()
{
unsigned int16 result_1; // The analog value variable
to read from AN0 is set
unsigned int16 result_2; // The analog value variable
to be read from AN1 is set
lcd_init(); // This command should be written before
calling the LCD functions
delay_ms(10); // Wait 10 ms
set_tris_c(0x00); // All C port pins are set as
output
setup_ccp1(CCP_PWM); // CCP1 PWM mode enabled
setup_ccp2(CCP_PWM); // CCP2 PWM mode enabled
setup_timer_2(T2_DIV_BY_16, 255, 1); // Timer2
settings done
setup_adc_ports(sAN0|sAN1); // AN0 and AN1 pins are
set as analog inputs
setup_adc(ADC_CLOCK_DIV_32); // ADC clock frequency
set to Fosc / 32
while(1)
{
set_adc_channel(0); // ADC operation will be
applied to the signal on channel AN0
delay_us(10); // Wait 10 us

127
MKT3811: Microprocessors and Programming

result_1 = read_adc(); // ADC is read and assigned


to the variable result_1
printf(LCD_PUTC,"\fValue1=%lu",result_1); // The
variable result_1 is printed on the
set_pwm1_duty(result_1); // PWM duty cycle time
set to "result_1"
set_adc_channel(1); // ADC operation will be
applied to the signal on channel AN1
delay_us(10); // Wait 10 us
result_2 = read_adc(); // ADC is read and assigned
to result_2
printf(LCD_PUTC,"\nValue2=%lu",result_2); // The
variable result_2 is printed on the
set_pwm2_duty(result_2); // PWM duty cycle time
set to "result_2"
delay_ms(200); // Wait 200ms
}
}

128
MKT3811: Microprocessors and Programming

Figure 89: Proteus diagram of the circuit

129
MKT3811: Microprocessors and Programming

If the circuit is to be built in real life, the connections


given in Figure 85 must be made in addition to the Proteus
diagram. These connections are the power connection of the
microcontroller and the backlight connection of the LCD.
Notes…………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

130
MKT3811: Microprocessors and Programming

…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

131
MKT3811: Microprocessors and Programming

3.8. Experiment 8: Serial Communication


3.8.1. Preliminary Study
For the preliminary preparation of the experiment, the
basic circuit setup information given in Section 1.3,
microcontroller pin configuration given in Section 1.2.1, crystal
connections given in Section 1.2.7, LCD pin configuration
given in Section 1.2.9 and the potentiometer connection
diagram given in Section 1.2.3 can be reviewed.

3.8.2. A list of materials


The materials to be used in the experiment are shown in
Table 17.
Table 17: A list of materials

Material Name Number


PIC16F887 2
LM016L LCD Display 1
22 pF Capacitor 4
1 kΩ Resistor 2
330 Ω Resistor 1
10 kΩ Potentiometer 1
4 MHz Crystal 2
5 V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

132
MKT3811: Microprocessors and Programming

3.8.3. Purpose of Experiment


The experiment aims to serially communicate two
different microcontrollers.

3.8.4. Experimental Procedure


The experiment can be simulated via the Proteus
software. The Proteus diagram of the experiment is given in
Figure 90. The CCS C code of the microcontroller acting as a
receiver is as follows:

133
MKT3811: Microprocessors and Programming

#include <16F887.h> // The header file of 16F887 is


introduced
#include <stdlib.h> // Required to use the atol()
command
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
#define use_portb_lcd TRUE // Specified that LCD is
connected to Port B
#include <lcd.c> // LCD library introduced
// Configuration settings for serial communication:
// Communication speed: 9600
// Transmit data pin: C6
// Data receive pin: C7
// Parity = N: No parity bit
// Stop = 1: The stop bit will consist of 1 bit
#use rs232 (baud=9600,xmit=PIN_C6, rcv=PIN_C7, parity=N,
stop=1)
int counter = 0; // Counter reset
char strInput[4]; // String array defined for input
characters
unsigned long inputString; // The variable named
inputString is defined to combine the characters
void main()
{
lcd_init(); // This command should be written before
calling the LCD functions
delay_ms(10); // Wait 10 ms
while(1) // Infinite loop
{
if(kbhit()) // If data is received, kbhit = 1
{
char i = getc(); // UART read
if (i == '*') // If the received character is
*, this condition is called
{
counter = 0; // Counter reset
printf("Incoming Data: %s",strInput); // The
received and collected data array
inputString = atol(strInput); // Str -> Int
conversion done

134
MKT3811: Microprocessors and Programming

inputString = inputString*2; // Data


multiplied by 2
printf(" Number: %lu ", inputString); //
Processed data is written to Serial
printf(LCD_PUTC,"\fValue=%lu",inputString);
// Processed data is printed to
memset(strInput, 0, 8); // first 8
characters of strInput array replaced with
}
else // This condition is called if the
received character is not *
{
strInput[counter] = i; // The incoming
character is added to the strInput array
counter++; // Counter incremented by 1
}
}
}
}

135
MKT3811: Microprocessors and Programming

The CCS C code of the microcontroller serving as the


transmitter is as follows:

#include <16F887.h> // The header file of 16F887 is


introduced
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
// Configuration settings for serial communication:
// Communication speed: 9600
// Transmit data pin: C6
// Data receive pin: C7
// Parity = N: No parity bit
// Stop = 1: The stop bit will consist of 1 bit
#use rs232 (baud=9600,xmit=PIN_C6, rcv=PIN_C7, parity=N,
stop=1)
void main()
{
while(1) // With 500 ms intervals, the following is
sent from the TX (C6) pin with the
{
printf("1");
delay_ms(500);
printf("2");
delay_ms(500);
printf("3");
delay_ms(500);
printf("4");
delay_ms(500);
printf("*");
delay_ms(5000);
}
}

136
MKT3811: Microprocessors and Programming

Figure 90: Proteus diagram of the circuit (with virtual terminal)

The virtual terminals given in the Proteus diagram are


for observing the communication during the simulation phase.
This circuit is given in Figure 91 in a simpler form as a real-life
circuit.

137
MKT3811: Microprocessors and Programming

Figure 91: Proteus diagram of the circuit (without virtual terminal,


simplified)

If the circuit is to be built in real life, the connections


given in Figure 85 must be made in addition to the Proteus
diagram. These connections are the power connections of both
microcontrollers and the backlight connection of the LCD.
Notes…………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

138
MKT3811: Microprocessors and Programming

…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

139
MKT3811: Microprocessors and Programming

3.9. Experiment 9: 7-Segment Display


3.9.1. Preliminary Study
For the preliminary preparation of the experiment, the
basic circuit setup information given in Section 1.3, the
microcontroller pin configuration given in Section 1.2.1, the
crystal connections given in Section 1.2.7 and the 7-Segment
display given in Section 1.2.10 can be reviewed. Also, please
refer the last part of Section 1.2.1 to determine the amount of
the maximum current can be sourced from and sinked in the
PIC18F887.

3.9.2. A list of materials


The materials to be used in the experiment are shown in
Table 18.
Table 18: A list of materials

Material Name Number


PIC16F887 1
22 pF Capacitor 2
1 kΩ Resistor 1
4 MHz Crystal 1
Common Cathode 7-Segment 1
330 Ω Resistor 7
5 V Power Supply (USB Alligator Cable) 1
Breadboard 1
Jumper Cable ~

140
MKT3811: Microprocessors and Programming

3.9.3. Purpose of Experiment


The experiment aims to teach the use of 7-segments.

3.9.4. Experimental Procedure


The experiment can be simulated via the Proteus
software. The Proteus diagram of the experiment is given in
Figure 92. Try the CCS C code is as follows:

#include <16F887.h> // The header file of 16F887 is


introduced
// 16F887 configuration settings:
#fuses
XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,
NOCPD
#use delay (clock = 4M) // Oscillator frequency set
int counter; // Counter defined
// Hex information required to generate values 0-9:
const int info[10]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D,
0x7C, 0x07, 0x7F, 0x6F};
void main()
{
set_tris_b(0x00); // Port B set as output
output_b(0x00); // Port B pins set to logic 0
while(1) // Infinite loop
{
for(counter = 0;counter <= 9;counter++) // Loop to
print values 0-9
{
output_b(info[counter]); // Assigned
info[counter] to port B
delay_ms(500); // Wait 500ms
}
}
}

141
MKT3811: Microprocessors and Programming

Figure 92: Proteus diagram of the circuit

142
MKT3811: Microprocessors and Programming

If the circuit is to be built in real life, the connection


given in Figure 80 must be made in addition to the Proteus
diagram. This connection is the power connection of the
microcontroller.
Notes…………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

143
MKT3811: Microprocessors and Programming

…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………
…………………………………………………………………

144
MKT3811: Microprocessors and Programming

REFERENCES
Akdogan, Erhan & Demir, Mehmet & Aktan, Mehmet & Koru, Ahmet.
(2016). ELECTRIC - ELECTRONICS LABORATORY EXPERIMENT
BOOK.

Mikrodenetleyiciler ve PIC Programlama, O. Altınbaşak, 2020. Altaş


Yayıncılık

Mikrodenetleyiciler, H. H. Genç, 2019. Nobel Yayıncılık

PIC Mikrodenetleyiciler; A. Bayrak; 2018; Ekin Yayıncılık

CCS C ile PIC Programlama, Serdar Çiçek, 2009, Altaş Yayıncılık

Programming 8-bit PIC Microcontrollers in C with interactive Hardware


Simulation; M.P. Bates,2008, Newnes

https://www.microchip.com/en-us/product/PIC16F887

https://www.mikroe.com/ebooks/pic-microcontrollers-programming-in-
assembly/pic16f887-microcontroller-device-overview

https://eepower.com/resistor-guide/resistor-fundamentals/what-is-a-
resistor/#

https://eepower.com/resistor-guide/resistor-applications/pull-up-resistor-
pull-down-resistor/

https://www.electrical4u.com/potentiometer/

https://www.electrical4u.com/crystal-oscillator/

https://www.electronics-tutorials.ws/capacitor/cap_1.html

https://www.electronics-tutorials.ws/blog/7-segment-display-tutorial.html

https://www.electronicwings.com/sensors-modules/4x4-keypad-module

https://www.techtarget.com/whatis/definition/LCD-liquid-crystal-display

145

You might also like