1.1 Introduction To PIC Microcontroller: Embedded System Design
1.1 Introduction To PIC Microcontroller: Embedded System Design
1.1 Introduction To PIC Microcontroller: Embedded System Design
B mn K Thut in T - HBK
2/6/2012
B mn K Thut in T - HBK
PIC microcontroller
Comparison of PIC families
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 mn K Thut in T - HBK
2/6/2012
PIC Microcontroller
B mn K Thut in T - HBK
B mn K Thut in T - HBK
2/6/2012
B mn K Thut in T - HBK
PIC speed
Can use crystal, clock oscillator, or even an RC circuit Some PICs have a built-in 4MHz RC clock, not very accurate, but requires no external components Instruction speed = clock speed
12C50x 12C67x 16Cxxx 17C4x/17C7xxx 18Cxxx 4MHz 10MHz 20MHz 33MHz 40MHz
B mn K Thut in T - HBK
2/6/2012
PIC Clock
B mn K Thut in T - HBK
FLASH
rewriteable much faster to develop on PIC examples: any F part 16F84, 16F87x, 18Fxxx
B mn K Thut in T - HBK
10
2/6/2012
PIC16 Series
http://tme.com.vn
B mn K Thut in T - HBK
11
B mn K Thut in T - HBK
12
2/6/2012
B mn K Thut in T - HBK
13
B mn K Thut in T - HBK
14
2/6/2012
B mn K Thut in T - HBK
15
2/6/2012
B mn K Thut in T - HBK
17
W Register
B mn K Thut in T - HBK
18
2/6/2012
B mn K Thut in T - HBK
19
B mn K Thut in T - HBK
20
10
2/6/2012
B mn K Thut in T - HBK
21
PIC16F84 Memory
There are two memory blocks in the PIC16C84; program memory and data memory. 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 mn K Thut in T - HBK
22
11
2/6/2012
0x3FF
B mn K Thut in T - HBK
23
Special-purpose registers
Indirect pointer PORT A PORT B Not used
(This is a simplified memory map that excludes bank1. Study the memory banks in the Microchip data sheets).
0x08 EEPROM prog. 0x09 0x0A 0x0B 0x0C PCLATH INTCON .. .. .. .. .. Program counter Interrupt control
\: \ | | / / General-purpose file registers.
B mn K Thut in T - HBK
24
12
2/6/2012
Ind. address. TMR0 PCL STATUS FSR PORTA data PORTB data
Special-purpose registers
Indirect pointer PORT A PORT B Not used
Referencing bits . Bits are numbered 7-0 from left to right, i.e., msb-lsb. One of the most common programming errors is incorrect specification of bit numbers.
B mn K Thut in T - HBK
25
bit 3
bit 2
bit 1-0
11 = RC oscillator (Resistor/capacitor) 10 = HS oscillator (High Speed Crystal/Resonator) 01 = XT oscillator (Crystal/Resonator) 00 = LP oscillator (Low Power Crystal)
26
B mn K Thut in T - HBK
13
2/6/2012
B mn K Thut in T - HBK
27
DIRECT/INDIRECT ADDRESSING
B mn K Thut in T - HBK
28
14
2/6/2012
DIRECT/INDIRECT ADDRESSING
A simple program to clear RAM locations 20h-2Fh using indirect addressing
movlw movwf NEXT clrf incf btfss goto CONTINUE : 0x20 FSR INDF FSR FSR,4 NEXT ;initialize pointer ;to RAM ;clear INDF register ;inc pointer ;all done? ;NO, clear next ;YES, continue
An effective 9-bit address is obtained by concatenating the 8bit FSR register and the IRP bit (STATUS<7>) However, IRP is not used in the PIC16F84A.
B mn K Thut in T - HBK
29
SRFs are:
EECON1 EECON2 EEDATA EEADR
B mn K Thut in T - HBK
30
15
2/6/2012
B mn K Thut in T - HBK
B mn K Thut in T - HBK
32
16
2/6/2012
Requirement
B mn K Thut in T - HBK
33
I/O Port
PORT A:
5-bit wide, bi-direction port Corresponding data direction register is TRISA (TRISB bit = 1: input; TRISB 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 mn K Thut in T - HBK
34
17
2/6/2012
I/O Port
Initializing Port A
BCF STATUS, RP0 ; CLRF PORTA ; Initialize PORTA by ; clearing output ; data latches BSF STATUS, RP0 ; Select Bank 1 MOVLW 0x0F ; Value used to ; initialize data ; direction MOVWF TRISA ; Set RA<3:0> as inputs ; RA4 as output ; TRISA<7:5> are always ; read as 0.
B mn K Thut in T - HBK
Initializing Port B
BCF STATUS, RP0 ; CLRF PORTB ; Initialize PORTB by ; clearing output ; data latches BSF STATUS, RP0 ; Select Bank 1 MOVLW 0xCF ; Value used to ; initialize data ; direction MOVWF TRISB ; Set RB<3:0> as inputs ; RB<5:4> as outputs ; RB<7:6> as inputs
35
Assignments
What are 8-bit and 16-bit families of PIC microcontrollers? What is maximum frequency of clock input for the PIC16F84A? How many word of program memory are physically implemented in PIC16F84A? 4. What is the maximum 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? 1. 2. 3.
B mn K Thut in T - HBK
36
18
2/6/2012
Assignments
1. Design a PIC16F84 schematic in which
Port A is input, connected with 4 buttons Port B is output, connected with 4 LEDs
B mn K Thut in T - HBK
37
Timer: timer0/timer1/timer2 2 Capturer, Compare, PWM modules 10-bit multi-channel ADC SSP with SPI and I2C USART
B mn K Thut in T - HBK
38
19
2/6/2012
B mn K Thut in T - HBK
39
PIC16F87x
Block diagram
B mn K Thut in T - HBK
40
20
2/6/2012
B mn K Thut in T - HBK
41
B mn K Thut in T - HBK
42
21
2/6/2012
B mn K Thut in T - HBK
43
B mn K Thut in T - HBK
44
22
2/6/2012
B mn K Thut in T - HBK
46
23
2/6/2012
how the PC is loaded on a write to PCL (PCLATH<4:0> PCH). how the PC is loaded during a CALL or GOTO instruction (PCLATH<4:3> PCH)
47
B mn K Thut in T - HBK
Stack
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 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 mn K Thut in T - HBK
48
24
2/6/2012
B mn K Thut in T - HBK
50
25
2/6/2012
B mn K Thut in T - HBK
51
B mn K Thut in T - HBK
52
26
2/6/2012
B mn K Thut in T - HBK
53
IO Ports
Port A Support Width Direction Direction register Buffer
16F87X 6-bit
Port B
16F87X 8-bit
Port C
16F87X 8-bit
Port D
PIC16F874 /877 8-bit
Port E
PIC16F874 /877 3-bit
Bi-directional Bi-directional Bi-directional Bi-directional Bi-directional TRISA TTL buffer / Schmitt trigger TRISB TTL buffer / Schmitt trigger TRISC Schmitt trigger TRISD Schmitt trigger TRISE Schmitt trigger Analog input
B mn K Thut in T - HBK
54
27
2/6/2012
Port A Functions
B mn K Thut in T - HBK
55
Port B Functions
B mn K Thut in T - HBK
56
28
2/6/2012
Port C Functions
B mn K Thut in T - HBK
57
Port D Functions
B mn K Thut in T - HBK
58
29
2/6/2012
Port E Functions
B mn K Thut in T - HBK
59
Initializing IO Ports
Example: initializing Port A
BCF BCF CLRF STATUS, RP0 STATUS, RP1 PORTA ; ; Bank0 ; Initialize PORTA by ; clearing output ; data latches ; Select Bank 1 ; Configure all pins ; as digital inputs ; Value used to ; initialize data ; direction ; Set RA<3:0> as inputs ; RA<5:4> as outputs ; TRISA<7:6>are always ; read as 0.
60
MOVWF TRISA
B mn K Thut in T - HBK
30
2/6/2012
B mn K Thut in T - HBK
61
B mn K Thut in T - HBK
62
31
2/6/2012
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? 6. What is the address of TRISB? 7. Is PCLATCH affected by PUSH and POP operation?
B mn K Thut in T - HBK
63
PIC Applications
LED Flasher
Loop: bsf call bcf call goto PORTB, 0 Delay_500ms PORTB, 0 Delay_500ms Loop
B mn K Thut in T - HBK
64
32
2/6/2012
PIC Applications
Button Read
Movlw movwf bsf Loop: btfsc goto goto Light: bsf goto No_light: bcf goto PORTB,0 Loop PORTB,0 Loop PORTD, 2 light No_light 0 TRISD, f TRISD, 2
B mn K Thut in T - HBK
65
33