b6f273e934442bc

advertisement
Microcontroller – EEC 2033
Microcontroller: PIC
Tutorial
Khulood AlZaabi – H00224598
To: Asad Hindash
 Introduction
In this tutorial I’m focusing in PIC16F877A. It is just one type of PIC microcontroller
family. It’s widely used between both professionals and beginners. It has many
features that what I’m going to write about. Adding to that, I will mention its uses,
memory organization, registers, and I’ll focus in status register. As we learnt the
assembly coding, I’ll include the steps of creating a project in the MPLAB, with an
example that I did.
This tutorial has been written by me, Khulood Alzaabi, using many resources, to be
submitted to Mr.Asad Hindash.
 PIC’s Background (Mid-range 14-bit instruction)
Mid-Range PIC Microcontrollers are the next tier in performance and features from
our Baseline PIC microcontrollers. Utilizing a 14-bit instruction word, these
peripheral-rich devices are ideal for multi-dimensional applications that require a
higher level of embedded control, yet with only 35 instructions to learn, achieving
optimum system performance remains an easy task.
-
35 (14-bit wide) easy instructions to learn
8K word (14 KB) addressable program memory
46 bytes RAM (max)
8 level hardware stack
1 (9-bit) file select register
Hardware interrupt handling
Highly integrated feature set, including EEPROM, LCD, mTouch™ sensing
solutions and serial communications. 1
* Mid-Range Architecture Block Diagram
 PIC16F877A
Microcontroller PIC16F877A is a type of PIC microcontroller family, which is
popular between both beginners and professionals. Using PIC16F877A is very easy,
it can write and erase till million times. It exceeds
other 8-bits with its speed and code. Low cost, low
consumption, flexibility and easy handing makes
PIC16F877A easy to buy and use in many areas such
as: timer functions, interface replacement on larger
systems, coprocessor applications. In terms of
programming PIC16F877A chip, it
makes the product much flexible,
and that makes it easy to create
assembly-line production, store
data, and improve programs on
finished products. PIC16F877A
suitable for many uses like:
automotive
industries
and
controlling home appliances to
industrial instruments, remote
sensors, electrical door locks and
safety devices, smart cards, and
because it's low consumption it
uses for battery supplied devices.
In the right side, we can see the figure of PIC16F877A shows the inputs and outputs
of chip PIC16F877A.
Analog Features:
-
10-bit, up to 8-channel Analog-to-Digital Converter (A/D)
Brown-out Reset (BOR)
Analog Comparator module (Two analog comparators , Programmable onchip voltage reference (VREF) module , Programmable input multiplexing
from device inputs and internal voltage reference , Comparator outputs are
externally accessible)
High-Performance RISC CPU:
-
Only 35 single-word instructions to learn
All single-cycle instructions except for program branches, which are twocycle
Operating speed: DC – 20 MHz clock input DC – 200 ns instruction cycle
-
Up to 8K x 14 words of Flash Program Memory, Up to 368 x 8 bytes of Data
Memory (RAM), Up to 256 x 8 bytes of EEPROM Data Memory
Pinout compatible to other 28-pin or 40/44-pin PIC16CXXX and PIC16FXXX
microcontrollers
Peripheral Features:
-
Timer0: 8-bit timer/counter with 8-bit prescaler
Timer1: 16-bit timer/counter with prescaler, can be incremented during
Sleep via external crystal/clock
Timer2: 8-bit timer/counter with 8-bit period register, prescaler and
postscaler
Two Capture, Compare, PWM modules
Synchronous Serial Port (SSP) with SPI™ (Master mode) and I2C™
(Master/Slave)
Universal Synchronous Asynchronous Receiver
Transmitter (USART/SCI) with 9-bit address detection
Parallel Slave Port (PSP) – 8 bits wide with external RD, WR and CS controls
(40/44-pin only)
Brown-out detection circuitry for Brown-out Reset (BOR)
Special Microcontroller Features:
-
100,000 erase/write cycle Enhanced Flash program memory typical
1,000,000 erase/write cycle Data EEPROM memory typical
Data EEPROM Retention > 40 years
Self-reprogrammable under software control
In-Circuit Serial Programming™ (ICSP™) via two pins
Single-supply 5V In-Circuit Serial Programming
Watchdog Timer (WDT) with its own on-chip RC oscillator for reliable
operation
Programmable code protection
Power saving Sleep mode
Selectable oscillator options
In-Circuit Debug (ICD) via two pins
CMOS Technology:
-
Low-power, high-speed Flash/EEPROM technology
Fully static design
Wide operating voltage range (2.0V to 5.5V)
Commercial and Industrial temperature ranges
Low-power consumption. 2

Memory Organization
When the user writes his assembly code of his program it directly goes into the
Program Memory. It’s the biggest in size memory of the PIC. The PIC16F877A has 4K
of Program Memory and it allocates up to 8K.
Memory organization has two types. First, Program Memory which is used for
storing compiled code. Each location in it is 14 bits long, and every instruction is
coded as a 14 bit word. Adding to that, address “H000” and “H004” are treated in a
special way. And its PC address can be up to 8K addresses.
Second, Register file memory, which consist of two types: General Purpose Registers
and Special Purpose Registers. Those memories are separated into many banks of
128 bytes long. 3

Registers:
Registers are placed inside the PIC. It reads or writes the data or the program. The
memory of the PIC is divided into many registers, and each register has its own
address and memory location. Addresses are denoted by hexadecimal numbers.
Registers are divided into two types:
a. General Purpose Register (GPS)
Small amount of storage can be more quickly than any other memory. The register
file can be directly or indirectly through FSR, file select register.
a. Special Purpose Register (SPR)
It’s a memory registers, used for various dedicated functions inside the PIC chip.
Any special function inside the PIC can be controlled by those registers. Those
registers are used by the CPU and Peripheral modules to control the desired
operations of the device. These registers are implemented of the form of static RAM
memory. Special function registers are categorized into two sets: Core (CPU) and
Peripheral. 4

Status Register
Status Registers use to store the operation’s result. It categorized into three bits
based on the result of the arithmetical operation. First, Zero Flag, this bit sets when
the result of the operation is zero. Second, Carry Flag, this bit sets when the result of
the operation is greater than (0xFF), which are 255. Finally, Digit Carry Flag, it sets
when the result is greater than (0x0F), which are 15. 5
Figures below shows SFR, Special Function Register, memory map of PIC16F877. 4
This table shows the memory map of General Purpose Register, GPR, of PIC16F877A. 4

Instruction set:
PIC16F877A has 35 instructions. Each one is codded in 14-bit word. All of them
take one cycle to execute. In most instructions W register used as source register.
And the result of the operation is stored in W register or back to the source register.
Below is a table summarized the 35 instructions in a simple way.
Instruction
ADDWF
ANDWF
CLRF
CLRW
COMF
DECF
DECFSZ
INCF
INCESZ
IORWF
MOVF
MOVWF
NOP
RLF
RRF
SUBWF
SWAPF
XORWF
BCF
BSF
BTFSC
BTESS
ADDLW
ANDLW
CALL
CLRWDT
GOTO
IORLW
MOVLW
RETEIF
RETLW
RETURN
SLEEP
SUBLW
XORLW
Description
Cycle
Byte oriented file register operations
Add W and F
1
AND W with f
1
Clear register f
1
Clear W
1
Complement f
1
Decrement f
1
Decrement f, skip if 0
1(2)
Increment f
1
Increment f, skip if 0
1(2)
Inclusive or W with f
1
Move f
1
Move W to f
1
No operation
1
Rotate Left f through Carry
1
Rotate Right f through Carry
1
Subtract W from f
1
Swap nibbles in f
1
Exclusive or W with f
1
Bit clear f
1
Bit set f
1
Bit test f, skip if clear
1(2)
Bit test if, skip if set
1(2)
Literal and control operation
Add literal and W
1
AND literal with W
1
Call Subroutine
2
Clear Watchdog Timer
1
Go to address
2
Inclusive or literal with W
1
Move literal to W
1
Return from interrupt
2
Return with literal in W
2
Return from subroutine
2
Go into standby mode
1
Subtract W from literal
1
Exclusive or literal with W
1
Status Affected
C, DC, Z
Z
Z
Z
Z
Z
Z
Z
C
C
C, DC, Z
Z
C, DC, Z
Z
TO, PD
Z
TO, PD
C, DC, Z
Z
 MicroElektronika Development Board
MikroElektronika EasyMx PRO™ v7 Development Board for STM32® ARM® is a
full-featured development board for STM32® ARM® Cortex™-M3 and Cortex™-M4
microcontrollers. The board is provided with an MCU card containing the
STM32F107VCT6 microcontroller. EasyMx PRO™ v7 for STM32® ARM® contains
17 on-board modules necessary for development of a variety of applications,
including multimedia, Ethernet, USB, CAN, and more. A TFT with touch panel along
with a stereo mp3 codec allow the user to develop multimedia applications. There
are 2 USB connectors and 2 USBUART connectors on the board as
well. The piezo buzzer will help test
sound signalization, and the I2C
EEPROM, Serial Flash, and microSD
card slot can be used to store data.
DS1820 and LM35 temperature
sensor sockets are also included. The
powerful on-board mikroProg™
programmer
and
hardware
debugger, based on the popular STLINK v2, supports over 180 ARM®
microcontrollers.
There are many features of using the MicroElektronika development board, such as:
it has four connectors for each part for amazing connectivity, hardware debugger
over 180 devices, powerful on board mikroProg, and Multimedia peripherals: TFT
320x240 with touch panel, stereo mp3 codec, audio input and output, navigation
switch, microSD card slot. Adding to that, it have many great specifications like: it
consume 76mA when all peripheral modules are disconnected, its power supply is
around 7-23V AC or 9-32V DC, and the dimensions of its board is 266x220mm. 6
 MPLAB (Integrated Development Environment, IDE)
MPLAB® X IDE is a software program that runs on a PC to develop applications for
Microchip microcontrollers and digital signal controllers. It is called an Integrated
Development Environment (IDE), because it provides a single integrated
“environment” to develop code for embedded microcontrollers.
MPLAB® X Integrated Development Environment brings many changes to the
PIC® microcontroller development tool chain. Unlike previous versions of
MPLAB® which were developed completely in-house, MPLAB® X is based on the
open source Net-Beans IDE from Oracle. Taking this path has allowed us to add
many frequently requested features very quickly and easily while also providing us
with a much more extensible architecture to bring you even more new features in
the future. 7

Explanation of the steps to create and build an assembler code:
1. Open the MPLAB program, choose create a new project to start.
1
2. In choose project step, choose “Microchip Embedded” category, and then choose
“Standalone Project”, and click next.
2
3
3. In select device step, choose “Mid-Range 8-bit MCUs (PIC12/16/MCP)” family, and
“PIC16F877A” device. And click next.
4
5
4. After clicking next, select “PIC Kit3” in Hardware Tools.
6
5. In the next step, Select Compiler step, choose “mpasm (v5.55)
]C:/ProgramFiles/Microchip/MPLABX/mpasmx[“, and hit next.
7
6. In this step, just name your project, and press Finish. And your project file has
been created.
8
7. To start coding and programming, click right in “Source File” > New > Other…
9
10
8. Choose “Assembler” file from the categories, and “Assemblerfile.asm” In file types.
12
11
9. Next, just name the file and press Finish.
13
14
10. It will open to you a white blank area, which is shaded below, you should write
your assembly code. Then choose “Clean and Build Main Project” to check if there
are any mistakes in your coding.
15
14
11. Down below, as shown, you can see the output of the project it tested, and here it
can shows you if there are any mistakes or errors in your assembly code to correct
it. If there wasn’t any mistake, them your program is perfect and ready to go.
16
Explanation of the code used above for testing LEDs project.
Line 4 and 5 starts with “;” which means that it’s a comment and that won’t affect
the coding. Line 6 “List P = 16F877A” this will not generate any code; it will just tell
the compiler that I’m using PIC16F877A, for declaring only. Line 7 “include
PIC16F877A.inc”, this is for compiler to know that it should use the file of this pic.
Line 9: It’s just a configuration.
Line 11 starts with “org” which tills the compiler what comes next, so next we have
an instruction that tells us that we need to go to “START” which is a label that we can
see it in line 15. Going back to line 14, “org 0x05” here is an order for the next step,
which is going to register 0x05 to start, that means next instruction will start in
address 5, why we leave some spaces in the memory and started from 5, that’s
because the compiler its self will use that space for something else.
In line 16 “clrf PORTB” this will clean register f in port B. We can replace this line
with “clrf 0x06” which will remove everything in that register and make it zero. In
line 17, “bsf STATUS, RP0” here it sets the register RPO to 1 in bank 1. And in line 18,
it moves the data in W register. In the next three lines, it moves the value to TRISB,
and switches the bank to 0. Finally it turns the LED into RB0.

Finally, we used the PIC Programmer to run our project.
We interred the data as shown above, and loaded the file of our assembly code, and
then we connect the development board with the PIC programmer to test the code.
The output in the development board was: testing the LED’s, which is flashed each
LED in order. In the right, is
the Board that we used, and
the result of the assembly
coding.


We were working in port B, so you can see that the LDE’s in port be are
flashing.
 Conclusion
In this tutorial I have mentioned a lot of information that helps me in
understanding the concept of PIC16F877A. Not only the PIC itself, it had also
helped me understanding the MPLAB and the Assembly code. Hope that every
single information that I write sticks in my mind and develop my knowledge in
microcontroller’s world.
References
1. Microchip Technology Inc.(2014). Mid-Range PIC Microcontrollers. Retrieved
(9.April.2014). From http://www.microchip.com/pagehandler/enus/family/8bit/architecture/midrange.html
2. hobbyprojects.com. (2011). Introduction to the PIC16F877A. Retrieved (13.April).
From http://www.hobbyprojects.com/microcontrollertutorials/pic16f877a/introduction-to-the-pic16f877a.html
3. Asad Hindash. (2014). Microcontrollers System. Handout2-1, page7,8,9.
4. Circuitstoday.com. (2011). Register Memory Organization in PIC 16F877. Retrived
(10.April.2014). From http://www.circuitstoday.com/register-memory-organizationin-pic-16f877
5. Asad Hindash. (2014). Microcontrollers System. Handout2-1, Page16, 17.
6. Mouser Electronics, Inc. (2014). mikroElektronika EasyMx PRO™ v7 Development
Board for STM32® ARM®. Retrived (2014). from
http://eu.mouser.com/new/mikroelektronika/mikroelektronika-EMP7STM32/
7. Microchip Technology Inc. (2014). MPLAB® X Integrated Development Environment
(IDE). Retrived (2014). From http://www.microchip.com/pagehandler/enus/family/mplabx/
Download