The hundreds digit is calculated in a similar way, but the tens calculation is a little easier as the maximum remainder from the previous stage is 99, so the high byte borrow handling is
Trang 1The data acquisition process is similar to the 8-bit system above, but the binary to BCD conversion process is rather more complicated The result is required in the range 0–4095, so the original result (0–1023) is shifted left twice to multiply it by four One thousand (03E8) is then loop subtracted from the result to calculate the number of thousands in the number Correct borrow handling between the high and low byte is particularly important The process stops when the remainder is less that 1000 The hundreds digit is calculated in
a similar way, but the tens calculation is a little easier as the maximum remainder from the previous stage is 99, so the high byte borrow handling is not necessary This process is outlined in Figure 7.5, and the source code shown in Program 7.2.
ADC8
Convert the analogue input to 8-bits and display Hardware: P16F877 (4MHz), Vref+ = 2.56, 16x2 LCD
Initialise
PortA = Analogue inputs (default) PortC = LCD outputs
ADC = Select f/8, RA0 input, left justify result, enable LCD = default setup (include LCD driver routines)
Main
REPEAT
Get ADC 8-bit input Convert to BCD Display on LCD
ALWAYS
Subroutines
Get ADC 8-bit input
Start ADC and wait for done Store result
Convert to BCD
Calculate hundreds digit Calculate tens digit Remainder = ones digit
Display on LCD
Home cursor Convert BCD to ASCII Send hundreds, point, tens, ones Send ‘Volts’
Include
LCD routines
Figure 7.3 ADC test program outline
Trang 2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Source File Name: VINTEST.ASM
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Demonstrates simple analogue input
; using an external reference voltage of 2.56V
; The 8-bit result is converted to BCD for display
; as a voltage using the standard LCD routines
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PR OCESSOR 16F877
; Clock = XT 4MHz, standard fuse settings CONFIG 0x3731
; LABEL EQUATES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#INCLUDE "P16F877A.INC" ; standard labels
; GPR 70 - 75 allocated to included LCD display routine count EQU 30 ; Counter for ADC setup delay ADbin EQU 31 ; Binary input value huns EQU 32 ; Hundreds digit in decimal value tens EQU 33 ; Tens digit in decimal value ones EQU 34 ; Ones digit in decimal value
; PROGRAM BEGINS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OR G 0 ; Default start address
; Port & display setup -
BA NKSEL TRISC ; Select bank 1
CL RF TRISD ; Display port is output
MOVLW B'00000011' ; Analogue input setup code
MO VWF ADCON1 ; Left justify result,
BA NKSEL PORTC ; Select bank 0
CL RF PORTD ; Clear display outputs
MOVLW B'01000001' ; Analogue input setup code MOVWF ADCON0 ; f/8, RA0, done, enable CALL inid ; Initialise the display
; MAIN LOOP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
start CALL getADC ; read input
CA LL condec ; convert to decimal
CA LL putLCD ; display input
GO TO start ; jump to main loop
; SUBROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Read ADC input and store - getADC BSF ADCON0,GO ; start ADC
wait BTFSC ADCON0,GO ; and wait for finish
MOVF ADRESH,W ; store result high byte
; Convert input to decimal - condec MOVWF ADbin ; get ADC result
CL RF huns ; zero hundreds digit
CL RF tens ; zero tens digit
CL RF ones ; zero ones digit
Program 7.1 8-bit analogue input
Trang 3; Calclulate hundreds
; Calculate tens digit
; Output to display
BSF Select,RS ; and restore data mode
; Convert digits to ASCII and display
; INCLUDED ROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Include LCD driver routines
; #I NCLUDE "LCDIS.INC"
; Contains routines:
Program 7.1 Continued
Trang 4Amplifier Interfaces
Having developed the analogue input conversion and display process, we can move on to the interfacing hardware itself Input signals often need condition-ing before becondition-ing fed to the MCU analogue inputs This can involve amplifiers
to increase the signal amplitude, attenuators to reduce it, and filters to change the frequency response For now, we will limit ourselves to DC signals and amplifiers, as these are most frequently used in MCU applications and are more straightforward For processing of AC signals, standard references should be consulted
Figure 7.6 shows a range of different amplifiers connected to the PIC MCU They are connected to RA0 by a multi-way switch, so that the output of each may be displayed, using the previously developed 8-bit conversion and display program (Program 7.1) The basic op-amp configurations are summarised in Figure 7.7.
The op-amp (IC amplifier) is a high-gain amplifier with inverting and non-inverting inputs, with the output voltage controlled by the input differential voltage However, since the differential gain is very high, typically >1,00,000, the operating input differential voltage is very small As a result, we can as-sume that the gain and bandwidth (frequency response) are controlled by the
Figure 7.4 10-bit conversion circuit
Trang 5When used as a linear amplifier, the feedback must be negative Essentially, this means the feedback signal path must be connected to the minus input terminal The basic rules for ideal op-amp circuit analysis are as follows:
• Differential gain ⫽ - (for voltage applied between + and – terminals)
• Differential voltage ⫽ 0 (terminals + and – are at the same voltage)
• Input resistance = - (zero input current at + and – terminals)
• Output impedance = 0 (infinite current can be sunk or source at the output)
• Bandwidth = - (all frequencies are amplified equally)
• Feedback is negative (signal connected from output to – terminal) These rules allow amplifier circuit analysis to be greatly simplified, and give results which are accurate enough for most applications.
IC (integrated circuit) amplifiers can operate with dual or single supplies Dual supplies, which are the norm, make the circuit design easier, because the output can swing positive and negative around 0 V ⫹/⫺ 15 V and ⫹/⫺ 5 V are typical supply values, with 15 V supplies giving a higher output voltage swing
ADC10
Load 10-bit, right justified binary (0-1023) Multiply by 4 (0-4092) by shift left Clear BCD registers
REPEAT
Subtract E816from low byte Subtract 316 from high byte Increment thousands digit UNTIL remainder < 03E816 (1000) REPEAT
Subtract 6416 from low byte Borrow from high byte Increment hundreds digit UNTIL remainder < 6416 (100) REPEAT
Subtract 10 from low byte Increment tens digit UNTIL remainder < 10 Remainder = ones digits RETURN
Figure 7.5 10-bit binary conversion routine outline
Trang 6Program 7.2 10-bit conversion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Project: Interfacing PICs
; Source File Name: TENBIT.ASM
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Demonstrates 10-bit voltage measurement
; using an external reference voltage of 4.096V,
; giving 4mV per bit, and an resolution of 0.1%
; The result is converted to BCD for display
; as a voltage using the standard LCD routines
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PROCESSOR 16F877
; Clock = XT 4MHz, standard fuse settings CONFIG 0x3731
; LABEL EQUATES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
INCLUDE "P16F877A.INC"
; standard register labels
; -; User register labels
; -; GPR 20 - 2F allocated to included LCD display routine count EQU 30 ; Counter for ADC setup delay ADhi EQU 31 ; Binary input high byte ADlo EQU 32 ; Binary input low byte thos EQU 33 ; Thousands digit in decimal huns EQU 34 ; Hundreds digit in decimal value tens EQU 35 ; Tens digit in decimal value ones EQU 36 ; Ones digit in decimal value
; -; PROGRAM BEGINS
; -ORG 0 ; Default start address NOP ; required for ICD mode
; -; Port & -; display setup BANKSEL TRISC ; Select bank 1 CLRF TRISD ; Display port is output MOVLW B'10000011' ; Analogue input setup code MOVWF ADCON1 ; Right justify result,
; Port A = analogue inputs
; with external reference BANKSEL PORTC ; Select bank 0 CLRF PORTD ; Clear display outputs MOVLW B'01000001' ; Analogue input setup code MOVWF ADCON0 ; f/8, RA0, done, enable CALL inid ; Initialise the display
; -; MAIN LOOP
; -start CALL getADC ; read input
CALL con4 ; convert to decimal CALL putLCD ; display input GOTO start ; jump to main loop
Trang 7; -
; SUBROUTINES
; -
; Read ADC input and store
; -
RETURN
; -
; Convert 10-bit input to decimal
; -
; Multiply by 4 for result 0 - 4096 by shifting left
; Clear BCD registers
; Calclulate thousands low byte
BTFSC STATUS,C ; borrow from high bits?
; Calculate thousands high byte
; Restore remainder when done
; Increment thousands digit and repeat
Program 7.2 Continued
Trang 8; Calclulate hundreds
GOTO inch ; no, inc hundreds & repeat
; Calculate tens digit
BTFSS STATUS,C ; and check if done
; Restore remainder
; -
; Output to display
; - putLCD BCF Select,RS ; set display command mode
BSF Select,RS ; and restore data mode
; Convert digits to ASCII and display
; -; INCLUDED ROUTINES
; -; Include LCD driver routine
; INCLUDE "LCDIS.INC"
;
; Contains routines:
; init: Initialises display
; send: sends a character to display
;
Program 7.2 Continued
Trang 9Figure 7.6 Basic amplifier interface circuits
Trang 10V o = V i
I o >>> I i
+ _
(b)
_
+ 0V
(c)
_
+ V+
(e)
I f = V o – 0 = 0 – V 1 + 0 – V 2
R f R 1 R 2
∴ V o = - ( (R f /R 1 ).V 1 + (R f / R 2 ).V 2 )
(f)
I f = V o – V x = V x - V 1
V x = R f / (R 1 + R f ) V 2
∴ V o = (R f /R 1 ) (V 2 – V 1 )
_
+
R f
V o
I f
0V
I 1
I 2
_
+
R 1
R f
V o
I f
0V
V 1
I f
R f
R 1
V 2
V x
_
V i
V i
V i
V i
I i
0V
R i
R i
R i
R f
R f
R f
V o
V o
V o
V o
I o
I f
I f
I f
I f
I f
I f = V o – V i =
R f R i
∴ V o = (R f /R i + 1).V i
V i – 0
I f = V o – 0 =
R f R i
∴ V o = - (R f /R i + 1).V i
∴ V o = - (R f / R 1 ).V i + ((R f /R 1 ) + 1).V r
0 – V i
I f = V o – V r =
R f R i
V r – V i
Figure 7.7 Basic amplifier configurations: (a) non-inverting amplifier; (b) inverting amplifier;
(c) inverting amplifier with offset; (d) unity gain buffer; (e) summing amplifier; (f) difference