Interfacing PIC Microcontrollers 3 ppt

10 256 0
Interfacing PIC Microcontrollers 3 ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

Thus, a list of instructions in memory is executed in turn to carry out the required process. In a word processor, for example, keystrokes are read in via the keyboard port, stored as character codes, and sent to a screen output for display. In a game, input from the switches on the control pad are processed and used to modify the screen. In this case, speed of the system is a critical factor. Memory There are two types of memory: volatile and non-volatile. Volatile memory loses its data when switched off, but can be written by the CPU to store current data; this is RAM (Random Access Memory). ROM (Read Only Memory) is non-volatile, and retains its data when switched off. In a PC, a small ROM is used to get the system started when it is switched on; it contains the BIOS (Basic Input Output System) program. However, the main Operating System (OS), for example, Windows™ and application program (e.g. Word) have to be loaded into RAM from Hard Disk Drive (HDD), which takes some time, as you may have noticed! So why not put the OS in ROM, where it would be instantly available? Well, RAM is faster, cheaper and more compact, and the OS can be changed or upgraded if required. In addition, an OS such as Windows is very large, and some elements are only loaded into RAM as needed. In addition, numerous applications can be stored on disk, and loaded only as required. The ideal memory is non-volatile, read and write, fast, large and cheap. Unfortunately, it does not exist! Therefore, we have a range of memory technologies as shown in Table 1.1, which provide different advantages, which Interfacing PIC Microcontrollers 6 Flash ROM ROM RAM CD-ROM DVD-RW HDD Description Chip Chip Chip Optical Optical Magnetic disk disk disk Sample size* 128 kb 128 Mb 512 Mb 650 Mb 4.7 Gb 30 Gb Non-volatile x  Write (many) Once Once  Large (bytes) x ? x  Cheap (per bit) ? x ?  Fast (access) ? xxx *1 byte ϭ 8 bits 1 kb ϭ 1 kilobyte ϭ 1024 bytes 1 Mb ϭ 1 megabyte ϭ 1024 kb 1 Gb ϭ 1 gigabyte ϭ 1024 Mb Table 1.1 Memory and data storage technologies Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 6 may all be used with a standard PC. The main trade-off is cost, size and speed of access. Flash ROM, as used in memory sticks and MP3 players, is closest to the ideal, having the advantages of being non-volatile and rewritable. This is why it is used as program memory in microcontrollers which need to be reprogrammed, such as the PIC 16F877. Input and Output Without some means of getting information and signals in and out, a data processing or digital control system would not be very useful. Ports are based on a data register, and set of control registers, which pass the data in and out in a controlled manner, often according to a standard protocol (method of communication). There are two main types of port: parallel and serial. In a parallel port, the data is usually transferred in and out 8 bits at a time, while in the serial port it is transmitted 1 bit at a time on a single line. Potentially, the parallel port is faster, but needs more pins; on the other hand, the port hardware and driver software are simpler, because the serial port must organise the data in groups of bits, usually 1 byte at a time, or in packets, as in a network (Figure 1.3). Taking printers as an example, the old standard is a parallel port (Centronics), which provides data to the printer 1 byte (8 bits) at a time via a multipin connector. The new standard, USB (Universal Serial Bus) is a serial data system, sending only 1 bit at a time. Potentially, the parallel connection is 8 times faster, but USB operates at up to 480 megabits (Mb) per second, and the printer is slow anyway, so there is no problem. One advantage of using PIC Hardware 7 (a) Parallel Port Register Internal Data Bus External data lines Read/Write Control External data line Serial Port Register Internal Data Bus Read/Write Control (b) Figure 1.3 Parallel and serial data ports: (a) parallel; (b) serial Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 7 USB is that it provides a simple, robust connector and this outweighs the fact that the interface protocol (driver software) is relatively complex, because this is hidden from the user. USB also provides power to the peripheral, if required, and the printer can be daisy-chained with other devices. USB also automati- cally configures itself for different peripherals, such as scanners and cameras. In the parallel port operating in output mode, the data byte is loaded from the internal data bus under the control of a read/write signal from the CPU. The data can then be seen on the output pins by the peripheral; for testing, a logic probe, logic analyser or just a simple LED indicator can be used. In input mode, data presented at the input pins from a set of switches or other data source are latched into the register when the port is read, and is then available on the data bus for collection by the CPU. One of the functions of the port is to separate the internal data bus from the external hardware, and the other is to temporarily store the data. The data can then be transferred to memory, or otherwise processed, as determined by the CPU program. The serial port register also loads data from the internal bus in parallel, but then sends it out 1 bit at a time, operating as a shift register. If an asynchronous serial format is used, such as RS232 (COM ports on old PCs), start and stop bits are added so that bytes can be separated at the receiving end. An error check bit is also available, to allow the receiver to detect corrupt data. In receive mode, the register waits for a start bit, and then shifts in the data at the same speed as it is sent. This means the clock rate for the send and receive port must be the same. The USART (Universal Synchronous/Asynchronous Receive/Transmit) protocol will be described in more detail later. A USB or network port is more sophisticated, and arranges the data bytes in packets of, say, 1k bytes, which are sent in a form which is self-clocking; that is, there is a transition within each bit (1 or 0), so each can be picked up indi- vidually. An error-correction code follows the data, which allows mistakes to be corrected, rather than just be detected. This reduces the need for retransmission of incorrectly received data, as required by simple error detection. Addressing information preceding the data allows multiple receivers to be used. The PIC 16F877, in common with most current MCUs, does not have USB or network interfaces built in, so we can avoid detailed consideration of these complex protocols. It does, nevertheless, have a good selection of other inter- faces, which will be discussed in detail and sample programs provided. PIC 16F877 Architecture Microcontrollers contain all the components required for a processor system in one chip: a CPU, memory and I/O. A complete system can therefore be built Interfacing PIC Microcontrollers 8 Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 8 using one MCU chip and a few I/O devices such as a keypad, display and other interfacing circuits. We will now see how this is done in practice in our typical microcontroller. PIC 16F877 Pin Out Let us first consider the pins that are seen on the IC package, and we can then discover how they relate the internal architecture. The chip can be obtained in different packages, such as conventional 40-pin DIP (Dual In-Line Package), square surface mount or socket format. The DIP version is recommended for prototyping, and is shown in Figure 1.4. Most of the pins are for input and output, and arranged as 5 ports: A(5), B(8), C(8), D(8) and E(3), giving a total of 32 I/O pins. These can all operate as simple digital I/O pins, but most have more than one function, and the mode of operation of each is selected by initialising various control registers within the chip. Note, in particular, that Ports A and E become ANALOGUE INPUTS by default (on power up or reset), so they have to set up for digital I/O if required. Port B is used for downloading the program to the chip flash ROM (RB6 and RB7), and RB0 and RB4–RB7 can generate an interrupt. Port C gives access to timers and serial ports, while Port D can be used as a slave port, with Port E providing the control pins for this function. All these options will be explained in detail later. PIC Hardware 9 Reset = 0, Run = 1 MCLR 140RB7 Port B, Bit 7 (Prog. Data, Interrupt) Port A, Bit 0 (Analogue AN0) RA0 239 RB6 Port B, Bit 6 (Prog. Clock, Interrupt)) Port A, Bit 1 (Analogue AN1) RA1 338 RB5 Port B, Bit 5 (Interrupt) Port A, Bit 2 (Analogue AN2) RA2 437 RB4 Port B, Bit 4 (Interrupt) Port A, Bit 3 (Analogue AN3) RA3 5 36 RB3 Port B, Bit 3 (LV Program) Port A, Bit 4 (Timer 0) RA4 635 RB2 Port B, Bit 2 Port A, Bit 5 (Analogue AN4) RA5 734RB1 Port B, Bit 1 Port E, Bit 0 (AN5, Slave control) RE0 833 RB0 Port B, Bit 0 (Interrupt) Port E, Bit 1 (AN6, Slave control) RE1 932V DD +5V Power Supply Port E, Bit 2 (AN7, Slave control) RE2 10 31 Vss 0V Power Supply +5V Power Supply V DD 11 30 RD7 Port D, Bit 7 (Slave Port) 0V Power Supply Vss 12 29 RD6 Port D, Bit 6 (Slave Port) (CR clock) XTAL circuit CLKIN 13 28 RD5 Port D, Bit 5 (Slave Port) XTAL circuit CLKOUT 14 27 RD4 Port D, Bit 4 (Slave Port) Port C, Bit 0 (Timer 1) RC0 15 26 RC7 Port C, Bit 7 (Serial Ports) Port C, Bit 1 (Timer 1) RC1 16 25 RC6 Port C, Bit 6 (Serial Ports) Port C, Bit 2 (Timer 1) RC2 17 24 RC5 Port C, Bit 5 (Serial Ports) Port C, Bit 3 (Serial Clocks) RC3 18 23 RC4 Port C, Bit 4 (Serial Ports) Port D, Bit 0 (Slave Port) RD0 19 22 RD3 Port D, Bit 3 (Slave Port) Port D, Bit 1 (Slave Port) RD1 20 21 RD2 Port D, Bit 2 (Slave Port) Figure 1.4 PIC 16F877 pin out Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 9 The chip has two pairs of power pins (V DD ϭϩ5 V nominal and V ss ϭ 0 V), and either pair can be used. The chip can actually work down to about 2 V sup- ply, for battery and power-saving operation. A low-frequency clock circuit using only a capacitor and resistor to set the frequency can be connected to CLKIN, or a crystal oscillator circuit can be connected across CLKIN and CLKOUT. MCLR is the reset input; when cleared to 0, the MCU stops, and restarts when MCLR ϭ 1. This input must be tied high allowing the chip to run if an external reset circuit is not connected, but it is usually a good idea to incorporate a manual reset button in all but the most trivial applications. PIC 16F877 Block Diagram A block diagram of the 16F877 architecture is given in the data sheet, Figure 1-2 (downloadable from www.microchip.com). A somewhat simplified version is given in Figure 1.5, which emphasises the program execution mechanism. The main program memory is flash ROM, which stores a list of 14-bits instructions. These are fed to the execution unit, and used to modify the RAM file registers. These include special control registers, the port registers and a set of general purpose registers which can be used to store data temporarily. A sep- arate working register (W) is used with the ALU (Arithmetic Logic Unit) to process data. Various special peripheral modules provide a range of I/O options. There are 512 RAM File Register addresses (0–1FFh), which are organised in 4 banks (0–3), each bank containing 128 addresses. The default (selected on power up) Bank 0 is numbered from 0 to 7Fh, Bank 1 from 80h to FFh and so on. These contain both Special Function Registers (SFRs), which have a dedicated purpose, and the General Purpose Registers (GPRs). The file regis- ters are mapped in Figure 2-3 of the data sheet. The SFRs may be shown in the block diagram as separate from the GPRs, but they are in fact in the same logical block, and addressed in the same way. Deducting the SFRs from the total number of RAM locations, and allowing for some registers which are re- peated in more than one bank, leaves 368 bytes of GPR (data) registers. Test Hardware We need to define the hardware in which we will demonstrate PIC program operation. Initially, a block diagram is used to outline the hardware design (Figure 1.6). The schematic symbol for the MCU is also shown indicating the pins to be used. For this test program, we simply need inputs which switch between 0 V and ϩ5 V, and a logic indication at the outputs. For simulation purposes, we will see that the clock circuit does not have to be included in the schematic; instead, the clock frequency must be input to the MCU properties dialogue. The power supply pins are implicit – the simulated MCU operates at ϩ5 V by default. Unused pins can be left open circuit, as long as they are programmed as inputs. Interfacing PIC Microcontrollers 10 Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 10 The full schematic is shown in Chapter 3 (Figure 3.1). The first test program, BIN1, will simply light a set of LEDs connected to Port B in a binary count sequence, by incrementing Port B data register. The second program, BIN4, will use two input push buttons attached to Port D to control the output (start, stop and reset). The program will also include a delay so that the output is slower, and visible to the user. Detailed design of the interfacing will be covered later. A simple CR clock will be used, which is set to 40 kHz (C ϭ 4.7 nF, R ≈ 5 kΩ (preset), CR ϭ 25 µs). This will give an instruction execution time of 100 µs. PIC Hardware 11 Flash Program Memory 8192 x 14 bits 0000 – 1FFF Instruction Register Instruction Decode & Control MCU control lines Program Counter (13 bits) Stack 13 bits x 8 levels RAM File Registers 368 x 8 bits 000-1FF Data Bus (8 bits) File Select Register (8) Working Register (8) File Address (7) Literal (8) Op- code Instructions (14) Address (13) ALU Status Register (8) Ports, Timers ADC, Serial I/O Status bits Clock, Timing & Programming Control EEPROM Clock Reset A B C D E Figure 1.5 16F877 program execution block diagram Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 11 The PIC Program The program is written as a source code (a simple text file) on a PC host com- puter. Any text editor such as Notepad™ can be used, but an editor is provided with the standard PIC development system software MPLAB (downloadable from www.microchip.com). The instructions are selected from the pre-defined PIC instruction set (Table 13-2 in the data sheet) according to the operational sequence required. The source code file is saved as PROGNAME.ASM. More details of the assembler program syntax are given later. The source code is assembled (converted into machine code) by the assembler program MPASM, which creates the list of binary instruction codes. As this is normally displayed as hexadecimal numbers, it is saved as PROGNAME.HEX. This is then downloaded to the PIC chip from the PC by placing the MCU in a programming unit which is attached to the serial port of PC, or by connecting the chip to a programmer after fitting it in the application board (in-circuit pro- gramming). The hex code is transferred in serial form via Port B into the PIC flash program memory. A list file is created by the assembler, which shows the source code and machine code in one text file. The list file for a simple program which outputs a binary count at Port B is shown in Program 1.1. Interfacing PIC Microcontrollers 12 (a) (b) PIC 16F877 RC clock 40kHz X8 LEDs Input Buttons Active Low Port B LEDs RB0 – RB7 Port D Buttons RD0, RD1 RC Clock = CLKIN MCLR = 1 Power Supply Pins Implicit Vdd = 5V Vss = 0V Figure 1.6 BINX hardware outline: (a) block diagram; (b) PIC 16F877 MCU connections Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 12 PIC Hardware 13 The program listing includes the source code at the right, with source line numbers, the hex machine code and the memory location where each instruction is stored (0000–0004). Notice that some statements are assembler directives, not instructions: PROCESSOR to specify the MCU type and END to terminate the source code. These are not converted into machine code. The ‘877 has 8k of program memory, that is, it can store a maximum of 1024 ϫ 8 ϭ 8192 14-bit instructions. By default, it is loaded, and starts executing, from address zero. In real-time (control) applications, the program runs continuously, and therefore loops back at the end. If it does not, be care- ful – it will run through the blank locations and start again at the beginning! Let us look at a typical instruction to see how the program instructions are executed. Source code: MOVLW 05A Hex code: 305A (4 hex digits) Binary code: 0011 0000 0101 1010 (16 bits) Instruction: 11 00xx kkkk kkkk (14 bits) The instruction means: Move a Literal (given number, 5Ah) into the Working register. The source code consists of a mnemonic MOVLW and operand 05A. This assembles into the hex code 305A, and is stored in binary in program memory Memory Address Hex Code Address Label Operation Mnemonic Operand 00001 PROCESSOR 16F877 00002 0000 3000 00003 MOVLW 00 0001 0066 00004 TRIS 06 00005 0002 0186 00006 CLRF 06 0003 0A86 00007 again INCF 06 0004 2803 00008 GOTO again 00009 00010 END Note: Lines 00001 and 00010 are assembler directives Line Number Program 1.1 BIN1 list file Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 13 as 11 0000 0101 1010. Since each hex digit represents four binary bits, the leading two bits are zero, and the leading digit will only range from 0 to 3 for a 14-bit number. In the instruction set (data sheet, Table 13-2), it is seen that the first 4 bits (11 00) are the instruction code, the next two are unused (xx, appearing as 00 in the binary code) and the last 8 are the literal value (5A). The literal is represented as ‘kkkk kkkk’ since it can have any value from 00000000 to 11111111 (00–FF). The format of other instructions depends mainly on the number of bits required for the operand (data to be processed). The number of op-code bits can vary from 3 to all 14, depending on the number of bits needed for the operand. This is different from a conventional processor, such as the Pentium, where the op-code and operand are each created as a whole number of bytes. The PIC instruction is more compact, as is the instruction set itself, for greater speed of operation. This defines it as a RISC (Reduced Instruction Set Computer) chip. Program BIN4 The program BIN4 contains many of the basic program elements, and the list file (Program 1.2) shows the source code, machine code, memory address and list file line number as before. There are additional comments to aid program analysis and debugging. Note that two types of labels are used in program to represent numbers. Label equates are used at the top of the program to declare labels for the file registers which will be used in the program. Address labels are placed in the first column to mark the destination for GOTO and CALL instructions. Chip Configuration Word In Program BIN4, the assembler directive __CONFIG is included at the top of the program, which sets up aspects of the chip operation which cannot be sub- sequently changed without reprogramming. A special area of program memory outside the normal range (address 2007h) stores a chip configuration word; the clock type, and other MCU options detailed below, are set by loading the con- figuration bits with a suitable binary code. The function of each bit is shown in Table 1.2, along with some typical configuration settings. Details can be found in the data sheet, Section 12. CODE PROTECTION Normally, the program machine code can be read back to the programming host computer, be disassembled and the original source program recovered. This can be prevented if commercial or security considerations require it. The Interfacing PIC Microcontrollers 14 Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 14 15 MPASM 03.00 Released BIN4.ASM 8-28-2005 19:54:36 PAGE 1 LOC OBJECT CODE SOURCE TEXT VALUE LINE 00001 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 00002 ; 00003 ; Source File: BIN4.ASM 00004 ; Author: MPB 00005 ; Date: 28-5-05 00006 ; 00007 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 00008 ; 00009 ; Slow output binary count is stopped, started 00010 ; and reset with push buttons. 00011 ; 00012 ; Processor: PIC 16F877 00013 ; 00014 ; Hardware: PIC Demo System 00015 ; Clock: RC = 40kHz 00016 ; Inputs: Port D: Push Buttons 00017 ; RD0, RD1 (active low) 00018 ; Outputs: Port B: LEDs (active high) 00019 ; 00020 ; WDTimer: Disabled 00021 ; PUTimer: Enabled 00022 ; Interrupts: Disabled 00023 ; Code Protect: Disabled 00024 ; 00025 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 00026 00027 PROCESSOR 16F877 ; Define MCU type 2007 3733 00028 __CONFIG 0x3733 ; Set config fuses 00029 00030 ; Register Label Equates 00031 00000006 00032 PORTB EQU 06 ; Port B Data Register 00000086 00033 TRISB EQU 86 ; Port B Direction Register 00000008 00034 PORTD EQU 08 ; Port D Data Register 00000020 00035 Timer EQU 20 ; GPR used as delay counter 00036 00037 ; Input Bit Label Equates 00038 00000000 00039 Inres EQU 0 ; 'Reset' input button = RD0 00000001 00040 Inrun EQU 1 ; 'Run' input button = RD1 00041 00042 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 00043 00044 ; Initialise Port B (Port A defaults to inputs) 00045 0000 1683 1303 00046 BANKSEL TRISB ; Select bank 1 0002 3000 00047 MOVLW b'00000000' ; Port B Direction Code 0003 0086 00048 MOVWF TRISB ; Load the DDR code into F86 0004 1283 1303 00049 BANKSEL PORTB ; Select bank 0 0006 280B 00050 GOTO reset ; Jump to main loop 00051 00052 00053 ; 'delay' subroutine 00054 0007 00A0 00055 delay MOVWF Timer ; Copy W to timer register 0008 0BA0 00056 down DECFSZ Timer ; Decrement timer register 0009 2808 00057 GOTO down ; and repeat until zero 000A 0008 00058 RETURN ; Jump back to main program 00059 00060 00061 ; Start main loop 00062 000B 0186 00063 reset CLRF PORTB ; Clear Port B Data 00064 000C 1C08 00065 start BTFSS PORTD,Inres ; Test reset button 000D 280B 00066 GOTO reset ; and reset Port B if pressed 000E 1888 00067 BTFSC PORTD,Inrun ; Test run button 000F 280C 00068 GOTO start ; and repeat if not pressed 00069 0010 0A86 00070 INCF PORTB ; Increment output at Port B 0011 30FF 00071 MOVLW 0FF ; Delay count literal 0012 2007 00072 CALL delay ; Jump to subroutine 'delay' 0013 280C 00073 GOTO start ; Repeat main loop always 00074 00075 END ; Terminate source code Program 1.2 BIN4 list file Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 15 . (Analogue AN1) RA1 33 8 RB5 Port B, Bit 5 (Interrupt) Port A, Bit 2 (Analogue AN2) RA2 437 RB4 Port B, Bit 4 (Interrupt) Port A, Bit 3 (Analogue AN3) RA3 5 36 RB3 Port B, Bit 3 (LV Program) Port. 000 23 ; Code Protect: Disabled 00024 ; 00025 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 00026 00027 PROCESSOR 16F877 ; Define MCU type 2007 37 33 00028 __CONFIG 0x3 733 ;. fuses 00029 00 030 ; Register Label Equates 00 031 00000006 00 032 PORTB EQU 06 ; Port B Data Register 00000086 00 033 TRISB EQU 86 ; Port B Direction Register 00000008 00 034 PORTD EQU 08

Ngày đăng: 02/07/2014, 04:21

Tài liệu cùng người dùng

Tài liệu liên quan