Code Problem?


Closed Thread
Results 1 to 5 of 5

Thread: Code Problem?

  1. #1
    Join Date
    Mar 2005
    Posts
    37

    Default Code Problem?

    Can someone see anything wrong with the following code? I can read GPIO.0 but when I try to read any other A/D I get nothing from any of the A/D. I am sure it is my code can someone help? If I can just get it to work I will clean up the code. The goal is the read GPIO.0 and GPIO.1 then subtract the lower from the higher. Any other suggestions?

    @ device pic12F675,intrc_osc,wdt_off,protect_off,MCLR_off

    dEFINE OSC 4 ' We're using a 4 MHz oscillator
    DEFINE OSCCAL_1k 1 ' DEFINE OSCCAL FOR PIC12C671 or PIC12F675

    adval VAR WORD 'Create adval to store result
    adval1 VAR WORD 'Create adval to store result
    adval_C VAR WORD 'Create adval_C for celsius Conversion
    adval_F_18 VAR WORD 'Create adval_F for farenheit Conversion
    adval_F_32 VAR WORD
    sign var word
    sign = "+"
    check var byte

    ANSEL = 49
    CMCON=7

    High GPIO.4
    pause 5
    low GPIO.4
    pause 500

    loop:

    ADCON0 = %10000001 ' + (J*8) ' SET A/D Fosc/8 + A/D = ON
    PAUSE 10 ' PAUSE 10mS FOR CHANNEL SETUP
    adcin GPIO.0, adval

    ADCON0 = %10000101
    PAUSE 10 ' PAUSE 10mS FOR CHANNEL SETUP
    adcin GPIO.1, adval1

    adval = (adval * 5) / 10
    adval1 = (adval1 * 5) / 10

    if adval < adval1 then
    adval_C = adval1 - adval
    check = 1
    else
    adval_C = adval - adval1
    check = 0
    endif

    if (check = 1) & (adval_C > 32) then
    sign = "-"
    else
    sign = "+"
    endif

    adval_F_18 = (adval_C * 18)/10
    adval_F_32 = (adval_F_18 + 32)

    SerOut2 GPIO.5,84, [sign, DEC adval_F_32,13,10]

    Pause 1000' Wait 1 seconds
    GoTo loop
    end

    Thanks in advance,

    Scott

  2. #2
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Hi Scott,
    I'm not too familiar with the '675, but don't you need to set your ANSEL register so that GP.1 is an analog input? It looks like only GP.0 is set as analog in.

    Arch
    "Data sheets? I ain't got no data sheets. I don't need no data sheets. I don't have to read any stinking data sheets!"

  3. #3
    Join Date
    Mar 2005
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    Arch,

    You are correct! I have changed this to ANSEL = 51 or ANSEL = %00110011. I have changed this but have the same problem! It only reads 32 over SEROUT2 no change.

    If I rem out the ADCIN1 I get the correct value via ADCIN0 checked by multimeter. It only happens when I add ADCIN1. Does it look like the ADCON0 register?

    Any other suggestions?

    Thanks,

    Scott

  4. #4
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    I've not used ADCIN much, but here's some other things to check:

    I don't see anywhere in your code that sets the pins as inputs - you might try setting up the TRISIO register as needed.

    In the PBP manual it shows an example like this:
    "ADCIN 0, B0 ; Read channel 0 to B0"

    The 'channel' is selected with a number rather than port pin name - don't know if that makes any difference or not.

    Arch
    "Data sheets? I ain't got no data sheets. I don't need no data sheets. I don't have to read any stinking data sheets!"

  5. #5
    Join Date
    Apr 2005
    Posts
    96


    Did you find this post helpful? Yes | No

    Default

    Here is another way of aproaching the A/D based on some code Melanie posted a long time ago, this uses direct access to the pics registers instead of using PBP's routines:

    Code:
     Analog_In:
         ANSEL=%00110001
        ADCON0=%10000001;
            ' Enable ADC Module on pin AN0
        PauseUS 50;
            ' 50uS Pause to allow sampling Capacitor to charge
        ADCON0.1=1
        ' Start Conversion
        While ADCON0.1=1:Wend
        ' Wait for conversion to complete
        AnalogIn.Highbyte=ADRESH
        AnalogIn.Lowbyte=ADRESL
        ' Read 16-bit Result
    RETURN
    By changing ADCON0 to different values you should be able to read analog in on all the other analog pins

Similar Threads

  1. USART+PWM Problem
    By alex-x in forum mel PIC BASIC Pro
    Replies: 43
    Last Post: - 2nd December 2009, 09:45
  2. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  3. 16F883 Code Verify Problem
    By munromh in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th February 2009, 11:47
  4. problem with my code
    By civicgundam in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 3rd February 2008, 01:52
  5. Code problem
    By chai98a in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th May 2006, 04:43

Members who have read this thread : 2

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