Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 34 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
34
Dung lượng
3,75 MB
Nội dung
8/9/2016 ĐẠ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 3: Microcontroller Introduction to PIC microcontroller PIC16F84 PIC16F877 Apply PIC microcontroller for embedded systems 1. Introduction to PIC microcontroller • PIC is a family of modified Harvard architecture microcontrollers made by Microchip Technology • The PIC1650 was originally developed by General Instruments • The name PIC initially referred to "Peripheral Interface Controller" Bộ mơn Kỹ Thuật Điện Tử - ĐHBK Chapter 8/9/2016 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 PIC18F4XK20 Starter Kit •128x64 Organic LED Display (SPI) • 32.768 kHz External Oscillator (Timer1) • Analog input filtering and gain control into RE1 • PWM output filtering from RC2 • push buttons for user interfacing Bộ mơn Kỹ Thuật Điện Tử - ĐHBK Chapter 3 PIC microcontroller • Comparison of PIC families ● Complete portfolio: 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 8/9/2016 PIC Microcontroller Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter Some members of PIC16 Series Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 8/9/2016 Some members of PIC16 Series Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 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 Bộ môn Kỹ Thuật Điện Tử - ĐHBK 4MHz 10MHz 20MHz 33MHz 40MHz Chapter 8/9/2016 PIC Clock Three ways to provide the clock signal to a PIC Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter PIC’s program memory • PICs have two difference types of program storage: – 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 10 8/9/2016 PIC16 Series http://tme.com.vn Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 11 2. The Microchip PIC16F84 • The Microchip PIC16F84 is a low‐cost, single‐chip, 8‐bit microcontroller – Only 35 single word instructions – All instructions single‐cycle except for 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 12 8/9/2016 The PIC 16F84A pin connection diagram Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 13 PIC16C8X PINOUT DESCRIPTION Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 14 8/9/2016 PIC16C8X PINOUT DESCRIPTION Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 15 Architecture of the PIC16F84 Buses: Communication lines for transferring data within the processor 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 1s. Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 16 8/9/2016 Architecture of the PIC16F84 Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 17 W Register Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 18 8/9/2016 The 16F84A Status register Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 19 The PIC16F84 Status register Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 20 10 8/9/2016 3 The PIC16F87x Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 39 Chapter 40 PIC16F87x Block diagram Bộ môn Kỹ Thuật Điện Tử - ĐHBK 20 8/9/2016 The program memory map and stack PIC16F877/876 Bộ môn Kỹ Thuật Điện Tử - ĐHBK PIC16F874/873 Chapter 41 Data memory Organization • Data memory: – partitioned into multiple banks which contain the General Registers and the Special Function Register – Bits RP1 (STATUS) and RP0 (STATUS) are the bank select bits • 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 42 21 8/9/2016 General Purpose Register File (1) Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 43 General Purpose Register File (2) Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 44 22 8/9/2016 Special Function Registers (1) Reference: 16F87X datasheet Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 45 Special Function Registers (2) Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 46 23 8/9/2016 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) are not readable, but are indirectly writable through the PCLATH register. • On any RESET, the upper bits of Loading of PC in different situations: the PC will be cleared • how the PC is loaded on a write to PCL (PCLATH → PCH) • how the PC is loaded during a CALL or GOTO instruction (PCLATH →PCH) Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 47 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ộ mơn Kỹ Thuật Điện Tử - ĐHBK Chapter 48 24 8/9/2016 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. • When doing a CALL or GOTO instruction, the user must ensure that the page select bits are programmed so that the desired program memory page is addressed. • If a return from a CALL instruction (or interrupt) is executed, the entire 13‐bit PC is popped off the stack • The contents of the PCLATH register are unchanged after a RETURN or RETFIE instruction Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 49 Program Memory Paging (2) • Example: – call of a subroutine in page 1 from page 0 – assumes that PCLATH is saved and restored by the Interrupt Service Routine (if interrupts are used) ORG 0x500 BCF PCLATH,4 BSF PCLATH,3 CALL SUB1_P1 : ORG 0x900 ;Select page 1 (800h‐FFFh) ;Call subroutine in page 1 (800h‐FFFh) ;page 1 (800h‐FFFh) SUB1_P1 : : RETURN Bộ môn Kỹ Thuật Điện Tử - ĐHBK ;called subroutine page 1 (800h‐FFFh) ;return to Call subroutine ;in page 0 (000h‐7FFh) Chapter 50 25 8/9/2016 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 (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), Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 51 Indirect Addressing (2) Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 52 26 8/9/2016 Indirect Addressing (3) • A simple program to clear RAM locations 20h‐2Fh using indirect addressing MOVLW 0x20 MOVWF FSR NEXT CLRF INDF FSR,F BTFSS FSR,4 GOTO NEXT CONTINUE : Bộ môn Kỹ Thuật Điện Tử - ĐHBK ;initialize pointer ;to RAM ;clear INDF register ;inc pointer ;all done? ;no clear next ;yes continue 53 Chapter IO Ports Port A Port B Port C Port D Port E Support 16F87X 16F87X 16F87X PIC16F874 /877 PIC16F874 /877 Width 6‐bit 8‐bit 8‐bit 8‐bit 3‐bit Direction Bi‐directional Bi‐directional Bi‐directional Bi‐directional Bi‐directional Direction register TRISA TRISB TRISC TRISD TRISE Buffer TTL buffer / Schmitt trigger TTL buffer / Schmitt trigger Schmitt trigger Schmitt trigger Schmitt trigger Multiplexed Analog input with Serial USART, PWM, Parallel slave Analog input Programming I2C, SPI port Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 54 27 8/9/2016 Port A Functions Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 55 Port B Functions Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 56 28 8/9/2016 Port C Functions Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 57 Port D Functions Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 58 29 8/9/2016 Port E Functions Bộ môn Kỹ Thuật Điện Tử - ĐHBK 59 Chapter Initializing IO Ports • Example: initializing Port A BCF BCF CLRF STATUS, RP0 STATUS, RP1 PORTA BSF MOVLW MOVWF MOVLW STATUS, RP0 0x06 ADCON1 0xCF MOVWF TRISA Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter ; ; 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 as inputs ; RA as outputs ; TRISAare always ; read as ’0’ 60 30 8/9/2016 PIC16CXXX Instruction Set (1) Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 61 PIC16CXXX Instruction Set (2) Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 62 31 8/9/2016 Class Assignment What are main differences between PIC16F84 and PIC16F87x? What is the capacity of the FLASH memory of PIC16F877? How many IO ports of PIC16F873 are there? What are multiplexed with PORTC ? How to initialize Port C as output of PIC16F877? What is the address of TRISB? Is PCLATCH affected by PUSH and POP operation? Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 63 4. Apply PIC microcontroller for embedded systems • PIC microcontrollers provide basic required hardware for an embedded system – Processing unit – 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 64 32 8/9/2016 4. Apply PIC microcontroller for embedded systems • A simple security system using PIC microcontroller Bộ mơn Kỹ Thuật Điện Tử - ĐHBK Chapter 65 PIC Applications LED Flasher Loop: bsf call bcf call goto PORTB, Delay_500ms PORTB, Delay_500ms Loop Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 66 33 8/9/2016 PIC Applications Button Read Movlw movwf bsf TRISD, f TRISD, btfsc goto goto PORTD, light No_light bsf goto PORTB,0 Loop bcf goto PORTB,0 Loop Loop: Light: No_light: Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 67 Group discussion Discuss about how to apply PIC microcontroller to your class project Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 68 34