
0  5V PIC voltmeter circuit consists only of a PIC16F876 (or other 16F87x), a 10MHz oscillator and a dotmatrix displa y. The analog voltage is fed to the PIC RA0 ind. To show the 10 Bittig measured ADC value is 4 digits on the LCD shown in decimal (in millivolts).
An analog input voltage of 0V ... 5V is measured and displayed on the LCD. The voltage measurement and the binary indicator was discussed.
The control of an LCD are not new.
If, however, the voltage measured in volts or millivolts on the LCD display, a little 'data processing' is necessary.
Circuit
The circuit consists only of a PIC16F876 (or other 16F87x), a 10MHz oscillator and a dotmatrix displa y. The analog voltage is fed to the PIC RA0 ind.
To show the 10 Bittig measured ADC value is 4 digits on the LCD shown in decimal (in millivolts).
For example, the building is suitable 16F876board test . and the LCD adapter board .
When burning the PIC is the HS oscillator select
+ + WARNING + +
to the PIC not to damage, should the input voltage at RA0 pin is not less than Vpp0, 3V or greater than Vdd +0.3 V! Who can not guarantee should input with a resistor and two protection diodes (Schottky diodes to Vdd and Vss) protect, such as for example in simple ADC sample is to be seen.
Problem No. 1
Is Vdd (+5 V) is used as a reference voltage, the ADC measured with a resolution of 5V / 1024 = 4.8828125 mV.
The binary 10bit A / D conversion result (U adc ) can be converted to a millivolt value in, if it is multiplied by 4.8828125.
U [mV] = 4.8828125 * Uadc
This corresponds to a multiplication by 5 and a subsequent division by 1.024 .
How can we solve this problem without having to operate a real floatingpoint math?
In a PIC can easily perform the following calculations:
Addition ( ADDWF )
Subtraction ( SUBWF )
Multiplying by 2 ( RLF ), minimum
Division by 2 ( RRF ), halving
The commands are in parentheses next to the operations. Which is really only 8bit operations can be relatively easily expanded simply to 16 bits. These 16 bits are sufficient to handle our ADC results (10 bit).
The above required multiplication by 5 to 2 can be easily implemented ways.
The number is added to itself 4 times
The number is doubled 2 times, this is again the number added to itself
The division by 1.024 is somewhat difficult. The same goes,
X / X = 1.024  X/64  X/128
This results, therefore, the voltage in millivolts following calculation method:
U [mV] = 5Uadc  5Uadc/64  5Uadc/128
This calculation is easy to Bitverschiebebefehle, a 16bit addition and subtraction, two 16bit back to them. To right multiplication and division can give themselves.
Problem No. 2
We now have the voltage at pin RA0 in millivolts determined. Our number is still a binary number. The LCD display we wish to display a decimal number. These binarytodecimal conversion is necessary.
To convert a binary number to a decimal number, there are two possibilities
Repeated Division by Ten
Repeated subtraction by orders of magnitude.
Also a division by 10 can be killed with the subtraction. Man is trying just as often can be subtracted from a number, without obtaining a negative result.
Way to solve: 16bit addition / subtraction
What we need in all circumstances the 16bit addition, and the 16bit subtraction. Both need one small subroutine. Since all the registers of the PIC is only 8 bits are great, I put each down two registers to store each composed a 16bit number:
f1 f0 together and save 16bit, with the high part (Bit8. .15) in f1 and the low part (Bit0. .7) is stored in f0.
XW1 xw0 together and save 16bit, with the high part (Bit8. .15) in XW1 and the low part (Bit0. .7) is stored in xw0.
The choice of name and xw f is completely arbitrary. must of course f0, f1, xw0 XW1 and have a promise with equ statements real registers.
16 bit addition
Following code shows a 16bit addition of the values f (f1, f0) and xw (XW1, xw0). The 16bit result is stored in f, xw is not changed. If the Ergebins 17 bits in size, so as a sign of the overflow, the C bit in the STATUS register ..
;************************************************* ********************
, 16 bit Adition, C flag is set on overflow
Add16, 16bit add: f: = f + xw
movf xw0, W; xw0 to W
addwf f0, F; f0 = f0 + xw0
movf XW1, W; XW1 to W
btfsc STATUS, C; case, an overflow occurred:
incfsz XW1, W; XW1 +1 to W
addwf f1, F, f1 = f1 + XW1
return; ready
16 bit subtraction
For the subtraction we need an additional auxiliary register 'error' in which we need to remember us, whether during the calculation, a borrowing from the highest point was no longer possible, an overflow occurred during computation. Also has the CFlag finally be inverted, as the SUBWF command exactly when overflow is not a C.
;************************************************* ****
, 16bit subtraction, if overflow (negative result) is C.
Sub16, 16 bit f: = fxw
clrf error, delete extra flags
movf xw0, w, f0 = f0xw0
subwf f 0, f
btfsc STATUS, C
goto Sub16a
movlw 0x01; borrow from F1
subwf f1, f
btfss STATUS, C
bsf error C Underflow
Sub16a
movf XW1, w, f1 = f1XW1
subwf f1, f
btfss STATUS, C
bsf error C Underflow
bcf STATUS, C, CFlag invert
btfsc error, C
bsf STATUS, C
return
Division by 2
Decimal (base 10 number) one can divide by 10, by simply shifts all the digits of the decimal one place to the right. (1250 / 10 = 125)
Similarly, one can divide binary numbers (base 2 number) by 2, by moving all points of the binary number one place for computing. (101010b / 10b = 10101b)
This is done for 8bit numbers of the displacement command RRF. Which can be easily applied to larger numbers, which are spread across multiple registers (eg XW1 and xw0).
Because we need division by 64 and 128, we must apply the same division by 2 more times in a row. (64 = 2 ^ 6, 128 = 2 ^ 7) performs the following routine:
;************************************************* ****
; Division by 2 is wtimes executed
And the number of divider might xw
Div2
movwf counter; save number of divisions
Div2a, 16 bit xw = xw / 2
bcf STATUS, C; delete carry
XW1 RRF f,
xw0 RRF f,
decfsz counter, ready?
goto Div2a; no, again
return
Solution for Problem No. 1: Conversion in millivolts
We now have all the features to unmzurechnen the ADC value to a millivolt value, according to the formula:
U [mV] = 5Uadc  5Uadc/64  5Uadc/128
;************************************************* ****
; Conversion of the ADC value in millivolts (binary)
; The ADC value is available in f1, f0
mV
, First multiplying by 5
movfw f0
movwf xw0
movfw f1
movwf XW1
call Add16 f: = 2xADC
call Add16 f: = 3xADC
call Add16 f: = 4xADC
call Add16 f: = 5xADC
; ADC * 5 copy to xw
movfw f0
movwf xw0
movfw f1
movwf XW1; xw: = 5xADC
; Xw divide by 64 (6 by x 2)
, Then xw = 5xADC/64
movlw 6
call Div2
Sub16 call f: = 5xADC  5xADC/64
; To reduce xw 5xADC/128
movlw 1
call Div2
Sub16 call f: = 5xADC  5xADC/64  5xADC/128
return; ready
This has however not hurt. Without floating point operations without proper multiplication / division to get the occasional goal.
Solution for Problem No. 2: Conversion of a 16 bit binary number to decimal
The measurement result of the ADC can not after the change in millivolts greater than about 5000 because the maximum input voltage is 5V = 5000 mV.
If one assumes that the value is certainly less than 10,000, then you can proceed as follows.
The thousands digit of the decimal results was determined one by going so often, it subtracts from the millivolt value 1000.
Of the remainder is withdrawn as often as possible from 100th The number shows the hundreds.
Then subtract from the remaining 10, as often as possible. The number is the tens digit.
The remainder is the units digit.
The following code contains the complete conversion. The result is in 4 registers (ST, SH, SZ, SE) dezimalstellenweise stored.
;************************************************* ****
; Conversion of a binary number (<10000) in a decimal
; The binary number is in f1, f0
And the decimal places in ST (thousands), SH (hundreds)
, SZ (ten) and SE (a) is stored in BCD code
B2D
; Test for thousands of 1000d = 0x03E8
movlw 0x03
movwf XW1
movlw 0xE8
movwf xw0
call B2Da
movwf ST
; Test hundreds 100d = 0x0064
clrf XW1
movlw 0x64
movwf xw0
call B2Da
movwf SH
; Test on tens 10d = 0x000A
clrf XW1
movlw 0x0A
movwf xw0
call B2Da
movwf SZ
movfw f0
movwf SE
return
B2Da
clrf counter
B2Db counter incf, f; deducted and how often?
Sub16 call f: = fxw
btfss STATUS, C; deducted too often?
goto B2Db; no, again
call Add16 f: = f + xw
Speed Camera Warning Device counter, w, because too much is always 1 counted
return
Can actually be 16bit numbers greater than 9999. Therefore, an extension would be appropriate to tens of thousands. For our example, the ADC is not necessary.
Program flow
Initialization of ports
Initialization of the ADC
Initialization of the LCD display
Beginning of the loop
Measuring the voltage on RA0 to the ADC.
Convert the ADC value in mV
Convert the binary to decimal value millivolts
Edition of the decimal value on the LCD
Jump to the top of the loop
Program Listing
Assembler Listing + HEX file
