16f88 - pin RA4 as analog input


Closed Thread
Results 1 to 21 of 21
  1. #1
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302

    Default 16f88 - pin RA4 as analog input

    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.
    Code:
                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
    Last edited by savnik; - 29th October 2006 at 12:10.

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    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.


    ------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    i have add this in my code:
    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;
    Code:
       
        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

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Read from AN4

    ADCIN 4, adval
    DT

  5. #5
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default now it works

    Quote Originally Posted by Darrel Taylor
    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;

  6. #6
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Post the schematic.
    Let us see your ADC reading part also.


    ---------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    At 0 volt show value: 0 and at 5 volt show value:3
    For 8 bit, you should Left justify the results.

    ADCON1.7 = 0
    <br>
    DT

  8. #8
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor
    For 8 bit, you should Left justify the results.

    ADCON1.7 = 0
    <br>
    i have this
    Code:
    ' 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

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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.
    DT

  10. #10
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    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

  11. #11
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    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 "

    ---------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  12. #12
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Ok, that makes sense now.

    -----------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  14. #14
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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
    Code:
    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
    DT

  15. #15
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor
    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
    Code:
    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

  16. #16
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default


  17. #17
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor
    Thank you

  18. #18
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Darrel Taylor,
    thank you and for this thread : http://www.picbasic.co.uk/forum/show...light=bargraph
    I use it to show to the second line on the LCD the volt as bargraph.

  19. #19
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor

    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:

    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"
    Last edited by savnik; - 15th December 2006 at 09:42.

  20. #20
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default solved

    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"

  21. #21


    Did you find this post helpful? Yes | No

    Default

    Why not use:

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

Similar Threads

  1. Is this a K Type sensor?
    By jessey in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 21st November 2009, 13:55
  2. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  3. Another RTC, DS1287
    By DavidK in forum Code Examples
    Replies: 0
    Last Post: - 12th December 2006, 17:07
  4. 16F88 reading Analog Input
    By thunderstrike44 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th August 2004, 22:41
  5. Pin RA4 doesn't work
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 0
    Last Post: - 15th July 2004, 12:03

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts