Newby with ADC problems.


Closed Thread
Results 1 to 4 of 4
  1. #1
    Mark Scotford's Avatar
    Mark Scotford Guest

    Default Newby with ADC problems.

    PicBasic Pro
    17C756A
    4 Mhz
    Slow Learner at the wheel.

    I am trying to use the ADC on a PIC for the first time and I have hit problems. I have spent several hours trying to work out the problem, but to no avail, your help would be most appreciated. I am trying to read the value of three different voltages on three different PIC pins of my 17C756A. The voltages to be measured are all in the range of 0 to 5V, and one of these to be measured, is simply a 10K Pot with its 2 ends connected to +5V and 0V and the wiper to PortG1. I have tied the A-VDD pin to the supply voltage +5V and tied the A-VSS pin to 0V. To experiment to see if I am reading anything or not, I have put a simple routine in so the the voltage on PortG1 is stored as value B21 (the other two values to be stored as B22 and B23). The value of these three variables is displayed on an LCD and re-read every 500. I am not reading the voltage correctly because the result (although stable if you leave the pot alone) is not 0-255 but something like either of the following:- 64, 128, 196, but not linear nor in the correct order, please would some kind soul look at my code and tell me where I am going wrong.

    B21 VAR BYTE
    B22 VAR BYTE
    B23 VAR BYTE

    TRISB = %00110000
    TRISC = %00000100
    TRISD = %11111111
    TRISE = %1101
    TRISF = %00000000
    TRISG = %11101110

    ADCON1 = %000000001110

    ADCON1.7 = 1

    DEFINE LCD_DREG PORTF
    DEFINE LCD_DBIT 0
    DEFINE LCD_RSREG PORTC
    DEFINE LCD_RSBIT 7
    DEFINE LCD_EREG PORTC
    DEFINE LCD_EBIT 6
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 2
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 50

    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50

    GoTo MAIN

    MAIN:

    GOSUB LCDIT
    PAUSE 500
    GOTO MAIN


    LCDIT: ADCIN 0, B23
    ADCIN 1, B22
    ADCIN 2, B21
    LCDOut $FE,1,#B7
    LCDOut $FE,$14,#B8
    LCDOut $FE,$14,#B21
    LCDOut $FE,$14,#B22
    LCDOUT $FE,$14,#B23
    LCDOUT $FE,$C0,"Readings "
    RETURN

    I have stripped out the code which I think is unecessary.

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    hello Mark,

    Mark try incorporating this in your program...


    TRISIO=%00001010 'Pic 12F675

    ' Define ADCIN parameters
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50

    'adval VAR byte

    'Pause 100
    'Loop:
    ADCIN 1, number ' Read channel 1 to adval (0-1023)
    'number = (number */ 500)>>2 equates to: (adval * 500)/1024
    number = number >> 6


    Your values should be between 1 and 1023...

    And use only ONE pin at first (until you get it working) then go to the using two pins.

    Make darn sure your pin has a A/C converter on it. and make darn sure your TRIS is set to a "1" to represent the A/D converter.

    Also, your reading will not be a "Voltage Reading" but a reading of a number between 0 and 1024... you must convert this number to a voltage reading.

    if 0=0 volts.
    if 5=1024

    Voltage= 5 X ADvalue / 1024



    Dwayne
    Last edited by Dwayne; - 3rd September 2004 at 16:50.
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  3. #3
    Mark Scotford's Avatar
    Mark Scotford Guest


    Did you find this post helpful? Yes | No

    Smile Got there in the end

    Thanks Dwayne, you pointed me in the right direction. I did not realise how important the Microchip Data sheets are, I thought PBP did everything for you. I had to learn (by trial an error) how to set the ADCON manually, got there now, can read my Pot and output 0-255 just lovely (what was that about the Blinkey Light?)
    Regards Mark

  4. #4
    GEEZER's Avatar
    GEEZER Guest


    Did you find this post helpful? Yes | No

    Default Ad Converter

    I have been looking at this post and may have picked up a little something. You guys are way ahead of me. I have only a few weeks experience.

    I have an 16F877A which I have managed to interface with an LCD and KEYPAD.
    All works great. Now I would like to read voltage through the ADC, A0 - A9.

    Here is the code I am using.

    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50

    X VAR BYTE
    TRISA = %11111111
    ADCON1 = % 10000010
    PAUSE 500

    MAIN:
    ADCIN 1, X
    LCDOUT X, " VOLTS"
    PAUSE 500
    GOTO MAIN
    END

    What I am getting now is random ascii characters for X. Sometimes they are sequential for a while.

    Any help will be appreciated.

    Geezer

Similar Threads

  1. Problems with ADC
    By mcbeasleyjr in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 29th January 2009, 17:34
  2. 16F819 ADC problems
    By MUC in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 22nd March 2008, 18:36
  3. PIC18f2423 two 12bit adc input problems
    By Brandon in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th December 2007, 09:47
  4. I have problems with de ADC...
    By jomavilla24 in forum Off Topic
    Replies: 1
    Last Post: - 16th February 2007, 16:24
  5. adc problems
    By MARAD75 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 3rd December 2006, 05:47

Members who have read this thread : 0

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