1. Trang chủ
  2. » Công Nghệ Thông Tin

Interfacing PIC Microcontrollers 21 pot

10 360 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 305,63 KB

Nội dung

since the PIC cannot provide enough current. When the coil is activated, the con- tacts change over, completing the load circuit, which operates a lamp. Other high power loads such as heaters and motors can also be interfaced in this way, as long as simple on–off, but infrequent, switching is needed. The transistor is selected for sufficient collector current handling, and the base resistor to give plenty of base current, which is calculated from the required coil current: Coil current ϭ 40 mA ϭ collector current ) Base current ϭ 40 mA/100 ϭ 400 A ) Base resistor Ͻ (5Ϫ0.6) ϫ 400 ϫ 10 Ϫ6 ϭ 17.6 k ; 10 k The relay has a diode connected across the coil; this is a sensible precau- tion for all DC inductive loads (anything with a coil such as a motor or Interfacing PIC Microcontrollers 186 Figure 8.4 Power outputs interfaces Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 186 solenoid). When the coil is switched off, a large reverse voltage may be gen- erated as the magnetic field collapses (this is the way the spark is generated in a car ignition). The diode protects the transistor from the back EMF by forward conduction. In normal operation, the diode is reverse biased and has no effect. The relay is selected for the load current and voltage requirements, and the interface designed to provide the necessary coil operating current. However, it is slow, consumes a fairly large amount of power (40 mA ϫ 5 V ϭ 200 mW), and is relatively unreliable. The relay provides low on resistance and high off resistance. However, it wastes a relatively large amount of power in the coil, is slow and unreliable due to wear on the contacts. An alternative is the solid-state relay, which is typi- cally designed to switch AC loads from digital outputs with a solid-state de- vice. It contains TTL buffering, isolation and triac (see below) drive in one package, with high reliability and switching speed. Triac Interface A relay can be used to control a DC or an AC load, as it operates as a me- chanical switch. However, it has significant disadvantages, as outlined above. A solid-state switch, such as a transistor, is inherently more reliable, since it has no moving parts; but the transistor can only handle current in one direc- tion, so is unsuitable for AC loads. The thyristor is an alternative type of solid- state switch; it has a latching mode of operation such that when switched on, it stays on, until the current falls to zero. It can therefore be pulse operated, and used to rectify AC current. By switching on at different points in the AC cycle, the average current can be controlled, allowing the power to the load to be var- ied. However, it only passes current in one direction, providing DC power only. The triac is basically two thyristors connected back to back, with a common gate (trigger) input, allowing current flow in both directions. The full AC wave can then be utilised, with switching at the same point in the positive and neg- ative half cycles of the current. A microcontroller can be used to carry out this function; the AC signal is monitored through its cycle, and the thyristor switched on at the required point in the cycle using a timer. In Figure 8.4, a simple MCU triac interface is shown. An opto-coupler is used to isolate the control system from the high voltage load circuit. This con- tains an LED and phototransistor, which conducts when the light from the LED falls on its base. There is therefore no electrical connection between the two devices, and it will isolate output circuits operating at high voltage. When the MCU output is high, the opto-switch is on, and the voltage at terminal 1 of the triac is applied to the gate, turning the triac on when the voltage passes through zero. When the switch is off, the triac does not come on. Power Outputs 187 Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 187 This example is manually controlled, but the output power could be controlled by monitoring the AC voltage via a feedback voltage divider and sampling it at an analogue input. An MCU timer would then be employed to control the delay between the zero crossing point in the cycle and the trigger point, where the triac is switched on each half cycle. A block diagram for this system is shown in Figure 8.5 (c). Interfacing PIC Microcontrollers 188 (a) (b) (c) Trigger Time Current Power delivered to load MCU TRIAC CONTROL Sample instantaneous voltage Trigger Load AC Power supply ∼ Trigger Time Current Power delivered to load Figure 8.5 Thyristor and triac control: (a) thyristor; (b) triac; (c) MCU control Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 188 Oscillator Interface If an output is required at a set frequency, it can be generated in a variety of ways. A software loop can set the output, delay, clear the output, delay and re- peat. However, this will prevent the processor from carrying out other useful tasks in the meantime. Using a hardware timer and interrupts is one option; but if these MCU resources are required for other tasks, the oscillator function can be delegated to external hardware, so that the MCU simply switches an output to enable the oscillator. A simple low-frequency oscillator can be implemented using a 555 Timer chip; the same chip can also be used for generating timed pulses and delays. In Figure 8.4, it drives a loudspeaker via a bipolar transistor. Input R on the chip enables the oscillator, and C2 controls the frequency. This is an illustration of a very important design principle. A given interface can be implemented principally in hardware or software. The software implementa- tion will use more MCU resources in terms of both the available peripheral inter- faces, processor time, and programming effort. The hardware approach saves on these resources, but involves additional cost, both in hardware design effort and components for each system produced. Software, on the other hand, once written, has a negligible reproduction cost. Motor Interfacing As discussed above, the basic function of a motor is to convert electrical input cur- rent into output mechanical power (torque). All use electromagnetic coils to pro- vide this conversion, and need current switches or amplifiers to operate them from an MCU. A simple method of controlling AC motors is to use a relay as switch. Another is to use a triac to control the current, as outlined above, but in prac- tice there are some tricky issues associated with controlling inductive loads with thyristors and triacs which require reference to specialist texts. Three- phase motors require, in simple terms, each phase to be controlled by a sepa- rate device, but simultaneously, that is, three relays or triacs operated by the same controller. Three typical small motor interfaces are shown in Figure 8.6, a DC motor, a DC servo and a stepper motor. The motors can be operated in turn by pressing the select button. Operating parameters (speed, position, direction) can then be changed via the additional push buttons. The control program outline is given in Figure 8.7, and the source code in Program 8.1. The operation of each in- terface will be explained in turn. Power Outputs 189 Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 189 PWM Speed Control The DC motor is controlled from the PWM output of the PIC MCU (see Chapter 6), via a power FET VN66. This has an operating current of about 1 A maximum, giving a maximum motor input rating of 12 W at the operating voltage of 12 V. The motor characteristics can be set in the simulation, so a minimum motor resistance of about 10  would be suitable, as the FET itself has a forward resistance of about 1 . The VN66 is a convenient device to use as it operates at TTL level gate volt- ages; that is, 0 V switches it off, ϩ5 V switches it on (threshold about 1 V). It has a very high input impedance, so reliability is improved by adding shunt resistance to the gate, to improve the noise immunity. The diode across the motor is required to cut off the back EMF from the inductive load. When the system is started and the DC motor selected, a default PWM output is generated with 50% mark/space ratio. The MSR (mark space ratio) Interfacing PIC Microcontrollers 190 Figure 8.6 Motor interfaces schematic Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 190 Power Outputs 191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Project: Interfacing PICs ; Source File Name: MOTORS.ASM ; Devised by: MPB ; Date: 19-8-05 ; Status: Working ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Demonstrates DC, SERVO & STEPPER MOTOR control ; Select motor and direction using push button inputs ; DC Motor PWM speed control - working ; DC Servo position control - rollover not fixed ; Stepper direction control - working ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; PROCESSOR 16F877 ; Clock = XT 4MHz, standard fuse settings __CONFIG 0x3731 ; LABEL EQUATES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; INCLUDE "P16F877A.INC" ; standard register labels ; ; User register labels ; Count1 EQU 20 ; delay counter Count2 EQU 21 ; delay counter Target EQU 22 ; servo target position ; ; PROGRAM BEGINS ; ORG 0 ; Default start address NOP ; required for ICD mode ; ; Port & PWM setup init NOP BANKSEL TRISB ; Select control registers CLRF TRISC ; Output for dc motors CLRF TRISD ; Output for stepper MOVLW B'00000010' ; Analogue input setup code ; PortA = analogue inputs ; Vref = Vdd MOVWF ADCON1 ; Port E = digital inputs MOVLW D'249' ; PWM = 4kHz MOVWF PR2 ; TMR2 preload value BANKSEL PORTB ; Select output registers CLRF PORTC ; Outputs off CLRF PORTD ; Outputs off MOVLW B'01000001' ; Analogue input setup code MOVWF ADCON0 ; f/8, RA0, done, enable MOVLW D'128' ; intial servo position MOVWF Target ; ; MAIN LOOP ; but0 BTFSC PORTE,0 ; wait for select button GOTO but0 MOVLW B'00001100' ; Select PWM mode MOVWF CCP1CON ; MOVLW D'128' ; PWM = 50% MOVWF CCPR1L ; but1 BTFSS PORTE,0 ; wait for button release GOTO but1 CALL motor ; check for speed change BTFSC PORTE,0 ; wait for select button GOTO but1 MOVLW B'00000000' ; deselect PWM mode MOVWF CCP1CON ; CLRF PORTC ; switch off outputs but2 BTFSS PORTE,0 ; wait for button release GOTO but2 CALL servo ; move servo cw or ccw BTFSC PORTE,0 ; wait for select button GOTO but2 CLRF PORTC ; switch off servo but3 BTFSS PORTE,0 ; wait for button release GOTO but3 CALL step ; output one step cycle BTFSC PORTE,0 ; wait for select button GOTO but3 CLRF PORTD ; disable stepper outputs GOTO but0 ; start again Program 8.1 Motor control program Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 191 Interfacing PIC Microcontrollers 192 ; ; SUBROUTINES ; ; Change dc motor speed by one step and wait 1ms ; to debounce and control rate of change motor BSF PORTC,1 ; switch on motor LED BTFSS PORTE,1 ; inc speed? INCF CCPR1L ; yes MOVLW D'248' ; max speed? SUBWF CCPR1L,W BTFSS STATUS,Z GOTO lower ; no DECF CCPR1L ; yes - dec speed lower BTFSS PORTE,2 ; dec speed? DECFSZ CCPR1L ; yes - min speed? GOTO done ; no INCF CCPR1L ; yes - inc speed done CALL onems ; 1ms debounce RETURN ; Move servo 10 bits cw or ccw servo BSF PORTC,4 ; switch on servo LED BSF PORTC,7 ; enable drive chip BTFSC PORTE,1 ; move forward? GOTO rev ; no wait1 BTFSS PORTE,1 ; yes- wait for button GOTO wait1 ; release MOVLW D'10' ; add 10 ADDWF Target ; to servo target position BSF PORTC,5 ; move BCF PORTC,6 ; forward getfor CALL getADC ; get position BSF STATUS,C ; set carry flag MOVF Target,W ; load position SUBWF ADRESH ; compare with target BTFSS STATUS,C ; far enough? GOTO getfor ; no - repeat BCF PORTC,5 ; yes - stop MOVLW D'250' ; wait 250ms CALL xms ; before next step rev BTFSC PORTE,2 ; move reverse? RETURN ; no wait2 BTFSS PORTE,2 ; yes- wait for button GOTO wait2 ; release MOVLW D'10' ; yes - sub 10 from SUBWF Target ; servo target position BCF PORTC,5 ; move BSF PORTC,6 ; reverse getrev CALL getADC ; get position BSF STATUS,C ; set carry flag MOVF Target,W ; load position SUBWF ADRESH ; compare with target BTFSC STATUS,C ; far enough? GOTO getrev ; no - repeat BCF PORTC,6 ; yes - stop MOVLW D'250' ; wait 250ms CALL xms ; before next step RETURN ; Output one cycle of stepper clock step BSF PORTD,0 ; switch on stepper LED BSF PORTD,1 ; enable stepper drive BTFSS PORTE,1 ; test cw button BSF PORTD,2 ; select clockwise BTFSS PORTE,2 ; test ccw button BCF PORTD,2 ; select counter-clockwise BSF PORTD,3 ; clock high MOVLW D'25' ; load delay time CALL xms BCF PORTD,3 ; clock low MOVLW D'25' ; load delay time CALL xms RETURN ; Stepper software delay xms MOVWF Count2 ; receive x ms in W down2 CALL onems DECFSZ Count2 GOTO down2 RETURN onems MOVLW D'249' ; delay one millisec MOVWF Count1 down1 NOP DECFSZ Count1 GOTO down1 RETURN ; Read ADC input and store getADC BSF ADCON0,GO ; start ADC wait BTFSC ADCON0,GO ; and wait for finish GOTO wait MOVF ADRESH,W ; store result, high 8 bits RETURN ; END ; of source code Program 8.1 Continued Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 192 can then be increased and decreased using the up/down buttons. Note that the software has to check each time the MSR is modified for the maximum (FF) or minimum (00) value, to prevent rollover and rollunder of the PWM value. DC Motor Position Control DC motors cannot be positioned accurately without some kind of feedback; in applications such as printers and robot arms, the DC motors have feedback de- vices, which allow the controller to monitor the motor shaft position, speed or acceleration. In digital control systems, this is usually achieved by using a slotted wheel and opto-sensor attached to the motor shaft. This may often be followed by a gearbox in the drive chain, for example, in robot arm where the output range of movement is less that 360°. The controller counts the pulses from the wheel to determine how far the output has moved; also, the pulse frequency can be converted to speed. In a printer, the linear position of the print head is moni- tored by a graduated strip attached to the traverse mechanism. The accuracy of the system can be further improved by interpolation; this means the reference strip has a sinusoidal pattern so that each cycle can be subdivided by a contin- uous variation in the sensor signal. The system block diagram shown in Figure 8.7 represents a general purpose position or speed controller. The motor has a slotted or perforated wheel attached. Say there are 100 slots, then there will be 200 edges, giving a reso- lution of 360/200 ϭ 1.8°. The motor is driven via a current amplifier with a PWM signal; the speed can then be controlled, and ramped up and down to prevent the motor from overshooting the target position. The MCU may act as a slave device, receiving a position or speed command from a master con- troller, carrying it out, and then signalling completion of the operation. An alternative speed control system could use a tachogenerator to measure the speed. This is a small DC generator that outputs a voltage or current in proportion to the speed of the shaft, operating in the inverse mode to a DC motor. The analogue tacho signal can then be used to control the speed. Analogue position control is even simpler, in principle. A pot is attached to the motor shaft, and pro- vides a voltage, which represents the position. An all analogue position controller can be implemented with op-amps, which will position the output according to an analogue input signal from a pot, DAC or amplifier. The main problem is that the pot only has a range of about 300°, and may not allow continuous rotation. A servo motor is one that incorporates a position feedback element. In Figure 8.6, the DC servo has a built-in pot, which provides a voltage representing the position, between ϩ5 and 0 V. The motor is driven from an L6202 full bridge driver. This is an IC, which provides drive to the motor in either direction under digital control. A block diagram of the chip is shown in Figure 8.8. Power Outputs 193 Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 193 Interfacing PIC Microcontrollers 194 MOTORS Test DC motor PWM speed, DC position step servo and stepper motor direction with push button inputs, using P16F877 (4MHz) Main Initialise Port A = Analogue inputs, servo pot = RA0 Port C = Outputs, DC motors Port D = Outputs, stepper motor Port E = Digital inputs, push buttons: Select, Up, Down PWM rate = 4kHz Servo target value = 128 Wait for ‘Select’ button REPEAT Select PWM mode, 50% MSR REPEAT CALL Motor UNTIL ‘Select’ button pressed again REPEAT CALL Servo UNTIL ‘Select’ button pressed again REPEAT CALL Step UNTIL ‘Select’ button pressed again ALWAYS Subroutines Motor IF ‘Up’ button pressed Increment speed unless maximum IF ‘Down’ button pressed Decrement speed unless minimum RETURN Servo IF ‘Up’ button pressed Add 10 to target position Move forward, until target position reached IF ‘Down’ button pressed Subtract 10 from target position Move reverse, until target position reached RETURN Step IF ‘Up’ button pressed Select forward mode IF ‘Down’ button pressed Select reverse mode Output one drive pulse RETURN Figure 8.7 Motor test program outline Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 194 The bridge circuit contains four power FETs connected such that when two are switched on together, current flows through the load. When the other pair is on, the current in the load is reversed. In a motor, the direction of rotation is reversed. The FETs are represented as simple switches. They are controlled from a simple logic circuit (see the L6202 data sheet), as summarised in the function table. Forward and reverse are selected by setting the IN1 and IN2 in- puts to opposite logic states. The chip operates from the motor supply voltage (ϩ12 V) and the digital logic supply is derived from it, so no separate ϩ5 V supply is needed. A current sensing resistor can be inserted in the 0 V connection, so that the current flow in either direction can be monitored for control purposes. Bootstrap capacitors must be fitted as shown in Figure 8.6 to ensure reliable switching of the bridge FETs. Although the FETs are protected internally with diodes, a series CR snubber network is connected across the output terminals to further protect the driver chip from current switching transients. The test program allows the user to move the servo in steps. The required position is represented by an 8-bit number, which is initially set to the mid- value of 128. If the ‘up’ button is pressed, the value is increased by 10, and the servo started in the forward direction. The actual position is monitored from the servo pot voltage read in via AD0. When the input value matches the tar- get value, the drive is stopped. The servo is moved in the reverse direction in the same way. Stepper Motor Control The third subcircuit in Figure 8.6 is the stepper motor interface. This also uses dedicated hardware, because a current driver chip would be needed in any case, and the stepper controller incorporates sequencing logic, which reduces the software burden. The stepper motor has a set of windings distributed around the stator, and a passive rotor, with fixed or induced magnetic poles. Incremental movement of the rotor is achieved by activating the windings in a suitable sequence. Power Outputs 195 PWM MCU Motor Drive Interface Position / Speed Request Motor & Slotted Wheel Pulses from opto-sensor Action achieved Figure 8.8 Digital position control system Else_IPM-BATES_ch008.qxd 6/27/2006 2:25 PM Page 195 . sensible precau- tion for all DC inductive loads (anything with a coil such as a motor or Interfacing PIC Microcontrollers 186 Figure 8.4 Power outputs interfaces Else_IPM-BATES_ch008.qxd 6/27/2006. switched on each half cycle. A block diagram for this system is shown in Figure 8.5 (c). Interfacing PIC Microcontrollers 188 (a) (b) (c) Trigger Time Current Power delivered to load MCU TRIAC. a default PWM output is generated with 50% mark/space ratio. The MSR (mark space ratio) Interfacing PIC Microcontrollers 190 Figure 8.6 Motor interfaces schematic Else_IPM-BATES_ch008.qxd 6/27/2006

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

TỪ KHÓA LIÊN QUAN