03-Jun-11 This presentation contains illustrations from the book ‘Programming 8-bit PIC Microcontrollers in C’ Programming 8-bit PIC Microcontrollers in C Martin Bates Elsevier 2008 Part Microcontroller Systems describes in detail the internal architecture and interfaces available in the PIC 16F887A, a typical PIC chip, as well as outlining the main features of the development system Part C Programming Essentials provides simple example programs for the microcontroller which show the basic principles of C programming, and interfacing to basic I/O devices Part C Peripheral Interfaces provides example programs for operating PIC chips with a full range of peripherals, using timers and interrupts Figure 1.1 Part MICROCONTROLLER SYSTEMS Elements of a digital controller Input Peripherals User input Program download Output Peripherals CPU Central Processing Unit ROM Read Only Memory User output RAM Read & Write Memory The microcontroller contains all these elements in one chip Figure 1.2 16F877 pin-out Figure 1.3 PIC 16F877 MCU Block diagram Flash ROM Program Memory 8192 x 14 bits Program Counter (13 bits) Address Stack 13 bits x8 levels 0000 – 1FFF Instructions RAM File Registers 368 X bits 000-1FF Instruction Register File Address Program address Working (W) Register Literal File Select Register Arithmetic & Logic Unit Status bits Status (Flag) Register Opcode Instruction Decode & CPU control Data Bus (8 bits) EEPROM 256 bytes MCU control lines Ports, Timers ADC, Serial I/O Timing control Clock Reset Port A B C D E The microcontroller pins have multiple functions Shows the main parts of the chip in simplified form 03-Jun-11 Table 1.1 PIC16F877 simplified file register map Bank (000 – 07F) Bank (080 – 0FF) Bank (100-180) Address Register Address Register Address Register Address Register 000h Indirect 080h Indirect 100h Indirect 180h Indirect 001h Timer0 081h Option 101h Timer0 181h Option 002h PC Low 082h PC Low 102h PC Low 182h PC Low 003h Status Reg 083h Status Reg 103h Status Reg 183h Status Reg 004h File Select 084h File Select 104h File Select 184h File Select 005h Port A data 085h PortA direction 105h - 185h - 006h Port B data 086h PortB direction 106h Port B data 186h PortB direction 007h Port C data 087h PortC direction 107h - 187h - 008h Port D data 088h PortD direction 108h - 188h - 009h Port E data 089h PortE direction 109h - 189h 00Ah PC High 08Ah PC High 10Ah PC High 18Ah PC High 00Bh Interrupt Control 08Bh Interrupt Control 10Bh Interrupt Control 18Bh Interrupt Control 00Ch to 01Fh 20 Peripheral Control Registers 08Ch to 09Fh 20 Peripheral Control Registers 020h to 06Fh 070h to 07Fh 80 General Purpose Registers 16 Common Access GPRs 0A0h to 0EFh 0F0h to 0FFh 80 General Purpose Registers Accesses 70h – 7Fh 10Ch to 10Fh 110h to 16Fh Peripheral Control Registers 96 General Purpose Registers 18Ch to 18Fh 190h to 1EFh Peripheral Control Registers 96 General Purpose Registers 170h to 17Fh Accesses 70h – 7Fh 1F0h to 1FFh Accesses 70h – 7Fh Figure 1.4 Write TRIS bit CPU Data Bus Write data bit Read data bit Table 1.2 Data word (bits) Program memory (bytes) Typical Instruction Set Speed MIPS =6 ... system + C Compiler ICD2 program and debug system USB ICD2 interface 6-WAY connector Figure 1 .20 Microchip ICD2 module ICD2 provides in-circuit debugging Figure 1 .22 ICD debugging windows PIC MCU Target... overflow INT_TIMER1 CCP Timer capture or compare detected INT_CCP1 Timer Timer register overflow INT_TIMER2 CCP2 Timer capture or compare detected INT_CCP2 TIMERS Chip select Read Write Interrupt... PICkit2 demo system hardware ICSP target board connections Application Board Reset 10k ICSP Interface MCU Vpp/!MCLR Vdd Vss PGD PGC Vdd Vss Board +5V Supply Connections to the target chip for programming