You are on page 1of 113

PROGRAM : TEKNOLOGI

ELEKTRONIK
NAMA KOD : DEB 2323
KURSUS : ASAS MIKROPENGAWAL
(BASIC MICROCONTROLLER)
KOMPETENSI 1 :
MENGENALPASTI ASAS PENGAWAL-MIKRO
(CLASSIFY BASIC MICRO-CONTROLLER)
PENGENALAN
KEPADA
DUNIA
MIKROPENGAWAL
Fungsi gambar di
atas?
s
ad tablet
i-p m
a
r
t
La to p
p- p
l m-
top h pa
o
n
e
Micro-processor
@
Mikro-pemprosess
Mikropengawal biasanya lebih
murah dan ringkas berbanding
mikropemproses
CPU, RAM dan memori
dalam mikropengawal
(µC) berada dalam satu
cip. Ini tidak untuk
computer (uP), komponen
tersebut berada dalam cip
yang berasingan dalam
komputer
Definisi mikro-pemproses :
- Mikro-pemproses (biasa disingkatkan µP atau uP) atau
CPU ialah sebuah cip berbentuk segiempat bujur yang
dipasang pada bahagian atas sebelah kanan papan
induk(mother-board) digunakan di dalam komputer
persendirian anda.
Definisi mikro-pemproses……..
- Fungsi utama uP di dalam sebuah peranti komputer
ialah memproses data dan maklumat yang diterima
daripada peranti input.
Definisi mikro-pemproses……
- Ciri-ciri uP boleh dikenalpasti melalui data yang tertulis
pada cip berkenaan yang menunjukkan kelajuan dan jenis
litar bersepadu yang digunakan.
Rajah blok mikro-pemproses
Rajah blok mikro-pemproses….
Fungsi ?????
Kereta kawalan

Penghawa
dingin ? Peti sejuk

Pemain
DVD
Micro-controllers
@
Mikro-pengawal
Pengenalan
Apakah itu mikro-pengawal?
 Mikro-pengawal (sinonim µC, uC or MCU) ialah
komputer mini pada satu litar bersepadu yang
mengandungi :
* teras pemproses (processor core)

* ingatan (ada memory)

* perisian pengaturcaraan
masukan/keluaran
(programmable input/output peripherals)
Pengenalan
 µC adalah seperti komputer kecil yang
menjalankan program kecil dan sering
digunakan untuk automasi dan robotic

 Perbezaan asas antara mikropengawal 8-bit


dan 16-bit dapat dirasakan semasa operasi
matematik

 µC 16-bit memberikan lebih ketepatan


daripada nombor 8-bit
 Mikropengawal 16-bit mempunyai dua kali
ganda paip data daripada mikropengawal 8-bit
Pengenalan……
. µC boleh melaksanakan tugasan ringkas dan
dikaitkan sebagai komputer di atas cip
(computer-on-a-chip)
 µc diformat semulajadi dalam keadaan
kosong (blank) seterusnya akan
3
diprogramkan menjadi program
kawalan spesifik (programmed with a
specific control program)

 Selepas µC diprogramkan sebagai satu


produk tertentu µC akan berfungsi dengan
lebih mudah dan pintar.
 Perekabentuk akan menggunakan µC sebagai :

- Mendapatkan maklumat/masukan daripada


pelbagai penderia
(Gather input from various sensors)

- Memproses masukan menjadi satu tindakan.


(Process this input into a set of actions)
G/rajah Blok PIC16F887
 PIC adalah microcontroller yang di keluaran oleh 
Microchip Technology. PIC ni popular kerana
harganya murah. Anda boleh gunakan assembly
language atau C language untuk memprogram
PIC. 
 Sebab itu, programnya lebih pantas & padat.
Penggunaan memori dalam PIC juga adalah kurang
berbanding jika menggunakan C language.
Antara features yang ada pada PIC ini :
i. I/O Ports
ii. Timer
iii. Pulse Width Modulation (PWM) Module
iv. Master Synchronous Serial Port (MSSP) Module
v. Addressable Universal Synchronous Asynchronous Receiver
Transmitter (USART)
vi. Analog-to-Digital Converter (ADC) Module
vii. Comparator Module
Mikropengawal dalam Kereta
 Dianggarkan, lebih 50 mikropengawal digunakan
di dalam sesebuah penciptaan kereta
 Salah satu bahagian : dalam ECU (Enginer Control
Unit) atau Unit Kawalan Enjin. Dalam kegunaan
ini, mikropengawal akan mengambil bacaan sensor
seperti sensor oksigen ‘knock sensor’, membuat
kiraan supaya dapat mengawal kadar pencampuran
minyak dan kadar waktu nyalaan palam pencucuh.
Mikropengawal dalam Peti Ais
Sistem komputer berasaskan µC
Rajah blok µc
Rajah dalaman sebenar µc
5
µP VS µC
 Mikropemproses adalah komputer persendirian
adalah “general purpose computer iaitu” =
komputer serbaguna. Mampu menjalankan beribu
program dalam 1 masa.
 Micropengawal adalah “special purpose computer”
= computer tujuan khas. Bermakna ; mampu
menjalankan 1 program dalam 1 masa.
Perbezaan sistem asas antara µP dan µC
4
µP vs µC
µP vs µC
 µP adalah untuk kegunaan am seperti keluarga Intel’s
x86.
(µP is general-purpose microprocessors such as Intel’s
x86 family).
 µP tidak mengandungi RAM,ROM atau I/O pot pada
cipnya.
(µP dose not contain RAM, ROM or I/O Ports on the
chip it self).
 µC mengandungi CPU dan nilai tetap bagi
RAM,ROM,I/O dan pemasa pada cip tunggal.
(µC has CPU and fixed amount of RAM, ROM, I/O Ports
and Timer all on a single chip).
µP vs µC
Pengkelasan (Classification) µC
•Kelebaran bas dalaman (Internal bus width):
•4 bit
•8 bit (8 nombor aksara) ; MPLab
•16 bit
•32 bit

•Set Arahan (Instruction set):


•C.I.S.C
•R.I.S.C

•Rekabentuk (Architecture):
•Harvard
•Von Neumann (or Princeton)
Pengkelasan (Classification) µc
Ingatan (Memory) :
•FLASH
•EEPROM
•SRAM
•Embedded memory
•External memory

•Intergrasi perisian (Integrated Peripherals):


•Number of I/O ports.
•Number and type of timers.
•A.D.C (masa/delay time)

•Keluarga (Families) :
•ATMEL AVR - atmega, Xmega, Attiny…
•PIC - PIC16F, PIC18F
•ARM - ARM7, ARM9, ARM11
•8051 - AT89s52, p89v51rd2
•Motorola - 68HC11
Kemajuan/Pembangunan µC
Jenis-jenis µC
9
 Parallax Propeller
 Freescale 68HC11 (8-bit)
 Intel 8051
 Silicon Laboratories Pipelined 8051
Microcontrollers
 ARM processors (from many vendors) using
ARM7 or Cortex-M3 cores are generally
microcontrollers
 STMicroelectronics STM8 (8-bit), ST10 (16-bit)
and STM32 (32-bit)
 Atmel AVR (8-bit), AVR32 (32-bit), and
AT91SAM (32-bit)
Jenis-jenis µC

 Freescale ColdFire (32-bit) and S08 (8-bit)


 Hitachi H8, Hitachi SuperH (32-bit)
 Hyperstone E1/E2 (32-bit, First full integration of
RISC and DSP on one processor core [1996])
 Infineon Microcontroller: 8, 16, 32 Bit
microcontrollers for automotive and industrial
applications.
 MIPS (32-bit PIC32)

 NEC V850 (32-bit)


Jenis-jenis µC
9
 Microchip PIC (8-bit PIC16, PIC18, 16-bit
dsPIC33/PIC24)
 PowerPC ISE
 PSoC (Programmable System-on-Chip)

 Rabbit 2000 (8-bit)

 Texas Instruments Microcontroller MSP 430

(16-bit), C2000 (32-bit), and Stellaris (32-bit)


 Toshiba TLCS-870 (8-bit/16-bit)

 Zilog eZ8 (16-bit), eZ80 (8-bit)


Sistem terbenam (Embedded system)
 µC juga dipanggil mikro-pengawal terbenam
yang merupakan sebahagian daripada sistem
terbenam (embedded system).
 Prinsip asas sistem terbenam tidak berfungsi
seperti komputer biasa tetapi dikawal oleh
komputer mini yang ditanam pada sistem
tersebut.
 Sistem terbenam tidak membolehkan
pengguna peralatan untuk mengaturcarakan
semula peralatan kepada situasi berbeza.
Aplikasi sistem terbenam bagi peti
sejuk.
Aplikasi sistem terbenam pintu kereta
Aplikasi ping - pong elektronik
Sistem terbenam (Embedded System)

 Peranti masukan bagi peralatan yang


mengaplikasikan sistem terbenam ialah suis,
geganti, LED, paparan LCD dan penderia
(sensor) bagi data seperti suhu, kelembapan
dan cahaya.
 Sistem terbenam tidak menggunakan papan
kekunci, skrin, pemacu (disk) dan pencetak.
Aplikasi sistem terbenam
 Perkakasan : pemain DVD, Hi-fi, TV dan penghawa
dingin.
 Peralatan paparan perubatan : ECG
(electrocardiogram), alat tekanan darah dan meter tekanan
darah.
 Sistem keselamatan : penggera dan kad lestari (smart
card + reader)
 Sistem kawalan tertutup (Closed loop process control :
kawalan kelajuan motor dan robot.
 Komputer personal : Papan kekunci, pencetak dan hub
USB.
 Kenderaan : Kawalan api (Ignition), transisi automatik
dan sistem anti kekunci brek.
 Komunikasi : modem, radio, radar dan satelit.
Micro-coded processor.
  Aplikasi rekabentuk sistem komputer boleh dilaksanakan
dengan setiap arahan (instruction) dikenalpasti melalui
beberapa langkah yang menggunakan subrutin kecil (small
subroutines) yang disimpan pada unit dekoder.

Rajah blok Micro-coded prosessor


Hard-coded processor.
  Aplikasi setiap set arahan (programme) boleh
dikombinasikan atau set arahan tersendiri (sequential) bagi
litar digital.
 Rekabentuk litar adalah kompleks (complex), susah untuk
menyahpijat (debug)/memadamkan erata (error) tetapi masa
pemprosesan (execution) amat efektif (pantas)

Rajah blok Hard-coded prosessor


Rekabentuk
(architectures)
mikropengawal
Rekabentuk Van Neumann

•Digunakan pada 80X86 (CISC-PCs).


•Hanya satu talian bus antara CPU dan ingatan (memory).
•RAM dan ingatan bagi program berkongsi talian bus
Rekabentuk Havard

• Kebanyakknya digunakan pada CPU-RISC.


• Talian bus data dan talian bus ingatan berasingan.
• Data RAM mengandungi 8 bit daftar (register).
• Ingatan program (ROM) mengandungi 12 bit, 14 bit dan 16
bit (pada EPROM, FLASH atau ROM)
Ingatan µC (µC memory)
• Format perisian (programme running ) dan data (programme
data) disimpan pada ingatan (memory) µC .

• Perisian dan data µC disimpan dalam bentuk kod binari.

• Istilah ingatan (memory terminology) :


Sel ingatan (Memory Cell)

- Peranti atau litar elektrikal yang digunakan untuk


menyimpan bit tunggal (0 @r 1) contohnya : flip-flop
 Kursor Ingatan (Memory Word)

- Kumpulan bit yang mewakili arahan atau data


contohnya : Indeks Pendaftar (Index Register) mengandungi
16 bit yang merupakan ingatan kursor (memory word)
Ingatan µC (µC memory)

• Bait (Byte) - Gabungan 8 bit menghasilkan satu data yang


bermakna.

• Unggisan (Nibble) - Separuh daripada bilangan bait ( 4bit).

• Kapasiti (Capacity) - Bilangan @ kuantiti bit yang boleh


disimpan oleh peranti ingatan bagi sistem µC .

• Unit matrik kapasiti (Capacity metrics unit).


- 1K = 210 = 1024.
- 1M = 220 = 1,048,576
- 1G = 230 = 1,073,741,824
Ingatan µC (µC memory)

ROM
(Read Only Memory)

1. MROM : Mask-programmed ROM.


2. PROM : Programmable ROM
3. EPROM : Erasable PROM
4. EEPROM : Electrically-erasable PROM
5. EAROM : Electrical Alterable ROM
6. FLASH MEMORY
Tugasan 1 : Individu (10%)
a. Nyatakan fungsi bagi ingatan (memory)
ROM bagi :
 INGATAN EEPROM (EEPROM MEMORY)
 INGATAN FLASH (FLASH MEMORY)

b. Terangkan perbezaan antara kedua-dua ingatan


tersebut.
Format tugasan :
i. Berjilid / binding
ii. Tulisan tangan sahaja
iii. Hanya muka hadapan sahaja boleh bercetak.
- Logo kolej
- Jabatan / unit
- Nama program / kod kursus
- Tajuk tugasan
- Nama pelajar
- Nama pengajar
Ingatan µC (µC memory)

RAM
(Read Acsses Memory)

1. SRAM : Static RAM


2. DRAM : Dynamic RAM
Contoh pengiraan ingatan µC
 Spesifikasi satu cip ingatan (memory chip) separuh
pengalir ialah 2K x 8.

1. Berapakah perkataan (words) boleh disimpan pada


cip tersebut?

2. Nyatakan saiz perkataan (word size) pada cip


tersebut?

3. Berapakah bilangan kapasiti boleh disimpan oleh


cip?
Jawapan pengiraan ingatan µC
• Formula unit matrik kapasiti :
 1K = 210 = 1024.
 1M = 220 = 1,048,576
 1G = 230 = 1,073,741,824
 Penyelesaian ;

1. 2K = 2 x 1024 = 2048 lokasi (location).

2. Saiz perkataan (word size) ialah 8-bit (1 bait).

3. Jumlah kapaciti ialah 2048 x 8bit = 16,384 bit.


Latihan 2 : Kirakan kapasiti ingatan (memory)
bit yang paling banyak bagi µC ?

Manakah bit simpanan ingatan (bits memory


stores) yang paling banyak ?

5M x 8bits ingatan (memory)


@
1M x 16bits ingatan (memory)
Jawapan :

Penyelesaian ; Kapasiti (Capacity)

5M x 8 = 5 x 1,048,576 x 8 = 41,943,040 bit

1M x 16 = 1,048,576 x 16 =16,777,216 bits

5M x 8 ingatan menyimpan lebih bit.


Latihan 1 : Kirakan ingatan (memory) bagi cip

(ROM) µC ?
Contoh aplikasi ingatan (ROM)
dan (RAM) pada ingatan µC
Mikropengawal
8051(Intel)
8 bit dan 16 bit.
Mikropengawal 8051- 16 bit
Spesifikasi pin (Pin Description)
µC 8051 - 16 bit
Rekabentuk (Architectures) µC 8051 16 bit
Rekabentuk (Architectures) µC 8065 – 16 bit
Contoh projek aplikasi µC 8051-16 bit
Mikropengawal 8051- 8 bit
Spesifikasi pin (Pin Description)
µC 8051 - 8 bit
P1.0 1 40 Vcc
P1.1 2 39 P0.0(AD0
P1.2 3 38 )P0.1(AD1)
P1.3
P1.4
4
5
8051 37
36 P
P0.2(AD2
) 0.3(AD3)
P1.5 6 (8031) 35 P0.4(AD4)
P1.6 7 34 P0.5(AD5)
P1.7 8 33 P0.6(AD6)
RST 9 32 P0.7(AD7)
(RXD)P3.0 10 31 EA/VPP
(TXD)P3.1 11 30 ALE/PROG
(INT0)P3.2 12 29 PSEN
(INT1)P3.3 13 28 P2.7(A15)
(T0)P3.4 14 27 P2.6(A14
(T1)P3.5 15 26 )P2.5(A13
(WR)P3.6 16 25 )P2.4(A12
(RD)P3.7 17 24 )P2.3(A11)
XTAL2 18 23 P2.2(A10)
XTAL1 19 22 P2.1(A9)
GND 20 21 P2.0(A8)
Contoh blok dalaman µC 8 bit jenis AVR
Microchip PIC
11
 PIC is a family of Harvard architecture
microcontroller made by Microchip Technology.
The name PIC initially referred to "Peripheral
Interface Controller“ . PIC microcontrollers were
the first RISC microcontroller.
 PICs 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.
PIC Microcontroller product family
13
 8-bit microcontrollers
32-bit microcontrollers
 PIC32
PIC10
 PIC12
 16-bit digital signal controllers
 PIC14
dsPIC30
 PIC16
dsPIC33F
 PIC17

 PIC18

 16-bit microcontrollers
 PIC24F
 PIC24H
PIC Microcontroller product family
14
 The F in a name generally indicates the PICmicro uses flash
memory and can be erased electronically.
 The C generally means it can only be erased by exposing the
die to ultraviolet light (which is only possible if a windowed
package style is used). An exception to this rule is the
PIC16C84 which uses EEPROM and is therefore electrically
erasable. 1 :
Why use PIC16F877?
15
 Why PIC16F877A is very popular?
 This is because PIC16F877A is very cheap. Apart from that
it is also very easy to be assembled. Additional components
that you need to make this IC work is just a 5V power
supply adapter, a 20MHz crystal oscillator and 2 units of
22pF capacitors.
 What is the advantages of PIC16F877A?
 This IC can be reprogrammed and erased up to 10,000
times. Therefore it is very good for new product
development phase.
 What is the disadvantages of PIC16F877A?
 This IC has no internal oscillator so you will need an
external crystal of other clock source.
Features of Key Features PIC16F877

PIC16F877 MAX Operating Frequency 20MHz

16 FLASH Program Memory


(14-bit words)
8K

Data Memory (bytes) 368

EEPROM Data Memory (bytes) 256


8 RA0-5 (6)
RB0-7 (8)
RC0-7 (8)
I/O Ports RD0-7 (8)
RE0-2 (3)

Timers 3

CCP 2

Serial Communications MSSP, USART

Parallel Communications PSP

10-bit Analog-to-Digital Module 8 Channels

Instruction Set 35 Instructions

Pins (DIP) 40 Pins


Bubble diagram
of PIC16F877
17

 As you can see the


PIC16F877A is rich
in peripherals so you
can use it for many
different projects.
Pin Diagram of PIC16F877
18
 Quad Flat Package  Plastic Leaded Chip Carrier
(QFP) Package (PLCC)
Pin Diagram
of
19 PIC16F877  
 

 Plastic dual in-


line package
(DIP)
 

PIC16F877
Architecture
20
PIC16F877
Internal Block Diagram
21
 The basic architecture of PIC16F877 consists of
Program memory, file registers and RAM, ALU
and CPU registers.

PIC16F877 Internal Block Diagram


Memory of the PIC16F877
22
 divided into 3 types of memories:

1. Program Memory – A memory that contains the program (which


we had written), after we've burned it. As a reminder, Program Counter
executes commands stored in the program memory, one after the other.
2. Data Memory – This is RAM memory type, which contains a special
registers like SFR (Special Function Register) and GPR (General
Purpose Register). The variables that we store in the Data Memory
during the program are deleted after we turn of the micro. These two
memories have separated data buses, which makes the access to each one
of them very easy.
3. Data EEPROM (Electrically Erasable Programmable Read-Only
Memory) – A memory that allows storing the variables as a result of
burning the written program.
…cont’d
23
 Each one of them has a different role. Program
Memory and Data Memory two memories that are
needed to build a program, and Data EEPROM is
used to save data after the microcontroller is turn off.
PIC16F877A
Program
24 Memory
 Is Flash Memory
 Used for storing compiled
code (user’s program)
 Program Memory capacity is
8K x 14 bit  Each location
is 14 bits long
 Every instruction is coded
as a 14 bit word
 PC can address up to 8K
addresses
 Addresses H’000’ and
H’004’ are treated in a special
way
PIC16F877A Data Memory
25
(RAM)
 Memory storage for variables
 Data Memory is also known as Register File and consists of
two components.
 General purpose register file (same as RAM).
 Special purpose register file (similar to SFR in 8051).
 Addresses range from 0 to 511 and partitioned into 4 banks 
each bank extends up to 7Fh (128 bytes).
 The user can only access a RAM byte in a set of 4 banks and
only one bank at a time. The default bank is BANK0.
 To access a register that is located in another bank, one should
access it inside the program. There are special registers which
can be accessed from any bank, such as STATUS register.
26

PIC16F877A register file


PIC16F877A Registers
27

 Some CPU Registers:


W
 PC
 FSR
 IDF
 PCL
 PCLATH
 STATUS
W Register
28
 W, the working register, is used by many
instructions as the source of an operand. This is
similar to accumulator in 8051.
 It may also serve as the destination for the result of
the instruction execution. It is an 8-bit register.

W, working register
Program Counter
29
 Program Counter (PC) is 13 bit and capable of addressing an 8K
word x 14 bit program memory space.
 PC keeps track of the program execution by holding the address
of the current instruction. It is automatically incremented to the
next instruction during the current instruction execution.

 Program Counter Stack


an independent 8-level stack is used for the program counter. As
the PC is 13-bit, the stack is organized as 8x13bit registers. When
an interrupt occurs, the PC is pushed onto the stack. When the
interrupt is being served, other interrupts remain disabled. Hence,
other 7 registers of the stack can be used for subroutine calls
within an interrupt service routine or within the mainline
program.
 FSR Register
30
 (File Selection Register, address = 04H, 84H)
is an 8-bit register used as data memory address pointer.
This is used in indirect addressing mode.

 INDF Register
 (INDirect through FSR, address = 00H, 80H)
INDF is not a physical register. Accessing INDF is actually
access the location pointed to by FSR in indirect addressing
mode.
 PCL Register
 (Program Counter Low Byte, address =02H, 82H)
PCL is actually the lower 8-bits of the 13-bit Program Counter. This is a
31 both readable and writable register.

 PCLATH Register
 (Program Counter LATcH, address = 0AH, 8AH)
PCLATH is a 8-bit register which can be used to decide the upper 5-bits of
the PC. PCLATH is not the upper 5bits of the PC. PCLATH can be read
from or written to without affecting the PC. The upper 3 bits of PCLATH
remain zero and they serve no purpose. When PCL is written to, the lower
5bits of PCLATH are automatically loaded to the upper 5bits of the PC, as
shown below:
32
 In order to start programming and build automated
system, there is no need to study all the registers of
the memory map, but only a few most important ones:
 STATUS register – changes/moves from/between the
banks.
 PORT registers – assigns logic values (“0”/”1”) to the
ports
 TRIS registers – data direction register (input/output)
STATUS Register
33
 Is an 8-bit register that stores the status of the processor.
 In most cases, this register is used to switch between the banks
(Register Bank Select), but also has other capabilities.

 IRP - Register Bank Select bit.


 RP1:RP0: - Register Bank Select bits.
 TO: Time-out bit Used in conjunction with PIC’s sleep mode
 PD: Power-down bit
 Z: Zero bit
 DC: Digit carry/borrow bit
 C: Carry/borrow bit
STATU
S
Registe
34
r
PIC16F877 Peripheral features
35
1. I/O Ports:
 PIC16F877 has 5 I/O ports:
 PORT A has 6 bit wide, Bidirectional
 PORT B, C, D have 8 bit wide, Bidirectional
 PORT E has 3 bit wide, Bidirectional

 In addition, they have the following alternate functions:


PIC16F877 Peripherals
36
 Each port has 2 control registers:
 TRISx sets whether each pin is an input(1) or output(0)
 PORTx sets their output bit levels or contain their input
bit levels.
 Pin functionality “overloaded” with other features.
 Most pins have 25mA source/sink thus it can drive
LEDs directly.
PIC16F877 Peripherals
37
2. Analog to Digital Converter (ADC)
 Only available in 14bit and 16bit cores
 Fs (sample rate) < 54KHz
 The result is a 10 bit digital number
 Can generate an interrupt when ADC conversion is done
 The A/D module has 4 registers:
 A/D Result High Register (ADRESH)
 A/D Result Low Register (ADRESL)
 A/D Control Register0 (ADCON0)
 A/D Control Register1 (ADCON1)
 Multiplexed 8 channel inputs
 Must wait T acq to change up sampling capacitor.
 Can take a reference voltage different from that of the
PIC16F877 Peripherals
38
3. Timer/counter modules
 Generate interrupts on timer overflow
 Can use external pins as clock in/ clock out (ie. for
counting events or using a different Fosc)
 There are 3 Timer/counter modules:
 Timer0: 8-bit timer/counter with 8-bit pre-scaler
 Timer1: 16-bit timer/counter with 8-bit pre-scaler, can be
incremented during SLEEP via external crystal/clock
 Timer2: 8-bit timer/counter with 8-bit period register, pre-
scaler and post-scaler.
PIC16F877 Peripherals
39
4. Universal Synchronous Asynchronous
Receiver Transmitter (USART/SCI)
with 9-bit address detection.
 Asynchronous communication: UART (RS 232
serial)
 Can do 300bps – 115kbps
 8 or 9 bits, parity, start and stop bits, etc.
 Outputs 5V  needs a RS232 level converter (e.g
MAX232)
PIC16F877 Peripherals
40
 Synchronous communication: i.e with clock signal
 SPI = Serial Peripheral Interface
 3 wire: Data in, Data out, Clock
 Master/Slave (can have multiple masters)
 Very high speed (1.6Mbps)
 Full speed simultaneous send and receive (Full duplex)
 I2C = Inter IC
 2 wire: Data and Clock
 Master/Slave (Single master only; multiple masters
clumsy)
 Lots of cheap I2C chips available; typically < 100kbps
PIC16F877 Peripherals
41
5. Capture, Compare, PWM modules
 Capture is 16-bit, max. resolution is 12.5 ns
 Compare is 16-bit, max. resolution is 200 ns
 PWM max. resolution is 10-bit

6. Synchronous Serial Port (SSP) with SPITM


(Master mode) and 12CTM (Master/Slave)

7. Parallel Slave Port (PSP) 8-bits wide, with


external RD, WR and CS controls
Clock And Instruction Cycles
42
 Clock from the oscillator enters a microcontroller
via OSC1 pin where internal circuit of a
microcontroller divides the clock into four even
clocks Q1, Q2, Q3 and Q4 which do not overlap.
 These four clocks make up one instruction cycle
(also called machine cycle) during which one
instruction is executed.
 Execution of instruction starts by calling an instruction that
is next in string.
43
 Instruction is called from program memory on every Q1
and is written in Instruction Register (IR) on Q4.
 Decoding and execution of instruction are done between the
next Q1 and Q4 cycles. The following diagram shows the
relationship between instruction cycle and clock of the
oscillator (OSC1) as well as that of internal clocks Q1 – Q4.
 Program Counter (PC) holds information about the address
of the next instruction.
Pipelining in PIC
44
 There are 35 single word instructions. A two-stage pipeline overlaps
fetch and execution of instructions. As a result, all instructions execute in
a single cycle except for program branches. These take two cycles since
the fetch instruction is “flushed” from the pipeline while the new
instruction is being fetched and then executed.
 A typical picture of the pipeline is shown in Figure 3.

Figure3: Instruction Pipeline Flow


SUMMARY 1
 • The microcontroller contains a processor, memory and input/output
devices
 • The program is stored in ROM memory in numbered locations
(addresses)
 • The P16F877 stores a maximum of 8k 14 instructions in flash ROM
 • The P16FXXX family uses only 35 instructions
 • The P16F877 has 368 bytes of RAM and 5 ports (33 I/O pins)
 • The ports act as buffers between the MCU and external systems
 • The program is executed in sequence, unless there is a jump
instruction
 • The program counter tracks the current instruction address
ASSESSMENT 1
1 State the three main elements in any microprocessor system. (3)
2 State the difference between a microprocessor and microcontroller. (3)
3 Describe briefly the process of fetching an instruction. (3)
4 State the advantages of flash ROM, compared with other memory types.
(3)
5 Explain why serial data communication is generally slower than
parallel. (3)
6 State why Ports A and E in the PIC 16F877 cannot be used for digital
input without initialisation. (3)
7 How many bits does the 8k MCU program memory contain? (3)

You might also like