> The program was written to output code for 10 bit ADC's. As max value of a
> 8 bit ADC is 1/4 that of a 10 bit ADC, you will have to enter a full scale
> reading, or 'Ref', that is 4 times the full scale reading of the 8 bit ADC.
> In your case this would be 80 for a full scale reading of 20 using 8 bits.
> As you do not use ADHIGH I have marked the main parts that use ADHIGH and
> they can be deleted, but don't have to be deleted as long as ADHIGH is zero.
>
> I have went to the generator site and entered 80 as reference and attached
> output with my ADHIGH comments. You will notice that at bottom of code
> there is a remark "max value a/d is 79.922". This is the sum of the 10
> bits for a Ref of 80,
>
> Here is web site to get new copy of code, That is where I obtained the
> attached code. Just enter 80 as 'Ref'
>
http://www.piclist.com/techref/A2DCodeGenerator.asp
>
> While you only want one decimal place results, it is necessary to calculate
> to at least two decimal places in order to insure better results. The
> program calculates to three decimal places.
>
> This program is "bit" orientated. All it does is add up the value for each
> bit set in the ADC reading. Looking at the code for the 8 bits (my code,
> not yours) you see the comments
>
> ; for bit 2 10
> ; for bit 3 5
> ; for bit 4 2.5
> ; for bit 5 1.25
> ; for bit 6 0.625
> ; for bit 7 0.3125
> ; for bit 8 0.15625
> ; for bit 9 0.078125
>
> This shows the value for a 10 bit ADC with Ref of 80. So the bit numbers
> are off by 2 for a 8 bit ADC. Bit 2 is bit 0 of ADLOW through bit 9 being
> bit 7 of 8 bit ADC. These remarks also show the decimal values each bit
> represents.
>
> Renumbered bits for clairity on 8 bit ADC
>
> ; for bit 0 10
> ; for bit 1 5
> ; for bit 2 2.5
> ; for bit 3 1.25
> ; for bit 4 0.625
> ; for bit 5 0.3125
> ; for bit 6 0.15625
> ; for bit 7 0.078125
>
> And the "collector" bytes
> CVD00 collectes hundreds
> CVD01 collectes tens
> CVD02 collectes units
> CVD03 collectes tenths
> CVD04 collectes hundreths
> CVD05 collectes thousands
>
> So if bit 0 of ADC is set, then 1 is added to the tens byte. If bit 1 set,
> add 5 to units. Bit 2 set, add 2 to units and 5 to tentjs. So on until, if
> bit 7 set then add 7 to hundreths and 8 to thousands.
>
> Using this method it is likely that some collector bytes will have a value
> greater than 9. so there is a need to adjust each bite so that it's value
> is from 0 to 9. If a value of a byte is greater than 9 (10 or more) we
> subtract 10 from it and add 1 to the next higher byte. We keep doing that
> to a byte until it's value is between 0 and 9. Then we go to the next
> higher byte and do the same. It is important that we start with the least
> sunficiant byte (LSByte) and work our way up the food chain. So the
> program starts doing this with CVD05, then CVD04 till CVD01. CVD00 should
> never be 10 or greater as the 'Ref' shouldn't be that high if using this
> code unmodified.
>
> Using this method of accumulating values, care needs to be taken in hwo many
> counts can go into each collector byte maximum. As when the count exceds
> 255, it goes to 0 and the counts are lost. But in our case we are adding a
> max of 9 for ten bits and 10 bits possible from previous byte's adding for
> each 10 counts in it, the most ever would be a count of 100. So that is ok.
>
> The method for adjusting each collector byte is the "sorry" method. Like
> when you go to the doctor. He does not preform tests on you to see what may
> be sore, he just pokes you till you say "ouch". Then he says "sorry" and
> hands you a piece of candy. The program does not test to see what the value
> of a byte is, it just increments the next higher byte by 1 and subtracts 10
> from the current byte. When it has went too far, the carry will be set
> "ouch". So undo that increment and subtract by a decrement or next higher
> byte "sorry" and adding 10 back to the current bytw "here is a piece of
> candy".
>
> Changing a decimal value in a byte to an ascii character requires setting
> bits 2 and 3 (32 hex) of the byte. So the program just adds decimal 48 to
> each byte.
>
> The code generator program was written to simply converting ADC reading to
> decimal character. The same program could be written by hand but with the
> calculations and typing necessary, it would be subject to errors and time
> consuming. Also code can easily be generated for a different 'Ref' just by
> going to the web site. In fact it should be possible tohave two or more
> copies of same code in one program by doing a "find and replace" on the
> lables of the other program's "CVDEC" and removing the duplicate register
> assignments "cblocks"
>
> So, go to web site and get new copy using a 'Ref' of 80 and it should work.
>
>
http://www.piclist.com/techref/A2DCodeGenerator.asp
>
> If it don't then it may be time to forget this code and use what works.
>
> Bill
>
> ---- Original Message -----
> From: "Chetan Bhargava" <
RemoveMEcbhargavaTakeThisOuT
spamgmail.com>
> To: "Microcontroller discussion list - Public." <
EraseMEpiclistspam
spamBeGonemit.edu>
> Sent: Wednesday, January 18, 2006 9:07 AM
> Subject: Re: [PIC] ADC code generator
>
>
> > ;$Id: div2.asm,v 1.2 2006/01/17 01:24:47 chetan Exp $
> >
> > ; Prigram to convert a ten bit a/d reading
> > ; to ascii decimal. A/d reference is 20 volts.
> > ; CVD00 to CVD05 contains ascii decimal characters
> > ; wuth the decimal point being between CVD02 and CVD03
> > ; Put a/d reading in ADHIGH and ADLOW, call CVDEC
> > ; and get value from CVD00 through CVD05
> > . Bill Cornutt Jan 1, 2006
> >
> >
> > cblock
> > CVD00
> > CVD01
> > CVD02
> > CVD03
> > CVD04
> > CVD05
> > ADHIGH
> > ADLOW
> > endc
> >
> >
> > CVDEC
> >
> > ; clear receiving bytes
> >
> > CLRF CVD00
> > CLRF CVD01
> > CLRF CVD02
> > CLRF CVD03
> > CLRF CVD04
> > CLRF CVD05
> >
> >
> > ; for bit 0 10
> >
> > CVDEC00
> > BTFSS ADHIGH,06
> > GOTO CVDEC01
> >
> > MOVLW D'01'
> > ADDWF CVD01,F
> >
> >
> >
> > ; for bit 1 5
> >
> > CVDEC01
> > BTFSS ADHIGH,07
> > GOTO CVDEC02
> >
> > MOVLW D'05'
> > ADDWF CVD02,F
> >
> >
> >
> > ; for bit 2 2.5
> >
> > CVDEC02
> > BTFSS ADLOW,00
> > GOTO CVDEC03
> >
> > MOVLW D'02'
> > ADDWF CVD02,F
> >
> > MOVLW D'05'
> > ADDWF CVD03,F
> >
> >
> >
> > ; for bit 3 1.25
> >
> > CVDEC03
> > BTFSS ADLOW,01
> > GOTO CVDEC04
> >
> > MOVLW D'01'
> > ADDWF CVD02,F
> >
> > MOVLW D'02'
> > ADDWF CVD03,F
> >
> > MOVLW D'05'
> > ADDWF CVD04,F
> >
> >
> >
> > ; for bit 4 0.625
> >
> > CVDEC04
> > BTFSS ADLOW,02
> > GOTO CVDEC05
> >
> > MOVLW D'06'
> > ADDWF CVD03,F
> >
> > MOVLW D'02'
> > ADDWF CVD04,F
> >
> > MOVLW D'05'
> > ADDWF CVD05,F
> >
> >
> >
> > ; for bit 5 0.3125
> >
> > CVDEC05
> > BTFSS ADLOW,03
> > GOTO CVDEC06
> >
> > MOVLW D'03'
> > ADDWF CVD03,F
> >
> > MOVLW D'01'
> > ADDWF CVD04,F
> >
> > MOVLW D'03'
> > ADDWF CVD05,F
> >
> >
> >
> > ; for bit 6 0.15625
> >
> > CVDEC06
> > BTFSS ADLOW,04
> > GOTO CVDEC07
> >
> > MOVLW D'01'
> > ADDWF CVD03,F
> >
> > MOVLW D'05'
> > ADDWF CVD04,F
> >
> > MOVLW D'06'
> > ADDWF CVD05,F
> >
> >
> >
> > ; for bit 7 0.078125
> >
> > CVDEC07
> > BTFSS ADLOW,05
> > GOTO CVDEC08
> >
> > MOVLW D'07'
> > ADDWF CVD04,F
> >
> > MOVLW D'08'
> > ADDWF CVD05,F
> >
> >
> >
> > ; for bit 8 0.0390625
> >
> > CVDEC08
> > BTFSS ADLOW,06
> > GOTO CVDEC09
> >
> > MOVLW D'03'
> > ADDWF CVD04,F
> >
> > MOVLW D'09'
> > ADDWF CVD05,F
> >
> >
> >
> > ; for bit 9 0.01953125
> >
> > CVDEC09
> > BTFSS ADLOW,07
> > GOTO CVDEC10
> >
> > MOVLW D'02'
> > ADDWF CVD04,F
> >
> >
> > CVDEC10
> > MOVLW D'10'
> >
> > ; adjust digit 5
> >
> > INCF CVD04,F
> > SUBWF CVD05,F
> > BTFSC STATUS,C
> > GOTO $-3
> >
> > ; went past zero
> >
> > DECF CVD04,F
> > ADDWF CVD05,F
> >
> > ; adjust digit 4
> >
> > INCF CVD03,F
> > SUBWF CVD04,F
> > BTFSC STATUS,C
> > GOTO $-3
> >
> > ; went past zero
> >
> > DECF CVD03,F
> > ADDWF CVD04,F
> >
> > ; adjust digit 3
> >
> > INCF CVD02,F
> > SUBWF CVD03,F
> > BTFSC STATUS,C
> > GOTO $-3
> >
> > ; went past zero
> >
> > DECF CVD02,F
> > ADDWF CVD03,F
> >
> > ; adjust digit 2
> >
> > INCF CVD01,F
> > SUBWF CVD02,F
> > BTFSC STATUS,C
> > GOTO $-3
> >
> > ; went past zero
> >
> > DECF CVD01,F
> > ADDWF CVD02,F
> >
> > ; adjust digit 1
> >
> > INCF CVD00,F
> > SUBWF CVD01,F
> > BTFSC STATUS,C
> > GOTO $-3
> >
> > ; went past zero
> >
> > DECF CVD00,F
> > ADDWF CVD01,F
> >
> >
> > ; no need to adjust high order byte
> >
> > ; convert to ascii
> >
> > MOVLW D'48'
> >
> > ADDWF CVD00,F
> > ADDWF CVD01,F
> > ADDWF CVD02,F
> > ADDWF CVD03,F
> > ADDWF CVD04,F
> > ADDWF CVD05,F
> >
> > ; max value a/d is 19.981
> >
> > RETURN