PDA

View Full Version : 16f88 - pin RA4 as analog input



savnik
- 29th October 2006, 12:07
I have the below code with 16f88 , and i want to know if may use the pin RA4
as analog input.
I want to read the voltage from a tuner.The range of voltage is 0.5-3.5V.


CMCON = 7 ' PortA = digital I/O

DEFINE LCD_DREG PORTB 'Selection of the port B
DEFINE LCD_DBIT 0 'Selection one RB0 with RB3
DEFINE LCD_RSREG PORTA 'RS on port A
DEFINE LCD_RSBIT 2 'RS on RA2
DEFINE LCD_EREG PORTA 'E on port A
DEFINE LCD_EBIT 3 'E on RA3
DEFINE LCD_BITS 4 'Mode 4 bits
DEFINE LCD_LINES 2 '2 lines

DEFINE I2C_SCLOUT 1

PAUSE 500

' ** DEFINITION OF THE ENTREES - EXITS

' Exits I2C

SCL var PORTA.1 'SCL on RA1 (pine 18)
SDA var PORTA.0 'SDA on RA0 (pin 17)

' Boutons

UP var PORTB.6 'increase the frequency (on RB6)
DOWN var PORTB.7 'decrease the frequency (on RB7)
CH_PAS var PORTB.5 'Change the step of synth (on RB5)


Input UP 'Up and Down are entries
Input DOWN
Input CH_PAS

sayzer
- 29th October 2006, 14:43
Hi Savnik,

RA4 can be used as analog input as data sheet says (16F88).

In this case, you need to add ANSEL = %00010000 to your code.
ANSEL = %00010000 will set RA4 as analog and all others as digital.
Make sure you set RA4 as an input pin prior to ANSEL.


You may need to work around CMCON a little.


------------------------

savnik
- 29th October 2006, 20:13
i have add this in my code:

CMCON = 7 ' PortA = digital I/O
ANSEL = %00010000 ' Will set RA4 as analog and all others as digital
ADCON0 = %11100001
ADCON1 = %10000010
adval var Byte 'Create adval to store result
IN var PORTA.4
Input IN

loop: ADCIN 0, adval ' Read channel 0 to adval

Lcdout $fe, 1 ' Clear LCD
Lcdout "Value: ", DEC adval ' Display the decimal value

Pause 100 ' Wait .1 second

Goto loop ' Do it forever
End


need to add this;


Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

but show on LCD Value:3 continues

Darrel Taylor
- 29th October 2006, 22:26
Read from AN4

ADCIN 4, adval

savnik
- 30th October 2006, 06:33
Read from AN4

ADCIN 4, adval
Thank you.
At 0 volt show value: 0 and at 5 volt show value:3
How i can at 0 volt show value: 0 and at 5 volt show value: 50 or 100 or something greater from 3;
Is easy to show the level as bargaph on LCD;

sayzer
- 30th October 2006, 08:16
Post the schematic.
Let us see your ADC reading part also.


---------------------

Darrel Taylor
- 30th October 2006, 08:50
At 0 volt show value: 0 and at 5 volt show value:3For 8 bit, you should Left justify the results.

ADCON1.7 = 0
<br>

savnik
- 30th October 2006, 09:30
For 8 bit, you should Left justify the results.

ADCON1.7 = 0
<br>
i have this


' Define ADCIN parameters

DEFINE ADC_BITS 8 'Set number of bits in result
DEFINE ADC_CLOCK 3 'Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 'Set sampling time in uS
CMCON = 7 'PortA = digital I/O
ANSEL = %00010000 'Will set RA4 as analog and all others as digital
ADCON0 = %11100001 'Configure and turn on A/D Module
ADCON1 = %00000010 'Set PORTA analog and RIGHT justify result

and modify the result:
adval = adval*2/100
and at 0 volt show value: 0 and at 5 volt show value:5

Darrel Taylor
- 30th October 2006, 13:50
For 8-bit results, the largest number will be 255 at +5V.

255 * 2 = 510
510 / 100 = 5 (integer)

So let's assume you were trying to convert the A/D reading to a Voltage number.

<pre>255 * 5 / 255 = 5 ; Single digit<br>255 * 50 / 255 = 50 ; 1 decimal place (5.0)<br>255 * 500 / 255 = 500 ; 2 decimals (5.00)</pre>Those could also be done with the */ (Mid word) multiplication.<pre>255 */ 5 = 5<br>255 */ 50 = 50<br>255 */ 500 = 500</pre>Be sure to use WORD variables for the calculation.

Then to display it ...<pre>LCDOUT DEC value,"V" ; Single digit<br>LCDOUT DEC value/10,".",DEC value // 10, "V" ; 1 decimal place<br>LCDOUT DEC value/100,".",DEC value // 100, "V" ; 2 decimals</pre>

If you go with 2 decimals, you may want to use 10-bit values from the A/D for better accuracy.
In which case you would Right Justify, and the math changes to...<pre>1023 * 5 / 1023 = 5 ; Single digit<br>1023 * 50 / 1023 = 50 ; 1 decimal place<br>1023 * 500 / 1023 = 500 ; 2 decimals</pre>But you'll need to use DIV32 for those.

savnik
- 30th October 2006, 14:19
ADCIN 4, adval 'Read channel 4 to adval
adval1 = adval*2/100
adval2 = adval*4//10
Lcdout "POWER: ", DEC2 adval1, ".", DEC1 adval2," WATT "

I have use this code and show ok

sayzer
- 30th October 2006, 15:05
I was going to ask how would you see say 2.30V, but Darrel already touched that point.

Now I wonder where did this "Watt" come from?

Lcdout "POWER: ", DEC2 adval1, ".", DEC1 adval2," WATT "

---------------------

savnik
- 30th October 2006, 15:23
Now I wonder where did this "Watt" come from?
I measure the RF of a FM linear with a wattmeter.The output is in volt.
This volt i want to measure , and show on LCD.

sayzer
- 30th October 2006, 16:18
Ok, that makes sense now.

-----------------------------

Darrel Taylor
- 30th October 2006, 16:24
ADCIN 4, adval 'Read channel 4 to adval
adval1 = adval*2/100
adval2 = adval*4//10
Lcdout "POWER: ", DEC2 adval1, ".", DEC1 adval2," WATT "


I don't think that's what you want.

Let's say that you have 4.8 V on the input. You'll get an A/D reading of 249.

249 * 2 / 100 = 4 &nbsp; This part is ok
249 * 4 = 996, and //10 gives 6
So that would display as 4.6 instead of 4.8

Or if you had 4.2 V, (214 A/D)

214 * 2 / 10 = 4 &nbsp; Still OK
214 * 4 = 856, and //10 gives 6
Still displays as 4.6 instead of 4.2

Here are some more results
Volts A/D Whole# 4*A/D //10 Result
5.00 255 5 1020 0 5.0
4.90 249 4 996 6 4.6
4.85 247 4 988 8 4.8
4.80 244 4 976 6 4.6
4.75 242 4 968 8 4.8
4.70 239 4 956 6 4.6
4.65 237 4 948 8 4.8
4.60 234 4 936 6 4.6
4.55 232 4 928 8 4.8
4.50 229 4 916 6 4.6
4.40 224 4 896 6 4.6
4.30 219 4 876 6 4.6
4.20 214 4 856 6 4.6


I think you should take another look at the formulas in post #9

savnik
- 30th October 2006, 16:40
I don't think that's what you want.

Let's say that you have 4.8 V on the input. You'll get an A/D reading of 249.

249 * 2 / 100 = 4 &nbsp; This part is ok
249 * 4 = 996, and //10 gives 6
So that would display as 4.6 instead of 4.8

Or if you had 4.2 V, (214 A/D)

214 * 2 / 10 = 4 &nbsp; Still OK
214 * 4 = 856, and //10 gives 6
Still displays as 4.6 instead of 4.2

Here are some more results
Volts A/D Whole# 4*A/D //10 Result
5.00 255 5 1020 0 5.0
4.90 249 4 996 6 4.6
4.85 247 4 988 8 4.8
4.80 244 4 976 6 4.6
4.75 242 4 968 8 4.8
4.70 239 4 956 6 4.6
4.65 237 4 948 8 4.8
4.60 234 4 936 6 4.6
4.55 232 4 928 8 4.8
4.50 229 4 916 6 4.6
4.40 224 4 896 6 4.6
4.30 219 4 876 6 4.6
4.20 214 4 856 6 4.6


I think you should take another look at the formulas in post #9

You are right , but I don't understand how to write the code for the remainder to show right the decimal

Darrel Taylor
- 30th October 2006, 16:57
http://www.darreltaylor.com/files/Num9.gif

savnik
- 30th October 2006, 18:46
http://www.darreltaylor.com/files/Num9.gif
Thank you

savnik
- 31st October 2006, 09:32
Darrel Taylor,
thank you and for this thread : http://www.picbasic.co.uk/forum/showthread.php?t=2359&highlight=bargraph
I use it to show to the second line on the LCD the volt as bargraph.

savnik
- 15th December 2006, 09:38
If you go with 2 decimals, you may want to use 10-bit values from the A/D for better accuracy.
In which case you would Right Justify, and the math changes to...<pre>1023 * 5 / 1023 = 5 ; Single digit<br>1023 * 50 / 1023 = 50 ; 1 decimal place<br>1023 * 500 / 1023 = 500 ; 2 decimals</pre>But you'll need to use DIV32 for those.
I want to use 10-bit values.
But i can use the DIV32 command to show the result on LCD.

Now for 8 bit i have this code:



ADCIN 0, adval ' Read channel 0 to adval
value = adval*50/255 ' 1 decimal place (5.0)

LCDOUT $FE,1,"VOLT : ",DEC value/10, ".",DEC value//10, " V"

savnik
- 15th December 2006, 12:59
i use this:
ADCIN 0, adval ' Read channel 0 to adval
dummy = adval * 5 * 100 ' Multiply to load internal registers with 32-bit value
value = Div32 1023 ' Divide 32-bit value by word and store in word
Lcdout $fe,1, "VOLT : ", DEC value/10,".", DEC value//10, " V"

CocaColaKid
- 15th December 2006, 13:55
Why not use:

adcin 0, adval
adval = adval * 196 / 100
lcdout $fe,1, "VOLTS: ", DEC1 adval/100,".", DEC2 adval//100, " V"