EEE237 Introduction to Microprocessors Week 1 Course overview İntroduction to Microprocessor, microcontroller and programming basics Course objectives • to develop an in-depth understanding of – the operation of microprocessors and microcontrollers – Assembly language programming and using C language. – microprocessor interfacing techniques • to be able to use PIC16F877 microcontroller Course objectives • to be able to design and implement microprocessor-based systems in both hardware and software • Ultimate goal: to be able to apply this knowledge to more advanced structures Outline • Microcontroller Concepts Basics and Programming • PIC microcontroller Architecture • Programming Model, Addressing Modes and Instruction Set • Programming examples • The Stack, Subroutines and Interrupts Outline • Timer Operations • Analog to Digital Conversion • Data acquisition and control examples • Interfacing Concepts • Parallel Input/Output • Serial Communications Common computer organization (simplified view) • ALU (Arithmetic Logic Unit) is a circuitry, which is capable of doing various operations (for example ADD, SHIFT, AND, OR, etc) on certain on-chip registers. • CPU (Central Processing Unit) is the combination of the control logic, associated registers and the arithmetic logic unit. Common computer organization (simplified view) ALU Input/Output (I/O) Memory Control CPU Common computer organization (1/2) Common computer organization (2/2) Computer Buses • Address bus: carries the address of a unique memory or input/output (I/O) device • Data bus: carries data stored in memory (or an I/O device) to the CPU or from the CPU to the memory (or I/O device) • Control bus: is a collection of control signals that coordinate and synchronize the whole system Block diagram of a typical CPU CPU Structure • The main functions are – data transfer – arithmetic and logic operations – decision making (instructional flow control) • The register array consists of at least one accumulator, program counter and stack pointer • The control unit controls all the operations in a CPU and basically it puts the CPU in one of the fetch and execution phases Memory (1/2) • The memory in a computer system stores the data and instructions of the programs. Adress decoder Storage Data bus Area Adress bus Other signals (Vcc,Gnd, CS, etc.) Memory (2/2) Main memory types • ROM (read-only memory) – programmed permanently at the factory, cannot be altered • RAM (random-access memory) – read and write memory • EPROM (erasable programmable ROM) – nonvolatile, written electrically but erased optically • EEPROM (electrically ROM) – nonvolatile, both written and erased electrically Operation of the CPU (1/2) • Fetch cycle (phase): – The CPU puts the address of the instruction to be executed on the address bus. The address info comes from the program counter (PC) maintained by the control unit. – The control bus holds the info. for reading the memory location and the data bus holds the instruction from the memory which is stored into the instruction register (IR) – PC is updated to point to the next Operation of the CPU (2/2) • Execute cycle (phase): – Instruction in the IR is decoded – The required data transfer and the required logical and arithmetic operation are performed – The result is written back either to one of the registers or memory or I/O device • Common operations performed in ALU are: – addition, subtraction, – logical AND, OR, XOR, NOT Operation of the CPU (2/2) Detailed Block Diagram Microcomputer (1/2) • CPU on a single IC microprocessor (µP). • the terms CPU, µP and MPU (microprocessor unit) are synonymous. • CPU is the combination of the ALU and control unit of any computer. • When the CPU is a single IC; it is called a µP and is also referred to as the MPU. • When the MPU ‘s connected to memory and I/O, the arrangement becomes a MICROCOMPUTER. Microcomputer (2/2) • Many different MPUs are produced by many different IC vendors (e.g., Motorola, Intel, Texas Instruments, Zilog, National Semiconductors, etc.). • Major differences are in – word size, – the number and types of instructions that can be performed, – the types of external control signals available, – the amount of memory that can be addressed. Microcontroller • A microcontroller unit (MCU) contains an MPU, memory, and I/O circuitry on a single chip. • Such a chip can perform control operations without the need for any external circuitry. • e.g. PIC16F877 • e.g. Intel 8051 Microprocessor vs microcontroller (1/3) • Microprocessors: high performance, general purpose “brains” for PCs and workstations – Instruction decode and control, arithmetic/logic operations, registers, timing, external control – Typical cost: $75 -- $500 – Annual demand: 10s of millions • Microcontrollers: devices with high levels of integration for embedded control – Microprocessor functions plus on-chip memory and peripheral functions (e.g. ports, timers) – "Swiss army knife" of microprocessor technology – Typical cost: $1-- $25 – Annual demand: billions! Microprocessor vs microcontroller (2/3) Microprocessor ALU Memory I/O Peripheral functions Control Address Data Control Microcontroller Microprocessor vs microcontroller (3/3) Not always a clear distinction: today’s mP may be tomorrow’s mC • Microprocessor – Includes memory management unit, lots of cache – Performance is the most important feature (cost is important, but secondary) – Used mainly in desktop machines • Microcontroller – – – – – Integrated RAM and ROM, no cache Includes lots of peripherals Used mainly in “embedded” applications Often involves real-time control Important features include: low cost, low power consumption, number of integrated peripherals, interrupt response time, amount of RAM and ROM Bottom-up view of microcontroller systems (1/3) Bottom-up view of microcontroller systems (2/3) Bottom-up view of microcontroller systems (3/3) Microcontroller Use What will you study in EEE237? • PIC16F877 is the MCU choice in EEE237 • Why? – Much easier to use, have attractive features and suitable for wide range applications – Harward architecture makes PICs faster • You will be studying – how it is programmed to perform various operations Embedded System • Microcontrollers are sometimes called embedded microcontrollers, which just means that they are part of an embedded system -- that is, one part of a larger device or system. • The majority of microcontrollers in use today are embedded in other machinery, such as automobiles, telephones, appliances, and peripherals for computer systems. These are called embedded system. • Typical input and output devices include switches, relay, solenoids, LEDs, small or custom LCD displays, radio frequency devices, and sensors for data such as temperature, humidity, light level etc. • Embedded systems usually have no keyboard, screen, disks, printers, or other recognizable I/O devices of a personal computer, and may lack human interaction devices of any kind. Microchip PIC • PIC is a family of Harvard architecture microcontroller made by Microchip Technology. The name PIC initially referred to "Peripheral Interface Controller“ . PIC microcontrollers were the first RISC microcontroller. • PICs are popular with both industrial developers and hobbyists alike due to their low cost, wide availability, large user base, extensive collection of application notes, availability of low cost or free development tools, and serial programming (and re-programming with flash memory) capability. Two Different Architectures Fetch & Execution Cycles / Pipelining • There are 35 single word instructions. A two-stage pipeline overlaps fetch and execution of instructions. As a result, all instructions execute in a single cycle except for program branches. These take two cycles since the fetch instruction is “flushed” from the pipeline while the new instruction is being fetched and then executed. • A typical picture of the pipeline is shown below. Clock And Instruction Cycles • Clock from the oscillator enters a microcontroller via OSC1 pin where internal circuit of a microcontroller divides the clock into four even clocks Q1, Q2, Q3 and Q4 which do not overlap. • These four clocks make up one instruction cycle (also called machine cycle) during which one instruction is executed. • Execution of instruction starts by calling an instruction that is next in string. • Instruction is called from program memory on every Q1 and is written in Instruction Register (IR) on Q4. • Decoding and execution of instruction are done between the next Q1 and Q4 cycles. The following diagram shows the relationship between instruction cycle and clock of the oscillator (OSC1) as well as that of internal clocks Q1 – Q4. • Program Counter (PC) holds information about the address of the next instruction. Why use PIC16F877? Why PIC16F877A is very popular? This is because PIC16F877A is very cheap. Apart from that it is also very easy to be assembled. Additional components that you need to make this IC work is just a 5V power supply adapter, a 20MHz crystal oscillator and 2 units of 22pF capacitors. What is the advantages of PIC16F877A? This IC can be reprogrammed and erased up to 10,000 times. Therefore it is very good for new product development phase. What is the disadvantages of PIC16F877A? This IC has no internal oscillator so you will need an external crystal of other clock source. Features of PIC16F877 Key Features PIC16F877 MAX Operating Frequency 20MHz FLASH Program Memory (14-bit words) 8K Data Memory (bytes) EEPROM Data Memory (bytes) I/O Ports 8 368 256 RA0-5 (6) RB0-7 (8) RC0-7 (8) RD0-7 (8) RE0-2 (3) Timers 3 CCP 2 Serial Communications MSSP, USART Parallel Communications PSP 10-bit Analog-to-Digital Module 8 Channels Instruction Set 35 Instructions Pins (DIP) 40 Pins Bubble diagram of PIC16F877 As you can see the PIC16F877A is rich in peripherals so you can use it for many different projects. Pin Diagram of PIC16F877 • Plastic dual in-line package (DIP) Pin Diagram of PIC16F877 Quad Flat Package (QFP) Plastic Leaded Chip Carrier Package (PLCC) PIC16F877 Internal Block Diagram • The basic architecture of PIC16F877 consists of Program memory, file registers and RAM, ALU and CPU registers. PIC16F877 Internal Block Diagram Memory of the PIC16F877 • divided into 3 types of memories: 1. Program Memory – A memory that contains the program (which we had written), after we've burned it. As a reminder, Program Counter executes commands stored in the program memory, one after the other. 2. Data Memory – This is RAM memory type, which contains a special registers like SFR (Special Function Register) and GPR (General Purpose Register). The variables that we store in the Data Memory during the program are deleted after we turn of the micro. These two memories have separated data buses, which makes the access to each one of them very easy. 3. Data EEPROM (Electrically Erasable Programmable Read-Only Memory) – A memory that allows storing the variables as a result of burning the written program. • Each one of them has a different role. Program Memory and Data Memory two memories that are needed to build a program, and Data EEPROM is used to save data after the microcontroller is turn off. PIC16F877A Program Memory • Is Flash Memory • Used for storing compiled code (user’s program) • Program Memory capacity is 8K x 14 bit Each location is 14 bits long Every instruction is coded as a 14 bit word • PC can address up to 8K addresses • Addresses H’000’ and H’004’ are treated in a special way PIC16F877A Data Memory (RAM) Memory storage for variables Data Memory is also known as Register File and consists of two components. General purpose register file (same as RAM). Special purpose register file (similar to SFR in 8051). Addresses range from 0 to 511 and partitioned into 4 banks each bank extends up to 7Fh (128 bytes). The user can only access a RAM byte in a set of 4 banks and only one bank at a time. The default bank is BANK0. To access a register that is located in another bank, one should access it inside the program. There are special registers which can be accessed from any bank, such as STATUS register.