Interfacing PIC Microcontrollers 10 pdf

10 255 0
Interfacing PIC Microcontrollers 10 pdf

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

Thông tin tài liệu

Note the effect of each error type and the message produced by the assembler. What general type of error are they? Warning ‘default destination being used’ should be received in the list file. What does this mean? Eliminate it by chang- ing the assembler error level to suppress messages and warnings. With the program restored so that it assembles correctly: Replace ‘BTFSS’ with ‘BTFSC’ Omit (comment out) ‘GOTO reset’ Note the effect of these errors. What general type of error are they? Describe the process used to detect each one. Interfacing PIC Microcontrollers 76 Else_IPM-BATES_ch003.qxd 6/27/2006 12:58 PM Page 76 Part 2 Interfacing Else_IPM-BATES_CH004.qxd 6/29/2006 11:07 AM Page 77 77 This page intentionally left blank 78 4 Input & Output We can now proceed to the main business of this book – describing a range of input & output techniques which will help you to design microcontroller cir- cuits. Today, the typical MCU-based consumer product can be extremely com- plex, and contains a wide range of technologies around the main controller. The mobile phone is a good example; in addition to the sophisticated digital communications subsystem which provides its main function, it can also have a full-colour, medium resolution liquid crystal display (LCD) screen, camera, sound system and so on. A detailed understanding of these technologies re- quires a very high level of engineering skill. To help develop this skill, some simpler equivalent technologies must be studied – for example, we will see how to display character-based information on a low-resolution monochrome alphanumeric LCD, and ignore its graphics capabilities for now. Switch Input The simplest input is a switch or push button. This can operate with just one additional support component, a pull-up resistor, but there are still some sig- nificant issues to consider, such as input loading and debouncing. We have seen in the BINX hardware how a simple push button or switch is interfaced with a pull-up resistor. Let us make sure we understand how this works (Figure 4.1). Else_IPM-BATES_CH004.qxd 6/29/2006 11:07 AM Page 79 79 When the switch is open, the output voltage of the circuit is pulled up to ϩ5 V via the resistor. Another way to look at it is that there is no current in the re- sistor (assuming there is no load on the output), so there is no volt drop, and the output voltage must be the same as the supply (ϩ5 V). When the switch is closed the output is connected direct to 0 V; the resistor prevents the supply being shorted to ground. The resistor value is not critical, but if there is any load on the output, the pull-up must be significantly lower in value than the load, for the digital volt- age levels to be valid. On the other hand, its value should be as high as possi- ble to minimise wasted power, especially if the circuit is battery-powered. Let us say the load on the output (R i ) was equivalent to 400k (100 A with a 5 V supply), the pull-up resistor (R p ) should be 100k or less, so that the output volt- age with the switch open would be at least 4 V. The minimum voltage which will be reliably recognised as logic 1 at a PIC input is 2.0 V, and the maximum voltage recognised as a logic 0 is 0.8 V, assuming a ϩ5 V supply. The input leakage current is actually only about 1 A. If power conservation is not criti- cal, a 10k resistor is a reasonable choice. If a PIC input is open circuit, it is pulled up to Vdd (normally ϩ5 V) internally, that is, it floats high. On some ports (Port B), weak pull-ups can be enabled to eliminate the need for external pull-up resistors. The switch symbol assumes toggle mode operation – the switch remains in the set position until changed. Push buttons normally assumed to be closed only when held – the toggle operation can be implemented in software if required, to obtain a push-on, push-off operation. Interfacing PIC Microcontrollers 80 0V 5V Output, Vo Switch Pull-up Resistor Rp Input Resistance Ri Vo = 5.Ri/(Rp+Ri) Debounce Capacitor PIC Figure 4.1 Input switch Else_IPM-BATES_CH004.qxd 6/29/2006 11:07 AM Page 80 Switch Debouncing When the contacts close in any mechanical switch or push button, they tend to bounce open before settling in closed position. In normal life, this is not notice- able or significant, but in microsystems it is liable to cause circuit misbehaviour if ignored. The effect generally lasts a few milliseconds, but if the switch is sam- pled at high speed, it can appear that it has been operated several times. On the other hand, the sequence of the program may be such that the switch bounce does not adversely affect the correct operation of the system. For ex- ample, if the program does not recheck the input until the bouncing has fin- ished anyway, no specific debouncing is needed. In Figure 4.2 (a), the output voltage from a switch jumps back up to 5 V due to the switch contacts bouncing open. If a suitable capacitor is connected across the switch, as in Figure 4.2 (b) it is initially charged up to 5 V. When the contacts close, it is quickly discharged by the short circuit. However, it can only recharge via the pull-up resistor, which takes more time. If the switch closes again before the logic 0 minimum threshold is crossed (0.8 V), the volt- age is prevented from going back to logic 1. The capacitor needs to be large enough to give a slow voltage rise in the charging phase, while not being so high in value as to cause a large discharge current through the switch contacts when the contacts close, or making the rise time too long when the switch is opened. With a 10k pull-up resistor, a 10 nF capacitor would give a time constant of 100 ms, which should be more than adequate. Input & Output 81 (a) (b) Time +5V 0.8V Switch closes Figure 4.2 Switch hardware debounce (a) without debounce capacitor; (b) with debounce capacitor Else_IPM-BATES_CH004.qxd 6/29/2006 11:07 AM Page 81 The circuit shown in Figure 4.3 will be used to illustrate debouncing. A bi- nary display count is to be incremented under manual control, via a switch input, one step at a time. The circuit now includes the ICD programming con- nections with a 6-way connector, as would be required in the actual hardware. These will not always be shown, but will be required if the ICD method is to be used when debugging the real circuit. To accommodate these connections, the LEDs have been moved to Port D, and spare lines in Port B used for the inputs. If the switch input is not debounced, several counts might be registered. The debounce process shown in Program 4.1 (LED1S) uses the same software delay routine previously used to provide delays between each output step. Note that the simulated switch model has a delay of 1 ms to represent the bounce ef- fect, which does not, unfortunately, accurately model the real switch behaviour. However, it can still be seen that if the software delay is commented out, the count is not reliable. Interfacing PIC Microcontrollers 82 Figure 4.3 LED counter with ICD interface Else_IPM-BATES_CH004.qxd 6/29/2006 11:07 AM Page 82 Input & Output 83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Source File: LED1S.ASM ; Author: MPB ; Date: 2-12-05 ; ; Output binary count is stepped manually ; and reset with push buttons. ; Demonstrates software delay switch debounce ; Hardware: BIN4X simulation ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; PROCESSOR 16F877 ; Define MCU type __CONFIG 0x3733 ; Set config fuses ; Register Label Equates PORTB EQU 06 ; Port B Data Register PORTD EQU 08 ; Port D Data Register TRISD EQU 88 ; Port B Direction Register Timer EQU 20 ; GPR used as delay counter ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Initialise Port B (Port A defaults to inputs) BANKSEL TRISD ; Select bank 1 MOVLW b'00000000' ; Port B Direction Code MOVWF TRISD ; Load the DDR code into F86 BANKSEL PORTD ; Select bank 0 GOTO reset ; Jump to main loop ; 'delay' subroutine delay MOVWF Timer ; Copy W to timer register down DECFSZ Timer ; Decrement timer register GOTO down ; and repeat until zero RETURN ; Jump back to main program ; Start main loop reset CLRF PORTD ; Clear LEDs start BTFSS PORTB,1 ; Reset? GOTO reset ; Yes - clear LEDs BTFSC PORTB,2 ; Step on? GOTO start ; No - wait MOVLW 0FF ; Delay count literal CALL delay ; Wait for count BTFSS PORTB,2 ; Step on? GOTO start ; Yes – wait INCF PORTD ; Increment LEDs GOTO start ; Repeat always END ; Terminate source code Program 4.1 Software debouncing Else_IPM-BATES_CH004.qxd 6/29/2006 11:07 AM Page 83 Timer and Interrupts The main problem with the delay loop method is that MCU time is being wasted (at 5 Mhz instruction rate, 5000 instruction cycles could be com- pleted in 1 ms). In a high performance system (high speed, large data throughput), this is very inefficient, so the use of a hardware timer would be preferred. This option allows the MCU to proceed with other tasks while car- rying out a timing operation concurrently. In addition, the switch problem is a good opportunity to examine the use of hardware timers and interrupts in general. If a hardware timer is started when the switch is first closed, the closure can be confirmed by retesting the input after a time delay to check if it is still closed. Alternatively, the switch input can be processed after the button is re- leased, rather than when it is closed. The system responds when the switch is released rather than when it is pressed, again avoiding the bounce problem. The same virtual hardware (Figure 4.3) will be used for Program 4.2 (LED1H) which illustrates the use of a hardware timer. TMR0 (Timer0) is lo- cated at file register address 01. It operates as an 8-bit binary up counter, driven from an external or internal clock source. The count increments with each input pulse, and a flag is set when it overflows from FF to 00. It can be pre-loaded with a value so that the time out flag is set after the required inter- val. For example, if it is pre-loaded with the number 156 10 , it will overflow after 100 counts (256 10 ). A block diagram of Timer0 is shown in Figure 4.4. Timer0 has a pre-scaler available at its input, which divides the number of input pulses by a factor of 2, 4, 8, 16, 32, 64, 128 or 256, which increases the range of the count but reduces its accuracy. For timing purposes, the internal clock is usually selected, which is the instruction clock seen at CLKOUT in RC mode (f osc /4). Thus the register is incremented once per instruction cycle (there are 1 or 2 cycles per instruction), without the pre-scaler. At 10 kHz, it will count in steps of 100 s. Interrupts are another way of increasing the program efficiency. They allow external devices or internal events to force a change in the execution sequence of the MCU. When an interrupt occurs, in the PIC the program jumps to pro- gram address 004, and continues from there until it sees a Return From Interrupt (RETFIE) instruction. It then resumes at the original point, the return address having been stored automatically on the stack as part of the interrupt process. Typically, a GOTO ISR (Interrupt Service Routine) is placed at the in- terrupt address 004, and the ISR placed elsewhere in memory using ORG. The interrupt source is identified by the associated flag (in this case, Timer0 over- flow flag). This has an associated interrupt enable bit to enable the MCU to re- spond to this particular source, and a global enable bit to disable all interrupts Interfacing PIC Microcontrollers 84 Else_IPM-BATES_CH004.qxd 6/29/2006 11:07 AM Page 84 Input & Output 85 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Source File: LED1H.ASM ; Author: MPB ; Date: 2-12-05 ; ; Output binary count incremented ; and reset with push buttons. ; Uses hardware timer to debounce input switch ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; PROCESSOR 16F877 ; Define MCU type __CONFIG 0x3733 ; Set config fuses ; Register Label Equates PORTB EQU 06 ; Port B Data Register PORTD EQU 08 ; Port D Data Register TRISD EQU 88 ; Port D Direction Register TMR0 EQU 01 ; Hardware Timer Register INTCON EQU 0B ; Interrupt Control Register OPTREG EQU 81 ; Option Register ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ORG 000 ; Start of program memory NOP ; For ICD mode GOTO init ; Jump to main program ; Interrupt Service Routine ORG 004 BCF INTCON,2 ; Reset TMR0 interrupt flag RETFIE ; Return from interrupt ; Initialise Port D (Port B defaults to inputs) init NOP ; BANKSEL cannot be labelled BANKSEL TRISD ; Select bank 1 MOVLW b'00000000' ; Port B Direction Code MOVWF TRISD ; Load the DDR code into F86 ; Initialise Timer0 MOVLW b'11011000' ; TMR0 initialisation code MOVWF OPTREG ; Int clock, no prescale BANKSEL PORTD ; Select bank 0 MOVLW b'10100000' ; INTCON init. code MOVWF INTCON ; Enable TMR0 interrupt ; Start main loop reset CLRF PORTD ; Clear Port B Data start BTFSS PORTB,1 ; Test reset button GOTO reset ; and reset Port B if pressed BTFSC PORTB,2 ; Test step button GOTO start ; and repeat if not pressed CLRF TMR0 ; Reset timer wait BTFSS INTCON,2 ; Check for time out GOTO wait ; Wait if not stepin BTFSS PORTB,2 ; Check step button GOTO stepin ; and wait until released INCF PORTD ; Increment output at Port B GOTO start ; Repeat main loop always END ; Terminate source code Program 4.2 Hardware timer debouncing Else_IPM-BATES_CH004.qxd 6/29/2006 11:07 AM Page 85 . they? Describe the process used to detect each one. Interfacing PIC Microcontrollers 76 Else_IPM-BATES_ch003.qxd 6/27/2006 12:58 PM Page 76 Part 2 Interfacing Else_IPM-BATES_CH004.qxd 6/29/2006 11:07. push-on, push-off operation. Interfacing PIC Microcontrollers 80 0V 5V Output, Vo Switch Pull-up Resistor Rp Input Resistance Ri Vo = 5.Ri/(Rp+Ri) Debounce Capacitor PIC Figure 4.1 Input switch Else_IPM-BATES_CH004.qxd. Initialise Timer0 MOVLW b' 1101 1000' ; TMR0 initialisation code MOVWF OPTREG ; Int clock, no prescale BANKSEL PORTD ; Select bank 0 MOVLW b&apos ;101 00000' ; INTCON init. code MOVWF

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