Lab_1_Introduction MPLAB 18F452
Lab_1_Introduction MPLAB 18F452
Name : Date :
Regd-No :
OBJECTIVES:
▪ Introduction to PIC16F877A, PIC18F452 and their peripherals
▪ Learn about MPLab IDE (interface and basics)
SUGGESTED READING:
▪ PIC18F452 Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/39564c.pdf
▪ Assembler/Linker/Librarian’s user guide (Microchip):
ww1.microchip.com/downloads/en/DeviceDoc/33014J.pdf
▪ Instruction Set for PIC18Fxxx
http://technology.niagarac.on.ca/staff/mboldin/18F_Instruction_Set/
▪ PIC18F4550 and Assembly Language Overview:
http://www.pic18f.com/tutorial/2007/12/04/18f4550-and-assembly-overview/
▪ Using MPLab to create hex files:
http://picprojects.org.uk/projects/mplab/mplabhowto.htm
▪ Creating projects using MPLab for PIC: http://www.wikihow.com/Create-a-Project-to-
Program-a-PIC-Using-MPLAB-IDE-V8.56
SOFTWARES REQUIRED:
▪ MPLAB IDE
▪ Proteus ™
INTRODUCTION:
PIC(Peripheral Interface Controller) microcontrollers are popular with both
industrial developers and hobbyists alike due to their low cost, wide availability,
large user base, extensive collection of application notes, availability of low cost or
free development tools, and serial programming (and re-programming with flash
memory) capability. PIC18F is easily available and very powerful and quiet
capable microcontroller which can easily enable you to add some Logic and Intel
to your projects. All you need is a Microcontroller, An IDE and a Microcontroller
programmer like pickit2 or JDM.
University of Engineering and Technology, Lahore
If you have ever wondered what microcontrollers are doing these days then,
all kinds of devices have microcontrollers in them. Microcontroller chip is must for
any devices which measures, stores, controls, calculate or displays information.
Automobile industry is the largest user of microcontrollers. Microcontrollers are
used in consumer products too. LCD/LED display units, Keyboards, Modems,
Digital camcorders, Optical players, Printers and other peripherals are devices where
you can find microcontrollers inside. Microcontrollers are chosen based on the task
specification and several other factors related to performance and functionality.
PIC16F877A
The PIC16F877A Microcontroller consists of an inbuilt CPU, I/O ports, memory
organization, A/D converter, timers/counters, interrupts, serial communication, oscillator
and CCP module which to gather makes the IC a powerful microcontroller for beginners
to start with. The general block diagram of the PIC Architecture is shown below:
University of Engineering and Technology, Lahore
As the name suggests the GPR are used for general register functions like addition,
subtraction etc. These operations are limited within 8-bit. All the registers under the GPR
are user writable and readable. They do not have any functions on their own unless it is
software specified.
Whereas the SFR is used to perform complicated special functions which also involves
some 16-bit handling, their registers can only be read (R) and we cannot write (W)
anything to them. So these registers have a predefined functions to perform, which are set
University of Engineering and Technology, Lahore
at the time of manufacturing and they just display the result to us, using which we can
perform some related operations.
Read Only Memory (ROM): Read only memory is the place where our program gets
stored. This decides the maximum size of our program; hence it is also called as program
memory. When the MCU is under operation, the program stored in the ROM is executed
as per each instruction cycle. This memory unit can be used only while programming the
PIC, during execution it becomes a read only memory.
Electrically Erasable Programmable Read Only Memory (EEPROM): EEPROM is
another type of Memory Unit. In this memory unit values can be stored during program
execution. The values stored here are only Electrically Erasable that is these values will
be retained in the PIC even when the IC is turned off. They can be used as small memory
space to store the executed values; however the memory space will be very less in turns of
KB.
Flash Memory: Flash memory is also Programmable Read Only Memory (PROM) in
which we can read, write and erase the program thousands of times. Generally, the
PIC microcontroller uses this type of ROM.
Bus:
The term Bus is just a bunch of wires that connects the Input or Output Device with CPU
and RAM. Data bus is used to transfer or receives the data. Address bus is used to
transmit the memory address from the peripherals to the CPU. I/O pins are used to interface
University of Engineering and Technology, Lahore
the external peripherals; UART and USART both serial communication protocols are used
for interfacing serial devices like GSM, GPS, Bluetooth, IR , etc.
Peripheral Features
The Peripheral Feature, mentions that it has 3 Timers, two of which are 8-bit and one is
16-bit prescaler. These Timers are used to create timing functions in our program. They
can also be used as counters. It also shows that it has CCP (Capture Compare and
PWM) options, which helps us to generate PWM signals and read the incoming frequency
signals. For communication with external device, it has SPI, I2C, PSP and USART. For
safety purpose it is equipped with Brown-out Reset (BOR), which helps in resetting the
while program.
The Analog Features, Indicates that the IC has 10-bit 8-channel ADC. This means, our
IC can convert Analog values to digital with a resolution of 10-bit, and has 8 analog pins
to read them. We also have two internal comparators that can be used to compare the
incoming voltage directly without actually reading them through the software.
The Special Microcontroller Features, signifies that it has 100,000 erase/write
cycle, meaning you can program it for about 100,000 times. In-Circuit Serial
Programming™ (ICSP™), helps us to program the IC directly
using PICKIT3. Debugging can be made via In-Circuit Debug (ICD). Another safety
feature is the Watchdog Timer (WDT), which is a self reliable timer that resets the whole
program if required.
The below image represents the pinouts of our PIC16F877A IC. This image represents
each pin against its name and its others features. This can also be found in the datasheet.
Keep this image handy for it will help us during our hardware works.
University of Engineering and Technology, Lahore
Peripheral Features:
▪ High-Current Sink/Source: 25 mA/25 mA
▪ Three External Interrupts
▪ Four Timer modules (Timer0 to Timer3)
▪ Up to 2 Capture/Compare/PWM (CCP) modules:
University of Engineering and Technology, Lahore
o Capture is 16-bit, max. resolution 5.2 ns (TCY/16)
o Compare is 16-bit, max. resolution 83.3 ns (TCY)
o PWM output: PWM resolution is 1 to 10-bit
▪ Enhanced Capture/Compare/PWM (ECCP) module:
o Multiple output modes
o Selectable polarity
o Programmable dead time
o Auto-shutdown and auto-restart
▪ Enhanced USART module:
o LIN bus support
▪ Master Synchronous Serial Port (MSSP) module supporting 3-wire SPI (all
4 modes) and I2C™ Master and Slave modes
▪ 10-bit, up to 8-channel Analog-to-Digital Converter module (A/D) with
Programmable Acquisition Time
▪ Dual Analog Comparators with Input Multiplexing
Special Features:
▪ Multiple power management modes
▪ Flexible oscillator structure
▪ 100,000 Erase/Write Cycle Enhanced Flash Program Memory typical
▪ Watchdog Timer (WDT) with its own On-Chip RC Oscillator for reliable
operation
▪ Single-Supply 5V In-Circuit Serial Programming™ (ICSP™) via two pins
▪ In-Circuit Debug (ICD) via two pins
University of Engineering and Technology, Lahore
Pin Description:
PORTS:
40 pins of PIC18F452 are divided into 5 ports. Out of which, 34 pins are Input-
Output pins which can be configured for general input or output by setting registers
associated with them. Please refer to the pin-out diagram above for a clear idea
about location of these pins on the microcontroller.
Assembly VS C Language:
Assembly Language:
▪ Referred to as a low-level-language
▪ deals directly with the internal structure of CPU
▪ deals with registers, memory addresses and call stacks
▪ all registers of CPU must be known by programmer
▪ Each statement corresponds to a single machine code instruction
▪ mnemonics or opcode (codes & abbreviations) are easier to remember than
machine code
▪ Translated into machine code by an Assembler
MPLAB IDE:
MPLab is the Integrated Development Environment (IDE) provided by
Microchip™ for the programming, debugging and simulation of the PIC
microcontrollers. By default it supports Assembly Language programming for
PIC12, PIC16, PIC18, and dsPIC series controllers. A C language supported
compiler (C18) can be installed with MPLab to provide support for C language
programming with PIC18 series.
MPLAB-IDE includes:
▪ Full-featured programmer’s text
editor that also serves as a window
into the debugger.
▪ Project manager (visible as the
Project window) that provides
integration and communication
between the IDE and the language
tools.
▪ Assembler converts instructions into
machine code. Error file & object
files are created.
▪ Linker takes object files and produce
hex file, list file, map files & debug
files. (List file shows binary & source
code, map file shows memory layout Fig.1.4: Work Flow of MPLab IDE
of used & unused memory locations)
▪ Debugger engine (uses debug file) that
provides breakpoints, single stepping, Watch windows and all the features of
a modern debugger. The debugger works in conjunction with debug tools,
both software and hardware.
▪ The simulator is actually composed of several device-specific simulator
executables. MPLAB IDE decides which one to use based on your project’s
device.
Programmers
MPLAB PM3 programmer, PICkit™ 2, PICkit 3 and MPLAB ICD 3 in-circuit
debuggers; and MPLAB REAL ICE™ in-circuit emulator can program code into
target devices. Universal programmers can also be used to separately program the
PIC Controllers outside of MPLab IDE.
University of Engineering and Technology, Lahore
7. Skip step 4 of the project wizard and click Finish on the summary tab to
complete the process. The summary tab shows the overview of your project
parameters.
University of Engineering and Technology, Lahore Spring 2019
Output Window
File Window: Shows the files currently added to the project. Each subfolder is
used to contain a specific type of file.
Output Window: shows the build status, errors, and output file locations.
Editor Window: The place to edit and write the code to.
2. Save the file with the extension of ‘.asm’ and add the asm file to the source
files.
10
University of Engineering and Technology, Lahore Spring 2019
3. Click on the BuildAll button to compile and build the code. The output
window shows the build errors and output file locations.
WREG register is used for all arithmetic and logic operations in PIC microcontroller.
Task 1:
Write a code to add the following numbers together:
0x05
0x13
0x4F
0x9D
0xB4
Monitor how the value of WREG changes during execution, and write the value of
WREG after code execution.
WREG = ___.
11