ME 515 Mechatronics Microcontroller Based Control of Mechanical Systems Asanga Ratnaweera Department of Faculty of Engineering University of Peradeniya Tel: 081239 (3627) Email: asangar@pdn.ac.lk A microprocessor Performs arithmetic and logic operations Receives digital input process according to stored program (variable) digital output Two main parts: arithmetic unit (AU) control unit (CU) 2 1
A microprocessor Arithmetic unit made up of: Arithmetic Logic Unit (ALU) arithmetic calculations + logic operations Accumulators (ACC) where arithmetic calculations are made Condition Code Register (CCR) set based on results of ALU; 4 common bits: Zero, Negative, Overflow, Carry 3 A microprocessor Control unit made up of: Memory Address Register (MAR) holds current address where µp is accessing Memory Data Register (MDR) holds instruction temporarily Program Counter (PC) holds next instruction address to be read from the program memory (flash memory). Generally the counter increments each time an instruction is executed Instruction Register (IR) The instruction at the address specified by the PC is read to this register. This operation is called a FETCH. Control Logic (CL) synchronisation 4 2
A microprocessor Instruction fletch cycle 5 Microcomputer system µp + memory + i/o chips = µcomputer system Memory devices store program, data I/O devices read input and/or display results Connected via buses. 3 types: Address (ex: 16-bit, unidirectional) Data (ex: 8-bit, bidirectional) Control (variable) e.g. Read, Write, Enable 6 3
Microcomputer system 8-bit data bus MPU ROM (program) RAM (data) PROGRAMMABLE I/O 16-bit address bus Control lines 7 Micro-controller Microcontrollers contain all of the functional sections (CPU, RAM, ROM, I/O, ports and timers) of a personal computer on a single integrated circuit. some people describe them as special purpose computers with several qualifying distinctions that separate them from other computers. Microcontrollers are "embedded" inside some other device (often a consumer product: kitchen appliances, TVs, VCRs, cameras, telephones) so that they can control the features or actions of the product. 8 4
Micro-controller 9 Building blocks of a microcontroller Memory unit Central Processing Unit Bus Input-output unit Serial communication Timer unit Watchdog Analog to Digital Converter 10 5
Building blocks: Input-output unit When working with ports, first of all it is necessary to choose which port we need to work with, and then to send data to, or take it from the port By selecting the appropriate register 11 Building blocks: Serial communication 12 6
Building blocks: Timer unit Timer unit can give information about time, duration, protocol etc. 13 Building blocks: Watchdog This timer watches over the normal operation of the microcontroller software. It must be regularly cleared by software. When it times out the microcontroller returns to the point in the program immediately after power on. This timer is used to recover program operation when the software has a defect (or bug). Even if the program is reinitialized the bug is still in the program. 14 7
Building blocks: Analog to Digital Converter Analog to Digital Converter is responsible for converting an information about some analog value to a binary number and for follow it through to a CPU block so that CPU block can further process it. Not available with all µp 15 Embedded Systems: General Block Diagram sensor sensor sensor Sensor conditioning Microcontroller (uc) Output interfaces actuator indicator 16 8
History in Brief 1959: first IC's 1971: µp 4004 4Bit Intel (~2000 Tr.) 1972: µp 8008 8Bit Intel (~5000 Tr.) 1974: µp 8080 8Bit Intel (~6000 Tr.) 1974: µp 6800 8Bit Motorola 1975: µp Z80 8Bit Zilog 1976: µp 9900 16Bit Texas Instruments 1978: µp 8086 16Bit Intel (50.000 Tr) 1982: µp 8286 16Bit Intel (100.000 Tr.) 1985: µp 80386 16Bit Intel (300.000 Tr.) 1989: µp 80486 16Bit Intel (106 Tr.) 1993: µp 80586 16Bit Intel (5 106 Tr.)... 17 Common Microcontrollers Families There are several families of microprocessors are widely used in industrial control systems Zilog Z8 series Intel 8051 series ARM 32 bit microcontrollers MicroChip PIC microcontrollers 18 9
PIC Range of low end 8 bit microcontrollers. smallest have only 8 pins, largest 40 pins. Typical chip is an 18 pin one. Very cheap, you can pick them up at less than Rs.500 each. Targeted at consumer products, burglar alarms etc. 19 20 10
PIC Microcontrollers The term PIC stands for Programmable Interface Controller. PIC microcontrollers have become very popular because Relatively cheap Small in size On chip memory Easy to program Reusable 21 PIC Microcontroller Architecture von Neumann architecture programs and data share a single memory area that is accessed over a common bus Harvard architecture separate memory areas and buses for data and programs von Neumann Architecture. Harvard Architecture. Note : PIC microcontroller follows Harvard design 22 11
PIC Microcontroller Characteristics CPUs 8-bit 16-bit Number of I/O pins: 4-70 Some chips have A/D converter and PWM Memory types and sizes: Flash; OTP; ROM 0.5k 256k 23 PIC Microcontrollers: Memory varieties Microchip produces field programmable microcontrollers in three basic varieties EPROM can be programmed electrically and erased, if needed, by a special ultraviolet light (window type) OTP can be programmed only once and is the least expensive of the three Flash can be both programmed and erased by electrical means 24 12
PIC Microcontrollers: Memory varieties Ex: 25 The PIC Family: Speed PICs require a clock to work. Can use crystals, clock oscillators, or even an RC circuit. Some PICs have a built in 4MHz RC clock Not very accurate, but requires no external components Instruction speed = 1/4 clock speed T cyc = 4 * T clk 13
The Minimum PIC System You need some kind of a clock: Internal 4MHz clock on some PICs, external RC, external crystal, or external clock oscillator. And you need some power: Most PICs can operate from 2.5V - 5.5V (see Data Sheets) New 16HVXX can directly use up to 14V (Great for direct 9V or 12V battery hook ups PIC Microcontrollers: Oscillator If the CPU is the brain of the system then the oscillator, or clock, is the heartbeat. It provides the critical timing functions for the rest of the chip. The original version of the 16F84 can operate at up to 4-MHz while the newer chip has a 20- MHz upper limit. This section of the PIC is extremely versatile. There are three principal ways to clock a PIC chip. 28 14
PIC Microcontrollers: Oscillator cont.. There are three principal ways to clock a PIC chip. Crystal controlled oscillator 29 PIC Microcontrollers: Oscillator cont.. RC oscillator Note: Not as precise as Xtal oscillator 30 15
PIC Microcontrollers: Oscillator cont.. External clock Note: This is useful when the chip must be synchronized with other components or an entire system must be operated from a single clock source 31 PIC Microcontrollers: PIC 16F84A PIC16F84A belongs to a class of 8-bit microcontrollers of RISC architecture. Its general structure is shown below. Microcontrollers with Harvard stands for. architecture are also called "RISC microcontrollers". RISC - Reduced Instruction Set Computer 32 16
PIC Microcontrollers: PIC 16F84A 33 PIC Microcontrollers: PIC 16F84 Program memory (FLASH) for storing a written program. Since memory made in FLASH technology can be programmed and cleared more than once, it makes this microcontroller suitable for device development. EEPROM - data memory that needs to be saved when there is no supply. RAM - data memory used by a program during its execution. In RAM are stored all inter-results or temporary data during run-time. 34 17
PIC Microcontrollers: PIC 16F84 PORTA and PORTB are physical connections between the microcontroller and the outside world. Port A has five, and port B has eight pins. FREE-RUN TIMER is an 8-bit register inside a microcontroller that works independently of the program. On every fourth clock of the oscillator it increments its value until it reaches the maximum (255), and then it starts counting over again from zero. CENTRAL PROCESSING UNIT The PIC has only 35 opcodes in its instruction set compared with more than 256 for a DEC Vax system6. 35 PIC 16F84 : Pin description PIC16F84 has a total of 18 pins. 36 18
PIC 16F84 : Pin description Pin Identifier RA[0-4] RB[0-7] V ss, V dd OSC1,OSC2 MCLR Description 5 bits of bidirectional I/O Port A 8 bits of bidirectional I/O Port B Power supply ground reference (ss source, dd drain) Oscillator crystal inputs Master clear, active low 37 PIC 16F84 A : Features Only 35 single word instructions to learn All instructions single-cycle except for program branches which are two-cycle Operating speed: 4 MHz clock input, 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 38 19
PIC 16F84A : Features 15 Special Function Hardware registers Eight-level deep hardware stack Direct, indirect and relative addressing modes Four interrupt sources: External RB0/INT pin TMR0 timer overflow PORTB<7:4> interrupt-on-change Data EEPROM write complete 39 PIC 16F84A : Peripheral Features 13 I/O pins with individual direction control High current sink/source for direct LED drive 25 ma sink max. per pin 25 ma source max. per pin TMR0: 8-bit timer/counter with 8-bit programmable prescaler 40 20
PIC 16F84A: Program memory The figure below shows the register file map inside the PIC16F84A 41 PIC 16F84 : Registers cont Bank 1 is used to control the actual operation of the PIC for example to tell the PIC which bits of Port A are input and which are output. Bank 0 is used to manipulate the data. To change from Bank 0 to Bank 1 we tell the STAUS register. We do this by setting bit 5 of the STATUS register to 1. To switch back to Bank 0, we set bit 5 of the STATUS register to 0. The STATUS register is located at address 03h (the h means the number is in Hexadecimal). 42 21
PIC16F877A : Features 5/6 Programming pins 8 A/D channels 2 Oscillator Inputs 2 RS-232 inputs 33 I/O ports 43 44 22
45 PIC Programming Language that microcontroller and man use to communicate is called "assembly language". Programs written in assembly language must be translated into a "language of zeros and ones" in order for a microcontroller to understand it (hex file). "Assembly language" and "assembler" are two different notions. The first represents a set of rules used in writing a program for a microcontroller, the other is a program on the personal computer which translates assembly language into a language of zeros and ones. A program that is translated into "zeros" and "ones" is also called "machine language". There are commercial software which could convert a C or Basic code in to PIC assembly language 46 23
PIC programming : Assembler The assembler expects to find specific terms in three columns. First column: Labels Second column: opcodes and assembler directives Third Columns & more: operands The three columns are used in five ways. Header section Equates section Origin section Program section The end statement. 47 PIC programming : Assembler Labels A label is a mnemonic symbol assigned to an address. Ex : portb means port B whose real hexadecimal address is 0x06. Rules for defining labels Must be positioned in the first column Must begin with an alpha character May contain numeric, underscore or question mark May be up to 31 characters 48 24
PIC programming : Assembler Literals Literals are constants or numbers, usually hexadecimal numbers. Ex: Literal start movlw 0x00 Label Instruction 49 PIC programming : Assembler Header Assembler directive list radix p=16f84 hex 50 25
PIC programming : Assembler Header The information at the top of the source file is called a header. ;=========== pict1.asm=========== list p=16f84 radix hex ;----------------------------------------------------------- first line defines the type of PIC to be used Second line says that the number system is hexadecimal 51 PIC programming : Assembler Equates Label This is an equates Hex address or number equ 52 26
PIC programming : Assembler Equates Equates are used to assign labels to addresses and names to numbers Hexadecimal address Ex: portb equ 0x06 Label Tells assembler this is an equate min equ 1 ; min =bit 1 flag equ 0x0c ; flag = file register 53 PIC programming : Assembler Origin Not used This is an origin Hex address org 54 27
PIC programming : Assembler Origin Usually origin statements used for three purposes. Defines where the program code starts org 0x000 To establish the reset vector org 0x1ff goto start To establish the start of an interupt service org 0x004 55 PIC programming : Assembler Program Label Instruction Literal or label org 56 28
PIC programming : Assembler End This is end end 57 PIC programming : Assembler Program format Standard header Equates Program End end 58 29
PIC programming : Assembler Assembler files All source code files must be saved with the file extension.asm. ex: pic_1.asm Assembler creates two from the source code.lst (assembler generated listing).hex (hexadecimal object code which can be downloaded to the chip) 59 PIC programming : Assembler The assembler program reads each text file line, ignores it if begins with a semicolon and steps through the three columns. Style list PIC16f84 radix hex portb equ 0x06 org 0x06 Start movlw 0x00 tris portb movewf portb circle goto circle end 60 30
PIC Programming The process of communication between a man and a microcontroller In order to do this we have to use a development system 61 Development Systems MPLAB C Compiler Assembly Simulate MPLAB SIM Program PRO MATE II PICSTART+ Edit Project MPLAB Emulate MPLAB-ICE PICMASTER ICEPIC 62 31
Development Systems MPLAB-ICE PICMASTER ICEPIC MPLAB-SIM PICSTART Plus PRO MATE II 63 Pin-1 Development Systems RS-232 Serial Interface PICSTART Plus DEVELOPMENT PROGRAMMER POWER ACTIVE 9V DC Input Power ON Programmer Active Upgrades done in MPLAB software - firmware upgrades occasionally required for new PICmicro sub-families Uses serial programming algorithms for all ICSP capable devices 40-Pin ZIF Socket to accommodate PICmicros from 8-8 to 40-pins. 68-pin PLCC can be programmed with an adapter 64 32
PIC16F84 8-bit RISC (35 instructions, pipelined) Program counter 13-bit (points to the current instruction, automatically incremented) Stack to support subroutines/interrupts can hold 8 levels of PC Memory Program memory (FLASH) 1024, 14-bit locations Data Memory RAM: 68, 8-bit locations EEPROM: 64, 8-bit locations (slow write access) 65 PIC programming : PIC16F84 instruction set PIC16F84 has 35 instruction in its instruction set Move or define data Note : k is a constant, f is the file register, d is destination-0 for w register and 1 for file register f Nothing 66 33
PIC programming : PIC16F84 instruction set PIC16F84 has 35 instruction in its instruction set Changing register content 67 PIC programming : PIC16F84 instruction set PIC16F84 has 35 instruction in its instruction set Control program flow 68 34
PIC programming : PIC16F84 instruction set PIC16F84 has 35 instruction in its instruction set Control Microcontroller 69 PIC programming : PIC16F84 instruction set PIC16F84 has 35 instruction in its instruction set Logic 70 35
PIC programming : PIC16F84 instruction set PIC16F84 has 35 instructions in its instruction set Arithmetic 71 Software: Programmers Model <- 12/14/16 bits -> Hardware Stack Stores addresses for subroutines Program Memory Burned in by programmer (can t change during execution). Stored instructions, addresses and literals (numbers). W Register (PCH) Program Counter-PCL <- 8 bits -> Status Special Purpose Registers I/O pin states, peripheral registers, etc. General Purpose Registers RAM or data memory. Variables are stored here. 36
Microcontroller Architecture 73 PIC16F84 : An Example Code Port configuration TRISA or TRISB are used to configure Port A and Port B respectively If a bit of TRSA is is high, corresponding pin of the Port A is configured as input, otherwise it is an output Ex: To configure pins 0 and 3 of Port A as outputs and 1 and 2 inputs BSF 03h,5 ;Go to Bank 1 MOVLW 06h ;Put 00110 (06h) into W MOVWF 85h ;Move 00110 onto TRISA BCF 03h,5 ;Come back to Bank 0 Note : see the memory map for the address, only the program section is shown 74 37
Software: Instruction Examples movlw 0xFF Move ( mov ) the number ( l for literal ) 0xFF - that s 256 in decimal- into the working register ( w ). In other words, load W with the value 0xFF. Software: Programmers Model <- 12/14/16 -> Program Memory (PCH) Hardware Stack Program Counter-PCL <- 8 bits -> Status Special Purpose Registers 0xFF W Register General Purpose Registers 38
Software: Instruction Examples PORTA 05h movwf PORTA Move ( mov ) the working register ( w ) into the file register ( f ) named PORTA. In other words, load the register called PORTA with whatever number is in the W register. Software: Programmers Model <- 12/14/16 -> Program Memory Value in W (PCH) Hardware Stack Program Counter-PCL <- 8 bits -> Status Special Purpose Registers PORTA W Register General Purpose Registers 39
Software: Instruction Examples movf PORTA, W Move ( mov ) the the value of the file register ( f ) named PORTA into the working register ( w ). In other words, load W with the whatever number is in PORTA. Software: Programmers Model <- 12/14/16 -> Program Memory (PCH) Hardware Stack Program Counter-PCL <- 8 bits -> Status Special Purpose Registers Value in PORTA PORTA W Register General Purpose Registers 40
Writing values to port A bcf 03h,5 ;Come back to Bank 0 Start movlw 02h ; Write 02h to the W register. In binary this is ; 00010, which puts a 1 on pin 2 while ; keeping ; the other pins to 0 movwf 05h ;Now move the contents of W (02h) oto the ;PortA, whose address is 05h movlw 00h ;Write 00h to the W register. This puts a 0 on ;all pins. movwf 05h ;Now move the contents of W (0h) onto the ;Port A whose address is 05h goto Start ;Go to where we say Start end 81 PIC16F84 : An Example Code STATUS equ 03h ;this assigns the word STATUS to the value of 03h, ;which is the address of the STATUS register. TRISA equ 85h ;This assigns the word TRISA to the value of 85h, ; which is the address of the Tri-State register for PortA PORTA equ 05h ;This assigns the word PORTA to 05h which is the ;address of Port A bsf 03h,5 ;Go to Bank 1 movlw 00h ;Put 00000 into W movwf 85h ;Move 00000 onto TRISA all pins set to output bcf 03h,5 ;Come back to Bank 0 Start movlw 02h ;Write 02h to the W register. In binary this is ;00010, which puts a 1 on pin 2 while keeping ;the other pins to 0 movwf PORTA ;Now move the contents of W (02h) onto the ;PortA, whose address is 05h movlw 00h ;Write 00h to the W register. This puts a 0 on ;all pins. movwf PORTA ;Now move the contents of W (0h) onto the Port A ; whose address is 05h 2 January 2007 goto end Start Asanga Ratnaweera, ;Go to where Department we say of Start 82 41
PIC16F84 : Hardware implementation 83 42