Professional Documents
Culture Documents
Chapter 4 - Microcontroller
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
CuuDuongThanCong.com https://fb.com/tailieudientucntt 1
9/19/2019
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
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
CuuDuongThanCong.com https://fb.com/tailieudientucntt 3
9/19/2019
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
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
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
12
CuuDuongThanCong.com https://fb.com/tailieudientucntt 6
9/19/2019
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 13
13
an
th
ng
14
CuuDuongThanCong.com https://fb.com/tailieudientucntt 7
9/19/2019
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 15
15
an
th
ng
16
CuuDuongThanCong.com https://fb.com/tailieudientucntt 8
9/19/2019
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
18
CuuDuongThanCong.com https://fb.com/tailieudientucntt 9
9/19/2019
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 19
19
an
th
ng
20
CuuDuongThanCong.com https://fb.com/tailieudientucntt 10
9/19/2019
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
• Each block has its own bus, so that access to each can occur
during the same clock cycle.
22
CuuDuongThanCong.com https://fb.com/tailieudientucntt 11
9/19/2019
ProgramProgram
PIC16F84 memoryMemory
Program memory
om
.c
0x3FF
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 23
23
an
th
ng
.. \
.. | General-purpose
| file registers.
.. /
.. /
24
CuuDuongThanCong.com https://fb.com/tailieudientucntt 12
9/19/2019
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
26
CuuDuongThanCong.com https://fb.com/tailieudientucntt 13
9/19/2019
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
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
Register (SFR)
• SRFs are:
– EECON1
– EECON2
– EEDATA
– EEADR
30
CuuDuongThanCong.com https://fb.com/tailieudientucntt 15
9/19/2019
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
32
CuuDuongThanCong.com https://fb.com/tailieudientucntt 16
9/19/2019
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.
33
an
th
ng
I/O Port
o
• PORT A:
du
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
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?
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
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
40
CuuDuongThanCong.com https://fb.com/tailieudientucntt 20
9/19/2019
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 41
41
an
th
ng
• 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
42
CuuDuongThanCong.com https://fb.com/tailieudientucntt 21
9/19/2019
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 43
43
an
th
ng
44
CuuDuongThanCong.com https://fb.com/tailieudientucntt 22
9/19/2019
om
.c
ng
Reference: 16F87X datasheet co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 45
45
an
th
ng
46
CuuDuongThanCong.com https://fb.com/tailieudientucntt 23
9/19/2019
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
48
CuuDuongThanCong.com https://fb.com/tailieudientucntt 24
9/19/2019
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
• Example:
du
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)
50
CuuDuongThanCong.com https://fb.com/tailieudientucntt 25
9/19/2019
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
52
CuuDuongThanCong.com https://fb.com/tailieudientucntt 26
9/19/2019
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
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
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
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
60
CuuDuongThanCong.com https://fb.com/tailieudientucntt 30
9/19/2019
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 4 61
61
an
th
ng
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
64
CuuDuongThanCong.com https://fb.com/tailieudientucntt 32
9/19/2019
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
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
68
CuuDuongThanCong.com https://fb.com/tailieudientucntt 34