Академический Документы
Профессиональный Документы
Культура Документы
. Informal Definition i) The physical, electrical and logical means of exchanging Information with a functional module ii) The process of enabling a computer to communicate with the external world through Software, Hardware and Protocols
2
. Interfacing is the process of connecting devices together so that they can exchange information . The process of reading input signals and sending output signals is called I/O . I/O conventions
. I/O direction is relative to the MCU . Input is data read by the MCU . Output is data sent out by the MCU
A microcontroller (sometimes abbreviated C, uC or MCU)
Connectors
For RF applications, or for extremely long signal runs, such as telephone circuits, impedance matching is essential. Matched impedances mean that the source, cable and load impedances are all the same.
10
11
System-Level Interfaces
Human-machine interface
. Input devices: keyboard, mouse, microphone, camera . Output devices: CRT, printer, light panel, audio amp
12
System-Level Interfaces
Digital - Analogue Interface
. Input devices: A/D converters, modems, sensors . Output devices: D/A converters, modems, transducers actuators, stepper motors . Control devices: switches, multiplexers, amplifiers Attenuators
level-shifters This great tiny board will allow to interface slave 3.3V I2C devices like magnetometers and pressure sensors with master 5V devices like AVR/PIC microcontrollers
An asynchronous circuit, or self-timed circuit, is a digital circuit which is not governed by a clock circuit or global clock signal ribbon cable
twisted pair
PCB
13
14
15
Introduction to Microcontroller
Embedded System . You are used to chips like the Pentium, but in terms of installed machines these are a small portion of total computer use. Think how many computers you have at home? . Digital cameras, video cameras, TVs, mobile phones, calculators, micro-wave ovens etc all contain processors. In order for a microprocessor to be used, other components such as memory, or components for receiving and sending data must be added to it. . On the other hand, microcontroller is designed to be all of that in one. . No other external components are needed for its application because all necessary peripherals are already built into it. . Thus, we save the time and space needed to construct devices. . The PICmicro was originally designed around 1980 by General Instrument as a small, fast, inexpensive embedded microcontroller with strong I/O 16 capabilities.
17
18
Microcontroller
. Microcontrollers, as the name suggests, are small controllers . They are like single chip computers that are often embedded into other systems to function as processing/controlling unit
. For example, the remote control you are using probably has microcontrollers inside that do decoding and other controlling functions . They are also used in automobiles, washing machines, microwave ovens, toys ... etc, where automation is needed
Microcontroller
.The key features of microcontrollers include: . High Integration of Functionality
. Microcontrollers sometimes are called single-chip computers because they have on-chip memory and I/O circuitry and other circuitries that enable them to function as small standalone computers without other supporting circuitry
. Easy to Use
. Assembly language is often used in microcontrollers and since they usually follow a RISC architecture, the instruction set is small . The development package of microcontrollers often includes an assembler, a simulator, a programmer to "burn" the chip and a demonstration board . Some packages include a high level language compiler such as a C compiler and more sophisticated libraries
20
. A microprocessor is a programmable digital electronic component that incorporates the functions of a central processing unit (CPU) into a single IC package
These functions are
. The ability to execute a stored set of instructions to carry out user defined tasks . The ability to be able to access external memory chips to both read and write data from and to the memory
21
22
. Most development work is done using EPROM or EEPROM type memory, ROM memory being used in the final production version
(when all the program code has been fully tested)
. A typical microprocessor system will contain both ROM (could be EPROM, EEPROM, or ROM) to store the program code, and RAM to store dynamic data
24
Input/Output (I/O)
. I/O (input/output) is the collection of interfaces that different functional devices, of any information processing system, use to communicate with each other . Every information transfer is an output from one device and an input into another . For instance, on a computer, a keyboard and mouse are considered input devices while monitors and printers are considered output devices . Typical devices for communication between computers, such as modems and network cards, operate as both input and output devices . I/O can be:
. A number of digital bits formed into a number of digital inputs or outputs called a port . These are usually eight bits wide and thus referred to as a BYTE wide port. i.e. byte wide input port, byte wide output port
25
Input/Output (I/O)
. A serial line from the microprocessor (Transmit or TX) and a serial line to the microprocessor (Receive or RX) allowing serial data in the form of a bit stream to be transmitted or received via a two wire interface . Other I/O devices such as Analogue to Digital Converters (ADC) and Digital to Analogue Converters (DAC), Timer modules, Interrupt controllers etc. (which will be discussed later in the context of microcontrollers)
. These are relatively complex sub systems that can be obtained as separate ICs . They are connected to the microprocessor in a similar manner to that of the memory devices. . Indeed, they often contain their own memory to support internal operations (i.e. registers)
26
Microcontroller Hardware
. Basically, a microcontroller is a device which integrates a number of the components of a microprocessor system onto a single chip (IC), with the following common features:
. The CPU core - ranging from simple 4-bit processors to sophisticated 32/64-bit processors . Memory (both ROM and RAM) . Some parallel digital I/O
27
Microcontroller Hardware
. Most microcontrollers will also combine other devices such as:
. A Timer module to allow the microcontroller to perform tasks for certain time periods . A serial I/O port to allow data to flow between the microcontroller and other devices such as a PC or another microcontroller . An ADC to allow the microcontroller to accept analogue input data for processing
Microcontroller Hardware
. Microcontrollers are nowadays brimming with added functionality on this single IC and often include:
. Communications Peripherals: SPI, I2C., UART, CAN, USB, Ethernet, IrDA, LIN . Control Peripherals: capture/compare, counters, real-time clock & calendar, motor control & PWM . Integrated Display Drivers: LED, LCD . On-chip internal oscillators and Phase-Locked Loop (closed loop feedback control) . Analog Peripherals: A/D Converters, comparators, op amps, brown-out detection & reset, low voltage detection, temperature sensors, D/A Converters and voltage regulators
29
Memory in a microcontroller
The amount of memory contained within a microcontroller varies between different microcontrollers Some may not even have any integrated memory (eg. Hitachi 6503, now discontinued) However, most modern microcontrollers will have integrated Memory. The memory will be divided up into ROM and RAM, with typically more ROM than RAM
30
Microcontroller ROM
. The ROM of a microcontroller is used to store the application program
31
Microcontroller ROM
. Typically, the amount of ROM type memory will vary between around 512 bytes and 4096 bytes . Some 16 bit microcontrollers such as the Hitachi H8/3048 can have as much as 128 Kbytes of ROM type memory . ROM type memory, as has already been mentioned, is used to store the program code . ROM memory can be either ROM (as in One Time Programmable memory), EPROM, or EEPROM
32
Microcontroller RAM
. The microcontroller RAM is used to store data
33
34
Microcontroller Manufacturers
. There are lots of microcontroller manufacturers
35
Advantages of PIC
. It is a RISC (Reduced Instruction Set Computer) design . Only thirty seven instructions to remember . Its code is extremely efficient, allowing the PIC to run with typically less program memory than its larger competitors. . It is low cost, high clock speed
36
Harvard architecture
. Like many micros the PIC is a Harvard not a von-Neumann machine . This is simpler and faster . Separate program bus and data bus: can be different widths! . For example, PICs use:
. Data memory (RAM): a small number of 8bit registers . Program memory (ROM): 12bit, 14bit or 16bit wide (in EPROM, FLASH, or ROM)
37
38
39
40
42
PIC Microcontroller
The PIC Family: Cores . PICs come with 1 of 4 CPU cores: . 12bit cores with 33 instructions: 12C50x, 16C5x . 14bit cores with 35 instructions: 12C67x,16Cxxx . 16bit cores with 58 instructions: 17C4x,17C7xx . Enhanced 16bit cores with 77 instructions: 18Cxxx The PIC Family: Packages . PICs come in a huge variety of packages: . 8 pin DIPs, SOICs: 12C50x (12bit) and 12C67x (14bit) . 18pin DIPs, SOICs: 16C5X (12bit), 16Cxxx (14bit) . 28pin DIPs, SOICs: 16C5X (12bit), 16Cxxx (14bit) . 40pin DIPs, SOICs: 16Cxxx (14bit), 17C4x (16bit) . 44 - 68pin PLCCs*: 16Cxxx (14bit), 17C4x / 17Cxxx (16bit)
43
PIC Microcontroller
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
44
PIC Microcontroller
The PIC Family: Program Memory
. PIC program space is different for each chip. . Some examples are:
12C508 512 12bit instructions 16C71C 1024 (1k) 14bit instructions 16F877 8192 (8k) 14bit instructions 17C766 16384 (16k) 16bit instructions
45
. Dont forget, programs are stored in program space (not in data space), so low RAM values are OK.
47
48
49
. Most pins have 25mA source/sink (directly drives LEDs) . WARNING: Other peripherals SHARE pins!
50
51
52
PIC Microcontrollers
INSTRUCTION SET PIC16F887
Harvard Architecture
von-Neumann Architecture
Encoding of instruction
Each instruction is of 14-bit long. These 14bits contain both op-code and the operand. bcf f, b Clear 'b' bit of register 'f Encoding: The instruction is executed in one instruction cycle, i.e., 4 clock cycles.
Encoding of instruction
goto K Encoding: Go to label 'k' instruction
Since this instruction requires modification of program Counter, it takes two instruction cycles for execution
Description: The content of the register W is added to the 8-bit literal k. The result is stored in the W register. Operation: (W) + k -> W Operand: 0 k 255 Status affected: C, DC, Z Number of cycles: 1 movlw 0x0F addlw 0x03 Kt qu: W = 0x12, Z=0 DC=1 C=0
Description: Add the contents of the W and f registers. If d = w or d = 0 the result is stored in the W register. If d = f or d = 1 the result is stored in register f. Operation: (W) + (f) -> d cblock 0x20 Operand: 0 f 127, d [0,1] REG Endc Status affected: C, DC, Z Number of cycles: 1 movlw 0x20
movwf REG movlw 0x17 ADDWF REG,w
Kt qu: W = 0x37,
Z=0
DC=0
C=0
cblock 0xC2 REG Endc movlw movwf movlw movwf movlw ADDWF
Indirect addressing
Indirect addressing
A simple program to clear RAM location 20h2Fh using indirect addressing
The INDF register is not a physical register. Reading INDF itself indirectly will produce 00h. Writing to the INDF register indirectly results in a no operation
Description: The content of the register W is ANDed with the 8-bit literal k. It means that the result will contain one (1) only if both corresponding bits of operand are ones (1). The result is stored in the W register. Operation: (W) AND k -> W Operand: 0 k 255 Status affected: Z movlw 0xAA Number of cycles: 1
ANDLW 0x55
Kt qu: W = 0x00,
Z=1
REG=0x02 REG=0xC2
andwf REG, w
Z=0 Z=0
f, b
Description: Bit b of register f is cleared. Operation: (0) -> f(b) cblock 0xC2 Operand: 0 f 127, 0 b 7 REG Status affected: endc banksel REG Number of cycles: 1
movlw movwf bcf
f,b
cblock Endc movlw movwf movlw movwf movlw bsf 0xC2 FSR 0x20 INDF 0x17 INDF, 3 0xC2 REG
Description: Bit b of register f is set. Operation: 1 -> f (b) Operand: 0 f 127, 0 b 7 Status affected: Number of cycles: 1 Kt qu:
f, b
Description: If bit b of register f is 0, the next instruction is discarded and a NOP is executed instead, making this a two-cycle instruction. Operation: Discard the next instruction if f(b) = 0 cblock 0x20 Operand: 0 f 127, 0 b 7 Kt qu: REG Status affected: Endc movlw 0x01 Number of cycles: 1 or 2 depending on bit b W = 0x18 movwf REG
movlw movwf movlw btfsc addlw addlw 0x00 REG 0x17 REG,0 0x01 0x00
Kt qu:
W = 0x17
f, b
Description: If bit b of register f is 1, the next instruction is discarded and a NOP is executed instead, making this a two-cycle instruction. Operation: Discard the next instruction if f(b) = 1 cblock 0x20 Operand: 0 f 127, 0 b 7 Kt qu: REG Status affected: Endc movlw 0x01 Number of cycles: 1 or 2 depending on bit b W = 0x17 movwf REG
movlw movwf movlw btfss addlw addlw 0x00 REG 0x17 REG,0 0x01 0x00
Kt qu:
W = 0x18
PCLATH Program Counter LATch High The size of the program memory of the PIC16F887 is 8K (13 bits address)
Call Subroutine
CALL k (2) 10 0kkk kkkk kkkk
Go to address
GOTO k (2) 10 1kkk kkkk kkkk
Description: The content of register f is cleared and the Z flag of the STATUS register is set. Operation: 0 -> f banksel TRISB Operand: 0 f 127 movlw 0xff Status affected: Z movwf TRISB Number of cycles: 1 clrf TRISB
Kt qu: TRISB = 0x00, Z=1
Kt qu: W = 0x00,
Z=1
f, d
Description: The content of register f is complemented (logic zeros (0) are replaced by ones (1) and vice versa). If d = w or d = 0 the result is stored in W. If d = f or d = 1 the result is stored in register f. Operation: (f) -> d Operand: 0 f 127, d[0,1] movlw 0x13 Status affected: Z movwf REG Number of cycles: 1
COMF REG,w
W = 0xEC, Z = 0
f, d
Description: Decrement register f by one. If d = w or d = 0, the result is stored in the W register. If d = f or d = 1, the result is stored in register f. Operation: (f) - 1 -> d Operand: 0 f 127, d[0,1] movlw 0x01 movwf REG Status affected: Z DECF REG,f Number of cycles: 1
REG = 0x00, Z = 1
f, d
Description: Decrement register f by one. If d = w or d = 0, the result is stored in the W register. If d = f or d = 1, the result is stored in register f. If the result is 0, then a NOP is executed instead, making this a two-cycle instruction. .... Operation: (f) - 1 -> d MOVLW .10 Operand: 0 f 127, d[0,1] MOVWF CNT ;10 -> CNT Status affected: Loop ...... ...... ;Instruction block Number of cycles: ...... 1 or 2 depending on the result. DECFSZ CNT,f ; decrement REG by one GOTO Loop ; Skip this line if CNT= 0 LAB_03 ....... ; Jump here if CNT = 0
f, d
Description: Increment register f by one. If d = w or d = 0, the result is stored in register W. If d = f or d = 1, the result is stored in register f. Operation: (f) + 1 -> d Operand: 0 f 127, d[0,1] movlw 0x10 Status affected: Z movwf REG Number of cycles: 1 incf REG,w
f, d
Description: Register f is incremented by one. If d = w or d = 0, the result is stored in register W. If d = f or d = 1, the result is stored in register f. If the result is 0, then a NOP is executed instead, making this a two-cycle instruction. Operation: (f) + 1 -> d Operand: 0 f 127, d[0,1] Status affected: Number of cycles: 1 or 2 depending on the result.
IORLW k
Description: The content of the W register is ORed with the 8-bit literal k. The result is stored in register W. Operation: (W) OR (k) -> W Operand: 0 k 255 movlw 0x9A Status affected: Z iorlw 0x35 Number of cycles: 1
W = 0xBF, Z=0
f, d
Description: The content of register f is ORed with the content of W register. If d = w or d = 0, the result is stored in the W register. If d = f or d = 1, the result is stored in register f. Operation: (W) OR (f) -> d movlw 0x13 movwf REG Operand: 0 f 127, d -> [0,1] movlw 0x91 Status affected: Z iorwf REG,w Number of cycles: 1
f, d
Description: The content of register f is moved to a destination determined by the operand d. If d = w or d = 0, the content is moved to register W. If d = f or d = 1, the content remains in register f. Option d = 1 is used to test the content of register f because this instruction affects the Z flag of the STATUS register. movlw 0xC2 Operation: (f) -> d movwf FSR Operand: 0 f 127, d -> [0,1] clrw movf FSR,w Status affected: Z Number of cycles: 1
Before instruction execution: FSR=0xC2 W=0x00 After instruction: W=0xC2 Z = 0
Description: 8-bit literal k is moved to register W. Operation: k -> (W) Operand: 0 k 255 Status affected: Number of cycles: 1 Const equ 0x40
MOVLW Const
W=0x40
Description: The content of register W is moved to register f. Operation: (W) -> f Operand: 0 f 127 banksel OPTION_REG Status affected: movlw 0x20 Number of cycles: 1 movwf OPTION_REG OPTION_REG=0x20
Description: 8-bit literal k is loaded into register W. The value from the top of stack is loaded to the program counter. Operation: (k) -> W; top of stack (TOP) -> PC Operand: Status affected: Number of cycles: 2
f, d
Description: The content of register f is rotated one bit to the left through the Carry flag. If d = w or d = 0, the result is stored in register W. If d = f or d = 1, the result is stored in register f. Operation: (f(n)) -> d(n+1), f(7) -> C, C -> d(0); Operand: 0 f 127, d[0,1] Status affected: C Number of cycles: 1
banksel STATUS bcf STATUS, 0 movlw 0xE6 movwf REG rlf REG,f banksel STATUS bcf STATUS, 0 movlw 0xE6 movwf REG rlf REG,w
REG = 0xCC
C=1
W = 0xCC
C=1
f, d
Description: The content of register f is rotated one bit right through the Carry flag. If d = w or d = 0, the result is stored in register W. If d = f or d = 1, the result is stored in register f. Operation: (f(n)) -> d(n-1), f(0) -> C, C -> d(7); Operand: 0 f 127, d -> [0,1] Status affected: C Number of cycles: 1 banksel STATUS
banksel STATUS bcf STATUS, 0 movlw 0xE6 movwf REG rrf REG, f bcf STATUS, 0 movlw 0xE6 movwf REG rrf REG,w
W = 0x73 C=0
C=0
REG = 0x73
W = 0x02, Z = 0, DC = 1, C = 1
movlw 0x03 sublw 0x03
W = 0x00, Z = 1, DC = 1, C = 1
movlw 0x04 sublw 0x03
W = 0xFF, Z = 0, DC = 0, C = 0
REG = 0x01, Z = 0, DC = 1, C = 1
movlw 0x02 movwf REG movlw 0x02 subwf REG,f
REG = 0x00, Z = 1, DC = 1, C = 1
movlw 0x01 movwf REG movlw 0x02 subwf REG,f
REG = 0xFF, Z = 0, DC = 0, C = 0
W = 0x1A, Z = 0
Const equ 0x37 movlw 0xAF XORLW Const
W = 0x98, Z = 0
REG = 0x1A, Z = 0
movlw 0xAF movwf REG movlw 0xB5 XORWF REG,w
W = 0x1A, Z = 0