You are on page 1of 34

9/19/2019

ĐẠI HỌC QUỐC GIA TP.HỒ CHÍ MINH


TRƯỜNG ĐẠI HỌC BÁCH KHOA
KHOA ĐIỆN-ĐIỆN TỬ
BỘ MÔN KỸ THUẬT ĐIỆN TỬ

Embedded System Design


Chapter 4: Microcontroller

om
1. Introduction to PIC microcontroller
2. PIC16F84
3. PIC16F877

.c
4. Apply PIC microcontroller for embedded systems

ng
co
1

1
an
th
ng

1. Introduction to PIC microcontroller


o
du

• PIC is a family of modified Harvard architecture microcontrollers


made by Microchip Technology
• The PIC1650 was originally developed by General Instruments
u
cu

• The name PIC initially referred to "Peripheral Interface


Controller"

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 2

CuuDuongThanCong.com https://fb.com/tailieudientucntt 1
9/19/2019

Why is PIC popular?


• PICs are popular with developers due to
– low cost
– wide availability
– large user base
– extensive collection of application notes
– availability of low cost or free development tools
– serial programming capability

om
PIC18F4XK20 Starter Kit
•128x64 Organic LED Display (SPI)

.c
• 32.768 kHz External Oscillator (Timer1)
• Analog input filtering and gain control into
RE1
• PWM output filtering from RC2

ng
• 4 push buttons for user interfacing
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 3

3
an
th
ng

PIC microcontroller
o

• Comparison of PIC families


du
u
cu

● Complete portfolio: 6 to 100 pins, 384B to 512KB of program memory, up to


80MHz
● Upward compatible architectures to preserve investment in code development
● Pin compatibility in multiple packages facilitates drop-in replacement
● Range of memory technologies: Self Programming Flash, OTP, ROM
● Easy Migration across 8, 16, and 32-bit families

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 4

CuuDuongThanCong.com https://fb.com/tailieudientucntt 2
9/19/2019

PIC Microcontroller

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 5

5
an
th
ng

Some members of PIC16 Series


o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 6

CuuDuongThanCong.com https://fb.com/tailieudientucntt 3
9/19/2019

Some members of PIC16 Series

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 7

7
an
th
ng

PIC speed
o
du

• Can use crystal, clock oscillator, or even an RC circuit


• Some PICs have a built-in 4MHz RC clock, not very accurate,
u
cu

but requires no external components


• Instruction speed = ¼ clock speed
– 12C50x 4MHz
– 12C67x 10MHz
– 16Cxxx 20MHz
– 17C4x/17C7xxx 33MHz
– 18Cxxx 40MHz

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 8

CuuDuongThanCong.com https://fb.com/tailieudientucntt 4
9/19/2019

PIC Clock

Three ways to
provide the
clock signal

om
to a PIC

.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 9

9
an
th
ng

PIC’s program memory


o
du

• PICs have two difference types of program


storage:
u
cu

– EPROM (Erasable Programmable ROM)


• need high voltage from programmer to program (~13V)
• need windowed chips and UV light to erase
• PIC examples: any ‘C’ part 12C50x, 17C7xx,…
– FLASH
• rewriteable
• much faster to develop on
• PIC examples: any ‘F’ part 16F84, 16F87x, 18Fxxx

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 10

10

CuuDuongThanCong.com https://fb.com/tailieudientucntt 5
9/19/2019

PIC16 Series

om
.c
ng
co
http://tme.com.vn
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 11

11
an
th
ng

2. The Microchip PIC16F84


o
du

• The Microchip PIC16F84 is a low-cost,


single-chip, 8-bit microcontroller.
– Only 35 single word instructions
u

– All instructions single-cycle except for


cu

program branches which are two-cycle


– Operating speed: DC - 20 MHz clock
input
– DC - 200 ns instruction cycle
– 1024 words of program memory
– 68 bytes of Data RAM
– 64 bytes of Data EEPROM
– 14-bit wide instruction words
– 8-bit wide data bytes

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 12

12

CuuDuongThanCong.com https://fb.com/tailieudientucntt 6
9/19/2019

The PIC 16F84A pin connection diagram

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 13

13
an
th
ng

PIC16C8X PINOUT DESCRIPTION


o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 14

14

CuuDuongThanCong.com https://fb.com/tailieudientucntt 7
9/19/2019

PIC16C8X PINOUT DESCRIPTION ..

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 15

15
an
th
ng

Architecture of the PIC16F84


o
du

Buses: Communication lines for transferring data within the


processor.
u
cu

Oscillator: Used to drive the microprocessor, clocking data and


instructions in the processor.

Timing: The PIC has an internal divide by 4 whereby 4 oscillator


pulses form one clock pulse. This makes instruction times easy to
calculate.

– Most instructions (except calls and returns and other instructions


involving jumps and branches) take one clock cycle, so with a 4MHz
oscillator (divided by 4), instructions take 1s.
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 16

16

CuuDuongThanCong.com https://fb.com/tailieudientucntt 8
9/19/2019

Architecture of the PIC16F84

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 17

17
an
th
ng

W Register
o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 18

18

CuuDuongThanCong.com https://fb.com/tailieudientucntt 9
9/19/2019

The 16F84A Status register

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 19

19
an
th
ng

The PIC16F84 Status register


o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 20

20

CuuDuongThanCong.com https://fb.com/tailieudientucntt 10
9/19/2019

16F84A memory features

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 21

21
an
th
ng

PIC16F84 Memory
o
du

There are two memory blocks in the PIC16C84;


u

program memory and data memory.


cu

• Each block has its own bus, so that access to each can occur
during the same clock cycle.

• The data memory can be further broken down into general


purpose memory and special purpose registers

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 22

22

CuuDuongThanCong.com https://fb.com/tailieudientucntt 11
9/19/2019

ProgramProgram
PIC16F84 memoryMemory
Program memory

Pointed to by reset vector 0x00


0x01
0x02
0x03
Pointed to by interrupt vector 0x04

om
.c
0x3FF

ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 23

23
an
th
ng

PIC16F84 Data Memory


o
du

0x00 Ind. address.


0x01 TMR0
0x02 PCL Special-purpose
u

0x03 STATUS registers


0x04 FSR Indirect pointer
cu

(This is a simplified memory map 0x05 PORTA data PORT A


0x06 PORTB data PORT B
that excludes bank1.
0x07 Not used
Study the memory banks
0x08 EEPROM prog.
in the Microchip data sheets). 0x09 “
0x0A PCLATH Program counter
0x0B INTCON Interrupt control
0x0C .. \:

.. \

.. | General-purpose
| file registers.
.. /

.. /

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 24

24

CuuDuongThanCong.com https://fb.com/tailieudientucntt 12
9/19/2019

PIC16F84 Register-Mapped I/O


• PORTA and PORTB can be programmed 0x00 Ind. address.
0x01 TMR0
as an input or an output. 0x02 PCL Special-purpose
• PORTA is 5 bits wide and PORTB is 8 0x03 STATUS registers
0x04 FSR Indirect pointer
bits wide. (Bit 4 of port A can be used for an 0x05 PORTA data PORT A
external timer input.) 0x06 PORTB data PORT B
• PORTA has pins/bits labelled RA4:RA0 0x07 Not used
0x08 EEPROM prog.
• PORTB has pins/bits labelled RB7:RB0
0x09 “

om
• Referencing bits …. Bits are numbered 0x0A PCLATH Program counter
0x0B INTCON Interrupt control
7-0 from left to right, i.e., msb-lsb. 0x0C .. \:

.. \
• One of the most common

.c
.. | General-purpose
| file registers.
programming errors is incorrect .. /

.. /
specification of bit numbers.

ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 25

25
an
th
ng

PIC16F84A CONFIGURATION WORD


o
du

• bit 13-4 CP: Code Protection bit


u

– 1 = Code protection disabled


cu

– 0 = All program memory is code protected


• bit 3 PWRTE: Power-up Timer Enable bit
– 1 = Power-up Timer is disabled
– 0 = Power-up Timer is enabled
• bit 2 WDTE: Watchdog Timer Enable bit
– 1 = WDT enabled
– 0 = WDT disabled
• bit 1-0 FOSC1:FOSC0: Oscillator Selection bits
– 11 = RC oscillator (Resistor/capacitor)
– 10 = HS oscillator (High Speed Crystal/Resonator)
– 01 = XT oscillator (Crystal/Resonator)
– 00 = LP oscillator (Low Power Crystal)

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 26

26

CuuDuongThanCong.com https://fb.com/tailieudientucntt 13
9/19/2019

OPTION REGISTER (ADDRESS 81h)

• bit 7 RBPU: PORTB Pull-up Enable bit


– 1 = PORTB pull-ups are disabled
– 0 = PORTB pull-ups are enabled by individual port latch values
• bit 6 INTEDG: Interrupt Edge Select bit
– 1 = Interrupt on rising edge of RB0/INT pin
– 0 = Interrupt on falling edge of RB0/INT pin

om
• bit 5 T0CS: TMR0 Clock Source Select bit bit 2-0 PS2:PS0: Prescaler
– 1 = Transition on RA4/T0CKI pin Rate Select bits
– 0 = Internal instruction cycle clock (CLKOUT)

.c
• bit 4 T0SE: TMR0 Source Edge Select bit
– 1 = Increment on high-to-low transition on RA4/T0CKI pin
– 0 = Increment on low-to-high transition on RA4/T0CKI pin

ng
• bit 3 PSA: Prescaler Assignment bit
– 1 = Prescaler is assigned to the WDT co
– 0 = Prescaler is assigned to the Timer0 module
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 27

27
an
th
ng

DIRECT/INDIRECT ADDRESSING
o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 28

28

CuuDuongThanCong.com https://fb.com/tailieudientucntt 14
9/19/2019

DIRECT/INDIRECT ADDRESSING
• A simple program to clear RAM locations 20h-2Fh using
indirect addressing
movlw 0x20 ;initialize pointer
movwf FSR ;to RAM
NEXT clrf INDF ;clear INDF register
incf FSR ;inc pointer
btfss FSR,4 ;all done?

om
goto NEXT ;NO, clear next
CONTINUE
: ;YES, continue

.c
• An effective 9-bit address is obtained by concatenating the 8-
bit FSR register and the IRP bit (STATUS<7>)

ng
• However, IRP is not used in the PIC16F84A.
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 29

29
an
th
ng

EEPROM Data Memory


o
du

• EEPROM data memory


– readable and writable during normal operation
u

– indirectly addressed through the Special Function


cu

Register (SFR)
• SRFs are:
– EECON1
– EECON2
– EEDATA
– EEADR

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 30

30

CuuDuongThanCong.com https://fb.com/tailieudientucntt 15
9/19/2019

EECON1 Register (Adr. 88h)

• bit 7-5 Unimplemented: Read as '0'


• bit 4 EEIF: EEPROM Write Operation Interrupt Flag bit
– 1 = The write operation completed (must be cleared in software)
– 0 = The write operation is not complete or has not been started
• bit 3 WRERR: EEPROM Error Flag bit
– 1 = A write operation is prematurely terminated (any MCLR Reset or any WDT Reset during normal operation)
– 0 = The write operation completed

om
• bit 2 WREN: EEPROM Write Enable bit
– 1 = Allows write cycles
– 0 = Inhibits write to the EEPROM

.c
• bit 1 WR: Write Control bit
– 1 = Initiates a write cycle. The bit is cleared by hardware once write is complete. The WR bit can only be set
(not cleared) in software.
– 0 = Write cycle to the EEPROM is complete

ng
• bit 0 RD: Read Control bit
– 1 = Initiates an EEPROM read RD is cleared in hardware. The RD bit can only be set (not cleared) in software.
– 0 = Does not initiate an EEPROM read
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 31

31
an
th
ng

Reading The EEPROM Data Memory


o

• Reading the EEPROM data memory


du

– write the address to the EEADR register


u

– set control bit RD


cu

– the data is available, in the next cycle, in the EEDATA


register

BCF STATUS, RP0 ; Bank 0


MOVLW CONFIG_ADDR ;
MOVWF EEADR ; Address to read
BSF STATUS, RP0 ; Bank 1
BSF EECON1, RD ; EE Read
BCF STATUS, RP0 ; Bank 0
MOVF EEDATA, W ; W = EEDATA

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 32

32

CuuDuongThanCong.com https://fb.com/tailieudientucntt 16
9/19/2019

Writing to the EEPROM Data Memory


• Writing to the EEPROM data memory
– write the address to the EEADR register and data to the
EEDATA register
– follow a specific sequence to initiate the write for each byte

BSF STATUS, RP0 ; Bank 1


BCF INTCON, GIE ; Disable INTs.

om
BSF EECON1, WREN ; Enable Write
MOVLW 55h ;
MOVWF EECON2 ; Write 55h

.c
MOVLW AAh ; Requirement
MOVWF EECON2 ; Write AAh
BSF EECON1,WR ; Set WR bit

ng
; begin write
BSF INTCON, GIE co ; Enable INTs.

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 33

33
an
th
ng

I/O Port
o

• PORT A:
du

– 5-bit wide, bi-direction port


u

– Corresponding data direction register is TRISA


cu

(TRISA bit = 1: input; TRISA bit = 0: output)


– On a Power-on Reset, PORTA pins are configured
as inputs and read as ‘0’
• PORT B:
– 8-bit wide, bi-direction port
– Corresponding data direction register is TRISB
– RB7:RB4, have an interrupt-on-change feature
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 34

34

CuuDuongThanCong.com https://fb.com/tailieudientucntt 17
9/19/2019

I/O Port
• Initializing Port A • Initializing Port B
BCF STATUS, RP0 ; BCF STATUS, RP0 ;
CLRF PORTA ; Initialize PORTA by CLRF PORTB ; Initialize PORTB by
; clearing output ; clearing output
; data latches
; data latches BSF STATUS, RP0 ; Select Bank 1
BSF STATUS, RP0 ; Select Bank 1 MOVLW 0xCF ; Value used to
MOVLW 0x0F ; Value used to ; initialize data

om
; initialize data ; direction
; direction MOVWF TRISB ; Set RB<3:0> as inputs
; RB<5:4> as outputs

.c
MOVWF TRISA ; Set RA<3:0> as inputs ; RB<7:6> as inputs
; RA4 as output

ng
; TRISA<7:5> are always
; read as ’0’. co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 35

35
an
th
ng

Assignments
o

1. What are 8-bit and 16-bit families of PIC microcontrollers?


du

2. What is maximum frequency of clock input for the PIC16F84A?


3. How many word of program memory are physically implemented in
u

PIC16F84A?
cu

4. What is the maximum program memory space the PIC16F84A can address?
5. What is the address of the PORT A?
6. What is the address of the Status Register?
7. How to set all bits of Port A as outputs?
8. How many program calls and interrupts can the stack handle?
9. How many bytes are there for EEPROM of the PIC16F84A?
10. Show how to read the EEPROM data memory for PIC16F84A?
11. Show how to write the EEPROM data memory for PIC16F84A?
12. Show how to perform write verification for PIC16F84A?

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 36

36

CuuDuongThanCong.com https://fb.com/tailieudientucntt 18
9/19/2019

Assignments
1. Design a PIC16F84 schematic in which
– Port A is input, connected with 4 buttons
– Port B is output, connected with 4 LEDs
Write a program to control 4 LEDs by 4 buttons
2. Design a PIC16F84 schematic in which
– PIC16F84 interfaces with ADC0808 and 4-digit 7-segment LED through Port B
– RA0 and RA1 are to select digits of 7-segment LED
– RA2 is to control START signal of ADC0808

om
– RA3 is to control OE signal of ADC0808

.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 37

37
an
th
ng

3. The PIC16F87x
o

• PIC16F873/ 874/ 876/ 877


du

– 35 single word instructions


– Operating speed
u

• DC – 20MHz clock input


cu

• DC – 200ns instruction cycle


– Timer: timer0/timer1/timer2
– 2 Capturer, Compare, PWM
modules
– 10-bit multi-channel ADC
– SSP with SPI and I2C
– USART

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 38

38

CuuDuongThanCong.com https://fb.com/tailieudientucntt 19
9/19/2019

3 The PIC16F87x

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 39

39
an
th
ng

PIC16F87x
o
du
u

Block diagram
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 40

40

CuuDuongThanCong.com https://fb.com/tailieudientucntt 20
9/19/2019

The program memory map and stack


PIC16F877/876 PIC16F874/873

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 41

41
an
th
ng

Data memory Organization


o
du

• Data memory:
– partitioned into multiple banks which contain the General Registers and
the Special Function Register
u

– Bits RP1 (STATUS<6>) and RP0 (STATUS<5>) are the bank select bits
cu

• Each bank extends up to 7Fh (128 bytes).


– The lower locations of each bank are reserved for the Special Function
Registers.
– Above the Special Function Registers are General Purpose Registers

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 42

42

CuuDuongThanCong.com https://fb.com/tailieudientucntt 21
9/19/2019

General Purpose Register File (1)

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 43

43
an
th
ng

General Purpose Register File (2)


o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 44

44

CuuDuongThanCong.com https://fb.com/tailieudientucntt 22
9/19/2019

Special Function Registers (1)

om
.c
ng
Reference: 16F87X datasheet co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 45

45
an
th
ng

Special Function Registers (2)


o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 46

46

CuuDuongThanCong.com https://fb.com/tailieudientucntt 23
9/19/2019

PCL and PCLATH


• The program counter (PC) is 13-
bits wide.
• The low byte comes from the PCL
register, which is a readable and
writable register.
• The upper bits (PC<12:8>) are not
readable, but are indirectly

om
writable through the PCLATH
register.

.c
• On any RESET, the upper bits of
the PC will be cleared. Loading of PC in different situations:
• how the PC is loaded on a write to PCL

ng
(PCLATH<4:0> → PCH).
• how the PC is loaded during a CALL or
GOTO instruction (PCLATH<4:3> →PCH)
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 47

47
an
th
ng

Stack
o
du

• The PIC16F87X family has an 8-level deep x 13-bit wide


hardware stack.
• The stack space is not part of either program or data space
u
cu

and the stack pointer is not readable or writable.


• The PC is PUSHed onto the stack when a CALL instruction is
executed, or an interrupt causes a branch.
• The stack is POPed in the event of a RETURN,RETLW or a
RETFIE instruction execution.
• PCLATH is not affected by a PUSH or POP operation.

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 48

48

CuuDuongThanCong.com https://fb.com/tailieudientucntt 24
9/19/2019

Program Memory Paging (1)


• All PIC16F87X devices are capable of addressing a continuous
8K word block of program memory.
• The CALL and GOTO instructions provide only 11 bits of address
to allow branching within any 2K program memory page.
• When doing a CALL or GOTO instruction, the upper 2 bits of the
address are provided by PCLATH<4:3>.
• When doing a CALL or GOTO instruction, the user must ensure

om
that the page select bits are programmed so that the desired
program memory page is addressed.

.c
• If a return from a CALL instruction (or interrupt) is executed, the
entire 13-bit PC is popped off the stack.

ng
• The contents of the PCLATH register are unchanged after a
RETURN or RETFIE instruction
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 49

49
an
th
ng

Program Memory Paging (2)


o

• Example:
du

– call of a subroutine in page 1 from page 0


– assumes that PCLATH is saved and restored by the Interrupt Service
u

Routine (if interrupts are used).


cu

ORG 0x500
BCF PCLATH,4
BSF PCLATH,3 ;Select page 1 (800h-FFFh)
CALL SUB1_P1 ;Call subroutine in page 1 (800h-FFFh)
:
ORG 0x900 ;page 1 (800h-FFFh)
SUB1_P1
: ;called subroutine page 1 (800h-FFFh)
:
RETURN ;return to Call subroutine
;in page 0 (000h-7FFh)

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 50

50

CuuDuongThanCong.com https://fb.com/tailieudientucntt 25
9/19/2019

Indirect Addressing (1)


• Indirect addressing is possible by using the INDF register.
• Any instruction using the INDF register actually accesses the
register pointed to by the File Select Register, FSR.
• Reading the INDF register itself, indirectly (FSR = ’0’) will read
00h.
• Writing to the INDF register indirectly results in a no operation

om
(although status bits may be affected).
• An effective 9-bit address is obtained by concatenating the 8-
bit FSR register and the IRP bit (STATUS<7>),

.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 51

51
an
th
ng

Indirect Addressing (2)


o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 52

52

CuuDuongThanCong.com https://fb.com/tailieudientucntt 26
9/19/2019

Indirect Addressing (3)


• A simple program to clear RAM locations 20h-2Fh using
indirect addressing

MOVLW 0x20 ;initialize pointer


MOVWF FSR ;to RAM
NEXT CLRF INDF ;clear INDF register
FSR,F ;inc pointer

om
BTFSS FSR,4 ;all done?
GOTO NEXT ;no clear next
CONTINUE

.c
: ;yes continue

ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 53

53
an
th
ng

IO Ports
o
du

Port A Port B Port C Port D Port E


Support 16F87X 16F87X 16F87X PIC16F874 PIC16F874
/877 /877
u

Width 6-bit 8-bit 8-bit 8-bit 3-bit


cu

Direction Bi-directional Bi-directional Bi-directional Bi-directional Bi-directional


Direction TRISA TRISB TRISC TRISD TRISE
register
Buffer TTL buffer / TTL buffer / Schmitt Schmitt Schmitt
Schmitt Schmitt trigger trigger trigger
trigger trigger
Multiplexed Analog input Serial USART, PWM, Parallel slave Analog input
with Programming I2C, SPI port

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 54

54

CuuDuongThanCong.com https://fb.com/tailieudientucntt 27
9/19/2019

Port A Functions

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 55

55
an
th
ng

Port B Functions
o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 56

56

CuuDuongThanCong.com https://fb.com/tailieudientucntt 28
9/19/2019

Port C Functions

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 57

57
an
th
ng

Port D Functions
o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 58

58

CuuDuongThanCong.com https://fb.com/tailieudientucntt 29
9/19/2019

Port E Functions

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 59

59
an
th
ng

Initializing IO Ports
o
du

• Example: initializing Port A

BCF STATUS, RP0 ;


u

BCF STATUS, RP1 ; Bank0


cu

CLRF PORTA ; Initialize PORTA by


; clearing output
; data latches
BSF STATUS, RP0 ; Select Bank 1
MOVLW 0x06 ; Configure all pins
MOVWF ADCON1 ; as digital inputs
MOVLW 0xCF ; Value used to
; initialize data
; direction
MOVWF TRISA ; Set RA<3:0> as inputs
; RA<5:4> as outputs
; TRISA<7:6>are always
; read as ’0’.

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 60

60

CuuDuongThanCong.com https://fb.com/tailieudientucntt 30
9/19/2019

PIC16CXXX Instruction Set (1)

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 61

61
an
th
ng

PIC16CXXX Instruction Set (2)


o
du
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 62

62

CuuDuongThanCong.com https://fb.com/tailieudientucntt 31
9/19/2019

Class Assignment
1. What are main differences between PIC16F84 and
PIC16F87x?
2. What is the capacity of the FLASH memory of PIC16F877?
3. How many IO ports of PIC16F873 are there?
4. What are multiplexed with PORTC ?
5. How to initialize Port C as output of PIC16F877?

om
6. What is the address of TRISB?
7. Is PCLATCH affected by PUSH and POP operation?

.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 63

63
an
th
ng

4. Apply PIC microcontroller for embedded systems


o

• PIC microcontrollers provide basic required


du

hardware for an embedded system


– Processing unit
u
cu

– Memory: Flash ROM, EEPROM


– Timer/interrupt
• Interface:
– Parallel I/O
– ADC
– PWM
– Serial communication: UART, SPI, I2C
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 64

64

CuuDuongThanCong.com https://fb.com/tailieudientucntt 32
9/19/2019

4. Apply PIC microcontroller for embedded systems


• A simple security system using PIC microcontroller

om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 65

65
an
th
ng

PIC Applications
o
du

◼ LED Flasher
u

Loop:
cu

bsf PORTB, 0
call Delay_500ms
bcf PORTB, 0
call Delay_500ms
goto Loop

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 66

66

CuuDuongThanCong.com https://fb.com/tailieudientucntt 33
9/19/2019

PIC Applications

◼ Button Read
Movlw 0
movwf TRISD, f
bsf TRISD, 2
Loop:
btfsc PORTD, 2
goto light

om
goto No_light
Light:
bsf PORTB,0

.c
goto Loop
No_light:
bcf PORTB,0

ng
goto Loop
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 67

67
an
th
ng

Group discussion
o
du

Discuss about how to apply PIC microcontroller to


your class project
u
cu

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 68

68

CuuDuongThanCong.com https://fb.com/tailieudientucntt 34

You might also like